﻿var cDivCount = 4;        //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 cSlideIncrement = cDivWidth / cSeqEnd;
var cFadeIncrement = 1 / cSeqEnd;

var vDoSwitch = false;
var vCurDiv = 1;
var vSeq = 0;

function TimeSwitchGen() {
   vDoSwitch = true;                      //Tell program to proceed with switch
   setTimeout(SwitchDiv, cTimeToSwitch);  //Activate switch after X seconds
}
function SwitchDiv() {

   if (vDoSwitch) {        //If switch is set to go, then choose switch type
         FadeDivOut();    //#1 - Slide current div out, then slide new div in
   }
}
function FadeDivOut() {
   if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = 1 - (vSeq * cFadeIncrement);
      document.getElementById('tab' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('tab' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
      setTimeout(FadeDivOut, cAnimateTime);
   }
   else {
      vSeq = 0;
      document.getElementById('tab' + vCurDiv).style.display = 'none';
      document.getElementById('tab' + vCurDiv).style.opacity = 1;
      document.getElementById('tab' + vCurDiv).style.filter = 'alpha(opacity=100)';
      AdvanceDiv();
      document.getElementById('tab' + vCurDiv).style.opacity = 0;
      document.getElementById('tab' + vCurDiv).style.filter = 'alpha(opacity=0)';
      document.getElementById('tab' + vCurDiv).style.display = 'block';
      setTimeout(FadeDivIn, cAnimateTime);
   }
}
function FadeDivIn() {
   if (vSeq < cSeqEnd) {
      vSeq++;
      var vOpacity = vSeq * cFadeIncrement;
      document.getElementById('tab' + vCurDiv).style.opacity = vOpacity;
      document.getElementById('tab' + vCurDiv).style.filter = 'alpha(opacity=' + (vOpacity * 100) + ')';
      setTimeout(FadeDivIn, cAnimateTime);
   }
   else {
      vSeq = 0;
      document.getElementById('tab' + vCurDiv).style.opacity = 1;  //Just in case animation was a tad off
      document.getElementById('tab' + vCurDiv).style.filter = 'alpha(opacity=100)';
      TimeSwitchGen();    //Start timer to switch again in X seconds
   }
}
function AdvanceDiv() {
   if (vCurDiv == cDivCount)
      vCurDiv = 1;
   else
      vCurDiv++;
}

function MoveTo(DivID) {
    Base = document.getElementById(DivID).parentNode;
    Sub = Base.getElementsByTagName('div');
    for (x = 0; x < Sub.length; x++) {
        if (Sub[x].parentNode == Base) {
            if (Sub[x].id == DivID) {
                Sub[x].style.display = "block";
            } else {
                Sub[x].style.display = "none";
            } 
        } 
    }
}

