﻿ev.getNamespace("eVision.Microsoft.Windays2010").importMembers({
    HorizontalNavigation: function() {
        var Constants = {
            SubmenuActive: "wd-subnav",
            ActiveMark: "wd-navlink1-li-current"
        };

        var mf = {
    };

    return function(id) {
        var that = $id(id);

        that.FirstLevelNodes = that.find(".wd-navlink1-li");
        that.Container = that.parent();
        that.CurrentNode = null;
        that.WasOutside = true;
        that.LastNodeLeft = null;

        that.FirstLevelNodes.each(function(i, domElement) {
            var node = $(domElement);

            node.SubMenu = node.find(".wd-subnav-hidden");

            if (node.hasClass(Constants.ActiveMark)) {
                that.CurrentNode = node;
            }

            node._onmouseenter = function() {

                var tryLeftSubmenuRelativeToParent = Math.round((node.width() - node.SubMenu.width()) / 2);
                var positionToLeftOfContainer = that.position().left + node.position().left;
                var absoluteToLeft = tryLeftSubmenuRelativeToParent + positionToLeftOfContainer;

                if (absoluteToLeft < 0) {
                    tryLeftSubmenuRelativeToParent += -absoluteToLeft; // fix to not left hang
                }
                else { // check right hang
                    var leftRightSpace = that.Container.width() - absoluteToLeft - node.SubMenu.width();

                    if (leftRightSpace < 0) {
                        tryLeftSubmenuRelativeToParent = tryLeftSubmenuRelativeToParent + leftRightSpace;
                    }
                }

                node.SubMenu.css({ left: tryLeftSubmenuRelativeToParent });
                node.SubMenu.addClass(Constants.SubmenuActive);
            };

            node._onmouseleave = function() {
                // if leaving current node
                node.SubMenu.removeClass(Constants.SubmenuActive);
                that.LastNodeLeft = node;
            };

            node.mouseenter(function() {
                // if entering current node
                if (that.WasOutside === true) {
                    if (that.CurrentNode === node) {
                        return;
                    }
                    else {
                        if (that.CurrentNode !== null) {
                            that.CurrentNode._onmouseleave();
                        }
                    }
                    that.WasOutside = false;
                }
                //alert("Enter");
                node._onmouseenter();
            });
            node.mouseleave(function() {
                node._onmouseleave();
                ////alert("Leave");
            });
        });

        that.mouseleave(function() {
            if (that.CurrentNode !== null) {
                that.CurrentNode._onmouseenter();
            }

            that.WasOutside = true;
        });

        if (that.CurrentNode !== null) {
            that.CurrentNode._onmouseenter();
        }

        return that;
    };
} ()
});