﻿/*
    MenuControlBehavior.js
*/

 /* Bind Application Load Event to method */
Sys.Application.add_init(UpdateMenuAppearance);
					
function UpdateMenuAppearance() {
    var elMenuContainer = $get("ucMenuControl_pnlMenu");
    var elUlMenuItems = $get("elUlMenuItems");

    if (elUlMenuItems && elMenuContainer) {

        var intContainerWidth = elMenuContainer.scrollWidth;
        var intUlWidth = elUlMenuItems.scrollWidth;
        
        var intBrowserDifferenceWidth = 0;
        
        if (Sys.Browser.agent != Sys.Browser.InternetExplorer) {
            intBrowserDifferenceWidth = 20;
        }

        var arrLiElements = elUlMenuItems.getElementsByTagName("li");
        
        if (arrLiElements && arrLiElements.length > 0) {

            /* Calculate PaddingLeft and PaddingRight according to number of MenuItems in the top level. */
            var intCorrectionWidth = Math.round((intContainerWidth - (intUlWidth + intBrowserDifferenceWidth)) / (arrLiElements.length - 1));
            var strPaddingWidth = intCorrectionWidth + "px";

            /* 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;
            }
        }

        /* Calculate MarginLeft for the containing Ul element. */
        intUlWidth = elUlMenuItems.scrollWidth;
        intCorrectionWidth = Math.round((intContainerWidth - intUlWidth) / 2);
        var strMarginWidth = intCorrectionWidth + "px";

        /* Apply margins to Ul element */
        elUlMenuItems.style.marginLeft = strMarginWidth;
    }
}
        

var dropDownStatus = 0;

function showDropDown(el) {
	var dropDownId = el.id.substring(2,4);

	if (dropDownId.indexOf("_") > 0)
	    dropDownId = dropDownId.substring(0,1);

	dropDownStatus = dropDownId;
	
	if (document.getElementById("sub_" + dropDownId)) {
		var dropDown = document.getElementById("sub_" + dropDownId);
		dropDown.style.display = "block";
	}
	
	// hide all others
	var uls = document.getElementsByTagName("ul");
	for (var i=0; i<uls.length; i++) {
		if (uls[i].id.indexOf("sub") > -1 && uls[i].id != "sub_" + dropDownId) {
			uls[i].style.display = "none";
		}
	}
}

function hideDropDown(el) {
	var dropDownId = el.id.substring(2,4);
	
	if (dropDownId.indexOf("_") > 0)
	    dropDownId = dropDownId.substring(0,1);
	
	dropDownStatus = 0;
	
	if (document.getElementById("sub_" + dropDownId)) {
		var dropDown = document.getElementById("sub_" + dropDownId);
		setTimeout(function() {
			if (dropDownStatus == 0) {
			    dropDown.style.display = "none";
			}
		}, 500);
	}
}