﻿//document.onmouseup = function () { setTimeout("XibAjax.ShowHideSelect()", 10); }

ShowHideSelect = function () {

    //Only for IE...
    if (navigator.userAgent.toUpperCase().indexOf("MSIE 6") > -1) {
        //Collect all of the relevant divs
        var floatingDivs = document.getElementsByTagName("UL");

        var allSelects = document.getElementsByTagName("SELECT");

        //Show all by default
        for (var i = 0; i < allSelects.length; ++i) {
            if (allSelects[i].style.visibility == "hidden") {
                allSelects[i].style.visibility = "visible";
            }
        }

        if (floatingDivs.length > 0) {
            for (var i = 0; i < floatingDivs.length; ++i) {

                if (floatingDivs[i].className == 'sub-category-container') {

                    for (var j = 0; j < allSelects.length; ++j) {
                        if (ElementOverlap(floatingDivs[i], allSelects[j])) {
                            allSelects[j].style.visibility = "hidden";
                        }
                    }
                }
            }
        }
    }
}

ElementOverlap = function (oElement1, oElement2) {

    var oLeft = new Object();
    oLeft.pos = GetAbsolutePosition(oElement1);
    oLeft.left = oLeft.pos.x;
    oLeft.top = oLeft.pos.y;
    oLeft.height = oElement1.offsetHeight;
    oLeft.width = oElement1.offsetWidth;

    var oRight = new Object();
    oRight.pos = GetAbsolutePosition(oElement2);
    oRight.left = oRight.pos.x;
    oRight.top = oRight.pos.y;
    oRight.height = oElement2.offsetHeight;
    oRight.width = oElement2.offsetWidth;

    oRight.topLeft = new Object();
    oRight.topLeft.x = oRight.left;
    oRight.topLeft.y = oRight.top;

    oRight.topRight = new Object();
    oRight.topRight.x = oRight.left + oRight.width;
    oRight.topRight.y = oRight.top;

    oRight.bottomLeft = new Object();
    oRight.bottomLeft.x = oRight.left;
    oRight.bottomLeft.y = oRight.top + oRight.height;

    oRight.bottomRight = new Object();
    oRight.bottomRight.x = oRight.left + oRight.width;
    oRight.bottomRight.y = oRight.top + oRight.height;


    //Check all 4 points of the right el If any are inside the left el
    //return true;
    if (Contains(oRight.topLeft.x, oRight.topLeft.y,
                        oLeft.left, oLeft.top, oLeft.width, oLeft.height))
        return true;

    if (Contains(oRight.topRight.x, oRight.topRight.y,
                        oLeft.left, oLeft.top, oLeft.width, oLeft.height))
        return true;

    if (Contains(oRight.bottomLeft.x, oRight.bottomLeft.y,
                        oLeft.left, oLeft.top, oLeft.width, oLeft.height))
        return true;

    if (Contains(oRight.bottomRight.x, oRight.bottomRight.y,
                        oLeft.left, oLeft.top, oLeft.width, oLeft.height))
        return true;

    return false;

}

GetAbsolutePosition = function (oElement) {
    var p = Calendar.getAbsolutePos(oElement);


    if ((oElement.ownerDocument.window) && (oElement.ownerDocument.parentWindow != window.top)) {
        var framePos = Calendar.getAbsolutePos(oElement.ownerDocument.parentWindow.frameElement);
        p.x += framePos.x;
        p.y += framePos.y;
    }

    return p;
}

Contains = function (px, py, elx, ely, elw, elh) {

    //debug("px:" + px + ", py:" + py + ", elx:" + elx + ", ely:" + ely + ", elw:" + elw + ", elh:" + elh);

    if ((px >= elx && px <= (elx + elw)) &&
        (py >= ely && py <= (ely + elh))) {
        return true;
    }

    return false;
}
