﻿// ---------------------------------------------------------------------------
//              This document create By Draft. (^_^")..... Version 2
// ---------------------------------------------------------------------------
var ThisDirection;
var ThisObjPane;
var ThisObjButton;
var IsAjaxPopup = new Boolean();
var IsPopupActive = new Boolean();
IsPopupActive = false;

//k document.attachEvent('onclick', hidePopup);
if (document.attachEvent) 
    document.attachEvent('onclick', hidePopup); //k ie
else 
    document.addEventListener('click',hidePopup,true); //k firefox
 
function hidePopup(e)
{
		if (!this.IsPopupActive) return; //ห้าม return false เพราะ module อื่นยังจำเป็นต้องใช้ event นี้อยู่
        //-->check is in divPane
        var objClicked =  e.srcElement;
        if (objClicked.parentElement == this.ThisObjButton)
       {
         return; // idle if click on btnButton, use method showMyPopup() instead.
       }
        var objParent = objClicked.offsetParent;
        while (objParent != null)
        {
            if (this.ThisObjPane == objParent) {
                return;
            }
            objParent = objParent.offsetParent;
        }
        //alert("hide by event clikc another"); 
        hideMyPopup();
};

//--> showMyPopup No Ajax version
function showMyPopupOld(objButton,paneID) 
{
       this.IsAjaxPopup = false; 
        
        if (this.IsPopupActive)
       {    
            var TempObjPaneID = this.ThisObjPane.id;
            hideMyPopup(TempObjPaneID);
            if (TempObjPaneID == paneID) {
                return;
            }
       }
        
        var objPane = getObj(paneID);
        objPane.style.display = "inline";
                      
	    var buttonLeft = objButton.offsetLeft;
	    var buttonTop = objButton.offsetTop + objButton.offsetHeight;
	    
	    var objParent = objButton.offsetParent;
	    while (objParent != null)
	    {
		   buttonLeft += objParent.offsetLeft;
		   buttonTop += objParent.offsetTop;
		   objParent = objParent.offsetParent;
	    }

	    var direction = "L" 
	    if ((buttonLeft + objPane.offsetWidth) >= document.body.clientWidth && (buttonLeft + objButton.offsetWidth - objPane.offsetWidth) > 0)
	    {
		    buttonLeft = buttonLeft + objButton.offsetWidth - objPane.offsetWidth;
		    direction = "R" 
	    }
	   
	   //window.status =  'left = ' + buttonLeft + 'px and top = '+buttonTop + 'px'

	    objPane.style.left = buttonLeft + 'px';
	    objPane.style.top = buttonTop + 'px';
	    
	    showHideCombo(false,objPane); 
	   
	   this.ThisDirection = direction;
	   this.ThisObjPane = objPane; 
	    
	   this.ThisObjPane.style.zIndex = 1000;
	    
	   runStepToShow(direction=='L'?0:objPane.offsetWidth,0);

        return;
};

//--> showMyPopup With Ajax version
function showMyPopup(objButton,pWidth,pHeight)
{
       this.IsAjaxPopup = true;

       if (this.IsPopupActive)
       {
            hideMyPopup();
            if (this.ThisObjButton.id == objButton.id) {
                return;
            }
       }

       //k var objLast = objButton.parentElement.lastChild;
       var objLast;
       if (objButton.parentElement)
            objLast = objButton.parentElement.lastChild;
       else
            objLast = objButton.parentNode.lastChild;
       
       if (!objLast.id || objLast.id.search("divPane") == -1 || objButton.id != this.ThisObjButton.id)
       {
        var RanID = String(Math.random()).replace(".","");
        objButton.id = "btnButton"+RanID;
        ThisObjPane = document.createElement('div');
        ThisObjPane.id = "divPane"+RanID;
        ThisObjPane.style.padding = "0px 5px 5px 0px";
        ThisObjPane.style.filter = "shadow(color=DimGray, direction=135, Strength=4)";
        ThisObjPane.style.position = "absolute";
        ThisObjPane.style.margin = "0px 0px 0px 0px";
        ThisObjPane.style.display = "inline";
        objButton.parentNode.appendChild(ThisObjPane);
 
        var objImg = document.createElement('img');
        objImg.src = "images/close.gif?x="+RanID;
        objImg.alt = "Close";
        objImg.style.cursor = "hand";
        objImg.style.position = "absolute";
        objImg.style.left = (pWidth - 15) +"px";
        objImg.style.top = "5px";
        objImg.style.zIndex = "4";
        objImg.onclick = hideMyPopup;
        ThisObjPane.appendChild(objImg);
       } 
       else
       {
           ThisObjPane.style.display = "inline";
       }
       
        var objAjax = document.createElement("div");
        objAjax.id = "divAjax";
        objAjax.style.border = "solid 1px gray";
        objAjax.style.backgroundColor = "White";
        objAjax.style.width = pWidth+"px";  
        if (pHeight) objAjax.style.height = pHeight+"px"; 
        objAjax.innerHTML = "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/clock.gif'> <b>Loading.....</b>";
        ThisObjPane.appendChild(objAjax);

	    var buttonLeft = objButton.offsetLeft;
	    var buttonTop = objButton.offsetTop + objButton.offsetHeight;
	    
	    var objParent = objButton.offsetParent;
	    while (objParent != null)
	    {
            buttonLeft += objParent.offsetLeft;
            buttonTop += objParent.offsetTop;
		    objParent = objParent.offsetParent;
	    }

	    var direction = "L" 
	    if ((buttonLeft + ThisObjPane.offsetWidth) >= document.body.clientWidth && (buttonLeft + objButton.offsetWidth - ThisObjPane.offsetWidth) > 0)
	    {
		    buttonLeft = buttonLeft + objButton.offsetWidth - ThisObjPane.offsetWidth;
		    direction = "R" 
	    }
	   
	   //window.status =  'left = ' + buttonLeft + 'px and top = '+buttonTop + 'px'

	    ThisObjPane.style.left = buttonLeft + 'px';
	    ThisObjPane.style.top = buttonTop + 'px';
	    
	    showHideCombo(false,ThisObjPane); 
	   
	   this.ThisDirection = direction;
	   //this.ThisObjPane = ThisObjPane; 
	    
	   this.ThisObjPane.style.zIndex = 1000;
	    
	   runStepToShow(direction=='L'?0:ThisObjPane.offsetWidth,0);
	   
       this.ThisObjButton = objButton;
       objButton = null;

       return false;
};

function hideMyPopup()
{
    showHideCombo(true,ThisObjPane);
    ThisObjPane.style.display = "none";
    if (IsAjaxPopup) //if old version it have not last child 
        ThisObjPane.removeChild(ThisObjPane.lastChild);
    IsPopupActive = false;
    return;
}

function showHideCombo(isShow)
{
		//k var combos = document.all.tags("select"); 
		var combos = document.getElementsByTagName("select");
		for (var i = 0; i < combos.length; i++)
		{
			if (this.isOverlap(combos[i], ThisObjPane))
			{
			    if (combos[i].id.search("_InDiv") == -1 ) {
			        combos[i].style.visibility = isShow?"visible":"hidden";
			    }
			}
		}
};

function isOverlap(combo)
{
		var paneLeft = ThisObjPane.offsetLeft;
		var paneRight = ThisObjPane.offsetLeft+ThisObjPane.offsetWidth;
		var paneTop = ThisObjPane.offsetTop;
		var paneBottom = ThisObjPane.offsetTop+ThisObjPane.offsetHeight;

	    var comboLeft = combo.offsetLeft;
	    var comboTop = combo.offsetTop + combo.offsetHeight;
	    var comboParent = combo.offsetParent;
	    while (comboParent != null)
	    {
		    comboLeft += comboParent.offsetLeft;
		    comboTop += comboParent.offsetTop;
		    comboParent = comboParent.offsetParent;
	    }

	   var comboRight = comboLeft+combo.offsetWidth;
	   var comboBottom = comboTop+combo.offsetHeight;
	    
        var isOverlap = true;
	    if (comboLeft >paneRight || comboRight < paneLeft || comboTop > paneBottom || comboBottom < paneTop)
	    {
		    isOverlap =  false;
	    }
	    return isOverlap;
};

function runStepToShow(pWidth,pHeight)
{
	var stepx = Math.ceil(this.ThisObjPane.offsetWidth / 10);
	var stepy = Math.ceil(this.ThisObjPane.offsetHeight / 10);

	if (this.ThisDirection == 'L' && (pWidth < this.ThisObjPane.offsetWidth || pHeight < this.ThisObjPane.offsetHeight))
	{
		pWidth += stepx;
		pHeight += stepy;
		this.ThisObjPane.style.clip = "rect(auto, " + pWidth + "px, " + pHeight + "px, auto)";
		objTimer = setTimeout("runStepToShow(" + pWidth + ", " + pHeight+");", 0);
	}
	else if (this.ThisDirection == 'R' && (pWidth > 0 || pHeight < this.ThisObjPane.offsetHeight))
	{
		pWidth -= stepx;
		pHeight += stepy;
		this.ThisObjPane.style.clip = "rect(auto, " + this.ThisObjPane.offsetWidth + "px, " + pHeight + "px, " + pWidth + "px)";
		objTimer = setTimeout("runStepToShow(" + pWidth + ", " + pHeight+");", 0);
	}
	else
	{
	    clearTimeout(objTimer);
	    this.ThisObjPane.style.clip = 'rect(auto, auto, auto, auto)';
	    IsPopupActive = true; 
	}
	
};