
// Requires Panic's FancyZoom
var sectionAnimationSteps = 25;
var sectionAnimationStepInterval = 20;
//var sectionAnimationStepInterval = 45;

var autoScrollAnim = {animatedSections:null, containerElement:null, firstSectionElement:null, currentSectionIndex:0, direction:1, autoreverse:false, timer:null, timeOut:0};
var sectionAnimationContext = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function clearSectionAnimationScrollTimer()
{
    if (sectionAnimationContext.timer != null) {
        clearInterval(sectionAnimationContext.timer);
        sectionAnimationContext.timer = null;
    }
}

function doScrollElement(elem, start, end)
{
    clearSectionAnimationScrollTimer();
    sectionAnimationContext.time = 0;
    sectionAnimationContext.begin = start;
    sectionAnimationContext.change = end - start;
    sectionAnimationContext.duration = sectionAnimationSteps;
    sectionAnimationContext.element = elem;
    
    sectionAnimationContext.timer = setInterval("scrollSectionAnimation();", sectionAnimationStepInterval);
}

function scrollSectionAnimation()
{
    if (sectionAnimationContext.time > sectionAnimationContext.duration) {
        clearSectionAnimationScrollTimer();
    } else {
        // FancyZoom function
        var newScrollPos = sineInOut(sectionAnimationContext.time, sectionAnimationContext.begin, sectionAnimationContext.change, sectionAnimationContext.duration);
        sectionAnimationContext.element.scrollLeft = newScrollPos;
        sectionAnimationContext.time++;
    }
}

function scrollToSection(nextSection)
{
    var position = findElementPos(document.getElementById(nextSection));

    if (autoScrollAnim.firstSectionElement != null) {
        var offsetPos = findElementPos(autoScrollAnim.firstSectionElement);
        position[0] = position[0] - offsetPos[0];
    }

    doScrollElement(autoScrollAnim.containerElement, autoScrollAnim.containerElement.scrollLeft, position[0]);
}

function scrollToNextSection()
{
    var nextIndex = autoScrollAnim.currentSectionIndex + autoScrollAnim.direction;
    
    if ((nextIndex >= autoScrollAnim.animatedSections.length) || (nextIndex < 0)) {
        if (autoScrollAnim.autoreverse) {
            autoScrollAnim.direction *= -1;
            nextIndex = nextIndex + autoScrollAnim.direction * 2;
        } else {
            nextIndex = 0;
        }
    }
    
    if (autoScrollAnim.currentSectionIndex != nextIndex) {
        autoScrollAnim.currentSectionIndex = nextIndex;
        
        scrollToSection(autoScrollAnim.animatedSections[nextIndex]);
    }
}

function clearSectionAutoScrollTimer()
{
    if (autoScrollAnim.timer != null) {
        clearInterval(autoScrollAnim.timer);
        autoScrollAnim.timer = null;
    }
}

function resetSectionAutoScrollTimer()
{
    clearSectionAutoScrollTimer();
    autoScrollAnim.timer = setInterval("scrollToNextSection();", autoScrollAnim.timeOut);
}

function SetupSectionScroll(sectionIDs, containerID, firstSectionID, timeOutSeconds, reverse)
{
    autoScrollAnim.animatedSections = sectionIDs;
    autoScrollAnim.containerElement = document.getElementById(containerID);
    autoScrollAnim.firstSectionElement = document.getElementById(firstSectionID);
    autoScrollAnim.direction = 1;
    autoScrollAnim.autoreverse = reverse;
    autoScrollAnim.timeOut = timeOutSeconds * 1000;
    resetSectionAutoScrollTimer();
}

function SetButtonImage(buttonID, image)
{
    var el = document.getElementById(buttonID);
    if (el != null) {
        el.src = image;
    }
}

function ToggleSectionAnimation(buttonID, playingImage, stoppedImage)
{
    if (autoScrollAnim.timer != null) {
        SetButtonImage(buttonID, stoppedImage);
        clearSectionAutoScrollTimer();
    } else {
        SetButtonImage(buttonID, playingImage);
        resetSectionAutoScrollTimer();
    }
}

function SelectNextSection(direction)
{
    var nextIndex = autoScrollAnim.currentSectionIndex + direction;
    if ((nextIndex < autoScrollAnim.animatedSections.length) && (nextIndex >= 0)) {
        autoScrollAnim.currentSectionIndex = nextIndex;
        scrollToSection(autoScrollAnim.animatedSections[nextIndex]);
        if (autoScrollAnim.timer != null) {
            resetSectionAutoScrollTimer();
        }
    }
}


