﻿if (typeof window.Csp == "undefined") {
    window.Csp = {};
}
Csp.Menu = function(id) {
    this._id = id;
    this._menu = document.getElementById(id);
    this._hider = null;
    this._shower = null;
    $().keypress(Function.createDelegate(this, this.bodyKeyPress)).mouseover(Function.createDelegate(this, this.bodyMouseOver));
    $("a.h15-group", this._menu).click(Function.createDelegate(this, this.linkClick)).mouseover(Function.createDelegate(this, this.triggerShow));
    $("a.h15-link", this._menu).click(Function.createDelegate(this, this.linkClick));
    $("div.h15-menu-container", this._menu).mouseover(Function.createDelegate(this, this.keepMenuVisible));   
};
Csp.Menu.prototype = {
    bodyKeyPress: function(e) {
        if (e.keyCode == 27) {
            var menu = $(".h15-menu-container:visible", this._menu);
            this.hideMenus();
            menu.prev("a.h15-group").each(function() {
                this.focus();
            });
        }
    },
    bodyMouseOver: function(e) {
        this.clearShower();
        this.triggerHide();
    },
    clearHider: function() {
        if (this._hider != null) {
            window.clearTimeout(this._hider);
            this._hider = null;
        }
    },
    clearShower: function() {
        if (this._shower != null) {
            window.clearTimeout(this._shower);
            this.shower = null;
        }
    },
    doubleQuote: function(s) {
        return s.replace("'", "''");
    },
    hideMenus: function(e) {
        $("div.h15-groups table td").removeClass("h15-selected");
        $(".h15-menu-container", this._menu).hide();
    },
    keepMenuVisible: function(e) {
        e.stopPropagation();
        this.clearHider();
        this.clearShower();
    },
    linkClick: function(e) {
        var link = $(e.target);
        var subgroup = link.parents("ul.h15-links").prev("h6.h15-menu-header");
        var group = subgroup.parents("div.h15-menu-container").prev("a.h15-group");
    },
    showFlyout: function(e) {
        var count = $(".h15-menu-container", this._menu).length;
        var index = $(".h15-menu-container", this._menu).index(e);
        if (document.documentElement.dir != "rtl") {
            if (index < 2) {
                this.showFlyoutLeft(e);
            } else if (index < count - 2) {
                if ($(".h15-column:visible", e).length > 1) {
                    this.showFlyoutRightMulti(e);
                } else {
                    this.showFlyoutLeft(e);
                }
            } else {
                if ($(".h15-column:visible", e).length > 1) {
                    this.showFlyoutLeftMulti(e);
                } else {
                    this.showFlyoutRight(e);
                }
            }
        } else {
            if (index < 2) {
                this.showFlyoutRight(e);
            } else if (index < count - 2) {
                if ($(".h15-column:visible", e).length > 1) {
                    this.showFlyoutLeftMulti(e);
                } else {
                    this.showFlyoutRight(e);
                }
            } else {
                this.showFlyoutLeft(e);
            }
        }
        e.show();
        window.setTimeout(function() {
           /* $(".h15-m1, .h15-m2, .h15-m3, .h15-m4", e).css("background", "#ec7125");*/
        },
        0);
    },
    showFlyoutLeft: function(e) {
        if (document.documentElement.dir == "rtl") {
            e.css("left", 220 - e.parent("td").width());
        }
    },
    showFlyoutLeftMulti: function(e) {
        if (document.documentElement.dir == "rtl") {
            e.css("left", 200 - e.parent("td").width());
        } else {
            e.css("left", -200 + e.parent("td").width());
        }
    },
    showFlyoutRight: function(e) {
        if (document.documentElement.dir == "ltr") {
            e.css("left", -220 + e.parent("td").width());
        }
    },
    showFlyoutRightMulti: function(e) {
        e.css("left", -200 + e.parent("td").width());
    },
    showMenu: function(e) {
        this.hideMenus();
        var el = $(e.target);
        el.parent("td").addClass("h15-selected");
        this.showFlyout(el.next(".h15-menu-container"));
        if (typeof el.attr("h15:tag") == "undefined") {
            el.attr("h15:tag", "1");
        }
    },
    toggleMenu: function(e) {
        e.preventDefault();
        this.clearHider();
        this.clearShower();
        var el = $(e.target).next(".h15-menu-container");
        var shown = el.css("display") != "none";
        this.hideMenus();
        if (!shown) {
            el.parent("td").addClass("h15-selected");
            this.showFlyout(el);
        }
    },
    triggerHide: function() {
        if (this._hider == null) {
            this._hider = window.setTimeout(Function.createDelegate(this, this.hideMenus), 1000);
        }
    },
    triggerShow: function(e) {
        e.stopPropagation();
        this.clearHider();
        if (this._shower != null) {
            this.clearShower();
        }
        this._shower = window.setTimeout(Function.createDelegate(this,
        function() {
            this.showMenu(e);
        }), 125);
    }
};
Csp.Menu.layout = function(layout, menu) {
    var columns = $("div.h15-column", menu);  
    var currentHeight = 0;
    var index = 0;
    var height = 0;
    
    $("div.h15-menu", layout).each(function() {

        height = $(this).height() + 20;
         //  alert("currentHeight : " + currentHeight + " @ index : " + index + " @ height : " + height);
        if (currentHeight > 0 && index <= 3) // && (currentHeight + height) > 115)
        {
            index++;
            currentHeight = 0;
        }
        currentHeight += height;
        $(this).appendTo(columns[index]);
    });
    var menuHeight = 0;
    columns.each(function() {
        menuHeight = Math.max(menuHeight, $(this).height());
    });
    var menuWidth = (index + 1) * 200;
    $(menu).height(Math.min(menuHeight, 350) + 10).width(menuWidth).css({
        visibility: "visible"
    });
    
    $(".h15-menu-inner", menu).height(menuHeight);
        
    columns.each(function() {
        var column = $(this);
        var ix = columns.index(this);

        if (ix < index) 
        { column.addClass("h15-dotted"); }
        
        if (ix <= index)
        { column.height(menuHeight); } 
        else
        { column.hide(); }
    });
};