﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

// JavaScript functions for ploughmains main page

var currentBannerIndex = 0;
var totalBanners = 0;
var currentlyAnimatingBanner = -1;
var intervalId = 0;
var normalBackground = "#F2E012";
var selectedBackground = "#FFFFFF";

// ----------- Banner rotation code ----------- //

function displayBanner(bannerIndex, isShow) {
    var bannerElement = $(String.format(".jRotator .Images .Item_{0}", bannerIndex)).stop();
    var menuElement = $(String.format(".jRotator .Pager .Item_{0}", bannerIndex)).stop();
    if (isShow) {
        bannerElement.show();
        bannerElement.fadeTo(0, 1.0);
        menuElement.addClass("Selected");
    }
    else {
        bannerElement.hide();
        menuElement.removeClass("Selected");
    }
}

function rotateBanner() {
    scrollBanner((currentBannerIndex + 1) % totalBanners);
}

function startRotator() {
    stopRotator();
    intervalId = setInterval("rotateBanner()", 6000);
}

function stopRotator() {
    if (intervalId != 0)
        clearInterval(intervalId);
    intervalId = 0;
}

function fadeBanner(bannerIndex, isFadeIn, callback) {
    var bannerElement = $(String.format(".jRotator .Images .Item_{0}", bannerIndex)).stop();
    var menuElement = $(String.format(".jRotator .Pager .Item_{0}", bannerIndex)).stop();

    if (isFadeIn) {
        bannerElement.fadeTo(200, 1.0, callback);
        menuElement.addClass("Selected");
    }
    else {
        bannerElement.fadeOut(200, callback);
        menuElement.removeClass("Selected");
    }
}

// hide current banner, fade in banner at targetBanner
function scrollBanner(targetBanner) {
    // don't interfere with animation unless changed mind
    if (targetBanner == currentlyAnimatingBanner)
        return;
    if (targetBanner == currentBannerIndex)
        return; // cannot go 'back' to a banner while animating away from it


    if (currentlyAnimatingBanner >= 0) {
        // if already mid-animation, jump ahead to the end of the last animation to begin this
        displayBanner(currentBannerIndex, false);
        displayBanner(currentlyAnimatingBanner, true);
        currentBannerIndex = currentlyAnimatingBanner;
    }

    currentlyAnimatingBanner = targetBanner;
    var target = String.format(".jRotator .Images .Item_{0}", targetBanner);
    var current = String.format(".jRotator .Images .Item_{0}", currentBannerIndex);

    // let's do this animation
    fadeBanner(currentBannerIndex, false, function() {
        fadeBanner(targetBanner, true, function() {
            displayBanner(currentBannerIndex, false);
            currentBannerIndex = currentlyAnimatingBanner;
            currentlyAnimatingBanner = -1;
        });
    });
}

// ----------- Initialisation code ----------- //

$(function() {
    // if we have any banners, setup the rotator
    $(".jRotator .Images .Item:first").each(function() {
        currentBannerIndex = 0;
        totalBanners = $(".jRotator .Images .Item").size();
        // start automatic rotator IF there is no visible lightbox (ie, if the lightbox is hidden)
        startRotator();
    });

    // setup the right hand menu
    $(".jRotator .Pager .Item").each(function(index) {
        $("a", $(this)).click(function() {
            startRotator();
            scrollBanner(index);
            return false;
        });
    });
});
