/*
 * Liberty High School Patriot Music
 * jQuery Slide Show
 * Copyright (c) 2010 Somers Technology
 */

var bannerImages = {"images/slideshow/trumpets.jpg": "Senior trumpet player Alex focuses intently during a performance.",
                    "images/slideshow/marchingband.jpg": "The 2011 Liberty High School Marching Patriots.",
                    "images/slideshow/battery.jpg": "The battery warms up before performing in Bulldog Stadium.",
                    "images/slideshow/opener.jpg": "The Marching Patriots perform their 2011 field show \"Spanish Conquest\".",
                    "images/slideshow/guard.jpg": "Members of the color guard pose for pictures before a football game.",
                    "images/slideshow/pit.jpg": "The pit gives it their all during a competitive performance.",
                    "images/slideshow/sweepstakes.jpg": "The Marching Patriots celebrate after sweeping the sweepstakes awards at Stockdale."};

var bannerIndex = 1;
var bannerSize = 0;
var bannerCycleTime = 5000;

function initBanner()
{   
    // inject images into the dom
    for (var i in bannerImages)
    {
        $("#slideshow").append('<img src="' + i + '" alt="' + bannerImages[i] + '" />');
        $("#slideshow img").css("position", "absolute").css("display", "none");
        $("#slideshow img:first").css("display", "inline").css("z-index", "2");
        bannerSize++;
    }
    
    // inject caption bar into the dom
    $("#slideshow").append('<div id="slideshow-caption"></div>');
    var topMargin = parseInt($("#slideshow").css("height")) - (parseInt($("#slideshow-caption").css("height")) + parseInt($("#slideshow-caption").css("padding-top"))) + "px";
    $("#slideshow-caption").css("position", "absolute").css("margin-top", topMargin).css("z-index", "3").fadeTo(0, 0.75);
    var caption = "";
    for (i in bannerImages)
    {
        $("#slideshow-caption").text(bannerImages[i]);
        break;
    }
    
    setTimeout(rotateBanner, bannerCycleTime);
}

function rotateBanner()
{   
    // calculate next and current indexes
    nextIndex = bannerIndex;
    curIndex = (bannerIndex + (bannerSize - 1)) % bannerSize;
    
    // shuffle the next image in front of the current one
    $("#slideshow img:eq(" + curIndex + ")").css("z-index", "1");
    $("#slideshow img:eq(" + nextIndex + ")").css("z-index", "2");
    
    // do the fade
    $("#slideshow img:eq(" + nextIndex + ")").fadeIn(1000, function()
    {
        // calculate current and previous indexes
        curIndex = bannerIndex;
        prevIndex = (bannerIndex + (bannerSize - 1)) % bannerSize;
        
        // hide the previous image and reset the z-index
        $("#slideshow img:eq(" + prevIndex + ")").css("display", "none").css("z-index", "");
        
        // increment the banner index
        bannerIndex = (bannerIndex + 1) % bannerSize;
        
        // reschedule the timeout
        setTimeout(rotateBanner, bannerCycleTime);
        
        // set the text in the caption bar
        var caption = "";
        var index = 0;
        for (i in bannerImages)
        {
            if (index == curIndex)
            {
                caption = bannerImages[i];
                break;
            }
            index++;
        }
        $("#slideshow-caption").text(caption);
    });
}

