////////////////////////////////////////////////////////////////////////////////
// Baylor University - copyright 2007
//
// Tab Rotation for the center tabs on the Baylor front page.
//

// Default time between rotations (in milliseconds)
var waitTime = 4500;

// Default fade parameters
var fadeTime = 300;
var fadeSteps = 3;

// Fade constants based on parameters
var millisecs;
var alphaDelta;

// Current fade alpha
var fadeAlpha;
var fadeTimer;
var fadeIn = false;
var fadeElem;

// Tab ID names
var elements = Array();
// Array index of the starting section
var currentElem = 0;
// Rotation flag (to stop rotation on mouseover)
var enableRotate = true;

// Start the rotation process
function InitRotate() { 

    // Calculate constants from parameters
    millisecs = fadeTime / fadeSteps;
    alphaDelta = 1.0 / fadeSteps;

    // Override the tabs failsafe mode
    var menu = document.getElementById("menu_wrapper");
    if( menu ) {
        menu.style.display = "block";
        //        menu.style.position = "absolute";
        //        menu.style.marginTop = "180px";
    }
    var wrapper = document.getElementById("alltabs_wrapper");
    if( wrapper ) {
        wrapper.style.overflow = "hidden";
    }

    // Hide the section titles
    for( var i=0; i<elements.length; i++ ) {
        var elem = document.getElementById(elements[i]);
        if( elem ) {
            //            elem.style.position = "absolute";
            elem.style.zIndex = elements.length - i; 
            if( i > 0 ) {
                elem.style.display = "none";
                elem.style.opacity = 0;
                elem.style.filter = "alpha(opacity=0)"; 
            } else {
                elem.style.display = "block";
                elem.style.opacity = .99;
                elem.style.filter = "alpha(opacity=100)"; 
            }
        } else {
            delete elements[i];
        }
        var title = document.getElementById(elements[i]+"_title");
        if( title ) {
            title.style.display = "none";
        }
    }

    // Hide the other sections
    RotateDIV(currentElem);

    // Start the rotation timer
    Ajax_StopTimer();
    Ajax_StartTimer("RotateDIV()",waitTime);
}

// Show the next or clicked section, hide the rest
function RotateDIV( clicked ) {
    if( clicked >= 0 && clicked < elements.length ) {
        Ajax_StopTimer();
        currentElem = clicked;
    } else {
        fadeElem = currentElem;
        currentElem = (currentElem + 1) % elements.length;
        FadeDIV(true);
    }
    for( var i=0; i<elements.length; i++ ) {
        var elem = document.getElementById(elements[i]);
        var link = document.getElementById(elements[i]+"_link");
        var current = document.getElementById(elements[i]+"_current");
        if( i == currentElem ) {
            if( elem && i==clicked ) {
                elem.style.display = "block";
                elem.style.opacity = .99;
                elem.style.filter = "alpha(opacity=100)";
            }
            if( link ) {
                link.style.display = "none";
            }
            if( current ) {
                current.style.display = "inline";
            }
        } else {
            if( elem && clicked>=0 ) {
                elem.style.display = "none";
            }
            if( link ) {
                link.style.display = "inline";
            }
            if( current ) {
                current.style.display = "none";
            }
        }
    }
    if( enableRotate ) {
        Ajax_StopTimer();
        Ajax_StartTimer("RotateDIV()",waitTime);
    }
}

// On mouse over, pause the rotation
function DisableRotate() { 
    enableRotate = false;
    Ajax_StopTimer();
}

// On mouse out, restart the rotation
function EnableRotate() {
    enableRotate = true;
    Ajax_StopTimer();
    Ajax_StartTimer("RotateDIV()",waitTime);
}

// Fade transition to the next tab 
function FadeDIV( first ) {
    var elem = document.getElementById(elements[fadeElem]);
    if( first ) {
        fadeIn = false;
        fadeAlpha = 1.0;
        elem.style.opacity = .99;
        elem.style.filter = "alpha(opacity=100)";
        fadeTimer = self.setTimeout( "FadeDIV();", millisecs );
    } else {
        if( fadeIn ) {
            elem.style.display = "block";
            fadeAlpha += alphaDelta;
            if( fadeAlpha < 1.0 ) {
                elem.style.opacity = Math.floor(fadeAlpha*100)/100;
                elem.style.filter = "alpha(opacity="+Math.floor((fadeAlpha*100))+")";
                fadeTimer = self.setTimeout( "FadeDIV();", millisecs );
            } else {
                elem.style.opacity = 0.99;
                elem.style.filter = "alpha(opacity=100)";
                fadeAlpha = 1.0;
            }
        } else {
            fadeAlpha -= alphaDelta;
            if( fadeAlpha > 0.0 ) {
                elem.style.opacity = Math.floor(fadeAlpha*100)/100;
                elem.style.filter = "alpha(opacity="+Math.floor((fadeAlpha*100))+")";
                fadeTimer = self.setTimeout( "FadeDIV();", millisecs );
            } else {
                elem.style.opacity = 0;
                elem.style.filter = "alpha(opacity=0)";
                fadeAlpha = 0.0;
                fadeIn = true;
                fadeElem = currentElem;
                var next = document.getElementById(elements[fadeElem]);
                next.style.opacity = 0;
                next.style.filter = "alpha(opacity=0)";
                elem.style.display = "none";
                next.style.display = "block";
                fadeTimer = self.setTimeout( "FadeDIV();", millisecs );
            }
        }
    }
}

// Timer Functions
var ajax_timerID = null;
function Ajax_StopTimer() {
    if( ajax_timerID != null )
        self.clearTimeout(ajax_timerID);
    ajax_timerID = null;
}

function Ajax_StartTimer( callback, millisecs ) {
    ajax_timerID = self.setTimeout(callback,millisecs);
}

// Start when the page loads
window.onload=InitRotate;

/*
Variable Compression:
millisecs       ms
alphaDelta      aD
fadeAlpha       fA
fadeTimer       fT
fadeIn          fI
fadeElem        fE
currentElem     cE
enableRotate    eR
InitRotate      IR
  menu          m
  wrapper       w
  title         t
RotateDIV       RD
  elem          e
  clicked       cl
  link          l
  current       c
FadeDIV         FD
  first         f
  elem          e
  next          n
ajax_timerID    aT
Ajax_StartTimer ST
  callback      c
Ajax_StopTimer  TT
*/
