// +--------------------------------------------------------+
// | Class: classWindow          
// |
// | Uma classe para o gerenciamento de janelas dinâmicas. Com
// | essa classe é possível criar janelas internas ao browser, 
// | sombreando o fundo.
// +--------------------------------------------------------+
function classWindow()
{
	// +--------------------------------------------------------+
	// | Variable: iWidth
	// | Largura da janela. Por padrão: 630 pixels.
	// +--------------------------------------------------------+
	var iWidth;
	// +--------------------------------------------------------+
	// | Variable: iHeight
	// | Altura da janela. Por padrão: 440 pixels.
	// +--------------------------------------------------------+	
	var iHeight;
	// +--------------------------------------------------------+
	// | Variable: sTitle
	// | Título da janela.
	// +--------------------------------------------------------+	
	var sTitle;
	// +--------------------------------------------------------+
	// | Variable: bCreated
	// | Responsável por manter uma só janela criada.
	// +--------------------------------------------------------+	
	var bCreated;
	// +--------------------------------------------------------+
	// | Variable: bCloseButtonFlag
	// | Define se um botão de fechar deve ser exibido na janela.
	// +--------------------------------------------------------+	
	var bCloseButtonFlag;
	this.setTitle = setTitle;
	this.setSize = setSize;
	this.createWindow = create;
	this.removeWindow = remove;	
	this.showWindow = show;
	this.insertContent = insertContent;
	this.changeTitle = changeTitle;

	// +--------------------------------------------------------+
	// | Function: setSize
	// | Atribui valor as propriedades iWidth e iHeight do objeto 
	// | instanciado, definindo a largura e altura da janela.
	// |             
	// | Parameters: 
	// | _iWidth - largura da janela
	// | _iHeight - altura da janela
	// +--------------------------------------------------------+
	function setSize(_iWidth, _iHeight)
	{
		if (_iWidth != "")
			iWidth = _iWidth;
		else
		    iWidth = 630;
		if (_iHeight != "")
			iHeight = _iHeight;	
		else
		    iHeight = 440;		
	}

	// +--------------------------------------------------------+
	// | Function: setTitle
	// | Atribui valor a propriedade sTitle do objeto instanciado,
	// | definindo o título que deve ser exibido no topo da janela.
	// |
	// | Parameters: 
	// | _sTitle - string contendo o título.
	// +--------------------------------------------------------+	
	function setTitle(_sTitle)
	{
		sTitle = _sTitle;	
	}

	// +--------------------------------------------------------+
	// | Function: changeTitle
	// | Troca o valor exibido no título da janela pelo valor da
	// | variável passada como parâmetro.
	// |
	// | Parameters: 
	// | _sNewTitle - string contendo o novo título.
	// +--------------------------------------------------------+		
	function changeTitle(_sNewTitle)
	{
		if (document.getElementById("div_titulo") !== null)
			$("#div_titulo").html(_sNewTitle);
	}

	// +--------------------------------------------------------+
	// | Function: position
	// | Calcula e define a correta posição da janela de acordo com
	// | o browser. A janela por padrão deve estar centrada no browser.
	// +--------------------------------------------------------+		
	function position() 
	{
		var sMarginLeft = parseInt((iWidth / 2),10) + "px";
		$("#div_janela").css({marginLeft: '-' + sMarginLeft, width: iWidth + 'px'});
		if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) 
		{
			var iMarginTop = parseInt((iHeight / 2),10) + "px";
			$("#div_janela").css({marginTop: '-' + iMarginTop});
		}
	}

	// +--------------------------------------------------------+
	// | Function: create
	// | Cria todas as DIVs necessárias para a exibição da janela. 
	// | Para ser acessada deve ser chamada como: createWindow
	// |
	// | Parameters: 
	// | _bCloseButtonFlag - define a existência ou não de um botão de fechar.
	// | _sTitle - título da janela.
	// | _iWidth - largura da janela.
	// | _iHeight - altura da janela.
	// +--------------------------------------------------------+		
	function create(_bCloseButtonFlag, _sTitle, _iWidth, _iHeight)
	{
		var sHTML = "";
		var sHTMLCloseButton = "";
		var sHTMLTitle = "";
		bCloseButtonFlag = _bCloseButtonFlag;
		sTitle = _sTitle;
			
		if (sTitle != "" || bCloseButtonFlag)
		{
			if (bCloseButtonFlag)
				sHTMLCloseButton = "<div id='fecharJanela'><a href='#' id='closeWindowButton'>Fechar</a></div>";
			
			if (sTitle != "")
				sHTMLTitle = "<div id='div_titulo'>" + sTitle + "</div>";
			
			sHTML = "<div id='div_titulo_bg'>" + sHTMLTitle + sHTMLCloseButton + "</div>";
		}

		// Verifica se é o IE6 ou superior. Caso positivo cria um iframe
		// para esconder os combobox da página.
	    /*if ( (jQuery.browser.msie) && (jQuery.browser.version >= 6) )
	    {
		    $("body","html").css({height: "100%", width: "100%"});
		    $("html").css("overflow","hidden");
		    if (document.getElementById("iframe_selects") === null) 
			    $("body").append("<iframe id='iframe_selects'></iframe>");
	    }*/
		if ( (jQuery.browser.msie) && (jQuery.browser.version >= 6) )
	    {			
			$("select").css({visibility: "hidden"});			
		}
		$(":input").css({visibility: "hidden"});
		$("html").css("overflow","hidden");
		
	    if (document.getElementById("div_background") === null)
	        $("body").append("<div id='div_background'></div>");
       
	    if (document.getElementById("div_janela") === null)
	        $("body").append("<div id='div_janela'></div>");

		setSize(_iWidth, _iHeight);

		var sDivWidth = (iWidth - 30) + "px";
		var sDivHeight = (iHeight - 45) + "px";
        
		sHTML = sHTML + "<div id='div_conteudo' style='width:" + sDivWidth + ";height:" + sDivHeight + "'></div>"
		
		$("#div_janela").append(sHTML);
		
		if (bCloseButtonFlag)
			$("#closeWindowButton").click(remove);
	}

	// +--------------------------------------------------------+
	// | Function: show
	// | Exibe a janela. Para ser acessada deve ser chamada como: showWindow
	// +--------------------------------------------------------+		
	function show()
	{
        $("#div_background").unbind();
		if(document.getElementById("div_background") !== null)
		{
			$("#div_background").addClass("div_backgroundBG"); /* Ativa a div_background e deixa o fundo opaco */
		}
        position();
		$("#div_janela").show();
		$("#div_janela").blur();
	}

	// +--------------------------------------------------------+
	// | Function: insertContent
	// | Insere conteúdo no interior da janela.
	// |
	// | Parameters: 
	// | _sHTML - conteúdo a ser exibido na janela.
	// +--------------------------------------------------------+		
	function insertContent(_sHTML)
	{
		$("#div_conteudo").html(_sHTML);	
	}

	// +--------------------------------------------------------+
	// | Function: remove
	// | Retira a janela da página. Para ser acessada deve ser chamada como:
	// | removeWindow
	// |
	// | Parameters: 
	// | _bBGFlag - essa flag define se o fundo continuará opaco ou não.
	// |            Se TRUE remove fundo opaco, c.c. fundo permanece.
	// +--------------------------------------------------------+
	function remove(_bBGFlag)
	{
		if(document.getElementById("div_background") !== null)
		{
			if ( ($("#div_background")[0].style.display != "none") && (_bBGFlag) )
			{
				$('#div_background').trigger("unload").unbind().remove();
			}
		}
        $('#div_janela,#iframe_selects').trigger("unload").unbind().remove();
		if (typeof document.body.style.maxHeight == "undefined")
		{
	        $("body","html").css({height: "auto", width: "auto"});
	        $("html").css("overflow","");
        }
		
		if ( (jQuery.browser.msie) && (jQuery.browser.version >= 6) )
	    {				
			$("select").css({visibility: "visible"});	
		}
		$(":input").css({visibility: "visible"});
		$("html").css("overflow","");
	}
}
