/*******************************************************************************************
FUÇÕES DA POPUP
*******************************************************************************************/
var tiposPopup = new Array();
tiposPopup.push("promo");
tiposPopup.push("popup");

function ToggleFloatingLayer(DivID, iState) // 1 visible, 0 hidden
{
    centerLayers();
	 
	 if(document.layers)	   //NN4+
    {
	if (document.layers[DivID]) {
    		document.layers[DivID].visibility = iState ? "show" : "hide";
	}
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(DivID);
        if (obj) {
        		obj.style.visibility = iState ? "visible" : "hidden";
        }
    }
    else if(document.all)	// IE 4
    {
        if (document.all[DivID]) {
    		document.all[DivID].style.visibility = iState ? "visible" : "hidden";
        }
    }
	 
}

/*******************************************************************************************
OPERAÇÕES
*******************************************************************************************/
function GetNomeJanela(tipo)
{
	var janela;
	switch (tipo)
	{
		case "promo":
			janela = "PromoFloating";
			break;
		case "popup":
			janela = "PopupFloating"
	}
	return janela;
}


function abrePopup(url,tipo,logo) {
	fechaPopup();
	// nome da janela
	var nomeJanela = GetNomeJanela(tipo);
	var nomeFrame = nomeJanela + "Frame";
	
	document.getElementById(nomeJanela).style.visibility = 'visible';	
	// abre a url
	eval("redirectPopup(window." + nomeFrame + ",'" + __URL__ + url + "');");
	
	return;
}

function redirectPopup(frame,url)
{
	frame.location = url;
}

function fechaPopup() {	
	
	for (var i = 0; i < tiposPopup.length; i++) {
		var nomeJanela = GetNomeJanela(tiposPopup[i]);
		var nomeFrame = nomeJanela + "Frame";
		
		if (document.getElementById(nomeJanela)) {
			eval("redirectPopup(window." + nomeFrame + ",'about:blank');");
		
			// fecha a janela
			ToggleFloatingLayer(nomeJanela,0);
		}
	}
	return;	
}

function iecompattest(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function centerLayers() {

	var ie5=document.all&&document.getElementById
	var ns6=document.getElementById&&!document.all
	
	var winWidth = ns6? window.innerWidth-40 : iecompattest().clientWidth - 40;
	var winHeight = ns6? window.innerHeight-40 : iecompattest().clientHeight - 40;	
	
	for (var i = 0; i < tiposPopup.length; i++) {
		
		var nome = GetNomeJanela(tiposPopup[i]); 
		
		if (document.getElementById(nome)) {
			var popupWidth	= document.getElementById(nome).style.width;	
			var popupWidth	= popupWidth.substr(0,popupWidth.length-2);
			
			var popupHeight 	= document.getElementById(nome).style.height;	
			var popupHeight	= popupHeight.substr(0,popupHeight.length-2);
			
			var winX = Math.ceil((winWidth - popupWidth) / 2);
			var winY = Math.ceil((winHeight - popupHeight) / 2);
		
			// POPUP
			document.getElementById(nome).style.left = winX + 'px';
			document.getElementById(nome).style.top = scrollY()+winY+30 + 'px';
		}
	}

}

function scrollY() {
	
	var scrolledY = 0;
	
	var ie5=document.all&&document.getElementById
	var ns6=document.getElementById&&!document.all
	
	if (ns6)  {
		scrolledY = window.pageYOffset;
	} else  {
		scrolledY = document.body.scrollTop;
	}
	
	return scrolledY;
	
}

window.onresize = function () { centerLayers();}