var rdCurrPopupPanel
var rdModalShade

function rdShowPopupPanel(elePopupPanel) {
    if (rdCurrPopupPanel) {
        if(!elePopupPanel.id.match('PPDatePickerForInputDate')){    // Fix to let the calendar pop out of a popup panel.
            //Only one panel at a time.
            if (rdCurrPopupPanel!=elePopupPanel) {
                rdCurrPopupPanel.style.display="none"
                if (rdModalShade) {
                    rdModalShade.style.display="none"
                }
            }
        }
    }
    rdCurrPopupPanel=elePopupPanel

    var sPopupId  //Add rdPopup to the ID. For tables, it goes for _Row#
//    if (sPopupPanelId.indexOf("_Row") != -1) {
//        sPopupId = sPopupPanelId.replace("_Row","_rdPopup_Row")
//    } else {
//        sPopupId = sPopupPanelId + "_rdPopup"
//    }
    
    //Set the location.
    var nLeft=0
    var nTop=0
    var sLocation = elePopupPanel.getAttribute("rdLocation")
    switch (sLocation) {
        case 'Center':
            nLeft = (document.body.clientWidth < document.documentElement.offsetWidth ? (document.documentElement.offsetWidth / 2) : (document.body.clientWidth / 2)) - (elePopupPanel.offsetWidth / 2)
            //nTop = (document.body.clientHeight < document.documentElement.offsetHeight ? (document.documentElement.offsetHeight / 2) : (document.body.clientHeight / 2)) - (elePopupPanel.offsetHeight / 2)   // # 11543.
            nTop = (document.documentElement.offsetHeight / 2) - (elePopupPanel.offsetHeight / 2)
            if(navigator.appName.indexOf("Microsoft Internet") == -1)   //#11543.
                nTop = (document.body.clientHeight / 2) - (elePopupPanel.offsetHeight / 2)
	        break;
        case 'Mouse':
            nLeft = rdMouse.x 
            nTop =  rdMouse.y 
	        //Don't position of the screen.
	        if (nLeft + elePopupPanel.offsetWidth > document.body.clientWidth)
	            {nLeft = document.body.clientWidth - elePopupPanel.offsetWidth}
            if (nTop + elePopupPanel.offsetHeight > document.documentElement.offsetHeight)
                {nTop = document.documentElement.offsetHeight - elePopupPanel.offsetHeight}
	        if(navigator.appName.indexOf("Microsoft Internet") == -1){  //#11543.
                if (nTop + elePopupPanel.offsetHeight > document.body.clientHeight)
                    {nTop = document.body.clientHeight - elePopupPanel.offsetHeight}
            }
	        if (nLeft < 0) {nLeft=0}
	        if (nTop < 0) {nTop=0}
	        break;
	    default:  //absolute position
	        var sPos = sLocation.split(",")
	        if (sPos.length == 2) {
	            nLeft = parseInt(sPos[0]);if(isNaN(nLeft)){nLeft=0}
	            nTop = parseInt(sPos[1]);if(isNaN(nTop)){nTop=0}
	        }
	        break;
   }
    rdCurrPopupPanel.style.left = (nLeft + (document.body.scrollLeft == 0 ? document.documentElement.scrollLeft : document.body.scrollLeft)) + "px" //#11612, #11642. 
    rdCurrPopupPanel.style.top = (nTop + (document.body.scrollTop == 0 ? document.documentElement.scrollTop : document.body.scrollTop)) + "px"
    
    if(elePopupPanel.id.match('PPDatePickerForInputDate')){     //#10957.
        rdCurrPopupPanel.style.whiteSpace='nowrap';
        if(elePopupPanel.id.match('rdAgFilterValueDate')){  //#11116.            
            rdCurrPopupPanel.style.width='625px';   
        }
        try{       
            var rdDashboardPanelObj = rdCurrPopupPanel.parentNode;
            var count = 1;
            if(rdDashboardPanelObj){
                while(rdDashboardPanelObj) {
                    if(!rdDashboardPanelObj.id.match("rdDashboardPanel")){
                        rdDashboardPanelObj = rdDashboardPanelObj.parentNode;
                        count++;
                    }
                    else{
                        rdDashboardPanelObj.style.cssText='';
                        break;
                    }
                }                
            }
        }
        catch(e){}
    } 
    
    try{  //#11804.  
        if(!elePopupPanel.id.match('PPDatePickerForInputDate')){
            if(navigator.appName.match('Microsoft') && navigator.appVersion.match('MSIE 7.0')){ 
                if(document.firstChild.text){
                    if(document.firstChild.text.match('DOCTYPE')){       
                        var aStyleSheets = document.styleSheets;
                        for(i=0;i<aStyleSheets.length;i++){
                            var eleStyleSheet = aStyleSheets[i];
                            if(eleStyleSheet.href.match('rdTemplate/rdPopup/rdPopupPanel.css')){
                                eleStyleSheet.addRule(".rdPopupContent", "Height : 85%");
                            }
                        }
                    }
                }
            }
        }
    }
    catch(e){}
    
    if (elePopupPanel.getAttribute("rdModal")=="True") {
        rdModalShade = document.getElementById("rdModalShade")
        if (rdModalShade) {
            rdPopupModalWindowResize()
            if (location.href.indexOf("rdFade")==-1){
                rdSetPanelOpacity(rdModalShade,.5)
            } else {
                rdFadePanel(rdModalShade,0,.5,125)
            }
            rdModalShade.style.display = '';
            
            // Adjust the shading when there's a resize or scroll.
            if (window.attachEvent) { //IE
                window.attachEvent("onresize", rdPopupModalWindowResize);
                window.attachEvent("onscroll", rdPopupModalWindowResize);
            } else { //Mozilla
                window.addEventListener("resize",rdPopupModalWindowResize, false);
                window.addEventListener("scroll",rdPopupModalWindowResize, false);
            }
        }
    }
    
    if (location.href.indexOf("rdFade")!=-1){
        rdFadePanel(rdCurrPopupPanel,0,1,250)
    }
    if (location.href.indexOf("rdExpand")!=-1){
        rdExpandPanel(rdCurrPopupPanel,250)
    }
    
    if (elePopupPanel.getAttribute("rdDraggable")=="True") {
	    var sId = elePopupPanel.id
	    var sTitleBarId = "rdPopupPanelTitle_" + elePopupPanel.id
	    var eleTitleBar = document.getElementById(sTitleBarId)
	    if (eleTitleBar) {
	        Drag.init(eleTitleBar,rdCurrPopupPanel,0,null,0,null,null,null,null,null,50)
        }
    }
}

function rdHidePopupPanel(elePopupPanel) {
    if (rdModalShade) {
        rdModalShade.style.display="none"
        rdModalShade=null
    }
}

function rdPopupModalWindowResize(e)
{
    if (rdModalShade) {            
        var cWidth = document.documentElement.clientWidth;
        if (cWidth == 0) {
        cWidth = document.body.clientWidth
        }
        var cHeight = document.documentElement.clientHeight;
        if (cHeight == 0) {
        cHeight = document.body.clientHeight
        }
        if (cWidth > document.body.scrollWidth) { //'11149.
	        rdModalShade.style.width=cWidth + 'px'
	    } else {
	        rdModalShade.style.width=document.body.scrollWidth + 'px'
	    }
        if (cHeight > document.body.scrollHeight) {
	        rdModalShade.style.height=cHeight + 'px'
	    } else {
	        rdModalShade.style.height=document.body.scrollHeight + 'px'
	    }
    }
}

function rdFadePanel(ele, nOpNext, nOpLast, nDuration, nOpIncrement) {
    if (nOpNext > nOpLast) {
        nOpNext = nOpLast
    }

    rdSetPanelOpacity(ele, nOpNext)
    
    var nFrameRate = 50
    if(!nOpIncrement){
        nOpIncrement = (nOpLast - nOpNext) / (nDuration / nFrameRate) 
    }
    
    nOpNext = nOpNext + nOpIncrement
        
    if (nOpNext <= nOpLast) {
        setTimeout(function(){rdFadePanel(ele, nOpNext, nOpLast, nDuration, nOpIncrement)},nFrameRate)
    }

}
function rdSetPanelOpacity(ele, alpha) {

    var style = ele.style;
    if( style.MozOpacity != undefined ) { //Moz and older
        style.MozOpacity = alpha;
    }
    else if( style.filter != undefined ) { //IE
       // style.height=ele.offsetHeight; //Workaround to make filter work.
        style.filter = "alpha(opacity=" + (alpha * 100) + ")";
        ele.filters.alpha.opacity = ( alpha * 100 );
    }
    else if( style.opacity != undefined ) { //Opera
        style.opacity = alpha;
    }
}

function rdExpandPanel(ele, nDuration, nLoopCnt, nLoopCurr, nLastHeight) {
    var nFrameRate = 50
    if(!nLoopCurr){
        nLoopCurr = 0
        nLoopCnt = Math.round(nDuration / nFrameRate)
        nLastHeight = ele.offsetHeight
    }
    nLoopCurr++
    ele.style.height = nLastHeight / nLoopCnt * nLoopCurr + "px"
    
    eleTable = ele.firstChild
    eleTable.style.position = 'absolute'
    eleTable.style.height = ele.style.height
    
//    alert(ele.style.height + " " + ele.outerHTML)
    
    if (nLoopCurr < nLoopCnt) {
        setTimeout(function(){rdExpandPanel(ele, nDuration, nLoopCnt, nLoopCurr, nLastHeight)},nFrameRate)
    } else {
        ele.style.height = nLastHeight + "px"
        eleTable.style.height = ele.style.height
        eleTable.style.position = ''
    }

}