﻿// Constants
var BOX_TOTAL = 3;
var TIMER_COUNT = 6000;

// Global Variables
var nextBox = null;
var selectedBox = 1;
var onSelectedBox = false;
var userInitiated = false;

// Timed Rotation
var timer;// = setTimeout('selectBox(getNextBox(), false)', TIMER_COUNT);

// Starts rotation animation
function selectBox(boxNumber, setByUser) {
    if ((boxNumber != selectedBox) && (nextBox == null)) {
        userInitiated = setByUser;
        clearTimeout(timer);
        nextBox = boxNumber;
        new Effect.Fade('box_' + nextBox, {duration:0.3, from:0.9, to:0.3, afterFinish:selectBoxComplete});
//        new Effect.Fade('main_image_' + selectedBox, {duration:0.3});
//        new Effect.Appear('main_image_' + nextBox, {duration:0.3});
    } else if (boxNumber == selectedBox) {
        onSelectedBox = true;
        clearTimeout(timer);
    }
}

// Performs switches necessary for rotation
function selectBoxComplete() {
    // Set new box to full display
    var box = document.getElementById('box_' + nextBox);
    var boxText = document.getElementById('box_' + nextBox + '_text');
    var boxLi = document.getElementById('box_' + nextBox + '_li');
    
    box.style.display = "block";
    box.setOpacity(1);
    boxText.style.display = "block";
    boxLi.className = "active";
    
    // Set old box to partial display
    var oldBoxLi = document.getElementById('box_' + selectedBox + '_li');
    oldBoxLi.className = "";
    document.getElementById('box_' + selectedBox + '_text').style.display = "none";
    
    // Reset variables
    selectedBox = nextBox;
    nextBox = null;
    if (!userInitiated) {
        clearTimeout(timer);
        timer = setTimeout('selectBox(getNextBox())', TIMER_COUNT);
    }
    userInitiated = false;
}

// Returns number of next box in rotation
function getNextBox() {
    if (selectedBox < BOX_TOTAL) {
        return selectedBox + 1;
    } else {
        return 1;
    }
}

// Resets timer when it was holding for user mouseover
function resetTimer() {
    if (onSelectedBox) {
        clearTimeout(timer);
        timer = setTimeout('selectBox(getNextBox())', TIMER_COUNT);
        onSelectedBox = false;
    } else if (userInitiated) {
        onSelectedBox = true;
    }
}

