/**
 * Plugins
 *
 * @author Jostein Eriksen <jostein@tujo.net> and Tobias Schibler <tobias@tujo.net>
 * @type JavaScript
 * @copyright 2007 tujo ANS
 */






(function ($){
	$.setTimeout = function(id, time, callback){
		$.timer.start(id, time, callback);
	};
	$.setInterval = function(id, time, callback){
		$.timer.loop(id, time, callback);
	};
	$.clearTimeout = function(id){
		$.timer.stop(id);
	};
	$.clearInterval = function(id){
		$.timer.stop(id);
	};

	/**
	 * timer function
	 *
	 * $.timer.start(id:string, time:int sec, callback:function);
	 * $.timer.loop(id:string, time:int sec, callback:function);
	 * $.timer.stop(id:string);
	 * $.timer.clear(id:string);
	 */
	$.timer = {
		/**
		 * start - starts a timeout, witch will stop and call a callback function after the timeout
		 *
		 * @param {String} id : a ID (string) to make the interval unik
		 * @param {String} f : a function
		 * @param {Number} t : the time in second
		 */
		start: function(id, time, callback){
			if(typeof id != 'string' || typeof time != 'number' || typeof callback != 'function') return;
			clearTimeout(this['t'+id]);
			var cb = function(){
				callback();
//				$.call(callback);
			};
			this['t'+String(id)] = setTimeout(cb, eval(time*1000));
		},

		/**
		 * lop - starts an intervall, witch will loop with a given ID
		 *
		 * @param {String} id : a ID (string) to make the interval unik
		 * @param {String} f : a function
		 * @param {Number} t : the time in second
		 */
		loop: function(id, time, callback){
			if(typeof id != 'string' || typeof time != 'number' || typeof callback != 'function') return;
			clearInterval(this['i'+id]);
			var cb = function(){
				callback();
//				$.call(callback);
			};
			this['i'+String(id)] = setInterval(cb, eval(time*1000));
		},

		/**
		 * stop - stops an timeout/intervall with a given ID
		 *
		 * @param {String} id : a ID (string) to make the interval unik
		 */
		stop: function(id){
			if(typeof id != 'string') return;
			clearTimeout(this['t'+id]);
			clearInterval(this['i'+id]);
		},

		/**
		 * clearInterval - stops an intervall with a given ID
		 *
		 * @param {String} id : a ID (string) to make the interval unik
		 */
		clear: function(id){
			if(typeof id != 'string') return;
			clearInterval(this['i'+id]);
		}
	};
})(jQuery);






(function ($){
	/**
	 * rand - a random function with min and max
	 *
	 * @param {Number} pint : how match inits afterthe comma (adjustable point)
	 * @param {Number} min : the min number
	 * @param {Number} max : the max number
	 *
	 * @return {Number} a random number
	 */
	$.rand = function(point, min, max){
		var r = (min+Math.random()*(max-min));
		if(point){
			var p = Math.pow(10, point);
			return Math.floor(r*p)/p;
		} else {
			return Math.floor(r);
		}
	};

	$.popUp = function(url, name, w, h){
//		w += 2;
//		h += 2;
		window[name] = window.open(url,name,'toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,width='+w+',height='+h)
		window[name].focus();
		return false;
	};
})(jQuery);



/**
 * screen dimensions
 *
 */
(function ($){
	/**
	 * agent screen dimension or the scrolled position
	 *
	 * @return {Number} returns the screen width/height or hte scrollet position - $.screen().width, $.screen().height, $.screen().x, $.screen().y
	 */
	$.screen = function(mode){
		return {
			width : document.body.scrollWidth>document.body.offsetWidth? document.body.scrollWidth: document.body.offsetWidth,
			height: document.body.scrollHeight>document.body.offsetHeight? document.body.scrollHeight: document.body.offsetHeight,
			x: self.pageXOffset || ((document.documentElement && document.documentElement.scrollLeft)? document.documentElement.scrollLeft: null) || (document.body? document.body.scrollLeft: null),
			y: self.pageYOffset || ((document.documentElement && document.documentElement.scrollTop)? document.documentElement.scrollTop: null) || (document.body? document.body.scrollTop: null)
		};
	};

})(jQuery);


