/*
 * Banner script
 * 
 * Controls the moving banner behavior. Currently using jQuery
 * version 1.4.2.
 */

var bannerNum = 0;     // Number of banner slides
var currentBanner = 1; // Currently selected banner slide
var timer;             // Timer object

/*
 * Cycle Banners
 * 
 * Changes the currently shown banner to the currently
 * selected banner (currentBanner).
 */
function cycleBanner()
{
    // Grab HTML content from #banner-content
    if (msie56)
    {
        // Fade in the new content and fade out the old content
        $("#_banner-content").fadeOut(500, function()
        {
            $(this).html($("#banner-content-" + currentBanner).html());
            $(this).fadeIn(500);
        });
        
        // Move the banner marker
        var newPos = (44 + ((currentBanner - 1) * 60)) + "px";
        $("img#banner-marker").css("left", newPos);
    }
    else
    {
        $("#_banner-content-2").html($("#banner-content-" + currentBanner).html());
            
        // Fade in the new content and fade out the old content
        $("#_banner-content").fadeOut(750);
        $("#_banner-content-2").fadeIn(750, function()
        {
            $("#_banner-content").show();
            $("#_banner-content").html($("#_banner-content-2").html());
            $("#_banner-content-2").hide();
        });
        
        // Move the banner marker
        var newPos = (44 + ((currentBanner - 1) * 63)) + "px";
        if (mobile) newPos = (44 + ((currentBanner - 1) * 64)) + "px";
        
        $("img#banner-marker").css("left", newPos);
    }
}

/*
 * Scroll through banners
 *
 * Will scroll to the next banner (increases
 * currentBanner until it reaches the limit, then goes to 1).
 */
function scrollContent()
{
    currentBanner++;
    if (currentBanner > bannerNum)
    {
        currentBanner = 1;
    }
    cycleBanner();
    timer = setTimeout(scrollContent, 10000);
}

/*
 * Initialization
 *
 * Initializes the banner functionality when
 * the document is done loading.
 */
$(document).ready(function()
{
    // Count and hide the banner slides
    $("[id|=banner-content]").each(function()
    {
        $(this).hide();
        bannerNum++;
    });
    
    if (bannerNum > 1)
    {
        // Grab the first banner HTML content
        $("#_banner-content").html($("#banner-content-1").html());
        $("#_banner-content-2").html($("#banner-content-2").html());
        $("#_banner-content-2").hide();
        $("#banner-button-1 img").show();
        
        // Register the button click event
        $("[id|=banner-button]").click(function()
        {
            // Select the new banner
            var id = $(this).attr("id");
            id = id.substring(14, id.length);
            if (id != currentBanner)
            {
                currentBanner = id;
                cycleBanner();
                
                // Reset the timer
                clearTimeout(timer);
                timer = setTimeout(scrollContent, 10000);
            }
        });
        
        // Start the timer to cycle through slides
        timer = setTimeout(scrollContent, 10000);
    }
});
