//variáveis
var controleBox = 0;
var resultados, divResultado;
//var icoCarregando = '../images/carregando_barra.gif';
var icoCarregando = '../images/carregando_logo.gif';
var strCarregando='<table border="0" align="center" cellpadding="20" cellspacing="0"><tr><td><br><br><br><br><img src="'+icoCarregando+'" align="middle" /></td></tr></table>';

function inicializaXmlHttp(){
	var xmlhttp;
  	try {
   		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType('text/plain');
  	} 
  	catch(ee){
   		try{
    		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   		} 
   		catch(e){
    		try{
     			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
    		catch(E){
     			xmlhttp = false;
			}
   		}
  }
  return xmlhttp;
}

function iniciaAjax(linksql, vars, div, acaoPosterior, semCarregando){
	var args = iniciaAjax.arguments.length;
	var xmlhttp=inicializaXmlHttp();
	xmlhttp.open("POST", linksql, true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState==4){
			resultados = xmlhttp.responseText;
			//alert(resultados);
			if (args >= 3){
				if(resultados.toString().toLowerCase().indexOf('javascript:')==0)
					setTimeout(resultados.replace(/javascript: /i,''),1);	
				else if(div)
					gel(div).innerHTML=unescape(resultados);	
				if(controleBox==1)
					setBox();
			}
			if(acaoPosterior)
				setTimeout(acaoPosterior,1);		
		}
		else{
			if(div && !semCarregando)
				document.getElementById(div).innerHTML = strCarregando;//peq
		}
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(vars);
}

function iniciaAjaxEfeito(linksql, vars, div, efeito, acaoPosterior, semCarregando){
	var args = iniciaAjaxEfeito.arguments.length;
	var xmlhttp=inicializaXmlHttp();
	xmlhttp.open("POST", linksql, true);
	$("#"+div).fadeOut("fast",function()
	{
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)
			{
				resultados=xmlhttp.responseText;
				if (args >= 3)
				{
					if(resultados.toString().toLowerCase().indexOf('javascript:')==0)
						setTimeout(resultados.replace(/javascript: /i,''),1);
					else
						gel(div).innerHTML=unescape(resultados);	
					if(efeito=='slide')
					{					
						$("#"+div).slideDown('medium');	
					}
					else
					{					
						$("#"+div).fadeIn('medium');
					}
					if(controleBox==1)
						setBox();
				}
				if(acaoPosterior)
					setTimeout(acaoPosterior,1);
			}
			else
			{
				if(div && !semCarregando)
					document.getElementById(div).innerHTML = strCarregando;//peq
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(vars);
	});
}
function iniciaPost(linksql,div,form,acaoPosterior){	
	try{
		var vars="";
		for(var i = 0; i < form.elements.length; i++){
			vars=vars + form.elements[i].name + "=" + escape(form.elements[i].value) + "&"
			if (form.elements[i].type == "text")
				form.elements[i].value = "";
		}
		while(vars.indexOf(" ")>=0)
			vars=vars.replace(" ","%20");

		var xmlhttp=inicializaXmlHttp();
		xmlhttp.open("POST", linksql, true);		
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState==4)
			{				
				var t = xmlhttp.responseText;
				
				if(t.toString().toLowerCase().indexOf('javascript:')==0)
					setTimeout(t.replace(/javascript: /i,''),1);
				else if(div)
					document.getElementById(div).innerHTML=t;
				if(controleBox==1)
					setBox();
				if(acaoPosterior)
					setTimeout(acaoPosterior,1);
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(vars);		
		document.getElementById(div).innerHTML = strCarregando;
	}
	catch(e){
		//
	}
}

function toogle(objt){
	if(objt.style.display=='')	
		objt.style.display='none';
	else
		objt.style.display='';
}

function exibeEfeito(config,idJanela,idResultado){
	var div, div2;	
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++)
		objSelect[i].style.visibility = "hidden";
	
	var tudo=document.getElementsByTagName("body")[0];	
	
	if(!idJanela)
		idJanela="divJanela";
	if(!idResultado)
		idResultado="divResultado";
		
	div2=document.createElement("div");
 	div2.id=(idJanela);
	div2.style.display='none';
	//div2.style=div2.style+'overflow: visible;';
	
	var y,z;
	if (document.body.scrollHeight > document.body.offsetHeight){// all but Explorer Mac
		y = document.body.scrollHeight;
		z = document.body.scrollWidth;
	}
	else {// Explorer Mac;
		y = document.body.offsetHeight;
		z = document.body.offsetWidth;
	}
	
	div2.style.width = "100%";
	div2.style.height = y+61+"px";
	//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite")
	//window.scrollbars.visible = false
	if(config)
		adicionaEvento(div2, "click", apagaEfeito);
	
	tudo.appendChild(div2);
	
	div2.style.display='';
	//div2.style=div2.style+'overflow: hidden;';
	
	div = document.createElement("div");
 	div.id = idResultado;
	tudo.appendChild(div);
	document.onkeydown = function(event){tecla(event)};
	divResultado = document.getElementById(idResultado);
	document.getElementById(idResultado).focus();		
	controleBox = 1;
	if(document.all)
	{
		window.scrollTo(0,0);	
	}
	
}
 
function setBox(){
	var box = document.getElementById("divResultado");
	var x,y, xp, yp;
	if (box.scrollHeight > box.offsetHeight){// all but Explorer Mac
		x = box.scrollWidth;
		y = box.scrollHeight;
		xp = document.body.scrollWidth;
		yp = document.body.scrollHeight;
	}
	else{// Explorer Mac;
		x = box.offsetWidth;
		y = box.offsetHeight;
		xp = document.body.offsetWidth;
		yp = document.body.offsetHeight;
	}
	box.style.left = eval(xp)/2-eval(x)/2+"px";
	box.style.top = "20%";//eval(yp)/2-eval(y)/2+"px";
}

function apagaEfeito(idJanela,idResultado){
	var objSelect = document.getElementsByTagName("select");
	for (i=0;i<objSelect.length;i++){
		objSelect[i].style.visibility = "visible";
	}
	
	if(!idJanela && gel("divJanela2"))
		idJanela="divJanela2";
	else if(!idJanela)
		idJanela="divJanela";
	if(!idResultado && gel("divResultado2"))
		idResultado="divResultado2";
	else if(!idResultado)
		idResultado="divResultado";
	
  	var tudo=document.getElementsByTagName("body")[0]	
  	var div1 = document.getElementById(idResultado);
	var div2 = document.getElementById(idJanela);
	div1.innerHTML="";
  	tudo.removeChild(div1);
	tudo.removeChild(div2);	
	
	document.getElementsByTagName("html")[0].style.overflow="";
	if(!gel("divJanela"))
		document.onkeydown='';
	controleBox=0;
}  
function tecla(e){
	if (!e) 
		e = window.event;	
	if (e.keyCode==27){// || e.keyCode==13
		apagaEfeito();	
		if(!gel("divJanela"))
			document.onkeydown='';
	}
}
function gel(id){
	return document.getElementById(id);
}
function gels(name){
	return document.getElementsByName(name);
}

/* Editor de Texto */
function inicializarEditor(tipo, elementos)
{
	if(!elementos)
		elementos="";
	
	tinyMCE.init({
		language: "pt", 
		mode : tipo,
		theme : "advanced",
		skin : "o2k7",
		plugins : "safari,pagebreak,style,layer,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",		
		theme_advanced_buttons1: "fontselect,fontsizeselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,forecolor,backcolor,styleprops|,media,charmap,absolute,visualchars,nonbreaking,pagebreak,hr,del,ins",
		theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,removeformat,|,search,replace,|,undo,redo,|,bullist,numlist,link,unlink,image,cleanup,code,|,preview,|,fullscreen,print",
		theme_advanced_buttons3: "",
		theme_advanced_buttons4: "",
		
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,	
		theme_advanced_resize_horizontal : false,
		elements: elementos
	});
}

//preload de imagens
function MM_preloadImagesBib() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImagesBib.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
MM_preloadImagesBib(icoCarregando);
MM_preloadImagesBib('../images/menu_globo/menu1_.gif');
MM_preloadImagesBib('../images/menu_globo/menu2_.gif');
MM_preloadImagesBib('../images/menu_globo/menu3_.gif');
MM_preloadImagesBib('../images/menu_globo/menu4_.gif');
MM_preloadImagesBib('../images/menu_globo/menu5_.gif');
MM_preloadImagesBib('../images/menu_globo/menu6_.gif');
MM_preloadImagesBib('../images/menu_globo/menu7_.gif');
/* --------------- */


/* Banners */

function APIBanners()
{ 	
	this.tipoBanner=0;
	this.rotativo=1;
	this.aleatorio=1;
	this.cod="";
	this.pagina=document.location;
	this.objBanner="divBanner";
	this.maxBanners=0;
	this.banners=null;	
	this.onLoadComplete=null;					
	this.quantBanners=null;						//quantidade de banners encontrados
	this.tempo=10;								//tempo de cada banner, em segundos
	this.largura=580;							//dimensoes do banner
	this.altura=170;							
	this.pastaBanners="../conteudo/banners/";	//pasta onde está o banner
	this.pos=0;							

	this.rodaBanners=function()
	{
		var cod=this.banners[this.pos].split('||')[0];
		var tipo=this.banners[this.pos].split('||')[1];
		var arquivo=this.banners[this.pos].split('||')[2];		
		var link=this.banners[this.pos].split('||')[3];
		var tempo=this.banners[this.pos].split('||')[4];
		var expansivel=this.banners[this.pos].split('||')[5];
		var larguraF=this.banners[this.pos].split('||')[6];
		var alturaF=this.banners[this.pos].split('||')[7];
		var str, titlePagina;		
		
		if(tipo==0)
		{
			if(expansivel==1 || expansivel=='1')
			{				
				str="<div style='position:relative;margin-top:-"+(this.altura-1)+"px'><div style='position:absolute'>"+flash(larguraF, alturaF, this.pastaBanners+arquivo+'?cod='+cod)+"</div></div>";
				
			}
			else
			{
				if(this.largura==0)	
					this.largura=larguraF;
				if(this.altura==0)	
					this.largura=alturaF;
				str=flash(this.largura, this.altura, this.pastaBanners+arquivo+'?cod='+cod)	//SWF			
			}
		}
		else if(tipo==1)					
			str=img(cod, this.pastaBanners+arquivo, link, this.largura, this.altura)	//IMG
		else
			str=arquivo;		
		titlePagina=document.title;
		obj(this.objBanner).innerHTML=str;		
		document.title=titlePagina;
		
		if(this.banners.length>1)
		{
			if(this.pos+1>=this.banners.length)
				this.pos=0;
			else
				this.pos=this.pos+1;			
			var p=this;
			if(tempo==0)
				tempo=this.tempo;
			setTimeout(function(){p.rodaBanners()},tempo*1000);
		}		
	}
	
	this.carregar = function()
	{		
		if(!obj(this.objBanner))
		{
			alert('O Objeto '+this.objBanner+' onde os banners serão exibidos não foi encontrado.');
			return false;
		}

		var p=this;
		var xmlhttp=inicializaXmlHttp();		
		xmlhttp.open("POST", "../page/_banners.asp", true);
		xmlhttp.onreadystatechange = function()
		{				
			if (xmlhttp.readyState==4)
				 p.inicializaBanners(xmlhttp.responseText)
		}				
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
		xmlhttp.send('codCidade='+this.codCidade+'&tipoBanner='+this.tipoBanner+'&rotativo='+this.rotativo+'&aleatorio='+this.aleatorio+'&cod='+this.cod+'&maxBanners='+this.maxBanners+'&pagina='+escape(this.pagina)+'&r='+Math.random());					
	}	
	
	this.inicializaBanners = function(strBanners)
	{		
		if(strBanners.indexOf("||")!=-1)
		{
			this.banners=strBanners.split('\n');
			this.quantBanners=this.banners.length;			
			this.rodaBanners();			
			obj(this.objBanner).style.display='';
		}
		else
		{
			obj(this.objBanner).style.display='none';
			this.quantBanners=0;				
		}		
		
		if(this.onLoadComplete)
		{
			setTimeout(this.onLoadComplete,1);
		}
	}	
	
	function obj(id)
	{
		return document.getElementById(id);
	}
	
	function flash(larguraf, alturaf, arquivo)
	{
		var strFlash;
		strFlash  = ('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+larguraf+'" height="'+alturaf+'" id="teste" align="middle">');
		strFlash += ('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" /><param name="menu" value="false" />');
		strFlash += ('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="transparent" menu="false" quality="best" width="'+larguraf+'" height="'+alturaf+'" name="teste" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
		strFlash += ('</object>');
		return strFlash;
	}	
	
	function img(cod, arquivo, link, largura, altura){
		var strIMG;		
		strIMG="<img src='"+arquivo+"' border='0' width='"+largura+"' height='"+altura+"'>";
		if(link!='')
			strIMG="<a onclick='bannerClique("+cod+");' href='"+link+"' target='blank'>"+strIMG+"</a>";
		return strIMG;
	}
	
	function inicializaXmlHttp(){
		var xmlhttp;
		try {
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType('text/plain');
		} 
		catch(ee){
			try {
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} 
			catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} 
				catch(E){
					xmlhttp = false;}
			}
		}
		return xmlhttp;
	}
}

function clickBanner(cod){
	
	var X = inicializaXmlHttp()
	X.open("POST", '../page/_clickBanner.asp', true);		
	X.onreadystatechange = function()	
	{
		/*if (X.readyState==4){}*/
	}
	X.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
	X.send("cod=" + cod)	
}

function flash(larguraf, alturaf, arquivo, modo, id)
{
	if(!modo)
		modo='';
	if(!id)
		id='flash'
	var strFlash;
	strFlash  = ('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+larguraf+'" height="'+alturaf+'" id="'+id+'" align="middle">');
	strFlash += ('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" /><param name="menu" value="false" /><param name="allowFullScreen" value="true" />');
	strFlash += ('<param name="movie" value="'+arquivo+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" wmode="transparent" menu="false" quality="best" width="'+larguraf+'" height="'+alturaf+'"  name="fl" allowScriptAccess="sameDomain" allowfullscreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	strFlash += ('</object>');
	return strFlash;
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

