

jQuery(function($) {

Function.createDelegate = function(a, b) {
    return function() {
        return b.apply(a, arguments)
    }
};
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).mouseover(Function.createDelegate(this, this.triggerShow));
    $("a.h15-link", this._menu).click(Function.createDelegate(this, this.linkClick));
    $("div.menu-container", this._menu).mouseover(Function.createDelegate(this, this.keepMenuVisible));
};
Csp.Menu.prototype = {
    bodyKeyPress: function(e) {
        if (e.keyCode == 27) {
            var menu = $(".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.menu-start ").removeClass("h15-selected");

        $(".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.menu-container").prev("a.h15-group");
        this.webTrends3(link.text(), subgroup.text(), group.text());
    },
    showFlyout: function(e) {
        var count = $(".menu-container", this._menu).length;
        var index = $(".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", "#fff");
        },
        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(".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(".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;
        if (currentHeight > 0 && index < 3 && currentHeight + height > 350) {
            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;

var menuWidth = $(".h15-column", menu).width() - 0;
    //console.log($('').width())
    $(menu).height(Math.min(menuHeight, 350) + 20).width(menuWidth).css({
        visibility: "visible"
    });

    	    $(".menu-item", 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();
        }
    });
};


$().ready(function() {
    var menu = new Csp.Menu("mainMenu");
    $("#" + "mainMenu div.menu-item").each(function() {
        var menu = $(this);
        var menuContainer = menu.parent();
        var layout = menuContainer.next("div.h15-layout-container");
        Csp.Menu.layout(layout, menu);
        
        menuContainer.hide();
    });
});	

});
