﻿var ContentRotator$Timer;
var ContentRotator$TimerInterval;
var ContentRotator$State = 0; // 0 = Paused, 1 = Playing
var ContentRotator$PauseResumeButton;
var ContentRotator$PlayImageUrl = "http://media.journalofaccountancy.com/JOA/images/Featureplaybackcontrol_play.gif";
var ContentRotator$PauseImageUrl = "http://media.journalofaccountancy.com/JOA/images/Featureplaybackcontrol_pause.gif";
function ContentRotator$_GetInterval()
{
    var _element = document.getElementById("TimerInterval");
    var TimerInterval = _element.value;
    TimerInterval = parseFloat(TimerInterval);
    if(!isNaN(TimerInterval))
    {
        return (TimerInterval * 1000);
    }
    else
    {
        return 7500;
    }
}

var ContentRotator$load = function(e)
{
 if(document.getElementById("TimerInterval")==null)
        return;
    ContentRotator$TimerInterval = ContentRotator$_GetInterval();
    ContentRotator$PauseResumeButton = document.getElementById("PauseResumeButton");
    var _image = new Image();
    _image.src = ContentRotator$PlayImageUrl;
    ContentRotator$Show(1);
    ContentRotator$Play();
}

function ContentRotator$Show(pagenum)
{
    var hiddenField = document.getElementById("ContentRotatorCurrentPage");
    hiddenField.value = pagenum.toString();
    
    var button = document.getElementById("ContentRotatorButton" + pagenum);
    button.style.color = "#333333";
    button.style.borderColor = "#333333";
    button.style.cursor = "none";
    
    for(var i = 1; i <= 4; i++)
    {
        if(i != pagenum)
        {
            button = document.getElementById("ContentRotatorButton" + i);
            button.style.color = "#B01823";
            button.style.borderColor = "#333333";
            button.style.cursor = "pointer";
        }
    }
    
    var page;
    for(i = 1; i <= 4; i++)
    {
        page = document.getElementById("ContentRotatorPage" + i);
        page.style.display = "none";
    }
    page = document.getElementById("ContentRotatorPage" + pagenum);
    page.style.display = "block";
}

function ContentRotator$Back()
{
    var hiddenField = document.getElementById("ContentRotatorCurrentPage");
    var pagenum = hiddenField.value.valueOf();
    
    pagenum--;
    
    pagenum = pagenum > 0 ? pagenum : 4;
    
    ContentRotator$Show(pagenum);
}

function ContentRotator$Forward()
{
    var hiddenField = document.getElementById("ContentRotatorCurrentPage");
    var pagenum = hiddenField.value.valueOf();
    
    pagenum++;
    
    pagenum = pagenum <= 4 ? pagenum : 1;
    
    ContentRotator$Show(pagenum);
}

function ContentRotator$Pause()
{
    ContentRotator$PauseResumeButton.title = "Resume";
    ContentRotator$PauseResumeButton.src = ContentRotator$PlayImageUrl;
    window.clearInterval(ContentRotator$Timer);
    ContentRotator$State = 0;
}

function ContentRotator$Play()
{
    ContentRotator$PauseResumeButton.title = "Pause";
    ContentRotator$PauseResumeButton.src = ContentRotator$PauseImageUrl;
    window.clearInterval(ContentRotator$Timer);
    ContentRotator$Timer = window.setInterval("ContentRotator$Forward()", ContentRotator$TimerInterval);
    ContentRotator$State = 1;
}

function ContentRotator$PauseResumeButton_Click()
{
    // Pause
    if(ContentRotator$State == 1)
    {
        ContentRotator$Pause();
    }
    // Resume
    else
    {
        ContentRotator$Play();
    }
}

function ContentRotator$BackButton_Click()
{
    ContentRotator$Back();
    ContentRotator$Pause();
}

function ContentRotator$ForwardButton_Click()
{
    ContentRotator$Forward();
    ContentRotator$Pause();
}

function ContentRotator$Image_Click()
{
    ContentRotator$Pause();
}

// Initialization
if(window.addEventListener)
{
    window.addEventListener("load", ContentRotator$load, false);
}
else if(window.attachEvent)
{
    window.attachEvent("onload", ContentRotator$load);
}