// SET UP CONTROL VARIABLES
var siteCookie = 'prefs';

function set_plain_cookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function set_cookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	var cookiePresent = get_cookie_old(siteCookie);
	var cookieCookie = unescape(cookiePresent);
	
	if(cookiePresent) {
		var cookieArray = cookieCookie.split('|');
		
		for (x in cookieArray)
		{
			var cookiePair = cookieArray[x].split('=');
			for (y in cookiePair)
			{
				if(cookiePair[y] == name) {
					//alert(name);
					var replaced = true;
					cookieArray[x] = (name+'='+value);
				}
			}
		}
		
		if(!replaced) {
			cookieArray.push((name+'='+value));
		}
		
	} else {
		var cookieArray = new Array();
		cookieArray.push((name+'='+value));
	}
	
	value = cookieArray.join("|");
	document.cookie = siteCookie + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function get_cookie(name) {	
	var cookieCookie = get_cookie_old(siteCookie);

	if(cookieCookie) {
		var cookieArray = cookieCookie.split('|');
		for (x in cookieArray)
		{
			var cookiePair = cookieArray[x].split('=');
			for (y in cookiePair)
			{
				if(cookiePair[y] == name) {
					var found = true;
					var theSegment = (parseInt(y) + 1);
					return cookiePair[theSegment];	
				}
			}
		}
	}
	return false;
}

// this function gets the cookie, if it exists
function get_cookie_old(name) {
	
	var start = document.cookie.indexOf( name + "=");
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length))) {
		return null;
	}
	if ( start == -1 ) {
		return null;
	}
	var end = document.cookie.indexOf(";", len);
	if ( end == -1 ) {
		end = document.cookie.length;
	}
	return unescape(document.cookie.substring(len, end));

}

// this deletes the cookie when called
function delete_cookie(name, path, domain) {
	if(Get_Cookie(name)) document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

function addFade(t) {
		target = document.getElementById(t);
		doBGFade(target,[245,235,100],[255,250,250],'transparent',75,20,4);
	}
	
function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
	if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
	var actStep = 0;
	elem.bgFadeInt = window.setInterval(
		function() {
			elem.style.backgroundColor = "rgb("+
				easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
				easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
				easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
			actStep++;
			if (actStep > steps) {
			elem.style.backgroundColor = finalColor;
			window.clearInterval(elem.bgFadeInt);
			}
		}
		,intervals)
}