﻿//# Mapping flash colour index to css index
var flashCssIndexMap = [ 5, 4, 1, 2, 3 ];

//# Mapping css colour index to flash index
var cssFlashIndexMap = [ 2, 3, 4, 1, 0 ];

function onColourIndexChange( index ) {

		setColourIndex( flashCssIndexMap[ index ] );
		createCookie("colourIndex", flashCssIndexMap[ index ], 365);
		
}

function getColourIndex() {
	
	var index = readCookie("colourIndex");
	
	if( index == null ) {
		index = Math.floor( Math.random() * 5 ) + 1;
				
	}
	
	setColourIndex(index);
	
	return cssFlashIndexMap[ Number( index ) - 1 ];
	
}

function setColourIndex( index ) {
	
	document.getElementsByTagName('body')[0].className = "bg_"+index;
		
}

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 = "";
	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) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
