﻿Type.registerNamespace("Mcw.UserControls");

Mcw.UserControls.MenuControl = function(element) {
    Mcw.UserControls.MenuControl.initializeBase(this, [element]);
    
    this._arrMenuItems = null;
    this._arrActiveChildMenus = null;
    this._activeChildMenu = null;
    this._blShowDropDownMenus = true;
    this._blDistributeHorizontalSpace = true;
    this._strDropDownBehavior = null;
    this._strHrefIdOfSelectedMenuItem = null;
}
Mcw.UserControls.MenuControl.prototype = {
    initialize: function() {
        Mcw.UserControls.MenuControl.callBaseMethod(this, 'initialize');
        
        // Initialize Arrays.
        this._arrMenuItems = new Array();
        this._arrActiveChildMenus = new Array();
        
        // Retrieve collection of all Href elements.
        this._arrHrefElements = this.get_element().getElementsByTagName("a");
        
        // Loop through collection of Href elements.
        for (var i = 0; i < this._arrHrefElements.length; i++) {
            // Create MenuItem instance and initialize it.
            var menuitem = $create(Mcw.UserControls.MenuItem, null, null, null, this._arrHrefElements[i]);
            
            // Set reference to MenuControl instance.
            menuitem.set_MenuControl(this);
                       
            if (this._blShowDropDownMenus) {
                // Hide child menu if drop down menu's are wanted.
                menuitem.hideChildMenu();
                
                // Add event handlers for drop down menu's if wanted.
                menuitem.addEventHandlers(this._strDropDownBehavior);
                
                // Show the active child menu
                if (this._strDropDownBehavior == "onClick") {
                    if(this._arrHrefElements[i].id == this._strHrefIdOfSelectedMenuItem)
                        menuitem.showChildMenu(this._arrHrefElements[i]);
                }
            }            
            
            // Add MenuItem instance to the MenuItems collection.
            Array.add(this._arrMenuItems, menuitem);
        }   
    
        // Distribute horizontal space if requested.
        if (this._blDistributeHorizontalSpace)
             this.distributeHorizontalSpace();       
    },
    dispose: function() {              
        Mcw.UserControls.MenuControl.callBaseMethod(this, 'dispose');
    },
    distributeHorizontalSpace: function() {
        var arrUlElements = this.get_element().getElementsByTagName("ul");   
            
        if (arrUlElements.length > 0) {
            var elUlMenuItems = arrUlElements[0];                
            var elMenuContainer = this.get_element();
            
            if (elUlMenuItems && elMenuContainer) {                
                // Set correction value if not IE. No longer needed because FF 3 and IE 7.
                //if (Sys.Browser.agent != Sys.Browser.InternetExplorer)
                            
                if (elUlMenuItems.childNodes && elUlMenuItems.childNodes.length > 0) {
                    // Retrieve child nodes.
                    var arrChildNodes = elUlMenuItems.childNodes;
                    var arrLiElements = new Array();
                    
                    // Filter TextNodes for FireFox when determening top level MenuItems Li elements.
                    for (var i = 0; i < elUlMenuItems.childNodes.length; i++) {
                        if (arrChildNodes[i].nodeType != 3)                    
                            Array.add(arrLiElements, arrChildNodes[i]);
                    }
                    
                    // Resest possible set padding values for top level MenuItems Li elements.
                    for (var i = 0; i < arrLiElements.length; i++) {
                        arrLiElements[i].childNodes[0].style.paddingLeft = "0px";
                        arrLiElements[i].childNodes[0].style.paddingRight = "0px";
                    }
                    
                    // Retrieve the width values of the elements.
                    var intContainerWidth = elMenuContainer.scrollWidth;
                    var intUlWidth = elUlMenuItems.scrollWidth;
                                        
                    // Calculate PaddingLeft and PaddingRight according to number of MenuItems in the top level.
                    var intWidthToDistribute = intContainerWidth - intUlWidth;
                    var strPaddingWidth = Math.floor((intWidthToDistribute / arrLiElements.length) / 2) + "px";
                    
                    try {
                        // Apply margins to Href elements in Li elements.
                        for (var i = 0; i < arrLiElements.length; i++) {                            
                            arrLiElements[i].childNodes[0].style.paddingLeft = strPaddingWidth;
                            arrLiElements[i].childNodes[0].style.paddingRight = strPaddingWidth;
                        }
                    } catch(e) { }
                    
                    // Calculate MarginLeft for the containing Ul element.
                    intUlWidth = elUlMenuItems.scrollWidth;
                    intWidthToDistribute = Math.round((intContainerWidth - intUlWidth) / 2);
                    
                    if (intWidthToDistribute > 0) {
                        var strMarginWidth = intWidthToDistribute + "px";

                        // Apply margins to Ul element.                
                        elUlMenuItems.style.marginLeft = strMarginWidth;
                    }
                }                
            }
        }
    },
    hideChildMenus: function(sender) {
        var menuItem;
        
        // Loop through collection of MenuItems.
        for (var i = 0; i < this._arrMenuItems.length; i++) {
            menuItem = this._arrMenuItems[i];
            
            // Hide Child Menu is the sender MenuItem is not equal or a child MenuItem 
            // of current MenuItem from loop. The MenuItem should have a ChildMenu UL element
            // and must be marked as active.
            if (Array.indexOf(menuItem._arrChildMenuItems, sender) == -1 && sender != menuItem) {
                if (menuItem._elChildMenu && menuItem._blIsActive)
                    menuItem.hideChildMenu();
            }
        }
    },
    
    // - Properties -
    set_ShowDropDownMenus: function(value) {
        if (this._blShowDropDownMenus != value)
            this._blShowDropDownMenus = value;
    },
    get_ShowDropDownMenus: function() {
        return this._blShowDropDownMenus;
    },
    set_DistributeHorizontalSpace: function(value) {
        if (this._blDistributeHorizontalSpace != value)
            this._blDistributeHorizontalSpace = value;
    },
    get_DistributeHorizontalSpace: function() {
        return this._blDistributeHorizontalSpace;
    },
    set_DropDownBehavior: function(value) {
        if (this._strDropDownBehavior != value)
            this._strDropDownBehavior = value;
    },
    get_DropDownBehavior: function() {
        return this._strDropDownBehavior;
    },
    set_HrefIdOfSelectedMenuItem: function(value) {
        if (this._strHrefIdOfSelectedMenuItem != value)
            this._strHrefIdOfSelectedMenuItem = value;
    },
    get_HrefIdOfSelectedMenuItem: function() {
        return this._strHrefIdOfSelectedMenuItem;
    }
}

Mcw.UserControls.MenuControl.registerClass("Mcw.UserControls.MenuControl", Sys.UI.Control);


// Add App Load Event handler.
Sys.Application.add_load(FindChildMenuItems);

function FindChildMenuItems() {
    // Retrieve collection of registered Components.
    var arrComponents = Sys.Application.getComponents();
    
    // Loop through collection of registered Components.
    for (var i = 0; i < arrComponents.length; i++) {
        // Retrieve Type name.       
        var type = Object.getType(arrComponents[i]).getName();
        
        // Perform findChildMenuItems method if component is of right type.
        if (type == "Mcw.UserControls.MenuItem")
            arrComponents[i].findChildMenuItems();
    }
}
