﻿$(window).load(function ()
{
    var catSwitchIsDirty = false;
    initNewsSwitch = function ()
    {
        if(document.getElementById("catnewsswitch") != null)
        {
            var newsswitch = $("#catnewsswitch");
            var subtext = $("#catnewsswitch .subtext");
            var largeimage = $("#catnewsswitch .image img");
            var newsswitchmenu = $("#catnewsswitch .menu li");

            newsswitch.removeClass("loading");

            $("#catnewsswitch .newsswitch ul").css("display", "none");

            $("#catnewsswitch .categories li").mouseenter(function (e)
            {
                catSwitchIsDirty = true;
                setSelectedCat(this);
            });

            $("#catnewsswitch .menu li").mouseenter(function (e)
            {
                catSwitchIsDirty = true;
                //set image
                var imgsrc = $(this).attr("imgsrc");
                largeimage.attr("src", imgsrc);

                //set css class
                newsswitchmenu.removeClass("active");
                $(this).addClass("active");

                //set sub text
                subtext.html($(this).attr("subtext"));
                subtext.animate(
                {
                    "bottom": 0
                }, 150);
            });

            $("#catnewsswitch .menu li").mouseleave(function (e)
            {
                subtext.html("");
                subtext.animate(
                {
                    "bottom": -34
                }, 150);
            });

            rotateNewsSwitch();
        }
    };

    var currentCategory = 0;
    rotateNewsSwitch = function ()
    {
        if(!catSwitchIsDirty)
        {
            setSelectedCat($("#catnewsswitch .categories li:eq(" + currentCategory + ")"));
            if (currentCategory > 4)
            {
                currentCategory = 0;
            }
            else
            {
                currentCategory++;
            }
            setTimeout("rotateNewsSwitch()", 3000);
        }
    };

    setSelectedCat = function (elm)
    {
        var catid = $(elm).attr("catid");
        var catname = $(elm).attr("name");
        var imgsrc = $("#" + catid + " li").attr("imgsrc");

        $("#catnewsswitch .newsswitch ul").css("display", "none");
        $("#" + catid).fadeIn(200);
        $("#" + catid + " li").attr("class", "");
        $("#" + catid + " li:eq(0)").attr("class", "active");

        $("#catnewsswitch .image img").attr("src", imgsrc);

        $("#catnewsswitch .categories li").removeClass("active");
        $(elm).addClass("active");

        $("#catnewsswitch .newsswitch .category").text(catname);
    };


    initNewsSwitch();
});
