﻿$(function() {
    // This is the number of seconds (milliseconds) between transitions in the rotator.
    var rotateDelay = 10000;

    var rotatorData = new Array();

    var objRotator = $('div.rotator');
    var numItemsInSlideshow = $('input[type="hidden"]', objRotator).size();
    var currItemInSlideshow = 3;
    var iframeBanner = $('iframe.banner');

    /* Automatic tranversal through the timeline. */
    var timer = setInterval(function() { TraverseSlideshow(); }, rotateDelay);

    function TraverseSlideshow() {
        // Rewind once the end has been reached.
        currItemInSlideshow--;

        if (currItemInSlideshow < 0)
            currItemInSlideshow = numItemsInSlideshow - 1;

        RotateSlideshow(currItemInSlideshow);
    }

    /* Manual navigation through the timeline. */
    $('div.toolbar ul li a', objRotator).click(function() {
        clearTimeout(timer);

        var currIndex = $('div.toolbar ul li a', objRotator).index(this);

        if (currIndex == currItemInSlideshow)
            return false;

        currItemInSlideshow = currIndex;

        RotateSlideshow(currIndex);

        return false;
    });

    function RotateSlideshow(itemIdx) {
        $(iframeBanner).fadeOut(750, function() {
            $.get($('input[type="hidden"]:eq(' + itemIdx + ')', objRotator).val(), function(data, textStatus) {
                $(iframeBanner).get()[0].contentWindow.self.document.body.innerHTML = data;
                $(iframeBanner).fadeIn(750);

                $('div.toolbar ul li', objRotator).removeClass('lit');
                $('div.toolbar ul li:eq(' + itemIdx + ')', objRotator).addClass('lit');
            });
        });
    }

    $('div.toolbar ul li:last', objRotator).addClass('lit');


    /* Help Us Identify and Find... */
    $('div.scroller').scrollable({
        size: 4
    });
});  