/* by 5ubliminal */
/*** --------------------------------------------------------------- ***/
jQuery.interval = function(callback, delay, initialize){
	// Copyright 5ubliminal 2009 - 2012 :)
	// Free for personal use
	// Thou shalt not change this, sell or claim
	var intervalObject = {
		ID: 0, // The interval ID
		delay: 0, // The interval Delay
		callback: 0, // The interval callback
		onStart: 0, // Callback to call on start
		onStop: 0, // Callback to call on stop
		// Assign / Update callback / delay
		assign: function(callback, delay, initialize){
			delay = parseInt(delay); // Make sure it's int
			// Validate parameters
			if((typeof(callback) != 'function') || (delay < 1)) return false;
			// Store for future re-use on stop / start
			this.delay = delay;
			this.callback = callback;
			// Call the one time only initialize function
			if(initialize && (typeof(initialize) == 'function')) initialize(this);
			return true;
		},
		// Assign / Update stop callback
		setStop: function(callback){
			this.onStop = 0;
			if(!callback || (typeof(callback) != 'function')) return this;
			this.onStop = callback;
			return this;
		},
		// Assign / Update stop callback
		setStart: function(callback){
			this.onStart = 0;
			if(!callback || (typeof(callback) != 'function')) return this;
			this.onStart = callback;
			return this;
		},
		// Start / Restart the interval
		// BeforeStart may be a function called [initialization] before interval starts
		start: function(beforeStart){
			this.stop();
			if((typeof(this.callback) != 'function') || (this.delay < 1)) return false;
			// Validate callback and assign it
			if(beforeStart && (typeof(beforeStart) == 'function')) this.setStart(beforeStart);
			// Now try to call it
			if(this.onStart && (typeof(this.onStart) == 'function')) this.onStart(this);
			// Assign / Create new interval ID
			this.ID = setInterval(this.callback, this.delay);
			return this;
		},
		// Stop / Pause the interval
		// AfterEnd may be a function called when the interval is stopped
		stop: function(afterEnd){
			if(!this.ID) return this;
			// Validate callback and assign it
			if(afterEnd && (typeof(afterEnd) == 'function')) this.setStop(afterEnd);
			// Now try to call it
			if(this.onStop && (typeof(this.onStop) == 'function')) this.onStop(this);
			// Clear the interval ... this stops it :)
			clearInterval(this.ID);
			this.ID = 0;
			return this;
		}
	};
	delay = parseInt(delay);
	// Validate parameters
	if((typeof(callback) != 'function') || (delay < 1))
		return intervalObject; // Manual initialization
	// Assign callback and delay
	intervalObject.assign(callback, delay, initialize);
	// Start the interval
	intervalObject.start();
	return intervalObject;
};
/*** --------------------------------------------------------------- ***/
jQuery.createTicker = function(itemID, delayTime){
	if(!delayTime) delayTime = 2500;
	var tickerInterval = jQuery.interval(function(){
		var index = parseInt(jQuery("#" + itemID + "").data('index'));
		var lastIndex = parseInt(jQuery("#" + itemID + "").data('lastIndex'));
		tickerInterval.stop(); // Pause the interval
		jQuery("#" + itemID + " > li:eq("+index+")").hide();
		var firstItems = jQuery("#" + itemID + " > li:gt(" + index + ")");
		var lastItems = jQuery("#" + itemID + " > li:lt(" + index + ")");
		var lastItem = jQuery("#" + itemID + " > li:eq(" + lastIndex + ")");
		var thisItem = jQuery("#" + itemID + " > li:eq(" + index + ")");
		firstItems.add(lastItems).not(lastItem).css('z-index', 0);
		thisItem.hide(); // Hide item
		lastItem.css('z-index', 1);
		thisItem.css('z-index', 2);
		// Fade in slowly
		thisItem.show('slow', function(){
			// Restart the interval
			jQuery("#" + itemID + "").data('interval').start();
		});
		jQuery("#" + itemID + "").data('lastIndex', index);
		index ++; index %= jQuery("#" + itemID + " > li").length;
		jQuery("#" + itemID + "").data('index', index);
	}, delayTime, function(interval){
		jQuery("#" + itemID + "").data('index', 0);
		var itemCount = jQuery("#" + itemID + " > li").length;
		jQuery("#" + itemID + "").data('lastIndex', itemCount - 1);
		jQuery("#" + itemID + "").data('interval', interval);
		//Prepare z-Indexes
		jQuery("#" + itemID + " > li").css('z-index', 0);
		jQuery("#" + itemID + " > li:eq(0)").css('z-index', 1);
		jQuery("#" + itemID + " > li:eq(" + (itemCount - 1) + ")").css('z-index', 2);
		//Pause on hover
		jQuery("#" + itemID + "").hover(function(){
			interval.stop();
		}, function(){
			interval.start();
		});
	});
}
/*** --------------------------------------------------------------- ***/
jQuery.cookies = function(){
	// Copyright 5ubliminal 2009 - 2012 :)
	// I'm Javascript-challenged ... there may be redundant code or could have been
	// made more simple but this is what a C++ hardcore and Sunday-JS coder can pull out
	var cookieObject = {
		names: [],
		values: [],
		// Get cookie value
		get: function(name){
			for(var pos = 0; pos < this.names.length; pos++){
				var cookieName = this.names[pos];
				if(cookieName.toLowerCase() != name.toLowerCase()) continue;
				return this.values[pos];
			}
			return null;
		},
		// Cookie count
		count: function(){
			return this.names.length;
		},
		// Name of cookie by index
		name: function(index){
			if(index >= this.count()) return null
			return this.names[index];
		},
		// Value of cookie by index
		value: function(index){
			if(index >= this.count()) return null
			return this.values[index];
		},
		// Find location of a cookie
		find: function(name){
			for(var pos = 0; pos < this.names.length; pos++){
				var cookieName = this.names[pos];
				if(cookieName.toLowerCase() != name.toLowerCase()) continue;
				return pos;
			}
			return -1;
		},
		// Cookie exists?
		has: function(name){
			return (this.find(name) > -1);
		},
		// Drop all cookies, won't work for HTTP only
		clear: function(){
			return this.drop(this.names);
		},
		// Drop a cookie
		drop: function(name, path, domain){
			if(!name) return false;
			if(!path) path = '/';
			var names = [];
			if(typeof name == 'object') names = name;
			else names.push(name);
			for(var index = 0; index < names.length; index++){
				var name = names[index];
				var pos = this.find(name);
				if(pos < 0) continue;
				document.cookie = name + "=" +
					( ( path ) ? ";path=" + path : "") +
					( ( domain ) ? ";domain=" + domain : "" ) +
					";expires=Thu, 01-Jan-1970 00:00:01 GMT";
			}
			this.refresh();
			return true;
		},
		// Set a cookie, update old or create new
		set: function(name, value, options){
			if (value === null) { return this.drop(name, options.path, options.domain); }
			if(typeof value == 'undefined') return false;
	        var expires = '', path = '', domain = '', secure = '';
	        if(!options) options = {path: '/'};
	        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 * 3600));
	            } else {
	                date = options.expires;
	            }
	            expires = ';expires=' + date.toUTCString();
	        }
			if(options.path && options.path.length) path = ';path=' + options.path;
	       	if(options.domain && options.domain.length) path = ';domain=' + options.domain;
			if(options.secure) secure = ';secure';
	        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
			this.refresh();
			return true;
		},
		// Refresh internal cookie arrays
		refresh: function(){
			this.names = [];
			this.values = [];
			var documentCookies = document.cookie.split(';');
			for(var pos = 0; pos < documentCookies.length; pos++){
				var documentCookie = documentCookies[pos].trim();
				var cookieParts = documentCookie.split('=', 2);
				if(cookieParts.length != 2) continue;
				var cookieName = decodeURIComponent(cookieParts[0].trim());
				var cookieValue = decodeURIComponent(cookieParts[1].trim());
				this.names.push(cookieName);
				this.values.push(cookieValue);
			}
			return this;
		}
	};
	return cookieObject.refresh();
}
/*** --------------------------------------------------------------- ***/
