﻿$(document).ready(function() {
    var easeIn = 'linear';
    var easeOut = 'linear';

    //Set Default Timing
    var inSpeed = 250;
    var outSpeed = 250;
    var timerDuration = 5000;

    var bannerBackgrounds = new Array();
    var bannerLink = new Array();
    var current = 0;

    var promoTimer = {};

    $.ajax({
        type: "GET",
        url: "/SilverLib/banners.xml",
        dataType: "xml",
        success: xmlParser
    });

    function xmlParser(xml) {
        var logoBackground = $(xml).find("Logo").find("Background").attr("ImageUrl");
        inSpeed = parseInt($(xml).find("Settings").find("Fading").attr("FadeIn"));
        outSpeed = parseInt($(xml).find("Settings").find("Fading").attr("FadeOut"));
        timerDuration = parseInt($(xml).find("Settings").find("SlideDisplayTime").text());

        $(xml).find("Banner").each(function() {
            var background = $(this).find("Background");
            bannerBackgrounds.push($(background).attr("ImageUrl"));
            bannerLink.push($(this).find("MoreLink").text());
        });

        $("#bannerBackgroundContainer").css("background-image", "url(" + bannerBackgrounds[0] + ")");

        $("#bannerContainer").mouseenter(function() {
            $.clearTimer(promoTimer);
            $(this).css('cursor', 'pointer');
        });

        $('#bannerContainer').mouseleave(function() {
            $(this).css('cursor', 'auto');
            autoMagical();
        });

        $('#bannerContainer').click(function() {
            window.location.href = bannerLink[current];
        });
    }

    function swapBackgrounds(newBackground) {
        $("#bannerContent").animate({ opacity: 0 }, 100, easeIn, function() {
            $("#bannerContent").html(bannerContent[current]);
            $("#bannerBackgroundSwap").css("background-image", "url(" + newBackground + ")");
            $("#bannerBackgroundSwap").animate({ opacity: 1 }, outSpeed, easeIn, function() {
                $("#bannerBackgroundSwap").css({ opacity: 0 });
            });
            $("#bannerBackgroundContainer").animate({ opacity: 0 }, inSpeed, easeIn, function() {
                $("#bannerBackgroundContainer").css("background-image", "url(" + newBackground + ")").css({ opacity: 1 });
            });
        });
    }

    function autoMagical() {
//		if (bannerBackgrounds.length > 1)
			promoTimer = $.timer(timerDuration, function() {
				$.clearTimer(promoTimer);
				if (current < bannerBackgrounds.length - 1) {
					current++;
					swapBackgrounds(bannerBackgrounds[current]);
					autoMagical();
				} else {
					current = 0;
					swapBackgrounds(bannerBackgrounds[current], true);				
					autoMagical();
				}
			});
    }

    autoMagical();
});

/*
* jQuery Timer Plugin
* http://www.evanbot.com/article/jquery-timer-plugin/23
*
* @version      1.0
* @copyright    2009 Evan Byrne (http://www.evanbot.com)
*/

jQuery.timer = function(time, func, callback) {
    var a = { timer: setTimeout(func, time), callback: null }
    if (typeof (callback) == 'function') { a.callback = callback; }
    return a;
};

jQuery.clearTimer = function(a) {
    clearTimeout(a.timer);
    if (typeof (a.callback) == 'function') { a.callback(); };
    return this;
};

