﻿/** 
 *  Includes methods to overload methods from the SharePoint core.js file.
 *  (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATES\LAYOUTS\1033\core.js)
 */

/* Overload SetMenuPosition because using document.body.clientHeight to determine
    maxHeight fails in Firefox if we're not in quirks mode.
*/
function SetMenuPosition(oMaster, oParent, oPopup, oInnerDiv, fFlipTop, fTopLevel) {
    var maxWidth = window.screen.width;
    var maxHeight = window.screen.height;

    var nRealWidth = oPopup.scrollWidth + oPopup.offsetWidth - oPopup.clientWidth;
    var nRealHeight = oPopup.scrollHeight + oPopup.offsetHeight - oPopup.clientHeight;
    var widthTooBig = false;
    var heightTooBig = false;
    if (nRealWidth > maxWidth - 50) {
        widthTooBig = true;
        nRealWidth = maxWidth - 50;
    }
    if (oMaster._fCompactItemsWithoutIcons && nRealHeight >= 375) {
        heightTooBig = true;
        nRealHeight = 375;
    }
    if (nRealHeight >= maxHeight - 50) {
        heightTooBig = true;
        nRealHeight = maxHeight - 50;
    }
    if (!widthTooBig && !heightTooBig) {
        oInnerDiv.style.overflow = "visible";
    }
    else {
        if (browseris.ie) {
            if (widthTooBig) {
                oPopup.style.width = nRealWidth + "px";
                oInnerDiv.style.width = nRealWidth + "px";
                oInnerDiv.style.overflowX = "scroll";
            }
            else {
                oInnerDiv.style.width = nRealWidth + "px";
                oInnerDiv.style.overflowX = "visible";
            }
            if (heightTooBig) {
                oPopup.style.height = nRealHeight + "px";
                oInnerDiv.style.height = nRealHeight + "px";
                oInnerDiv.style.overflowY = "scroll";
            }
            else {
                oInnerDiv.style.height = nRealHeight + "px";
                oInnerDiv.style.overflowY = "visible";
            }
        }
        else {
            oPopup.style.height = nRealHeight + "px";
            oInnerDiv.style.height = nRealHeight + "px";
            oPopup.style.width = nRealWidth + "px";
            oInnerDiv.style.width = nRealWidth + "px";
            oInnerDiv.style.overflow = "auto";
        }
    }
    nRealWidth = oPopup.scrollWidth + oPopup.offsetWidth - oPopup.clientWidth;
    nRealHeight = oPopup.scrollHeight + oPopup.offsetHeight - oPopup.clientHeight;
    var EdgeLeft = 0;
    var EdgeRight = maxWidth;
    var ParentLeft = 0;
    var EdgeTop = 0;
    var ParentTop = 0;
    var oCurrent = oParent;
    if (browseris.safari) {
        if (oCurrent.tagName == "TR" && oCurrent.childNodes.length > 0)
            oCurrent = oCurrent.childNodes[0];
    }
    var p = MenuHtc_GetElementPosition(oCurrent);
    ParentLeft = p.x;
    ParentTop = p.y;
    var nParentWidth;
    if (fTopLevel) {
        nParentWidth = p.width;
        ParentTop += p.height;
        ParentTop -= 1;
    }
    else {
        nParentWidth = p.width + 1;
    }
    var fTryGoDefault = !fFlipTop && !document.body.getAttribute("flipped");
    var fFlippedDefault, fFlippedNonDefault;
    var xDefault, xFlipped;
    if (!oMaster._fIsRtL) {
        var MenuRightDefault;
        var MenuLeftFlipped;
        if (fTopLevel) {
            xDefault = ParentLeft;
            MenuRightDefault = ParentLeft + nRealWidth;
            MenuLeftFlipped = ParentLeft + nParentWidth - nRealWidth;
        }
        else {
            xDefault = ParentLeft + nParentWidth;
            MenuRightDefault = ParentLeft + nParentWidth + nRealWidth;
            MenuLeftFlipped = ParentLeft - nRealWidth;
        }
        xFlipped = MenuLeftFlipped;
        fFlippedDefault = MenuRightDefault > EdgeRight && MenuLeftFlipped > EdgeLeft;
        fFlippedNonDefault = !(MenuLeftFlipped < EdgeLeft && MenuRightDefault < EdgeRight);
    }
    else {
        var MenuLeftDefault;
        var MenuRightFlipped;
        if (fTopLevel) {
            MenuLeftDefault = ParentLeft + nParentWidth - nRealWidth;
            MenuRightFlipped = ParentLeft + nRealWidth;
            xFlipped = ParentLeft;
        }
        else {
            MenuLeftDefault = ParentLeft - nRealWidth;
            MenuRightFlipped = ParentLeft + nParentWidth + nRealWidth;
            xFlipped = ParentLeft + nParentWidth;
        }
        xDefault = MenuLeftDefault;
        fFlippedDefault = MenuLeftDefault < EdgeLeft && MenuRightFlipped < EdgeRight;
        fFlippedNonDefault = !(MenuRightFlipped > EdgeRight && MenuLeftDefault > EdgeLeft);
    }
    var fFlipped = fTryGoDefault ? fFlippedDefault : fFlippedNonDefault;
    var x = fFlipped ? xFlipped : xDefault;
    var xOffset;
    var yOffset;
    if (browseris.nav) {
        xOffset = window.pageXOffset;
        yOffset = window.pageYOffset;
    }
    else {
        var htmlElement = document.body.parentElement;
        if (!IsElementRtl(document.body)) {
            xOffset = document.body.scrollLeft;
            xOffset += htmlElement.scrollLeft;
        }
        else {
            xOffset = -document.body.scrollWidth + document.body.offsetWidth + document.body.scrollLeft;
            xOffset += -htmlElement.scrollWidth + htmlElement.offsetWidth + htmlElement.scrollLeft;
        }
        yOffset = document.body.scrollTop;
        yOffset += htmlElement.scrollTop;
    }
    if (nRealWidth >= maxWidth) {
        x = xOffset;
    }
    else if (x - xOffset + nRealWidth >= maxWidth) {
        x = xOffset + maxWidth - nRealWidth;
    }
    var y;
    if (nRealHeight >= maxHeight) {
        y = yOffset;
    }
    else if (ParentTop + nRealHeight - yOffset >= maxHeight) {
        y = yOffset + maxHeight - nRealHeight;
    }
    else {
        y = ParentTop;
    }
    oPopup.setAttribute("flipped", fFlipTop ? fFlipped && fFlippedDefault : fFlipped);
    var posLeft = Math.max(x, xOffset);
    var posTop = Math.max(y, yOffset);
    oPopup.style.left = posLeft + "px";
    oPopup.style.top = posTop + "px";
}

