/**
* FUNÇÃO RESPONSÁVEL PELA CRIAÇÃO DA JANELA AUXILIAR.
**/
function showQWindow(url, pars){
	myWindow = new QWindow();
	myWindow.defineParameters(url, pars);
	myWindow.getWindow();
}

/**
* FUNÇÃO RESPONSÁVEL POR MINIMIZAR A JANELA.
**/
function minimizeWindow(name){
	if($("divConteudo" + name).className == "DIVHIDE")
		$("divConteudo" + name).className = "DIVSHOW";
	else
		$("divConteudo" + name).className = "DIVHIDE";
}

/**
* FUNÇÃO RESPONSÁVEL POR MAXIMIZAR A JANELA.
**/
function maximizeWindow(name){
	var tamanhoTela = capturaTamanhoTela();

	$(name).style.left = 0;
	$(name).style.top = 0;

	$(name).style.width = tamanhoTela[0];
	$(name).style.height = tamanhoTela[1];

	$("divConteudo" + name).style.width = tamanhoTela[0];
	$("divConteudo" + name).style.height = tamanhoTela[1] - 23;

	if($("iFrame" + name)){
		$("iFrame" + name).style.width = tamanhoTela[0] - 2;
		$("iFrame" + name).style.height = tamanhoTela[1] - 24;
	}
}

/**
* FUNÇÃO RESPONSÁVEL PELO FECHAMENTO DA JANELA.
**/
function closeWindow(objectName, divName)
{
	// Fechar window.
	if (!objectName)
	{
		// Hack Chrome.
		window.open('', '_self', '');
		// Fecha a janela.
		window.close();
	}
	// Fechar QWindow.
	else
	{	
		if (!$(objectName))
		{
			return;
		}
		
		if(divName != ""){
			var htmlDiv = $("divConteudo" + objectName).innerHTML;
			$("divConteudo" + objectName).innerHTML = "";
			$(divName).innerHTML = htmlDiv;
		}
	
		fechaObjeto(objectName);
		showLayerCombos();
	}
}

/**
* CLASSE DE JANELAS AUXILIARES DO QUALITOR.
**/
function QWindow(){
	this.name = "divAuxWindow";
	this.title = "";
	this.icon = "qualitor.gif";
	this.modal = true;

	this.width = 450;
	this.height = 250;
	this.scroll = false;

	this.minimize = false;
	this.maxmize = false;
	this.close = true;

	this.url = "";
	this.parameters = "";
	this.method = "get";
	this.showDivQ = false;
	this.externalUrl = false;
	this.showDiv = false;

	this.onBeforeClose = "";

	/**
	* DEFINE OS PARAMETROS DA CLASSE.
	**/
	this.defineParameters = function(url, pars){
		this.url = url;

		if(pars){
			if(pars.name) this.name = pars.name;
			if(pars.title) this.title = pars.title;
			if(pars.icon) this.icon = pars.icon;
			if(pars.modal == false) this.modal = false;
			if(pars.width) this.width = pars.width;
			if(pars.height) this.height = pars.height;
			if(pars.scroll == true) this.scroll = true;
			if(pars.minimize == true) this.minimize = true;
			if(pars.maximize == true) this.maximize = true;
			if(pars.close == false) this.close = false;
			if(pars.parameters) this.parameters = pars.parameters;
			if(pars.method) this.method = pars.method;
			if(pars.showDivQ == true) this.showDivQ = true;
			if(pars.externalUrl == true) this.externalUrl = true;
			if(pars.showDiv == true) this.showDiv = true;
			if(pars.onBeforeClose) this.onBeforeClose = pars.onBeforeClose;
		}

		hiddenLayerCombos();
	},

	/**
	* FUNÇÃO QUE GERA A JANELA AUXILIAR.
	**/
	this.getWindow = function(){
		// ATUALIZA O ÍNDICE DE OBJETOS.
		layerIndex++;

		// VERIFICA O PARAMETRO MODAL.
		if(this.modal){
			if($("divDesabilitaTela"))
				$("divDesabilitaTela").style.zIndex = layerIndex;
			else
				desabilitaTela();
		}

		// CRIA O ELEMENTO DIV NA TELA.
		var body = document.getElementsByTagName("body")[0];
		var div = document.createElement("div");
		div.id = this.name;
		div.style.width = this.width;
		div.style.height = this.height;
		div.style.zIndex = layerIndex;

		// DEFINE A CLASSE DA DIV.
		var classDiv = (!this.externalUrl ? "DIVHIDE" : "DIVAUXWINDOW");
		div.className = classDiv;

		// DEFINE O CONTEÚDO DA DIV.
		var conteudo = this.returnTitle();
		conteudo += this.returnBody();

		// ESCREVE O CONTEÚDO NA DIV.
		div.innerHTML = conteudo;

		// ESCREVE A DIV NA TELA.
		body.appendChild(div);

		// CENTRALIZA A DIV NA TELA.
		centralizaObjeto(this.name, this.width, this.height);

		// FAZ A LEITURA DA URL DA DIV.
		if(this.showDiv){
			var htmlDiv = $(this.url).innerHTML;
			$(this.url).innerHTML = "";

			$("divConteudo" + this.name).innerHTML = htmlDiv;
			$(this.name).className = "DIVAUXWINDOW";

			this.enableCombos();
			this.enableCalendar();
		}else if(!this.externalUrl)
			new Ajax.Updater({success: "divConteudo" + this.name}, this.url, {parameters: this.parameters, method: this.method, evalScripts: true, showDivQ: this.showDivQ, onSuccess: $(this.name).className = "DIVAUXWINDOW"});
	},

	/**
	* HABILITA OS COMBOS DA JANELA AUXILIAR.
	**/
	this.enableCombos = function(){
		var windowChildren = $("divConteudo" + this.name).getElementsByTagName("select");
		for(var i = 0; i < windowChildren.length; i++)
			$(windowChildren[i]).style.visibility = "visible";
	},

	/**
	* HABILITA OS CALENDÁRIOS DA JANELA AUXILIAR.
	**/
	this.enableCalendar = function(){
		var windowChildren = $("divConteudo" + this.name).getElementsByTagName("img");
		for(var i = 0; i < windowChildren.length; i++){
			if(windowChildren[i].id.substring(0, 5) == "fcal_"){
				var arrayImage = windowChildren[i].id.split("_");
				eval('Calendar.setup({inputField: "' + arrayImage[1] + '", ifFormat: "%d/%m/%Y", button: "fcal_' + arrayImage[1] + '", singleClick: true});');
			}
		}
	},

	/**
	* RETORNA O HTML DA BARRA DE TÍTULO DA JANELA.
	**/
	this.returnTitle = function(){
		var strRetorno = '<table width="100%" cellpadding="0" cellspacing="0" class="WINDOWHEADER" onmousedown="dragStart(event, this.parentNode.id)">';
		strRetorno += '<tr>';

		// ÍCONE.
		strRetorno += '<td width="20" height="20" align="center">';
		strRetorno += '<img src="' + pathWeb + '/framework/images/default/16/' + this.icon + '" class=\"WINDOWICON\" alt="" />';
		strRetorno += '</td>';

		// TÍTULO.
		strRetorno += '<td width="100%" height="20" alt="" />';
		strRetorno += '<font class="WINDOWTITLE">' + this.title + '</font>';
		strRetorno += '</td>';

		if(this.minimize){
			strRetorno += '<td width="27" nowrap="true" height="20">';
			strRetorno += '<img src="' + pathWeb + '/framework/images/default/general/btn_minimize.gif" style="margin-top:2px;" class="POINTER" onmouseover="setObjectImage(this, \'general/btn_minimize_over.gif\');" onmouseout="setObjectImage(this, \'general/btn_minimize.gif\');" onclick="minimizeWindow(\'' + this.name + '\');" alt="" />';
			strRetorno += '</td>';
		}

		if(this.maximize){
			strRetorno += '<td width="26" nowrap="true" height="20" alt="" />';
			strRetorno += '<img src="' + pathWeb + '/framework/images/default/general/btn_maximize.gif" style="margin-top:2px;" class="POINTER" onmouseover="setObjectImage(this, \'general/btn_maximize_over.gif\');" onmouseout="setObjectImage(this, \'general/btn_maximize.gif\');" onclick="maximizeWindow(\'' + this.name + '\');" alt="" />';
			strRetorno += '</td>';
		}

		// DETERMINA OS BOTÕES DA JANELA.
		strRetorno += '<td width="42" nowrap="true" height="20" align="right">';

		if (this.close) {
			strRetorno += '<img src="' + pathWeb + '/framework/images/default/general/btn_close.gif" id="btnCloseWindow' + layerIndex + '" class="WINDOWCLOSE" onmouseover="setObjectImage(this, \'general/btn_close_over.gif\');" onmouseout="setObjectImage(this, \'general/btn_close.gif\');" onclick="' + (this.onBeforeClose != '' ? this.onBeforeClose : '') + ' closeWindow(\'' + this.name + '\', \'' + (this.showDiv ? this.url : "") + '\');" alt="' + QLabel[79] + '" title="' + QLabel[79] + '" />';
		}

		strRetorno += '</td>';
		strRetorno += '</tr>';
		strRetorno += '</table>';

		return strRetorno;
	},

	/**
	* RETORNA O BODY (CORPO) DA DIV.
	**/
	this.returnBody = function(){
		var strRetorno = '<table width="100%" cellpadding="0" cellspacing="0" class="WINDOWCONTENT">';
		strRetorno += '<tr>';
		strRetorno += '<td class="WINDOWSIDE">&nbsp;</td>';
		
		strRetorno += '<td valign="top" id="divConteudo' + this.name + '" style="width:' + this.width + '; height:' + (this.height - 20) + '; ' + (this.scroll ? "overflow-y:auto; " : "") + 'background:#FFFFFF; border:1px solid #A4A4A5">';
		strRetorno += '<div style="position:relative; top:50%; margin-top:-16px; margin-left:-50px; width:100px; left:50%;">';
		strRetorno += '<img src="' + pathWeb + '/framework/images/default/general/loading.gif" alt="' + QLabel[3] + '" title="' + QLabel[3] + '" class=\"VMIDDLE\" /> <span class="COLORLABEL"> ' + QLabel[3] + '</span>';
		strRetorno += '</div>';
		
		if (this.externalUrl) {
			strRetorno += '<iframe id="iFrame' + this.name + '" width="' + (this.width - 2) + '" height="' + (this.height - 20) + '" frameborder="0" src="' + this.url + '"></iframe>';
		}

		strRetorno += '</td>';

		strRetorno += '<td class="WINDOWSIDE">&nbsp;</td>';
		strRetorno += '</tr>';
		strRetorno += '<tr><td class="WINDOWFOOTER" colspan="3">&nbsp;</td></tr>';
		strRetorno += '</table>';

		return strRetorno;
	}
}