controls.PopUp=new Object();
controls.PopUp.selectedPopupId=-1;
controls.PopUp.selectedPopupPosition=0;
controls.PopUp.selectedPopupVisible=false;
controls.PopUp.lastEventX=-1;
controls.PopUp.lastEventY=-1;
controls.PopUp.TableID='table_popup';
controls.PopUp.selectedPopupContext=null;

controls.PopUp.centerPopUp=function ()
{//debugger;
    var tableid=this.TableID;
    var table=document.getElementById(tableid);
    var tableTapa=document.getElementById(tableid+'_tapa');
    var iframe=document.getElementById(tableid+'_shim');
    if (this.selectedPopupVisible)
    {
        if (this.selectedPopupPosition==2 || this.selectedPopupPosition==3)
        {
            var xCenter=JScriptPage.centerX();
            var yCenter=JScriptPage.centerY();
            table.style.left=xCenter-(table.offsetWidth/2);
            table.style.top=yCenter-(table.offsetHeight/2);
            
            
            tableTapa.style.left=tableTapa.style.top='0px';
            tableTapa.style.width=JScriptPage.clientRight()+'px';
            tableTapa.style.height=JScriptPage.clientBottom()+'px';
            
            tableTapa.style.display='';
//                        tableTapa.style.left=table.style.left;
//                        tableTapa.style.top=table.style.top;
//                        tableTapa.style.width=table.style.width;
//                        tableTapa.style.height=table.style.height;

            iframe.style.left=tableTapa.style.left;
            iframe.style.top=tableTapa.style.top;
            iframe.style.width=tableTapa.style.width;
            iframe.style.height=tableTapa.style.height;
        }else
        if (this.selectedPopupPosition==1)
        {   
            if (lastEventX>0 && lastEventY>0)
            {
                table.style.left=lastEventX+document.body.scrollLeft+5;
                table.style.top=lastEventY+document.body.scrollTop+5;
                
                if (table.offsetLeft+table.offsetWidth>JScriptPage.clientRight())
                    table.style.left=table.offsetLeft-table.offsetWidth-10;
                if (table.offsetTop+table.offsetHeight>JScriptPage.clientBottom())
                    table.style.top=table.offsetTop-table.offsetHeight-10;
            }
            iframe.style.left=table.style.left;
            iframe.style.top=table.style.top;   
            iframe.style.width=table.offsetWidth;
            iframe.style.height=table.offsetHeight;

            tableTapa.style.display='none';
        }
    }
}
controls.PopUp.displayPopUp = function(key,postRenderControl,postRenderInstructions,position,scriptWhenVisible,scriptWhenHidden,e)
{
    this.createPopUpControls();
    lastEventX=e.clientX;
    lastEventY=e.clientY;
    var tableid=this.TableID;
    var spanid=tableid+'_span';
    var table=document.getElementById(tableid);
    var iframe=document.getElementById(tableid+'_shim');
    var innerSpan=document.getElementById(spanid);
   
    this.centerPopUp();
    
    if ( table.style.display!='')
    {
        table.style.display='';
        iframe.style.display='';
    }
    if ((this.selectedPopupId!=key && this.selectedPopupPosition==1) ||innerSpan.innerHTML.length==0 ||!this.selectedPopupVisible)
    {   
//        if (this.selectedPopupVisible)
//            this.hidePopUp(tableid);
        if (this.selectedPopupId!=key || position==2 || position==3 || innerSpan.innerHTML.length==0)
        {
            
            innerSpan.innerHTML="<table cellpadding=0 cellspacing=0><tr><td><img src='img/lupa_contacto_ani.gif' width='20px' height='19px' /></td><td valign='middle'><span style='font-family:Arial;font-size:11px;white-space:nowrap;'>" + /*GetString(MultiLang.StringTableEnum.POR_FAVOR_ESPERE)*/"&nbsp;Por favor espere..." + "</span></td></tr></table>";
            
            var objetoPaso=new Object();
            objetoPaso.tableid=tableid;
            objetoPaso.position=position;
            objetoPaso.scriptWhenVisible=scriptWhenVisible;
            objetoPaso.scriptWhenHidden=scriptWhenHidden;
            objetoPaso.key=key;
            controls.PopUp.selectedPopupContext=objetoPaso;
            var thisTmp=this;
            if (postRenderControl==null || postRenderControl.length==0)
                PopUp.GetPopUpHtml(key,function(valor){thisTmp.paintPopUp.call(thisTmp,valor);},objetoPaso);
            else 
                PopUp.GetPopUpHtmlPostRender(postRenderControl,postRenderInstructions,function(valor){thisTmp.paintPopUp.call(thisTmp,valor);},objetoPaso);
                

        }

        this.selectedPopupId=key;
        this.selectedPopupPosition=position
        this.selectedPopupVisible=true;
        this.centerPopUp();
        if (position!=1)
        {   
            table.style.filter='alpha(opacity=100)';
        }
        else
            table.style.filter='alpha(opacity=91)';
        
    }

    
    
}
controls.PopUp.paintPopUp= function(valor)
{
try {
    if (this.selectedPopupId==valor.context.key)
    {
        var html=valor.value;
        var tableid=valor.context.tableid;
        var position=valor.context.position;
        var table=document.getElementById(tableid);
        var iframe=document.getElementById(tableid+'_shim');
        var innerSpan=document.getElementById(tableid+'_span');

        
        if ( table!=null)
        {
            table.style.width='';
            table.style.height='';
            innerSpan.innerHTML=html!=null?html:valor.error.Message;

            if (position==2) 
                document.getElementById(tableid+'_CloseButton').style.display='';

            eval(valor.context.scriptWhenVisible);

            //setTimeout(this.centerPopUp,10);
        }

    }
}catch(e){}
}

controls.PopUp.hidePopUp= function()
{
    var tableid=this.TableID;
    var table=document.getElementById(tableid); 
    if (table!=null)
    {     
        var tableTapa=document.getElementById(tableid+'_tapa');
        var iframe=document.getElementById(tableid+'_shim');    
        var innerSpan=document.getElementById(tableid+'_span');
        table.style.display=tableTapa.style.display='none';
        iframe.style.display='none';
        document.getElementById(tableid+'_CloseButton').style.display='none';
        this.selectedPopupVisible=false;
        
        if (this.selectedPopUpPosition==2 || this.selectedPopUpPosition==3)
            innerSpan.innerHTML='<span></span>';
        if (controls.PopUp.selectedPopupContext!=null)
            eval(controls.PopUp.selectedPopupContext.scriptWhenHidden);
            
    }
}

controls.PopUp.resetPopUps= function()
{
   controls.PopUp.selectedPopupId=-1; 
}
controls.PopUp.createPopUpControls=function()
{
    var table=document.getElementById(this.TableID);
    if (table==null)
    {
        table =document.createElement( "<table id='" + this.TableID + "' style='position:absolute;z-index:5000;display:none;border:black 1px solid;filter:alpha(opacity=91);' bgcolor='white' >");
        var row=table.insertRow();
        var cell=row.insertCell();
        cell.appendChild(document.createElement("<span id='" +  this.TableID + "_span' >"));
        
        cell=row.insertCell();
        cell.align="center"
        cell.id=this.TableID + '_CloseButton';
        cell.style.display='none';
        cell.style.cursor='pointer';
        cell.vAlign="top";
        cell.appendChild(document.createElement(
            '<img src="img/close.gif" onClick="javascript:controls.PopUp.hidePopUp();">'
            ));
            
        var iframe=document.createElement(
            '<iframe id="' +  this.TableID + '_shim" style="display:none;position:absolute;z-index:4999;border:0px;scrolling:no;filter:alpha(opacity=1);">');
        var tableTapa=document.createElement(
            '<table id="' +  this.TableID + '_tapa" style="position:absolute;z-index:4998;background-color:white;filter:alpha(opacity=65);">');    
        tableTapa.insertRow().insertCell().innerHTML='&nbsp;';
        var form=document.getElementsByTagName('form')[0];
        form.insertBefore(table,form.firstChild);
        form.insertBefore(iframe,form.firstChild);
        form.insertBefore(tableTapa,form.firstChild);
//        form.appendChild(table);
//        form.appendChild(iframe);
//        form.appendChild(tableTapa);
        JScriptPage.attachJSEvent(this,table,'onresize',this.centerPopUp);
        
        JScriptPage.attachJSEvent(this,window,'onscroll',this.centerPopUp);
        JScriptPage.attachJSEvent(this,window,'onresize',this.centerPopUp);
        JScriptPage.attachJSEvent(this,window,'onload',this.hidePopUp);
        
        this.selectedPopupId=-1;
    }
}
controls.PopUp.removePopUpControls=function()
{
    var table=document.getElementById(this.TableID);
    table.parentElement.removeChild(table);
    var iframe=document.getElementById(this.TableID+ '_shim');
    iframe.parentElement.removeChild(iframe);
    var tableTapa=document.getElementById(this.TableID+'_tapa');
    tableTapa.parentElement.removeChild(tableTapa);
}
controls.PopUp.attachPopUp=function(cacheHTMLKey,controlTrigger,controlDisplay,popUpPosition,renderType,postRenderInstructions,scriptWhenVisible,scriptWhenHidden)
{   
    if (controlTrigger!=null)
    {
        var triggerShow="",triggerHide1="",triggerHide2="";
        if (popUpPosition == PopUpPosition.mousePosition)
            triggerShow = "onmousemove";
        else
            triggerShow = "onclick";
            

        JScriptPage.attachJSEvent(this,controlTrigger,triggerShow,function(){this.displayPopUp(cacheHTMLKey,controlDisplay,postRenderInstructions,popUpPosition,scriptWhenVisible,scriptWhenHidden,window.event);},null,'popUpDisplay1'+triggerShow);
        if (popUpPosition == PopUpPosition.mousePosition)
        {
            JScriptPage.attachJSEvent(this,controlTrigger,'onmouseout',function(){if(controls.PopUp.selectedPopupPosition==1) this.hidePopUp();},null,'popUpHide1'+triggerShow);
            //JScriptPage.attachJSEvent(this,controlTrigger,'onmouseup',this.hidePopUp,null,'popUpHide2'+triggerShow);
        }
    }  
}
controls.PopUp.PostRenderInstruction=function()
{
    this.Type=PopUpPostRenderType.Property;
    this.InstructionName="";
    this.InstructionParameters=new Array();
}
controls.PopUp.PostRenderInstructionParameter=function(parameter,type)
{
    this.Parameter=parameter;
    this.TypeString=type;
}
