// Declare timeout variables for delaying the disappearance of the menu
var to_handle = false;
var to_target = false;
var to_delay  = 500; // in milliseconds
var to_call   = "to_target.style.visibility = 'hidden'; to_handle = false;";

// Browser detection
var isIE, isMac, isWin, isSaf;

// We could avoid hardcoding this if we could pull from the class itself
var offSetPadding = 5;

function getNextSibling(objElm, tagSearch) {
// This function returns the next sibling element of a given tag or false if it fails
    var index, nextObj;
    while (nextObj = objElm.nextSibling) {
        if (nextObj && (nextObj.tagName == tagSearch)) { return nextObj; }
    }
    return false;
}

function getNextChild(objElm, tagSearch) {
// This function returns the next child of an element or false if it fails
    var index, objChild;
    if (objElm && objElm.childNodes && objElm.childNodes.length > 0) {
        for (index = 0; index < objElm.childNodes.length; index++) {
            objChild = objElm.childNodes[index];
            if (objChild && (objChild.tagName == tagSearch)) {
                return objElm.childNodes[index];
            }
        }
    }
    return false;
}

function to_onMouseOver(objElm) {
    if (to_handle) {
        clearTimeout(to_handle);
        to_target.style.visibility = "hidden";
		//alert('clearing handle');
    }
    objElm.style.visibility = "visible";
    to_target = objElm;
}

function to_onMouseOut() {
    to_handle = setTimeout(to_call, to_delay);
}

function initMenu(objID, isHorizontal, isAlt) {
// isAlt means offset to the left for Vertical nav, and offset UP for Horizontal nav
// This function loops through the list beginning with the div id of objID.

    var objChild, elm, index, point, start;
    var offSetY    = 0;
    var divIndex   = 0;
    var totalElms  = 0;
    var totalPad   = 0;
    var mainHolder = document.getElementById(objID);
    
    // Detect what browser/os we're dealing with
    isIE  = ((navigator.userAgent.indexOf('MSIE')   != -1) ? true : false);
    isMac = ((navigator.userAgent.indexOf('Mac')    != -1) ? true : false);
    isWin = ((navigator.userAgent.indexOf('Win')    != -1) ? true : false);
    isSaf = ((navigator.userAgent.indexOf('Safari') != -1) ? true : false);
    
    // Yet another IE fix. The parent DIV tends to kill the top and bottom border in IE
    if (isHorizontal && isIE && !isMac) { mainHolder.style.padding = (offSetPadding + 1); }
    
    // Begin at the first UL after the initial DIV
    start = getNextChild(mainHolder, 'UL');

    // Loop through and make changes based on browser and OS
    for (index = 0; index < start.childNodes.length; index++) {

        objChild = start.childNodes[index];
        
        if (objChild && (objChild.tagName == 'LI')) {
            
            // We're altering the DIV and its parent A
            elm = getNextChild(objChild, 'DIV');
            objChild = getNextChild(objChild, 'A');

            totalElms++;

            if (elm) {

                // Set z-index of A
                objChild.style.zIndex = 1000 - divIndex++;

                // Correct appearance in Windows IE ("display: block;" bugfix)
                if (!isHorizontal && isWin && isIE) {

                    // Correct position of A
                    point = getPageXY(objChild);
                    objChild.style.position = "absolute";

                    // If this is the first element, point the offset to the beginning
                    if (offSetY == 0) {
                        setPageXY(objChild, point.x, point.y);
                        offSetY = point.y;
                    } else {
                        setPageXY(objChild, point.x, offSetY);
                    }

                    // Add the height and spacing to the offset
                    offSetY  += (objChild.offsetHeight + offSetSpacing);
                    
                    point = getPageXY(elm);

                    // Correct position of DIV
                    if (isRightAligned) {
                        setPageXY(elm, (point.x - objChild.offsetWidth), offSetY - (objChild.offsetHeight / 2));
                    } else if (isCenterAligned) {
                        setPageXY(elm, (point.x - 100), offSetY - (objChild.offsetHeight / 2));
                    } else {
                        setPageXY(elm, (point.x + 20), offSetY - (objChild.offsetHeight / 2));
                    }
                    
                }
                
                if (isHorizontal) {
                    
                    // Control placement of DIV element
                    point = getPageXY(objChild);
                    
                    if (isRightAligned) {
                        if (isMac && isIE) {
                            point.x += (objChild.offsetWidth / 2);
                        } else if (isSaf) {
                            point.x = point.x - elm.offsetWidth + objChild.offsetWidth;
                        } else {
                            point.x = point.x - elm.offsetWidth + objChild.offsetWidth - (offSetPadding * 2);
                        }
                    }

                    if (isAlt) {
						if (isSaf) {
							point.y -= (elm.offsetHeight + (objChild.offsetHeight * 2));
						} else {
							point.y -= (elm.offsetHeight + objChild.offsetHeight + (offSetPadding * 2));
						}
                    }
                    
                    if (isSaf) {
                        setPageXY(elm, point.x, point.y + (objChild.offsetHeight * 2) - offSetPadding);
                    } else if (isMac && isIE) {
                        setPageXY(elm, point.x, point.y + objChild.offsetHeight);
                    } else {
                        setPageXY(elm, point.x, point.y + objChild.offsetHeight + offSetPadding);
                    }
                    
                } else { // vertical menus
                    // Control placement of DIV element

                    point = getPageXY(objChild);

					point.y -= 3;
                    
                    if (isRightAligned) {
                        if (isMac && isIE) {
                            point.x += (objChild.offsetWidth / 2);
                        } else if (isSaf) {
                            point.x = point.x - elm.offsetWidth + objChild.offsetWidth;
                        } else {
                            point.x = point.x - elm.offsetWidth + objChild.offsetWidth - (offSetPadding * 2);
                        }
                    }

                    if (isAlt) {
						if (isSaf) {
							point.x -= (elm.offsetWidth + (objChild.offsetWidth * 2));
						} else {
							point.x -= elm.offsetWidth + (offSetPadding * 2);
						}
					} else {
						point.x += 20; // #12151#
					}

                    if (isSaf) {
                        setPageXY(elm, point.x, point.y + (objChild.offsetHeight * 2) - offSetPadding);
                    } else if (isMac && isIE) {
                        setPageXY(elm, point.x, point.y + objChild.offsetHeight);
                    } else {
                        setPageXY(elm, point.x, point.y + objChild.offsetHeight + offSetPadding);
                    }
				}
                
            } else {
                
                objChild.style.zIndex = 999;
                
                // Fix the placement of the A
                if (!isHorizontal && isWin && isIE) {
                    point = getPageXY(objChild);
                    objChild.style.position = "absolute";
                    
                    // If this is the first element, point the offset to the beginning
                    if (offSetY == 0) {
                        setPageXY(objChild, point.x, point.y);
                        offSetY = point.y;
                    } else {
                        setPageXY(objChild, point.x, offSetY);
                    }

                    // Add the height and spacing to the offset
                    offSetY += (objChild.offsetHeight + offSetSpacing);

                }
                
            }

            totalPad += (objChild.offsetHeight + offSetSpacing);

        }
        
    }

    if (objChild && !isHorizontal && isWin && isIE) {
        totalPad -= mainHolder.offsetHeight;
        if (mainHolder && mainHolder.style) {
			mainHolder.style.paddingBottom = totalPad + "px";
		}
        //mainHolder.style.paddingBottom = objChild.offsetHeight + (totalElms * offSetSpacing) + "px";
        if (editMode) {
            elm = document.getElementById("buttons-container");
            elm.style.position = "relative";
            elm.style.top = objChild.offsetHeight + offSetSpacing + "px";
        }
    }

}
