var backgrounds = new Array();
var buttons = new Array();
var count = -1;
var speed = 5;
var fadeid;
var intervalid;
var delaytimeoutid;
var timeoutvar;
var stopall = 0;
var delay = 5;

backgrounds[0] = "b1b";
backgrounds[1] = "b2b";
backgrounds[2] = "b3b";
backgrounds[3] = "b4b";

buttons[0] = "ul";
buttons[1] = "ur";
buttons[2] = "ll";
buttons[3] = "lr";



function nextslide(){
	if(stopall == 0)
	{
		count++;
		
		
		var num = (count % 4);
		var nextnum = ((count+1) % 4);
		
		var from = backgrounds[num];
		var to = backgrounds[nextnum];
		
		//document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + "from= " + from + ", ";
		//document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + "to= " + to + ", ";
		fade_proc(from, to);
		
	}
}
function fade_proc(from, to)
{
	var timer = 0;
	
	clearTimeout(timeoutvar);
	document.getElementById(to).style.opacity = 0;
	document.getElementById(to).style.filter = 'alpha(opacity=0)';
	document.getElementById(to).style.MozOpacity = 0;
	document.getElementById(to).style.KhtmlOpacity = 0;
	document.getElementById(to).style.visibility = "visible";
	for(i = 0; i < 100; i++) {		
		fadeid = setTimeout("fade_func('" + from + "', '" + to + "')", (timer * speed));
		timer++;
	}
	timeoutvar = setTimeout("hide('"+from+"')", (timer * speed));
}

function hide(div) {
	document.getElementById(div).style.visibility = "hidden";
}

function fade_func(from, to){
	//fade curent div out while fading new div in
	
	var curopac = document.getElementById(from).style.opacity - .01;
	//document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + "curopac= " + curopac + ", ";
	document.getElementById(from).style.opacity = curopac;
	document.getElementById(from).style.filter = 'alpha(opacity=' + curopac*100 + ')';
	document.getElementById(from).style.MozOpacity = curopac;
	document.getElementById(from).style.KhtmlOpacity = curopac;

	var nextopac = parseFloat(document.getElementById(to).style.opacity) + .01;
	//document.getElementById("debug").innerHTML = document.getElementById("debug").innerHTML + "nextopac= " + nextopac + ", ";
	document.getElementById(to).style.opacity = nextopac;
	document.getElementById(to).style.filter = 'alpha(opacity=' + nextopac*100 + ')';
	document.getElementById(to).style.MozOpacity = nextopac;
	document.getElementById(to).style.KhtmlOpacity = nextopac;
}

function killfade(to){
	clearInterval(intervalid);
	clearTimeout(delaytimeoutid);
	
	count++;
	var timer = 0;
	var num = (count % 4);
	var from = backgrounds[num];
	
	var littleguy = "l" + to.substr(1,1) + "b";
	document.getElementById(littleguy).style.backgroundColor = '#CCCCCC' ;
	
	if (to != from){
		fade_proc(from, to);
	}
	count = (to.substr(1,1) * 1) + 2;
}

function resumefade(from) {

	var littleguy = "l" + from.substr(1,1) + "b";
	document.getElementById(littleguy).style.backgroundColor = '#EBEBEB' ;
	
	delaytimeoutid = setTimeout("resumefade_func('" + from + "')", (delay * 1000));
}

function resumefade_func(from) {
	
	if(stopall == 0)
	{
		var timer = 0;
		var num = (count % 4);
		var to = backgrounds[num];
		
		fade_proc(from, to);
		
		count--;
		go();
	}
}





function go(){
	stopall = 0;
	intervalid = setInterval("nextslide()", 2500);
}

function stopallf() {
	clearInterval(intervalid);
	stopall = 1;
}