var interval, step, callWhenFinished, o;
var color0 = [255, 255, 255];
var color1 = [245, 235, 100];

function findIntro() {
	o = document.getElementById('front3colsCol1');
	if (o != null) {
		objs = o.getElementsByTagName('div');
		if (objs.length > 0) o = objs[0];
	}
}

function col(d) {
	return Math.max(0, Math.min(255, Math.round(d)));
}

function calcCol(d, colA, colB) {
	rgb = [0, 0, 0];
	for (i = 0; i < 3; i++) rgb[i] = col(d * (colB[i] - colA[i]) + colA[i]);
	return rgb;
}

function fadeUp() {
	step = 1;
	callWhenFinished = fadeDown;
	interval = window.setInterval('fade(color0, color1, 10)', 75);
}

function fadeDown() {
	step = 1;
	callWhenFinished = null;
	interval = window.setInterval('fade(color1, color0, 20)', 75);
}

function fade(colA, colB, steps) {
	if (step <= steps) {
		rgb = calcCol(step / steps, colA, colB);
		o.style.backgroundColor = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
		step++;
	} else {
		window.clearInterval(interval);
		if (callWhenFinished != null) callWhenFinished();
	}
}

function isNewVisitor() {
	ca = document.cookie.split(';');
	for(i = 0;i < ca.length; i++) {
		c = ca[i].replace(/^\s+|\s+$/g, ''); // Trim
		if (c.indexOf('is_returning_visitor=') == 0) return false;
	}
	return true;

}

function highlightBg() {
	if (isNewVisitor()) {
		findIntro();
		if (o != null) { fadeUp(fadeDown); }
		
		// Set cookie
		date = new Date();
		date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000);
		document.cookie = 'is_returning_visitor=1; expires=' + date.toGMTString() + '; path=/';
	}
}

window.onload = highlightBg;