﻿//<![CDATA[
$(document).ready(function () {
    var campaignCnt = $(".campaignContainer").length;
    if ($(".mainCampBgcolor").length == 1) {
        var bgColor = $.trim($(".mainCampBgcolor").html());
        $("#header-wrapper").css("background-color", bgColor);
    }
    var oldFlashIndx = 0;
    var tmpFlashLink = "";
    var flashTimeOut = 10000;
    var flashZIndx = 10;
    var t;
    $("#campaign-content").click(function () {
        var $tmpEl = $(".campaignContainer:eq(" + oldFlashIndx + ")");
        var $flshLnkEl = $("a.flashlink", $tmpEl);
        if ($flshLnkEl.length > 0) {
            var tmpLink = $.trim($flshLnkEl.attr("href"));
            if (tmpLink.length > 5)
                location.href = tmpLink;
        }
    });

    if (campaignCnt < 2)
        return;

    //        $("#CampaignBottomShelf span img").each(function () {
    //            $(this).hide();
    //        });

    $("#CampaignCircles span img").each(function () {
        $(this).hide();
    });

    //var campaignwidth = $(".FlashPnl").width();
    //$(".campaign-wrapper").width(campaignwidth);

    //$(".campaignContainer").css("{'position' : 'absolute', 'left' : '0px', 'top' : '0px'}");
    $(".campaignContainer:gt(0)").hide();

    //Fix flash page Link
    $("a.flashlink").click(function (e) {
        var $tmpEl = $(".campaignContainer:eq(" + oldFlashIndx + ")");
        var $flshLnkEl = $("a.flashlink", $tmpEl);
        if ($flshLnkEl.length == 0) {
            e.preventDefault();
        }
        else {
            var tmpLink = $.trim($flshLnkEl.attr("href"));
            if (tmpLink.length < 5)
                e.preventDefault();
        }
    });
    

    function loopCampaigns() {
        var $activeEl = $(".campaignContainer:eq(" + oldFlashIndx + ")");
        campaignGoInactive(oldFlashIndx);

        oldFlashIndx++;
        if ((campaignCnt - 1) < oldFlashIndx)
            oldFlashIndx = 0;
        campaignGoActive(oldFlashIndx);

        var $newEl = $(".campaignContainer:eq(" + oldFlashIndx + ")");

        $newEl.show().css("{'position' : 'absolute', 'left' : '0px', 'top' : '0px', 'z-index' : '999999'}");
        $activeEl.fadeOut();
        if (oldFlashIndx == 0)
            $(".campaignContainer:gt(0)").hide();

        t = setTimeout(loopCampaigns, flashTimeOut);
    }

    //        if ($("#CampaignBottomShelf a").length > 0) {
    //            $("#CampaignBottomShelf a").click(function (e) {
    //                e.preventDefault();
    //                var tmpOldFlashIndx = oldFlashIndx;
    //                oldFlashIndx = $("#CampaignBottomShelf a").index(this);
    //                if (oldFlashIndx == tmpOldFlashIndx)
    //                    return;
    //                $(".campaignContainer:eq(" + oldFlashIndx + ")").show().css("{position' : 'absolute', 'left' : '0px', 'top' : '0px'}");
    //                $(".campaignContainer:eq(" + tmpOldFlashIndx + ")").hide();

    //                campaignGoInactive(tmpOldFlashIndx);
    //                campaignGoActive(oldFlashIndx);
    //                clearTimeout(t);
    //                t = setTimeout(loopCampaigns, flashTimeOut);
    //            });
    //        }

    if ($("#CampaignCircles a").length > 0) {
        $("#CampaignCircles a").click(function (e) {
            e.preventDefault();
            var tmpOldFlashIndx = oldFlashIndx;
            oldFlashIndx = $("#CampaignCircles a").index(this);
            if (oldFlashIndx == tmpOldFlashIndx)
                return;
            $(".campaignContainer:eq(" + oldFlashIndx + ")").show().css("{position' : 'absolute', 'left' : '0px', 'top' : '0px'}");
            $(".campaignContainer:eq(" + tmpOldFlashIndx + ")").hide();

            campaignGoInactive(tmpOldFlashIndx);
            campaignGoActive(oldFlashIndx);
            clearTimeout(t);
            t = setTimeout(loopCampaigns, flashTimeOut);
        });
    }

    /***************************************
    Browser window resize event 
    ***************************************/
    // Change background-color on resize
    $(window).resize(function () {
        $(".bgColPnl").css("width", $("#header-wrapper").width() + "px");
    });
    /* END - Browser window resize event
    ______________________________________*/


    // Boook Shelf
    function campaignGoActive(indx) {
        $("#CampaignCircles a:eq(" + indx + ")").addClass("activeImg");

        //        $("#CampaignBottomShelf a img:eq(" + indx + ")").animate({
        //            height: '94px',
        //            width: '106px',
        //            left: '0px'
        //        }, 300);
        //        $("#CampaignBottomShelf span img:eq(" + indx + ")").animate({
        //            height: '118px',
        //            width: '130px',
        //            left: '-12px',
        //            bottom: '0px',
        //            opacity: 1
        //        }, 300, function () {
        //            $(this).css('filter', 'none');
        //        });
        //        $("#CampaignBottomShelf div:eq(" + indx + ")").animate({
        //            bottom: '8px'
        //        }, 300);

        // Fix Background color
        var $bgCol = $.trim($(".mainCampBgcolor:eq(" + indx + ")").html());
        $("#header-wrapper").prepend("<div class='bgColPnl' style='background-color:" + $bgCol + ";display:none;height:" + $("#header-wrapper").height() + "px;width:" + $("#header-wrapper").width() + "px'>&nbsp;</div>");
        $tmpBgColorEl = $(".bgColPnl:first");

        if ($(".bgColPnl").length < 2) {
            $tmpBgColorEl.fadeIn("slow");
        }
        else {
            $tmpBgColorEl.show();
            $(".bgColPnl:gt(0)").each(function () {
                $(this).remove();
            });
        }
    }

    function campaignGoInactive(indx) {
        $("#CampaignCircles a:eq(" + indx + ")").removeClass("activeImg");

        //        $("#CampaignBottomShelf a img:eq(" + indx + ")").animate({
        //            height: '74px',
        //            width: '86px',
        //            left: '10px'
        //        }, 300);
        //        $("#CampaignBottomShelf span img:eq(" + indx + ")").animate({
        //            height: '94px',
        //            width: '106px',
        //            left: '0px',
        //            bottom: '2px',
        //            opacity: 0
        //        }, 300);
        //        $("#CampaignBottomShelf div:eq(" + indx + ")").animate({
        //            bottom: '0px'
        //        }, 300);
    }
    
    // Init 
    t = setTimeout(loopCampaigns, flashTimeOut);
    campaignGoActive(oldFlashIndx);

});
//]]>
