var layer = new String();
var style = new String();
var type = new String();
var pages = 5;//number of pages in loop
var num = Math.floor(Math.random()*(pages+1));
var div;
var divSpeed = 1000;

function checkBrowser(){
	if (navigator.userAgent.indexOf("Opera")!=-1
    && document.getElementById) type="OP"; 
	if (document.all) type="IE"; 
	if (document.layers) type="NN"; 
	if (!document.all && document.getElementById) type="MO";
}

function changeDiv(layerRef, state, action){
	checkBrowser();

	for(i=1; i<pages+1; i++)
	{
		div = "div"+i;
		if (type=="IE") eval("document.all." + div + ".style.visibility='hidden'");		
		if (type=="NN") eval("document." + div + ".visibility='hidden'");
  		if (type=="MO" || type=="OP") eval("document.getElementById('" + div + "').style.visibility='hidden'");	
	}
	
	if (type=="IE") eval("document.all." + layerRef + ".style.visibility='" + state + "'");
  	if (type=="NN") eval("document." + layerRef + ".visibility='" + state + "'");
  	if (type=="MO" || type=="OP") eval("document.getElementById('" + layerRef + "').style.visibility='" + state + "'");
	
	if(action == 'Stop'){
		running = false;
	}
}

function getActive() {
	checkBrowser();

	var active=0;
	
	for(i=1;i<=pages; i++)
	{
	 div = "div"+i;
	 if(type=="IE") if (document.all.div.style.visibility!='hidden') active=i;
	 if(type=="NN") if (document.div.visibility!='hidden') active=i;
	 if(type=="MO" || type=="OP") if (document.getElementById(div).style.visibility!='hidden') active=i;
	}
	
	return (active);
}

function nextPage(){
	active = getActive();
	if(active==pages) { changeDiv('div1','visible','Stop') } else { changeDiv('div'+(active+1),'visible','Stop');}
}

function previousPage(){
	active = getActive();
	if(active==1) { changeDiv('div'+pages,'visible','Stop') } else { changeDiv('div'+(active-1),'visible','Stop');}
}

var countDownInterval=5;
var running = true;
var countDownTime=countDownInterval+1;

function countDown(){
	if(running){
		countDownTime--;
		if (countDownTime <=0){
			countDownTime=countDownInterval;
			clearTimeout(counter);
			if(num<=pages)
			{
				changeDiv("div"+num, "visible");
				num++;
			}
			else
			{
				changeDiv("div1", "visible");
				num = 2;
			}
		}
		counter=setTimeout("countDown()",divSpeed);
	}
}

function startSweetspot(){
 if (num==0) { num=1; }
 changeDiv("div"+num, "visible");
 countDown();
}

//if (document.all || document.getElementById) startSweetspot(); else window.onload=startSweetspot();
