// JavaScript Document
var cDivCount = 7;        //Keep track of number of divs to rotate
var cDivWidth = 350;      //Keep track of div width
var cSeqEnd = 20;         //Keep track of number of steps in animation
var cTimeToSwitch = 5000; //Keep track of milliseconds before switching to next div
var cAnimateTime = 50;    //Keep track of milliseconds between steps in animation
var cFadeIncrement = 1 / cSeqEnd;

var vDoSwitch = false;
var vDoSwitchOnce = false;
var vCurDiv = 1;
var vSeq = 0;
var vMoveBack = false; //For Back Button -- This decrements the div id
var clockTimeoutID = 0; //For IDing the Timeout being used.

function TimeSwitch() {
   vDoSwitch = true;  //Tell program to proceed with switch
   vDoSwitchOnce = false; 
   clearTimeout(clockTimeoutID);
   clockTimeoutID = setTimeout(SwitchDiv, cTimeToSwitch);  //Activate switch after X seconds
}
function killClock(){
	clearTimeout(clockTimeoutID);
	}
	

function StopSwitch() {
   vDoSwitch = false;     //Tell program to cancel switch
   vDoSwitchOnce = false; 
   clearTimeout(clockTimeoutID);
}


function NextSwitch(){
    clearTimeout(clockTimeoutID);
	vDoSwitch = false;
	vDoSwitchOnce = true;   //Tell program to proceed with switch
    SwitchDiv();  //Activate switch after X seconds
 
}
function PreviousSwitch(){
    clearTimeout(clockTimeoutID);
	vDoSwitch = false;
	vDoSwitchOnce = true;   //Tell program to proceed with switch
    vMoveBack = true;
    SwitchDiv();  //Activate switch after X seconds

}
function SwitchDiv() 
{
   if (vDoSwitch ) {        //If switch is set to go, then choose switch type
        FadeDivOut(); 
   }
   
   if (vDoSwitchOnce){
	FadeDivOutOnce();
   }

}
   
   
function FadeDivOutOnce(){
 if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = 1 - (vSeq * cFadeIncrement);
      document.getElementById('div' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
 	  FadeDivOutOnce();
   }
   else {
      vSeq = 0;
      document.getElementById('div' + vCurDiv).style.display = 'none';
      document.getElementById('div' + vCurDiv).style.opacity = 1;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=100)';
      AdvanceDiv();
	  document.getElementById('div' + vCurDiv).style.opacity = 0;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=0)';
      document.getElementById('div' + vCurDiv).style.display = 'block';
      FadeDivInOnce();
   }
}
   
function FadeDivOut() {
   if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = 1 - (vSeq * cFadeIncrement);
      document.getElementById('div' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
      
	  setTimeout(FadeDivOut, cAnimateTime);
   }
   else {
      vSeq = 0;
      document.getElementById('div' + vCurDiv).style.display = 'none';
      document.getElementById('div' + vCurDiv).style.opacity = 1;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=100)';
      AdvanceDiv();
	  document.getElementById('div' + vCurDiv).style.opacity = 0;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=0)';
      document.getElementById('div' + vCurDiv).style.display = 'block';
      setTimeout(FadeDivIn, cAnimateTime);
   }
}

function FadeDivInOnce() {
   if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = vSeq * cFadeIncrement;
      document.getElementById('div' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
       FadeDivInOnce();
   }
   else {
      vSeq = 0;
      document.getElementById('div' + vCurDiv).style.opacity = 1;  //Just in case animation was a tad off
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=100)';
   }
}




function FadeDivIn() {
   if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = vSeq * cFadeIncrement;
      document.getElementById('div' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
      setTimeout(FadeDivIn, cAnimateTime);
   }
   else {
      vSeq = 0;
      document.getElementById('div' + vCurDiv).style.opacity = 1;  //Just in case animation was a tad off
      document.getElementById('div' + vCurDiv).style.filter = 'alpha(opacity=100)';
      TimeSwitch();    //Start timer to switch again in X seconds
   }
}



function AdvanceDiv() {
	
	if	(vMoveBack == true){
		if (vCurDiv == 1)
		  vCurDiv = cDivCount;
	    else
		  vCurDiv--;
		
		vMoveBack = false;
		 

	}
	else
	{
	   if (vCurDiv == cDivCount)
		  vCurDiv = 1;
	   else
		  vCurDiv++;
	}
}