
var timerObject;
//Initial load
$(document).ready(function(){ startTimer(); });

//Start the timer for automatically switching divs
function startTimer()
{
   //Starts the count down timer to switch the visible divs
	timerObject=setTimeout("changeDivOnTimeOut()",5000);
	//timerObject=setTimeout("changeDivOnTimeOut()",900);

	var playPauseButton = $("#playPauseButton");

	if(playPauseButton  != null)
		playPauseButton.attr("src","../../Style%20Library/images/CPSI/pausebtn.gif");
}

//Hides all divs
function hideDivs()
{
    $("#contentContainer > div").css("display", "none");
}

//Gets the id of the next available tab item after the current one
function getNextTabId()
{
    //Get the id of the next div to set as visible
    var tabId = $("#currentTabId").val();
    if($("#" + tabId).length > 0)//old id exists get the next tab item
        tabId = $("#" + tabId).nextAll("div:first").attr("id");
    else//old id doesn't exist get the first tab item
        tabId = $("#contentContainer > div:first").attr("id");
    
    //check to see if we need to loop around from last to first.
    if(tabId == null)
        tabId = $("#contentContainer > div:first").attr("id");
        
    return tabId;
}

//Gets the id of the previous available tab item after the current one
function getPreviousTabId()
{
    //Get the id of the next div to set as visible
    var tabId = $("#currentTabId").val();
    if($("#" + tabId).length > 0)//old id exists get the previous tab item
        tabId = $("#" + tabId).prevAll("div:first").attr("id");
    else//old id doesn't exist get the first tab item
        tabId = $("#contentContainer > div:first").attr("id");
    
    //check to see if we need to loop around from last to first.
    if(tabId == null)
        tabId = $("#contentContainer > div:last").attr("id");
        
    return tabId;
}

//Gets the id of the next available menu item after the current one
function getNextMenuId()
{
    //Get the id of the next menu div to hightlite
    var menuId = $("#currentMenuId").val();
    if($("#" + menuId).length > 0)//old id exists get the next menu item
        menuId = $("#" + menuId).nextAll("div:first:not(#navcontrols)").attr("id");
    else//old id doesn't exist get the first menu item
        menuId = $("#menuArea > div:first:not(#navcontrols)").attr("id");
    
    //check to see if we need to loop around from last to first.
    if(menuId == null)
        menuId = $("#menuArea > div:first:not(#navcontrols)").attr("id");
        
    return menuId;
}

//Gets the id of the previous available menu item after the current one
function getPreviousMenuId()
{
    //Get the id of the next menu div to hightlite
    var menuId = $("#currentMenuId").val();
    if($("#" + menuId).length > 0)//old id exists get the next menu item
        menuId = $("#" + menuId).prevAll("div:first:not(#navcontrols)").attr("id");
    else//old id doesn't exist get the first menu item
        menuId = $("#menuArea > div:first:not(#navcontrols)").attr("id");
    
    //check to see if we need to loop around from last to first.
    if(menuId == null)
        menuId = $("#menuArea > div:not(#navcontrols):last").attr("id");
        
    return menuId;
}

function changeDivOnTimeOut()
{
    //console.log('tab: ' + tabId + ' menu: ' + menuId);
    // hide all the divs
	hideDivs(); 
    highlightTab(getNextMenuId(), getNextTabId());
    //set the id of the visible div to the hidden input control
    $("#currentTabId").val(getNextTabId());
    $("#currentMenuId").val(getNextMenuId());

    //start the timer to continue to the next round
    startTimer();
}

//Click event handler for the menu items.  Clicking an item shows the related tab post.
function show_div(menuId, tabId) 
{
    //Reset the timer object
    window.clearTimeout(timerObject);
    
    // hide all the divs
	hideDivs();    
    //Set the css of the content div's related tab to be highlighted
    highlightTab(menuId, tabId);
    //set the id of the visible div to the hidden input control
    $("#currentTabId").val(tabId);
    $("#currentMenuId").val(menuId);
    
	//Reset the timer object
	startTimer();
}


//Makes the next div in the index visible
function moveNext()
{
    //Reset the timer object
    window.clearTimeout(timerObject);
    //hide all the tabs
    hideDivs();
    //show the next tab
    highlightTab(getNextMenuId(), getNextTabId()); 
    
    //set the id of the visible div to the hidden input control
    $("#currentTabId").val(getNextTabId());
    $("#currentMenuId").val(getNextMenuId());
    
    //Reset the timer object
    startTimer();
}

//Makes the previous div in the index visible
function movePrevious()
{
    //Clear the timeout object during the method
    window.clearTimeout(timerObject);
    //hide all the tabs
    hideDivs();
    //show the next tab
    highlightTab(getPreviousMenuId(), getPreviousTabId()); 
    
    //set the id of the visible div to the hidden input control
    $("#currentTabId").val(getPreviousTabId());
    $("#currentMenuId").val(getPreviousMenuId());

    //Reset the timer object
    startTimer();
}

//toggles the timer and associated button from play to pause
function playPauseTimer(button)
{
    if(timerObject == null)
    {
        timerObject = startTimer();
    }
    else
    {
        button.src = "../../Style%20Library/images/CPSI/playbtn.gif";
        window.clearTimeout(timerObject);
        timerObject = null;
    }       
}

//highlites the specified menu and displays the matching tab.
function highlightTab(menuId, tabId)
{
    $("#menuArea").children("div:not(#navcontrols)").css("backgroundColor", "#32372F");
    $("#menuArea").children("div:not(#navcontrols)").css("color", ""); 

    var menuTab = $("#" + menuId);
    menuTab.css("backgroundColor", "#4d5859");
    menuTab.css("color", "#fff");
    
    $("#" + tabId).css("display", "block");
}