﻿var Custom = {

    // Assign elements in onload event
    Elements: {
        MainImage: null,
        Menu: null,
        News: null,
        League: null,
        Polls: null
    },

    Tween: function(iFrom, iTo, fCallback, oTween, iSpeed) {
        tweenTA = new Tween(new Object(), 'prop', oTween || Tween.strongEaseOut, iFrom, iTo, iSpeed || 0.3);
        tweenTA.onMotionChanged = fCallback;
        tweenTA.start();
    },

    FocusInput: function(oEl, sText) {
        if ($.trim(oEl.value) == sText)
            oEl.value = '';
    },

    BlurInput: function(oEl, sText) {
        if ($.trim(oEl.value) == '')
            oEl.value = sText;
    },

    InitMenu: function() {
        $("#Menu").find(".MenuSub a").each(function() {
            if (location.href.indexOf(this.href) > -1) {
                $(this).parents("LI:eq(1)").addClass("selected");
                $(this).parents("LI:first").addClass("selected");
            }
        });
    },

    SendContactForm: function() {
        var sFormParams = $("#frmContact").serialize();

        var aErrors = [];

        if (jQuery.trim($("#sFirstname").val()) == "")
            aErrors.push("שם פרטי");

        if (jQuery.trim($("#sPhone").val()) == "")
            aErrors.push("טלפון");

        if (aErrors.length) {
            alert("ישנם שדות חובה שלא מילאת:\n\n" + aErrors.join("\n"));
            return;
        }

        // Getting the Transformer Info
        $.ajax({
            type: "POST",
            url: "/he/getXml.aspx",
            dataType: "xml",
            data: "obj=MailSender&sFunc=SendContactForm&" + sFormParams,
            success: function(xml) {
                alert("פנייתך התקבלה במערכת, תודה");
                $("#frmContact").get(0).reset();
            }
        });

    },

    Header: {
        iTotalImages: 0,
        iMainImageIndex: 0,
        iMainImageWidthWithMargings: 962,
        StopInterval: false,
        NextImage: function(bAuto) {

            var iIndex = Custom.Header.iMainImageIndex;
            var iTotal = Custom.Header.iTotalImages;

            if (iTotal == 1)
                return;

            Custom.Header.iMainImageIndex++;
            iIndex++;

            var iStart = ((20000 - ((iIndex) * Custom.Header.iMainImageWidthWithMargings)) * -1);
            var iStop = ((20000 - ((iIndex + 1) * Custom.Header.iMainImageWidthWithMargings)) * -1);

            var oLast = $(Custom.Elements.MainImage).children(":eq(" + (Custom.Header.iTotalImages - 1) + ")");
            var oCur = $(Custom.Elements.MainImage).children(":eq(" + (iIndex - 1) + ")");

            if ($(oLast).get(0) == $(oCur).get(0)) {
                var oFirst = $(Custom.Elements.MainImage).children(":first");
                $(oFirst).insertAfter(oCur);
                iStart -= Custom.Header.iMainImageWidthWithMargings;
                iStop -= Custom.Header.iMainImageWidthWithMargings;
                Custom.Header.iMainImageIndex--;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.MainImage.style.marginLeft = iPos + "px";
            }, Tween.strongEaseOut, 0.4);

            try {

                if (typeof (bAuto) != "boolean") {

                    clearInterval(Custom.HpInterval);
                }

            }
            catch (e) {
            }
        },
        PrevImage: function(bAuto) {

            var iIndex = Custom.Header.iMainImageIndex;
            var iTotal = Custom.Header.iTotalImages;

            if (iTotal == 1)
                return;

            Custom.Header.iMainImageIndex--;


            var iStart = ((20000 - ((iIndex + 1) * Custom.Header.iMainImageWidthWithMargings)) * -1);
            var iStop = ((20000 - ((iIndex) * Custom.Header.iMainImageWidthWithMargings)) * -1);

            var oFirst = $(Custom.Elements.MainImage).children(":first");
            var oCur = $(Custom.Elements.MainImage).children(":eq(" + (iIndex) + ")");

            if ($(oFirst).get(0) == $(oCur).get(0)) {
                iIndex--;
                var oLast = $(Custom.Elements.MainImage).children(":eq(" + (Custom.Header.iTotalImages - 1) + ")");
                iStart = ((20000 - ((iIndex + 1) * Custom.Header.iMainImageWidthWithMargings)) * -1);
                iStop = ((20000 - ((iIndex) * Custom.Header.iMainImageWidthWithMargings)) * -1);

                $(oLast).insertBefore(oCur);
                iStart += 2 * Custom.Header.iMainImageWidthWithMargings;
                iStop += 2 * Custom.Header.iMainImageWidthWithMargings;
                Custom.Header.iMainImageIndex++;
            }


            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.MainImage.style.marginLeft = iPos + "px";
            }, Tween.strongEaseOut, 0.4);


            try {
                if (typeof (bAuto) != "boolean") {
                    clearInterval(Custom.HpInterval);
                }
            }
            catch (e) {
            }
        }
    },

    HP: {

        iTotalLeagueItems: 0,
        iLeaguePageIndex: 0,
        iLeagueWidthWithMargins: 322,
        LeagueNext: function(bAuto) {

            var iIndex = Custom.HP.iLeaguePageIndex;
            var iTotal = Custom.HP.iTotalLeagueItems;

            if (iTotal <= 2)
                return;

            var iNextIndex = 2;

            var iStart = iIndex * Custom.HP.iLeagueWidthWithMargins;
            iIndex += iNextIndex;
            var iStop = iIndex * Custom.HP.iLeagueWidthWithMargins;
            Custom.HP.iLeaguePageIndex = iIndex;
            for (var i = iTotal - iIndex; i <= 2; i++) {
                var oLast = $(Custom.Elements.League).children(".tbl-cont:last");
                var oFirst = $(Custom.Elements.League).children(".tbl-cont:first");
                $(oFirst).insertAfter(oLast);
                iStart -= Custom.HP.iLeagueWidthWithMargins;
                iStop -= Custom.HP.iLeagueWidthWithMargins;
                Custom.HP.iLeaguePageIndex--;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.League.style.marginLeft = (-1 * (20000 - 640 - iPos)) + "px";
            });

        },

        LeaguePrev: function(bAuto) {

            var iIndex = Custom.HP.iLeaguePageIndex;
            var iTotal = Custom.HP.iTotalLeagueItems;

            if (iTotal <= 2)
                return;

            Custom.Header.iNewsPageIndex++;
            var iNextIndex = -2;

            var iStart = iIndex * Custom.HP.iLeagueWidthWithMargins;
            iIndex += iNextIndex;
            var iStop = iIndex * Custom.HP.iLeagueWidthWithMargins;
            Custom.HP.iLeaguePageIndex = iIndex;

            for (var i = iIndex; i < 0; i++) {
                var oLast = $(Custom.Elements.League).children(".tbl-cont:last");
                var oFirst = $(Custom.Elements.League).children(".tbl-cont:first");
                $(oLast).insertBefore(oFirst);
                iStart += Custom.HP.iLeagueWidthWithMargins;
                iStop += Custom.HP.iLeagueWidthWithMargins;
                Custom.HP.iLeaguePageIndex++;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.League.style.marginLeft = (-1 * (20000 - 640 - iPos)) + "px";
            });
        }
    },

    MenuOutInterval: null,
    MenuHover: function() {

        try { clearInterval(Custom.MenuOutInterval); }
        catch (e) { }

        var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
        if (oEl.tagName != "LI" || !$(oEl).hasClass("item"))
            oEl = $(oEl).parents("LI.item:first").get(0);

        $(oEl).parent().children().removeClass("hover");
        $(oEl).addClass("hover");

    },

    MenuOut: function() {

        var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
        if (oEl.tagName != "LI" || !$(oEl).hasClass("item"))
            oEl = $(oEl).parents("LI.item:first").get(0);

        Custom.MenuOutInterval = setInterval(function() {
            $(oEl).removeClass("hover");
        }, 300);
    },

    MenuHoverWithoutOpen: function() {

        try { clearInterval(Custom.MenuOutInterval); }
        catch (e) { }

        var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
        if (oEl.tagName != "LI" || !$(oEl).hasClass("item"))
            oEl = $(oEl).parents("LI.item:first").get(0);
        $(oEl).addClass("dock");

        $(oEl).parent().children().removeClass("hover");
    },

    MenuOutWithoutOpen: function() {
        var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
        if (oEl.tagName != "LI" || !$(oEl).hasClass("item"))
            oEl = $(oEl).parents("LI.item:first").get(0);
        $(oEl).removeClass("dock");
    },

    Gallery: {

        ScrollInterval: null,
        ScrollLeft: function() {
            var oThumbs = $("#gallery-thumbnails").get(0);
            if (oThumbs.scrollLeft > 0) {
                Custom.Gallery.ScrollInterval = setInterval(function() {
                    oThumbs.scrollLeft -= 2;
                    if (oThumbs.scrollLeft == 0)
                        Custom.Gallery.StopScroll();

                }, 2);
            }
        },

        ScrollRight: function() {
            var oThumbs = $("#gallery-thumbnails").get(0);
            if (oThumbs.scrollLeft < oThumbs.scrollWidth) {
                Custom.Gallery.ScrollInterval = setInterval(function() {
                    oThumbs.scrollLeft += 2;
                    if (oThumbs.scrollLeft == oThumbs.scrollWidth)
                        clearInterval(Custom.Gallery.ScrollInterval);

                }, 20);
            }
        },

        StopScroll: function() {
            try {
                clearInterval(Custom.Gallery.ScrollInterval);
            }
            catch (e) {
            }
        },

        SetImage: function(src) {
            var oContainer = $("#gallery-image").css("visibility", "visible");
            $(oContainer).children().remove();
            
            var oObj;

            if (src.indexOf('<') == -1)
                oObj = $(oContainer).append('<img alt="" src="' + src + '" />');
            else
                oObj = $(oContainer).append('<div></div>').html(src);
                
            $(oObj).fadeTo(0, 0.1, function() {
                $(oObj).fadeTo(300,1);
            });
        }


    },

    Polls: {

        iTotalPollItems: 0,
        iIndex: 0,
        iBoxSize: 317,

        Next: function(bAuto) {

            var iIndex = Custom.Polls.iIndex;
            var iTotal = Custom.Polls.iTotalPollItems;

            if (iTotal == 1)
                return;


            Custom.Polls.iIndex++;
            iIndex++;
            var iStart = ((20000 - ((iIndex) * Custom.Polls.iBoxSize)) * -1);
            var iStop = ((20000 - ((iIndex + 1) * Custom.Polls.iBoxSize)) * -1);

            var oLast = $(Custom.Elements.Polls).children(":last");
            var oCur = $(Custom.Elements.Polls).children(":eq(" + (iIndex - 1) + ")");

            if ($(oLast).get(0) == $(oCur).get(0)) {
                var oFirst = $(Custom.Elements.Polls).children(":first");
                $(oFirst).insertAfter(oCur);
                iStart -= Custom.Polls.iBoxSize;
                iStop -= Custom.Polls.iBoxSize;
                Custom.Polls.iIndex--;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.Polls.style.marginLeft = iPos + "px";
            });

        },
        Prev: function(bAuto) {

            var iIndex = Custom.Polls.iIndex;
            var iTotal = Custom.Polls.iTotalPollItems;

            if (iTotal == 1)
                return;

            var iStart = ((20000 - ((iIndex + 1) * Custom.Polls.iBoxSize)) * -1);
            var iStop = ((20000 - ((iIndex) * Custom.Polls.iBoxSize)) * -1);
            Custom.Polls.iIndex--;
            iIndex--;

            if (Custom.Polls.iIndex < 0) {

                var oFirst = $(Custom.Elements.Polls).children(":first");
                var oLast = $(Custom.Elements.Polls).children(":last");
                $(oLast).insertBefore(oFirst);
                iStart += Custom.Polls.iBoxSize;
                iStop += Custom.Polls.iBoxSize;

                Custom.Polls.iIndex++;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.Polls.style.marginLeft = iPos + "px";
            });

        }

    },

    News: {

        IntervalOut: null,
        IntervalOutEl: null,
        InAction: false,

        LastHovered: null,
        iTotalNewsItems: 0,
        iNewsPageIndex: 0,
        iNewsWidthWithMargins: 322,

        Next: function(bAuto) {

            var iIndex = Custom.News.iNewsPageIndex;
            var iTotal = Custom.News.iTotalNewsItems;

            if (iTotal <= 2)
                return;

            Custom.Header.iNewsPageIndex++;
            var iNextIndex = 2;

            var iStart = iIndex * Custom.News.iNewsWidthWithMargins;
            iIndex += iNextIndex;
            var iStop = iIndex * Custom.News.iNewsWidthWithMargins;
            Custom.News.iNewsPageIndex = iIndex;

            for (var i = iTotal - iIndex; i <= 2; i++) {
                var oLast = $(Custom.Elements.News).children(":last");
                var oFirst = $(Custom.Elements.News).children(":first");
                $(oFirst).insertAfter(oLast);
                iStart -= Custom.News.iNewsWidthWithMargins;
                iStop -= Custom.News.iNewsWidthWithMargins;
                Custom.News.iNewsPageIndex--;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.News.style.marginLeft = (-1 * (20000 - 640 - iPos)) + "px";
            });


            if (typeof (bAuto) != "boolean") {
                clearInterval(Custom.HpInterval);
            }

        },

        Prev: function(bAuto) {

            var iIndex = Custom.News.iNewsPageIndex;
            var iTotal = Custom.News.iTotalNewsItems;

            if (iTotal <= 2)
                return;

            Custom.Header.iNewsPageIndex++;
            var iNextIndex = -2;

            var iStart = iIndex * Custom.News.iNewsWidthWithMargins;
            iIndex += iNextIndex;
            var iStop = iIndex * Custom.News.iNewsWidthWithMargins;
            Custom.News.iNewsPageIndex = iIndex;

            for (var i = iIndex; i < 0; i++) {
                var oLast = $(Custom.Elements.News).children(":last");
                var oFirst = $(Custom.Elements.News).children(":first");
                $(oLast).insertBefore(oFirst);
                iStart += Custom.News.iNewsWidthWithMargins;
                iStop += Custom.News.iNewsWidthWithMargins;
                Custom.News.iNewsPageIndex++;
            }

            Custom.Tween(iStart, iStop, function(event) {
                var iPos = event.target._pos;
                Custom.Elements.News.style.marginLeft = (-1 * (20000 - 640 - iPos)) + "px";
            });


            if (typeof (bAuto) != "boolean") {
                clearInterval(Custom.HpInterval);
            }
        },

        oTween: null,
        Hover: function() {

            try { clearInterval(Custom.News.IntervalOut); }
            catch (e) { }

            var oEl = this;
            if ($(oEl).hasClass("inner") || (!oEl))
                return;

            if (Custom.News.oTween) {
                Custom.News.oTween.stop();
                Custom.News.InAction = false;
            }

            oContentItem = $(oEl).find(".new-content");

            if (Custom.News.LastHovered && Custom.News.LastHovered.get(0) != oContentItem.get(0))
                Custom.News.LastHovered.css("height", "23px").find(".arrow-grey-top").removeClass("arrow-grey-top").addClass("arrow-grey-bottom");

            /*
            $(Custom.Elements.News).children().find(".new-content").each(function() {
            try
            {
            if ($(this).parents(".new:first").get(0).className != oEl.className)
            {
            this.style.height = "23px";
            $(this).find(".arrow-grey-top").removeClass("arrow-grey-top").addClass("arrow-grey-bottom");
            }
            }
            catch (e)
            {
            }
            });
            */

            if (Custom.News.InAction)
                return;

            var oContentItem;

            Custom.News.LastHovered = oContentItem;

            oContentItem = oContentItem.get(0);

            if (typeof (oContentItem) == "undefined")
                return;

            var iHeight = oContentItem.style.height.replace(/\D/gi, "") * 1;

            try {
                Custom.News.oTween.stop();
            }
            catch (e) { }

            Custom.News.InAction = true;
            Custom.News.oTween = new Tween(oContentItem.style, 'height', Tween.strongEaseOut, iHeight, 117, 0.15, 'px');
            Custom.News.oTween.onMotionFinished = function() {
                Custom.News.InAction = false;
                $(oEl).find(".arrow-grey-top").removeClass("arrow-grey-top").addClass("arrow-grey-bottom");
            }
            Custom.News.InAction = true;
            Custom.News.oTween.start();

        },
        Out: function() {

            var oEl = window.addEventListener ? arguments[0].target : event.srcElement;
            if ($(oEl).hasClass("inner"))
                return;

            try { clearInterval(Custom.News.IntervalOut); }
            catch (e) { }


            Custom.News.IntervalOut = setInterval(function() {

                oEl = $(oEl).parents(".new:first").get(0);

                if (Custom.News.oTween)
                    Custom.News.oTween.stop();

                var oContentItem = $(oEl).find(".new-content").get(0);

                if (typeof (oContentItem) == "undefined" || !(oContentItem))
                    return;

                var iHeight = oContentItem.style.height.replace(/\D/gi, "") * 1;

                Custom.News.oTween = new Tween(oContentItem.style, 'height', Tween.strongEaseInOut, iHeight, 23, 0.2, 'px');
                Custom.News.oTween.start();
                Custom.News.oTween.onMotionFinished = function() {
                    $(oEl).find(".arrow-grey-bottom").removeClass("arrow-grey-bottom").addClass("arrow-grey-top");
                }
                Custom.News.IntervalOutEl = null;

            }, 320);

        }
    },

    HpIntervalAction: function() {
        Custom.Header.NextImage(true);
        Custom.News.Next(true);
    },

    HpInterval: null,
    InitHP: function() {

        $("#header-prevpic").bind("click", Custom.Header.NextImage);
        $("#header-nextpic").bind("click", Custom.Header.PrevImage);
        Custom.Elements.MainImage = $("#main-image").children(".inn").get(0);
        Custom.Header.iTotalImages = $(Custom.Elements.MainImage).children().length;

        Custom.Elements.League = $("#tables-league").get(0);
        $("#leagues-prev").bind("click", Custom.HP.LeagueNext);
        $("#leagues-next").bind("click", Custom.HP.LeaguePrev);
        Custom.HP.iTotalLeagueItems = $(Custom.Elements.League).children(".tbl-cont").length;

        if ((Custom.HP.iTotalLeagueItems < 6) && (Custom.HP.iTotalLeagueItems > 0)) {
            for (; Custom.HP.iTotalLeagueItems < 6; ) {
                for (var i = 0; i < Custom.HP.iTotalLeagueItems; i++)
                    $("#tables-league").children(".tbl-cont:eq(" + i + ")").clone().appendTo("#tables-league");
                Custom.HP.iTotalLeagueItems = $(Custom.Elements.League).children(".tbl-cont").length;
            }
        }

        Custom.Elements.News = $("#allnews").get(0);

        Custom.News.iTotalNewsItems = $(Custom.Elements.News).children().length;
        if ((Custom.News.iTotalNewsItems < 6) && (Custom.News.iTotalNewsItems > 0)) {
            for (; Custom.News.iTotalNewsItems < 6; ) {
                for (var i = 0; i < Custom.News.iTotalNewsItems; i++)
                    $("#allnews").children(":eq(" + i + ")").clone().appendTo("#allnews");
                Custom.News.iTotalNewsItems = $(Custom.Elements.News).children().length;
            }
        }

        Custom.Elements.News = $("#allnews").get(0);
        $(Custom.Elements.News).find(".new").bind("mouseover", Custom.News.Hover);
        $(Custom.Elements.News).find(".new").bind("mouseout", Custom.News.Out);


        $("#news-next").bind("click", Custom.News.Prev);
        $("#news-prev").bind("click", Custom.News.Next);

        Custom.Elements.Polls = $("#polls-in").get(0);
        Custom.Polls.iTotalPollItems = $(Custom.Elements.Polls).children().length;
        $("#polls-next").bind("click", Custom.Polls.Prev);
        $("#polls-prev").bind("click", Custom.Polls.Next);

        Custom.HpInterval = setInterval(function() {
            Custom.HpIntervalAction();
        }, 7 * 1000);

    },

    Init: function() {

        Custom.Elements.Menu = $("#menu").find("UL:first").get(0);

        $(Custom.Elements.Menu).children("LI").each(function() {
            if ($(this).find("UL").children().length) {
                $(this).bind("mouseover", Custom.MenuHover);
                $(this).bind("mouseout", Custom.MenuOut);
            } else {
                $(this).bind("mouseover", Custom.MenuHoverWithoutOpen);
                $(this).bind("mouseout", Custom.MenuOutWithoutOpen);
            }
        });

        $("<div class=\"hover-first-r\"></div>").insertBefore($("#menu").find("UL LI:first").addClass("first").find("A:first"));
        $("<div class=\"hover-chiquili\"></div>").insertBefore($("#menu").find("UL LI").find("A:first"));

        var iMaxHeight = 0;
        $("#footer-links").children().each(function() {
            iMaxHeight = Math.max(this.offsetHeight, iMaxHeight);
        }).css("height", iMaxHeight + "px");

    },

    IntervalHideLangs: null,
    ShowLangs: function() {
        try { clearInterval(Custom.IntervalHideLangs); }
        catch (e) { }
        $("#lstLanguages").css("display", "block");
    },

    HideLangs: function() {
        try { clearInterval(Custom.IntervalHideLangs); }
        catch (e) { }
        Custom.IntervalHideLangs = setInterval(function() {
            $("#lstLanguages").css("display", "none");
        }, 200);
    }

}
