﻿/// <reference path="jquery-vsdoc.js" />
/// <reference path="lw.js" />

lw.AppendInit(function () {
    var dur = 300;
    var easing = "easeOutQuad";

    $(".mainmenu>li").each(function (i, el) {
        el.menu = createMenu(el, null, i);
    });

    function createMenu(el, parent) {
        var m = {};
        m.el = $(el);
        m._a = $(el).children("a");

        m.div = $(el).children("div.mainmenu-sub");
        m._ol = m.div.children("ol");
        if (m._a.length == 0 || m._ol.length == 0)
            return;

        var span = m._a.children("span");
        /*if (span.length > 0)
        this._a = span;
        */
        m.a = m._a[0];
        m.ol = m._ol[0];

        m.li = m._ol.children("li");
        //alert();
        m.maxHeight = m._ol.outerHeight() + 0; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);

        m.visible = false;
        m.hideMe = false;
        m.parent = parent;
        m.a.menu = m;
        m.div[0].menu = m;

        m.show = function () {
            this.hideMe = false;
            if (this.visible)
                return;
            this.visible = true;

            this.div.css({
                overflow: "visible",
                visibility: "visible",
                height: 0,
                top: this.parent === null ?
				37 :
				this._a.offset().top - $(this.a.parentNode.parentNode).offset().top - 7,
                left: this.parent === null ?
					this._a.offset().left - $(this.a.parentNode.parentNode).offset().left + 10 :
					this._a.outerWidth() + 2,
                width: this.div.width() < this._a.width() ? this._a.width() - 4 : this.div.width()
            });
            this.div.stop();
            var animateTo = { height: this.maxHeight };
            if (!$.browser.msie)
                $.extend(animateTo, { opacity: 1 });
            this.div.animate(animateTo, { duration: dur, easing: easing });
            this._a.addClass("_hover");
        };
        m.hide = function () {
            this.hideMe = true;
            window.setTimeout(function () {
                m._hide();
            }, 50);
        }
        m._hide = function () {
            if (!this.hideMe)
                return;
            this.visible = false;
            for (var i = 0; i < this.children.length; i++) {
                if (this.children[i].visible)
                    return;
            }
            this.div.stop();
            var animateTo = { height: 0 };
            if (!$.browser.msie)
                $.extend(animateTo, { opacity: 0 });

            this.div.animate(animateTo, { duration: dur / 2, easing: easing,
                complete: function () {
                    if (!this.menu.hideMe)
                        return;
                    this.menu.div.css("visibility", "hidden");
                    this.menu._a.removeClass("_hover");
                }
            });
        }
        m.children = [];
        m.li.each(function (i, el) {
            m.children.push(el);
            el.menu = createMenu(el, this);
            var $a = $($(this).children("a"));
            if ($(el).children("div.mainmenu-sub").length == 0)
                $a.addClass("arrow1");
        });
        if (m.children.length > 0 && m.parent != null) {
            m._a.addClass("arrow");
        }
        m._a.bind("mouseover", function () {
            if (this.menu) {
                this.menu.show();
            }
        });
        m._a.bind("mouseout", function () {
            if (this.menu)
                this.menu.hide();
        });
        m.div.bind("mouseover", function () {
            if (this.menu)
                this.menu.show();
        });
        m.div.bind("mouseout", function () {
            if (this.menu)
                this.menu.hide();
        });
        return m;
    }

    function hideMenu(el) {
        window.setTimeout(function () {
            _hideMenu(el);
        }, 200);
    }
    function _hideMenu(el) {
        if (el.childVisible)
            return;
        alert(el);
        el._sub.css("display", "none");
        el._ol.css("height", 0)
        el._a.removeClass("_hover");
    }

    /*
    var NetworkCombo;
    Ext.onReady(function () {
        NetworkCombo = new Ext.form.ComboBox({
            triggerAction: 'all',
            transform: 'Language',
            width: 70,
            editable: false,
            emptyText: "Select a Language",
            listeners: {
                select: function (c, r) {
                    switch (r.id) {
                        case "1":
                            location.href = lw.vroot + "/";
                            break;
                        case "2":
                            location.href = lw.vroot + "/kurdish";
                            break;
                        case "3":
                            location.href = lw.vroot + "/ar";
                            break;
                    }
                }
            }
        });

    });
    */
});
