﻿var __WindowDependency = new Array();
var __HtmlOriginal = new Array();
var __HashTamanhos = new Hashtable();
var __HashFade = new Hashtable();
var __tempIndex = 1;

Window = function(){
    this.__frmValues = {};
    this.__setIframes = false;
};

Window.prototype.create = function(id, titulo, altura, largura, btnFechar, btnMinimizar, fade){
    var sb = new StringBuilder();
    sb.append('<span id="' + id + '_move">');
    sb.append('<table style="CURSOR: move" cellSpacing="0" cellPadding="2" width="100%" border="0">');
    sb.append('<tr><td style="FONT-SIZE: 11px; FONT-FAMILY: tahoma; BACKGROUND-COLOR: buttonface;" width="0px"');
    sb.append('height="20px">&nbsp;<font color=#000000><span id="' + id + 'title"></span></font>');
    sb.append('</td>');
    if(btnFechar && btnMinimizar){
        sb.append('<td width="10" align="right"><table><tr><td><img style="CURSOR: pointer" onclick="javascript:win.minimize(\'' + id + '\', ' + altura + ');" alt="" src="images/cima_fechar.gif" border="0" id="____imgse' + id + '"></td><td><img onclick="javascript:win.close(\'' + id + '\')" style="CURSOR: pointer" alt="Fechar Janela"');
        sb.append('src="images/fechar.gif" border="0" /></td></tr></table>');
        sb.append('</td>');	
    }else if(btnFechar){
    	sb.append('<td width="10" align="right"><table><tr><td><img onclick="javascript:win.close(\'' + id + '\')" style="CURSOR: pointer" alt="Fechar Janela"');
        sb.append('src="images/fechar.gif" border="0" /></td></tr></table>');
        sb.append('</td>');	
    }else if(btnMinimizar){
    	sb.append('<td width="10" align="right"><table><tr><td><img style="CURSOR: pointer" onclick="javascript:win.minimize(\'' + id + '\', ' + altura + ');" alt="" src="images/cima_fechar.gif" border="0" id="____imgse' + id + '"></td></tr></table>');
        sb.append('</td>');
    }else{
	    sb.append('<td width="15" align="right"></td>');		
    }
    
    __HashTamanhos.add(id, altura);
    __HashFade.add(id, fade);
    
    sb.append('</tr></table></span>');	
    sb.append('<table align=center cellspacing=0 cellpadding=0 width=100%><tr><td width="3px"></td><td><div id="__bordainterna' + id + '" style="BORDER-RIGHT: 1px inset; BORDER-TOP: 1px inset; PADDING-BOTTOM: 0px; BORDER-LEFT: 1px inset; WIDTH: ' + (parseInt(largura) - 8) + 'px; HEIGHT: ' + (parseInt(altura) - 29) + 'px; PADDING-TOP: 0px; BORDER-BOTTOM: 1px inset;">');
    sb.append('<span id="' + id + 'conteudo" selecionavel="true"></span>'); 		
    sb.append('</div></td></tr></table>');
    
    var newObj = document.createElement('div');
    newObj.id = id;
    newObj.style.position = "absolute";
    newObj.style.display = "none"; 
    newObj.style.background = "buttonface";
    newObj.style.borderBottom = "2px outset";
    newObj.style.borderRight = "2px outset";
    newObj.style.borderLeft = "2px outset";
    newObj.style.borderTop = "2px outset";
    newObj.style.zIndex = 1;
    newObj.style.width = largura + "px";
    newObj.style.height = altura + "px";
    //alert(screen.width);
    newObj.style.left = (screen.width-largura)/2 + "px";
    newObj.style.top = (screen.height-altura)/4 + "px";
    newObj.innerHTML = sb.toString();	        
    document.body.appendChild(newObj);
    
    if(Request.browser.type.isIe){
        if(Request.browser.version < 7){
            this.__setIframes = true;
            var newObjIframe = document.createElement('iframe');
            newObjIframe.id = id + '_iframe';
            newObjIframe.style.position = "absolute";
            newObjIframe.style.display = "none"; 
            newObjIframe.style.zIndex = 1;
            newObjIframe.style.width = (largura + 3) + "px";
            newObjIframe.style.height = (altura) + "px";
            newObjIframe.frameBorder = "0";
            newObjIframe.scrolling = "no";
            newObjIframe.style.left = (screen.width-largura)/2 + "px";
            newObjIframe.style.top = (screen.height-altura)/4 + "px";	
            document.body.appendChild(newObjIframe); 
        }   
    }
    
    var d = new Dragger($(id), moveDirection.both, null, $(id + '_move'));
        
    $(id + 'title').innerHTML = titulo;
    $(id).style.zIndex = 1;
    //if(fade){
        d.setOpacity(0.5);
    //}
};

Window.prototype.closeAll = function(){
    for(var i = 0; i < __HashTamanhos.keys.length; i++){
        //alert(__HashTamanhos.keys[i]);
        this.close(__HashTamanhos.keys[i]);
    }
};

Window.prototype.resize = function(id, altura, largura){
    $(id).style.height = altura + "px"; 
    $(id).style.width = largura + "px";
    if(this.__setIframes) $(id + '_iframe').style.height = altura + "px"; 
    if(this.__setIframes) $(id + '_iframe').style.width = largura + "px";
    $('__bordainterna' + id).style.width = (parseInt(largura) - 10) + "px";
    $('__bordainterna' + id).style.height = (parseInt(altura) - 29) + "px";
    __HashTamanhos.hashtable[id] = altura;
};

Window.prototype.insertHtml = function(id, html, htmlfnc){
    $(id + 'conteudo').innerHTML = html();
    if(htmlfnc==null){
        __HtmlOriginal[__HtmlOriginal.length] = {idDiv: id, htmlDiv: html, htmlFnc: null}; 
    }else{
        __HtmlOriginal[__HtmlOriginal.length] = {idDiv: id, htmlDiv: html, htmlFnc: htmlfnc};    
    }
};

Window.prototype.setDependency = function(idPai, id){
    __WindowDependency[__WindowDependency.length] = new Array(idPai, id);   
};

Window.prototype.trancaTela = function(trancar){
    var lockDiv = $("AreaDeBloqueio");
    
    if(!lockDiv){
        var lockObj = document.createElement('div');
        lockObj.style.position = "absolute";
	    lockObj.style.height = window.screen.availHeight + "px";
	    lockObj.style.width = window.screen.availWidth + "px";
	    lockObj.style.top = "0px";
	    lockObj.style.display = "none";
	    lockObj.style.left = "0px";	    	
	    lockObj.id = "AreaDeBloqueio";
	    document.body.appendChild(lockObj);
	    $("AreaDeBloqueio").innerHTML = '<table width="100%" height="100%" background="images/fundolg.gif"><tr><td></td></tr></table>';
	    lockDiv = $("AreaDeBloqueio");
	}	
	if(trancar==null){
	    if(lockDiv.style.display == ""){
	        lockDiv.style.display = "none";
	    }else{
	        lockDiv.style.zIndex = 1000;
	        lockDiv.style.display = "";
	    }
	}else{
	    if(trancar){
	        lockDiv.style.zIndex = 1000;
	        lockDiv.style.display = "";
	    }else{
	        lockDiv.style.display = "none";
	    }
	}
};

Window.prototype.minimize = function(id, alturaOriginal){
    if($("__bordainterna" + id).style.display == "none"){
        $("____imgse" + id).src = "images/cima_fechar.gif";
        $("__bordainterna" + id).style.display = "";
        $(id).style.height = __HashTamanhos.getValue(id) + "px";
        if(this.__setIframes) $(id + '_iframe').style.height = __HashTamanhos.getValue(id) + "px";
    }else{
        $("____imgse" + id).src = "images/cima_abrir.gif";
        $("__bordainterna" + id).style.display = "none";
        $(id).style.height = "24px";
        if(this.__setIframes) $(id + '_iframe').style.height = "30px";
    }
};

Window.prototype.move = function(id, top, left){
    $(id).style.left = left + "px";
    $(id).style.top = top + "px";
    if(this.__setIframes) $(id + '_iframe').style.left = left + "px";
    if(this.__setIframes) $(id + '_iframe').style.top = top + "px";
};

Window.prototype.open = function(id, open, zIndexn){
    if(Dragger.ui.zIndex > __tempIndex){
        __tempIndex = Dragger.ui.zIndex;   
    }
    
    var scrollYValue = 0;
    if (window.scrollY)
	    scrollYValue = window.scrollY;
    else if (document.documentElement.scrollTop)
	    scrollYValue = document.documentElement.scrollTop;
    else
	    scrollYValue = document.body.scrollTop;
	    
    var scrollXValue = 0;
    if (window.scrollX)
	    scrollXValue = window.scrollX;
    else if (document.documentElement.scrollLeft)
	    scrollXValue = document.documentElement.scrollLeft;
    else
	    scrollXValue = document.body.scrollLeft;
	    
    scrollXValue /= 2;    
    scrollYValue /= 2;
    
    var posLeft = (scrollXValue + (( document.body.offsetWidth  - parseInt($(id).style.width)  ) /2) );
    if(posLeft < 0) posLeft = 0;
    
    var posTop  = (scrollYValue + (( document.body.offsetHeight - parseInt($(id).style.height) ) /2) );
    if(posTop < 0) posTop = 0;
    
    
    
    
    var alteraPosJanela = ($(id).style.display == "none")?true:false;
    
    if(open==null){
        if($(id).style.display == ""){
            this.close(id);
        }else{
            var html = "";
            var fnc = null;
            for(var i = 0; i < __HtmlOriginal.length; i++){
                if(__HtmlOriginal[i].idDiv == id){
                    html = __HtmlOriginal[i].htmlDiv();
                    i = __HtmlOriginal.length;  
                }
            }  
            //_dragIndexAtual++;
            try{
            $("____imgse" + id).src = "images/cima_fechar.gif";
            }catch(e){}
            $("__bordainterna" + id).style.display = "";
            
            $(id).style.height = __HashTamanhos.getValue(id);
            $('__bordainterna' + id).style.height = (parseInt(__HashTamanhos.getValue(id)) - 29) + "px";
            $(id + 'conteudo').innerHTML = html;
            
            for(var i = 0; i < __HtmlOriginal.length; i++){
                if(__HtmlOriginal[i].idDiv == id){
                    if(__HtmlOriginal[i].htmlFnc!=null){                        
                        __HtmlOriginal[i].htmlFnc();   
                    }                
                }
            } 
            if(zIndexn!=null){
                if(this.__setIframes) $(id + '_iframe').style.zIndex = zIndexn;
                $(id).style.zIndex = zIndexn+1;
            }else{
                if(this.__setIframes) $(id + '_iframe').style.zIndex = __tempIndex + 1;
                $(id).style.zIndex = __tempIndex + 2;
                __tempIndex = __tempIndex + 2;
            }
            
            //só altera a posição da janela se ela estiver sendo aberta agora (se ela já estiver aberta, não altera a posição)
            if(alteraPosJanela)
            {
                $(id).style.left = posLeft + 'px';
                $(id).style.top  = posTop  + 'px';
            }
           
            $(id).style.display = "";
            if(this.__setIframes) 
            {
                if(alteraPosJanela)
                {
                    $(id + '_iframe').style.left = posLeft + 'px';
                    $(id + '_iframe').style.top  = posTop  + 'px';
                }
                $(id + '_iframe').style.display = "";
                //$(id + '_iframe').style.visibility = "hidden";
            }
        }
    }else{
        if(open){  
        
            var html = "";
            for(var i = 0; i < __HtmlOriginal.length; i++){
                if(__HtmlOriginal[i].idDiv == id){
                    html = __HtmlOriginal[i].htmlDiv();
                    i = __HtmlOriginal.length;                   
                }
            }
            //_dragIndexAtual++;
            try{
            $("____imgse" + id).src = "images/cima_fechar.gif";
            }catch(e){}
            $("__bordainterna" + id).style.display = "";
            $(id).style.height = __HashTamanhos.getValue(id);
            $('__bordainterna' + id).style.height = (parseInt(__HashTamanhos.getValue(id)) - 29) + "px";
            $(id + 'conteudo').innerHTML = html;
            for(var i = 0; i < __HtmlOriginal.length; i++){
                if(__HtmlOriginal[i].idDiv == id){
                    if(__HtmlOriginal[i].htmlFnc!=null){                        
                        __HtmlOriginal[i].htmlFnc();   
                    }                
                }
            } 
            
            if(zIndexn!=null){
                if(this.__setIframes) $(id + '_iframe').style.zIndex = zIndexn;
                $(id).style.zIndex = zIndexn+1;
            }else{
                if(this.__setIframes) $(id + '_iframe').style.zIndex = __tempIndex + 1;
                $(id).style.zIndex = __tempIndex + 2;
                __tempIndex = __tempIndex + 2;
            }
            
                
            if(alteraPosJanela)
            {
                $(id).style.left = posLeft + 'px';
                $(id).style.top  = posTop  + 'px';
            }
            
            $(id).style.display = "";
            if(this.__setIframes) 
            {
                if(alteraPosJanela)
                {
                    $(id + '_iframe').style.left = posLeft + 'px';
                    $(id + '_iframe').style.top  = posTop  + 'px';
                }
                $(id + '_iframe').style.display = "";
                //$(id + '_iframe').style.visibility = "hidden";
            }
           
        }else{
            this.close(id);
        }        
    }
};

Window.prototype.close = function(id){
    for(var i = 0; i < __WindowDependency.length; i++){
        if(__WindowDependency[i][0] == id){
                $(__WindowDependency[i][1]).style.display = "none";
                $(__WindowDependency[i][1]).style.zIndex = 1;
                if(this.__setIframes) $(__WindowDependency[i][1] + '_iframe').style.display = "none";
                if(this.__setIframes) $(__WindowDependency[i][1] + '_iframe').style.zIndex = 1;                  
                        
        }
    }
        $(id).style.display = "none";
        $(id).style.zIndex = 1; 
        if(this.__setIframes) $(id + '_iframe').style.display = "none";
        if(this.__setIframes) $(id + '_iframe').style.zIndex = 1; 
};

