﻿function ShowActionPopup(addlid, apcid, apid){
    var hgcActionDropdownlistContainer = document.getElementById(addlid);
    var hgcActionPopupContainer = document.getElementById(apcid);
    var htActionPopup = document.getElementById(apid);

    if(hgcActionDropdownlistContainer != null && hgcActionPopupContainer != null){
        hgcActionDropdownlistContainer.style.zIndex = 1;
        
        //alert(htActionPopup.offsetWidth);
        hgcActionPopupContainer.width = (htActionPopup.offsetWidth + 4).toString() + "px";
        //alert(hgcActionPopupContainer.style.width);
        
        //hgcActionPopupContainer.style.top = 0;
        //alert(hgcActionPopupContainer.style.left);
        //alert(hgcActionDropdownlistContainer.offsetWidth + "|" + hgcActionPopupContainer.width.replace("px", ""));
        hgcActionPopupContainer.style.left = (hgcActionDropdownlistContainer.offsetWidth - hgcActionPopupContainer.width.replace("px", "") - 2).toString() + "px";    
        //hgcActionPopupContainer.style.left = "-10px";    
        //alert(hgcActionPopupContainer.style.left);
        hgcActionPopupContainer.style.visibility = "visible";
        hgcActionPopupContainer.style.zIndex = 100;
        
        timeoutActionPopup(addlid, apcid);
    }
}
function CloseActionPopup(addlid, apcid){
    var hgcActionDropdownlistContainer = document.getElementById(addlid);
    var hgcActionPopupContainer = document.getElementById(apcid);
    
    if(hgcActionDropdownlistContainer != null && hgcActionPopupContainer != null){                
        hgcActionDropdownlistContainer.style.zIndex = 0;
        
        hgcActionPopupContainer.style.visibility = "hidden";
        hgcActionPopupContainer.style.zIndex = 0;
    }
}
        

if(document.all || document.getElementById){
    document.onmousemove = captureMousePosition;
}


var xMousePos = 0;
var yMousePos = 0;
var xMousePosMax = 0;
var yMousePosMax = 0;

function timeoutActionPopup(addlid, apcid){
    var hgcActionDropdownlistContainer = document.getElementById(addlid);
    var hgcActionPopupContainer = document.getElementById(apcid);
    
    //alert(hgcActionPopupContainer.offsetLeft + "|" + hgcActionDropdownlistContainer.offsetLeft + "|" + hgcActionDropdownlistContainer.offsetParent.offsetLeft);
    //alert(hgcActionPopupContainer.offsetTop + "|" + hgcActionDropdownlistContainer.offsetTop + "|" + hgcActionDropdownlistContainer.offsetParent.offsetTop);
    
    //alert(hgcActionPopupContainer.scrollLeft + "|" + hgcActionPopupContainer.scrollTop);

    var hasTwoOffsetParents = (hgcActionDropdownlistContainer.offsetParent.offsetParent != null);
    //alert(hgcActionDropdownlistContainer.offsetParent.offsetParent.tagName);
    var boxX, boxY;
    if (hasTwoOffsetParents == true && hgcActionDropdownlistContainer.offsetParent.offsetParent.tagName.toUpperCase() != "HTML") {
        // grid action
        boxX = parseFloat(hgcActionPopupContainer.offsetLeft + hgcActionDropdownlistContainer.offsetLeft + hgcActionDropdownlistContainer.offsetParent.offsetLeft + hgcActionDropdownlistContainer.offsetParent.offsetParent.offsetLeft - hgcActionDropdownlistContainer.offsetParent.offsetParent.scrollLeft);
        boxY = parseFloat(hgcActionPopupContainer.offsetTop + hgcActionDropdownlistContainer.offsetTop + hgcActionDropdownlistContainer.offsetParent.offsetTop + hgcActionDropdownlistContainer.offsetParent.offsetParent.offsetTop - hgcActionDropdownlistContainer.offsetParent.offsetParent.scrollTop);
    }
    else {
        // global action
        boxX = parseFloat(hgcActionPopupContainer.offsetLeft + hgcActionDropdownlistContainer.offsetLeft + hgcActionDropdownlistContainer.offsetParent.offsetLeft);
        boxY = parseFloat(hgcActionPopupContainer.offsetTop + hgcActionDropdownlistContainer.offsetTop + hgcActionDropdownlistContainer.offsetParent.offsetTop);
    }
    var boxW = parseFloat(hgcActionPopupContainer.offsetWidth);
    var boxH = parseFloat(hgcActionPopupContainer.offsetHeight);
    
    //alert(boxW + "|" + hgcActionPopupContainer.width);
    //alert(boxX + "|" + boxY + "|" + boxW + "|" + boxH + "\n" + xMousePos + "|" + yMousePos);
    
//    var hgcMouseX = document.getElementById("hgcMouseX");
//    var hgcBoxY = document.getElementById("hgcBoxY");
//    var hgcBoxW = document.getElementById("hgcBoxW");
//    var hgcBoxH = document.getElementById("hgcBoxH");

//    hgcMouseX.innerHTML = boxX;
//    hgcBoxY.innerHTML = boxY + "|" + hgcActionDropdownlistContainer.offsetParent.offsetParent.scrollTop;
//    hgcBoxW.innerHTML = boxW;
//    hgcBoxH.innerHTML = boxH;
        
    if(xMousePos > boxX && xMousePos < (boxX + boxW) && yMousePos > boxY && yMousePos < (boxY + boxH)){
        //alert("here");
        setTimeout("timeoutActionPopup('" + addlid + "', '" + apcid + "')", 700);
    }
    else{
        //alert("or here");
        CloseActionPopup(addlid, apcid);
    }
}

function captureMousePosition(e){

    //alert(document.body.scrollTop);
    //alert(document.getElementsByTagName("html")(0).scrollTop);
                
    if(document.layers){
        xMousePos = e.pageX; 
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth + window.pageXOffset;
        yMousePosMax = window.innerHeight + window.pageYOffset;
    }
    else{
        if(document.all){
            if(document.body != null){
                xMousePos = window.event.clientX + document.getElementsByTagName("html")(0).scrollLeft;
                yMousePos = window.event.clientY + document.getElementsByTagName("html")(0).scrollTop;
                xMousePosMax = document.body.clientWidth + document.getElementsByTagName("html")(0).scrollLeft;
                yMousePosMax = document.body.clientHeight + document.getElementsByTagName("html")(0).scrollTop;
//                xMousePos = window.event.clientX + document.body.scrollLeft;
//                yMousePos = window.event.clientY + document.body.scrollTop;
//                xMousePosMax = document.body.clientWidth + document.body.scrollLeft;
//                yMousePosMax = document.body.clientHeight + document.body.scrollTop;
            }
        }
        else{
            if(document.getElementById){
                xMousePos = e.pageX;
                yMousePos = e.pageY;
                xMousePosMax = window.innerWidth + window.pageXOffset;
                yMousePosMax = window.innerHeight + window.pageYOffset;
            }
        }
    }
}