﻿
/// <reference path="../Edentity.Global.js" />
/// <reference path="../External/jquery-1.3.2-vsdoc2.js" />


Edentity.RegisterNamespace("Whiskas.Modules.HomeSplashSelector");

(function(HPS, $) {

    HPS.InitFlashPlayer = function(divSWFFLVPlayer) {
        var flashvars = {};
        var params = {
            menu: "false",
            wmode: "opaque",
            allowscriptaccess: "always"
        };
        var attributes = {
    };
    var swf = $('#' + divSWFFLVPlayer).attr('swf');

    swfobject.embedSWF(swf, divSWFFLVPlayer, "638", "380", "9", "expressInstall.swf", flashvars, params, attributes);
};

var pnlHomeSplashSelector = null;

var mouseOverItems = false;
var AutoShowIndex = 0;
var AutoShowTimeout = 3000; //Miliseconds
var AutoShowInterval;

HPS.OnPlayEnd = function() {

    if (!mouseOverItems) {

        var banners = $('div.Banners > a', pnlHomeSplashSelector);
        var items = $('div.Items > a', pnlHomeSplashSelector);
        var i = pnlHomeSplashSelector.attr('selectedIdx');
        if (i != undefined) {
            $('img', items.eq(i)).eq(1).hide();
        }

        banners.hide();
        $('img:first', items.eq(AutoShowIndex)).next().show();
        banners.eq(AutoShowIndex + 1).show();

        //Save selected
        pnlHomeSplashSelector.attr('selectedIdx', AutoShowIndex);

        if (AutoShowInterval == undefined) {
            //Auto show
            AutoShowInterval = setInterval(function() {
                AutoShowIndex++;
                if (AutoShowIndex == items.size()) {
                    AutoShowIndex = 0;
                }
                Whiskas.Modules.HomeSplashSelector.OnPlayEnd(); //Loop
            }, AutoShowTimeout);
        }
    }
    else {
        clearInterval(AutoShowInterval);
    }
};

HPS.OnInit = function(pnlHomeSplashSelectorID, autoShowTimeout) {

    var div = pnlHomeSplashSelector = $('#' + pnlHomeSplashSelectorID);

    AutoShowTimeout = autoShowTimeout;

    // Setup rollovers
    var banners = $('div.Banners > a', div);
    var items = $('div.Items > a', div);

    items.each(function(idx) {
        var a = $(this);
        $('img:first', a).each(function() {
            $(this).mouseover(function() {

                mouseOverItems = true;

                //Hide the previous
                var i = div.attr('selectedIdx');
                if (i != undefined) {
                    $('img', items.eq(i)).eq(1).hide();
                }

                banners.hide();
                $(this).next().show();
                banners.eq(idx + 1).show();

                //Save selected
                div.attr('selectedIdx', idx);
            });
        });
    });

    //Show default
    div.mouseover(function(event) {
        event.stopPropagation();
    });

    $(document).mouseover(function() {

        if (mouseOverItems) {

            //Hide the previous
            var i = div.attr('selectedIdx');
            if (i != undefined) {
                $('img', items.eq(i)).eq(1).hide();
                banners.hide();
            }
            else {
                div.removeAttr('selectedIdx');
            }
            $('div.Banners > a:first', div).hide();
            $('div.Banners > a:first', div).show();
        }
    });

    $('div.Banners > a:first', div).show();
};

})(Whiskas.Modules.HomeSplashSelector, jQuery);
