// cookie plugin
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+options.path:'';var domain=options.domain?'; domain='+options.domain:'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};
$(document).ready(function(){
    	//read from cookie
	var old_time = $.cookie('timing');
	//time out time in secs
	var time_out_time = 1800

	if(old_time)
	{// check wenn seite geändert wird .. zeit kommt aus cookie
		//var current_time = Date.now(); // now method not supported in ie6
		var current_time = new Date();
    current_time.setTime(current_time.getTime());
		// measure time difference of cookie time and current time
		var time_diff_second = (current_time-old_time)/1000;
		if(time_diff_second > time_out_time)
		{//time difference is bigger than timout-time so call show_this
			show_this();
		}
	}
	else
	{// no cookie present
		reset_time();
	}
	//counter for user staying on the present page
        var time_out_in_milisecs = time_out_time*1000;
	$.cp_timer.set({delay:time_out_in_milisecs});

});
//call the thickbox tb_show function
function show_this()
{
	markup = $('#TB_overlay');
	//window.console.log(markup);
	if(markup.length < 1){
		//trigger thickbox if not already present
		name = ['01','02','03','04','05','06','07','08','09','10'].random();
		tb_show('Pause',"upload/pause/"+name+".jpg",'');
	}
	// reset time in cookie
	reset_time();
}
//reset the time in the cookie
function reset_time()
{
	$.cookie('timing', null,{path: '/'});
  var date = new Date();
  date.setTime(date.getTime());
	$.cookie('timing',date,{path: '/'});
}

// plugin um zeiten innerhalb der seite zu messen
$.cp_timer = {
    options: {
		delay: 60000 //default delay in millisecs 60 secs
    },
	timeoutobj:  {
		id: -1
	},
    set: function(options) {
		if (this.timeoutobj.id > -1) { //got an object
			clearTimeout(this.timeoutobj);
		}
        if (options) {
            $.extend(this.options, options);
        }
		// set timeout and beat function for current obj
		this.timeoutobj.id = setTimeout("$.cp_timer.beat();", this.options.delay);

    },
	//do something when the timeout is reached and reset the timer
	//calls a global function show_this, which must be present in calling scope
    beat: function() {
		this.timeoutobj.id = setTimeout("$.cp_timer.beat();", this.options.delay);
     	show_this();
    }
};

/* takes an array and returns a random element*/
Array.prototype.random = function( r )
{
	var i = 0, l = this.length;
	if( !r ) { r = this.length; }
	else if( r > 0 ) { r = r % l; }
	else { i = r; r = l + r % l; }
	return this[ Math.floor( r * Math.random() - i ) ];
};
