function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";

	if (typeof value == "string") {
		value = value.replace(/\"/g, '\\"');
	}
	value = '"' + value + '"';
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			var rv = c.substring(nameEQ.length+1,c.length-1).replace(/\\\"/g, '"');
			return rv;
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function attach_event(el, evt, func)
{
	if (el.attachEvent) {
		return el.attachEvent('on' + evt, func);
	} else if (el.addEventListener) {
		el.addEventListener(evt, func, false);
	}
}
function ColourChanger(element, colours)
{
	this.element = element;
	this.colours = colours;

	this.tick  = parseInt(readCookie('tick'));
	this.start = eval('(' + readCookie('startColour') + ')');
	this.end = eval('(' + readCookie('endColour') + ')');
	this.colour_index = parseInt(readCookie('colourIndex')) || 0;

	var self = this;
	$(window).bind('beforeunload', function() {
		createCookie('tick',		self.tick);
		createCookie('startColour', '{"r":' + self.start.r + ',"g":' + self.start.g + ',"b":' + self.start.b + '}');
		createCookie('endColour', '{"r":' + self.end.r + ',"g":' + self.end.g + ',"b":' + self.end.b + '}');
		createCookie('currentColour', '{"r":' + self.current_colour.r + ',"g":' + self.current_colour.g + ',"b":' + self.current_colour.b + '}');
		createCookie('colourIndex', self.colour_index);
	});

	this.change_colour();
}
ColourChanger.prototype = {
get_colour:
	function(start, end, percentage)
	{
		var frac = percentage/100;

		var r = start.r + Math.round((end.r - start.r)*frac);
		var g = start.g + Math.round((end.g - start.g)*frac);
		var b = start.b + Math.round((end.b - start.b)*frac);

		return {r: r, g: g, b: b};
	},
get_next_colour:
	function()
	{
		return this.get_colour(this.start, this.end, this.tick+=2);
	},
change_colour:
	function()
	{
		if (!this.start) {
			this.tick = 0;
			this.colour_index = 0;
			this.start = this.colours[0];
			this.end = this.colours[1];
		}
		var col = this.get_next_colour();
		this.current_colour = col;
		this.element.style.backgroundColor = 'rgb(' + col.r + ', ' + col.g + ', ' + col.b + ')';
		if (this.tick < 100) {
			var self = this;
			setTimeout(function() { self.change_colour(); }, 400);
		} else {
			this.tick = 0;
			this.colour_index++;
			if (this.colour_index >= this.colours.length) {
				this.colour_index = 0;
			}
			this.start = this.end;

			if (this.colour_index == this.colours.length -1) {
				this.end = this.colours[0];
			} else {
				this.end = this.colours[this.colour_index+1];
			}
			this.change_colour();
		}
	}
};

$(document).ready(function()
{
	var colours = [
		{r: 120, g: 60, b: 60},
		{r: 120, g: 120, b: 60},
		{r: 60, g: 120, b: 60},
		{r: 60, g: 120, b: 120},
		{r: 60, g: 60, b: 120},
		{r: 120, g: 60, b: 120}
	];
	new ColourChanger(document.body, colours);
});
