﻿$ = jQuery;
FG = {

    currentSub: null,
    currentId: 0,
    timeout: null,
    currentHoverLI: null,
    currentSubLI: null,
    selectedTopLink: null,
    selectedSubMenu: null,
    adminMenu: null,
    init: function ()
    {

        $("#subMenu").append($(".subMenu"));
        /* var $adminMenu = $(".subMenu.m16");
        this.adminMenu = $adminMenu;
        $adminMenu.css({ "position": "absolute", "display": "none" });
        $(".adminMenu").append($adminMenu);
        if ($adminMenu.length > 0) {

        $(".adminLink").bind("mouseover", function () {
        $this = $(this);
        $adminMenu.fadeTo(0, 0);
        $adminMenu.show();
        $adminMenu.css({
        "left": $this.position().left,
        "top": 0,
        "z-index": 5000
        });
        $adminMenu.fadeTo(200, 1);
        return false;
        });
            
        $adminMenu.bind("mouseleave", function () {
        $adminMenu.fadeTo(100, 0);
        setTimeout("FG.adminMenu.hide()", 100);
        });
        }
        */
        $("li", $(".subMenu")).bind("click", function ()
        {
            var a = $("a", $(this));
            document.location = a.attr("href");
        });
        $("li", $(".mainMenu")).bind("click", function ()
        {
            var a = $("a", $(this));
            if ($("li", $(".m" + a.attr("id").substr(4))).length > 0)
            {
                return false;
            }
            document.location = a.attr("href");
        });
        $("a", $(".mainMenu")).bind("click", function ()
        {
            FG.menuOver($(this).parent());
            if ($("li", $(".m" + $(this).attr("id").substr(4))).length > 0)
            {
                return false;
            }
        });
        // $(".mainMenuContainer").bind("mouseleave", function (e) { FG.timeout = setTimeout(function () { FG.menuOut(e) }, 200); });
        $("li", $(".mainMenu")).bind("click", function () { FG.menuOver($(this)); });

        try
        {
            if (TOP_SELECTED)
            {

                this.selectedTopLink = $("#lnk_" + TOP_SELECTED).parent();

                if (SUB_SELECTED /*&& SUB_SELECTED != 17*/)
                {
                    this.menuOver(this.selectedTopLink);

                }
            }
        } catch (e) { }

        setTimeout(function () { $(".message").hide("slow"); }, 5000);

        this.ShowModal();

    },
    ShowModal: function ()
    {
        var modal = $(".modal");
        if (modal.length > 0)
        {
            if (this.bg == null)
            {
                var bk = document.createElement("div");
                this.bg = $(bk);
                $("form").append(this.bg);
            }

            this.bg.css({ "position": "fixed", "z-index": 10000, "opacity": 0.5, "background-color": "#000000", "top": 0, "left": 0, "display": "none" });


            this.modal = modal
            $("form").append(this.modal);
            this.modal.css({ "z-index": 10000,
                "position": "absolute"
            });
            // this.bg.fadeTo(0, 0);
            this.bg.show();
            // this.bg.fadeTo(100, 0.7);
            this.modal.fadeTo(0, 0);
            this.modal.show();
            this.modal.css(
        { "top": $(window).height() / 2 + $(window).scrollTop() - this.modal.height() / 2,
            "left": $(window).width() / 2 - this.modal.width() / 2
        });
            setTimeout('FG.modal.fadeTo(500, 1);', 500);

            this.onWindowResize();

            $(window).bind("resize", function () { FG.onWindowResize() });

        }
    },
    onWindowResize: function ()
    {
        /**/
        if (this.bg != null && this.modal != null)
        {
            this.bg.height($(window).height());
            this.bg.width($(window).width());
            this.modal.css(
        { "top": $(window).height() / 2 + $(window).scrollTop() - this.modal.height() / 2,
            "left": $(window).width() / 2 - this.modal.width() / 2
        });
        }

    },
    menuOver: function ($li)
    {


        $item = $("a", $li);
        var id = $item.attr("id").substring(4);
        var $sub = $(".subMenu.m" + id);
        if (this.currentId == id)
        {
            return;
        }
        clearTimeout(this.timeout);

        if (this.currentSub != null)
            this.currentSub.hide();
        if (this.currentHoverLI != null && this.currentHoverLI != this.selectedTopLink)
            this.currentHoverLI.removeClass("selected");

        $li.addClass("selected");
        this.currentHoverLI = $li;

        this.currentId = id;

        $sub.fadeTo(0, 0);
        var left = $li.position().left + $li.width() / 2 - $sub.width() / 2;
        if (left < 0)
            left = 0;
        $sub.css({ "left": left, "top": 0 });
        $sub.fadeTo(300, 1);
        this.currentSub = $sub;
        if (this.selectedTopLink == $li)
        {
            this.selectedSubMenu = $sub;
        }

    },

    menuOut: function (e)
    {

        if (this.currentSub != this.selectedSubMenu)
            this.currentSub.fadeTo(200, 0);
        this.currentId = 0;
        if (this.selectedSubMenu != null)
        {
            this.selectedSubMenu.show();
            this.currentSub = this.selectedSubMenu;
        }
        FG.currentHoverLI.removeClass("selected");
        FG.selectedTopLink.addClass("selected");

    }




}

$(document).ready(function () { FG.init() });
