﻿OAS_sitepage = "estadao/esportes/pequim2008";
OAS_listpos = "Position1,Top,Middle,x03,x05,Frame2";
var sEditoria = 'esportes';
var sSubEditoria = 'pequim2008';
		
String.prototype.replaceAll = function(needed, replacement){ 
	return this.split(needed).join(replacement); 
};

function toHexa(str){
	if(!str) return '';
	var sHexa = "";
	var sHexaString = "";
	for (var i=0; i<str.length; i++){
		var sHexa = str.charCodeAt(i).toString(16).toUpperCase();
		if (sHexa.length < 2) sHexa = "0" + sHexa;
		sHexaString += sHexa;
	};
	return sHexaString;
};

function getTitle(titulo){
	var titulo = titulo.split('::');
	return titulo[titulo.length-1].replace(/^ +| +$/g,'');
};

var serverBusca = "/busca";
var serverInt = "/interatividade";
var serverRender = "http://www.estadao.com.br";
var serverEstatistica = "http://estat.estadao.com.br";
var serverComentarioPop = "http://int.estadao.com.br";

function Class(){};
function ComponentesClass(){
	var isVisibleBanner = true;
	if( window.navigator.userAgent.toLowerCase().indexOf('windows ce') != -1 ){ isVisibleBanner = false };

	var objSelf = this;
	var parent = this;

	this.montaQString = function(obj){
		var qString = new Array();
		for(var att in obj){
			qString.push(att + '=' + escape(obj[att]));
		};
		if(!qString.length) return '';
		return '?' + qString.join('&');
	};

	this.enviarBusca = function(url){
		if(document.getElementById("formbusca_ITEMPESQUISADO").value){
			objQstring = {};
			objQstring.e = document.getElementById("formbusca_EDITORIA").value;
			objQstring.s = document.getElementById("formbusca_ITEMPESQUISADO").value;
			objQstring.s = document.getElementById("formbusca_ITEMPESQUISADO").value;
			objQstring.s = objQstring.s.replace(/([^\\]?)(["\\])/g, '$1\\$2');
			window.location = url + this.montaQString(objQstring);
		};
	};

	this.montarRanking = function(editoria, subeditoria, idPagina, GUIDPagina, target){
		RankingClass = function(){
			this._init = function(){
				var objSelf = this;
				$(window).bind( "load", function(event){
					var voteLegend = document.getElementById("eT");
					if(Cookies.get('estadao.ranking.' + GUIDPagina + '.nota')){
						if(voteLegend){
							voteLegend.innerHTML = "Você já votou";
						};
						return;
					};
					if(voteLegend){
						voteLegend.innerHTML = "Avalie esta Notícia";
					};
					$("#starContainer li a").each(function(index){
						this.starIndex = index;
					});

					$("#starContainer li a").hover(function(){
						objSelf.overStar(this, true);
					}, function(){
						objSelf.overStar(this, false);
					});

					$("#starContainer li a").click(function(){
						objSelf.sendVote(this.starIndex);
					});
				});
			};

			this.setEvent = function(obj, event, handler){
				if(obj.addEventListener){
					obj.addEventListener(event, handler, true);
				};
				if (obj.attachEvent){
					obj.attachEvent("on" + event, handler);
				};
			};

			this.getEventTarget = function(event){
				if(event.target){
					return event.target;
				}else{
					return event.srcElement;
				};
			};

			this.overStar = function(element, isOver){
				var voteLegend = document.getElementById("eT");
				if(isOver){
					for(var currElement = element.parentNode; currElement; currElement = currElement.previousSibling){
						if(currElement.nodeType != 3){
							$(currElement).removeClass('eC');
							$(currElement).addClass('eV');
						};
					};
					for(var currElement = element.parentNode.nextSibling; currElement; currElement = currElement.nextSibling){
						if(currElement.nodeType != 3){
							$(currElement).addClass('eC');
							$(currElement).removeClass('eV');
						};
					};
					if(voteLegend){
						voteLegend.innerHTML = element.title;
					};

				}else{
					for(var currElement = element.parentNode.parentNode.firstChild; currElement; currElement = currElement.nextSibling){
						if(currElement.nodeType != 3){
							$(currElement).addClass('eC');
							$(currElement).removeClass('eV');
						};
					};
					if(Cookies.get('estadao.ranking.' + GUIDPagina + '.nota')){
						var vote = Cookies.get('estadao.ranking.' + GUIDPagina + '.nota');
					}else{
						var vote = Number(document.getElementById('averageVotes').innerHTML);
					};
					$("#starContainer li").lt(vote).addClass('eV');
					$("#starContainer li").lt(vote).removeClass('eC');
					if(voteLegend){
						voteLegend.innerHTML = "Avalie esta Notícia";
					};
				};
			};

			this.sendVote = function(starIndex){
				var titulo = getTitle(document.title);

				var url = serverInt + "/Ranking/rankingVotacao.do?UID="+ GUIDPagina +"&valorNota="+ (starIndex + 1) +"&produto=ESTADAO&editoria=" + editoria + "&tipoMidia=noticia&tituloPagina=" + escape(titulo) + "&urlPagina=" + escape(window.location);

				var arrHandlers = new Array();
				$("#starContainer li a").each(function(i){
					arrHandlers.push({ over: this.onmouseover, out: this.onmouseout, click : this.onclick });
					this.onmouseover = null;
					this.onmouseout = null;
					this.onclick = null;
				});

				getXMLJson(url, true, function(objData){
					if(objData.resposta != "true"){
						alert("Erro: Não foi possivel contabilizar seu voto. Tente novamente mais tarde");

						$("#starContainer li a").each(function(i){
							this.onmouseover = arrHandlers[i].over;
							this.onmouseout = arrHandlers[i].out;
							this.onclick = arrHandlers[i].click;
						});

						return false;
					};
					var voteLegend = document.getElementById("eT");
					if(voteLegend){
						voteLegend.innerHTML = "Você já votou";
					};
					Cookies.set('estadao.ranking.' + GUIDPagina + '.nota', (starIndex + 1));
					this.showVote();
					var totalVotes = Number(document.getElementById('totalVotes').innerHTML) + 1;
					document.getElementById('totalVotes').innerHTML = totalVotes;
				}, this);
			};

			this.showVote = function(starIndex){
				if(!starIndex){
					starIndex = Cookies.get('estadao.ranking.' + GUIDPagina + '.nota') - 1;
				};
				$("#starContainer li").lt(starIndex).addClass('eV');
				$("#starContainer li").lt(starIndex).removeClass('eC');
				$("#starContainer li").gt(starIndex).addClass('eC');
				$("#starContainer li").gt(starIndex).removeClass('eV');
			};
			/* calling "constructor" */
			this._init();
		};
		Ranking = new RankingClass();

		var url = serverInt + "/Ranking/rankingNota.do?UID=" + GUIDPagina;

		getXMLJson(url, true, function(objData){
			objData.media = Math.round(Number(objData.media));
			include('/include/ranking/ranking.htm', target, null, objData);
		}, this);
	};

	this.montarAbaComentarios = function(GUIDPagina, target, aba){
		$("#"+target).hide("fast");
		var guid = GUIDPagina.substring(0,8) + "-" + GUIDPagina.substring(8,12) + "-" + GUIDPagina.substring(12,16) + "-" + GUIDPagina.substring(16,20) + "-" + GUIDPagina.substring(20);
		var url = "/estadao/includes/comentarios/listar.php?guid="+guid;
		$.get( url, function( data ){
			$("#"+target).html( data );
		});	
		$('#abaCR .header UL LI A').removeClass('selected');
		$('#abaCR .header UL LI #abaComentarios').addClass('selected');
		$("#"+target).fadeIn("slow");
	};

	this.mostrarTodosComentarios = function(editoria, subeditoria, idPagina, GUIDPagina){
		var titulo = $('#c H1').text();
		var guid = GUIDPagina.substring(0,8) + "-" + GUIDPagina.substring(8,12) + "-" + GUIDPagina.substring(12,16) + "-" + GUIDPagina.substring(16,20) + "-" + GUIDPagina.substring(20);
		var url = "http://cadastro.estadao.com.br/comentarios?guid=" + guid + "&editoria="+editoria+"&titulo_pg=" + titulo + "&url_pg=" + document.location;
		window.open(url,"comentarios","width=525, height=600, top=30, left=300, scrollbars=yes");
	};

	this.deixarComentario = function(editoria, subeditoria, idPagina, GUIDPagina){
		var titulo = $('#c H1').text();
		var guid = GUIDPagina.substring(0,8) + "-" + GUIDPagina.substring(8,12) + "-" + GUIDPagina.substring(12,16) + "-" + GUIDPagina.substring(16,20) + "-" + GUIDPagina.substring(20);
		var url = "http://cadastro.estadao.com.br/comentar?guid=" + guid + "&editoria="+editoria+"&titulo_pg=" + titulo + "&url_pg=" + document.location;
		window.open(url,"comentarios","width=500, height=600, top=30, left=300, scrollbars=yes");
	};
	
	this.montarControleTexto = function(target){
		var oTarget = document.getElementById(target);
		var strHTML = 'Tamanho do texto?';
		strHTML += ' <span id="tm01" onclick="sizeFonts(11),selectedFonts(\'tm01\'); return false">A</span>';
		strHTML += ' <span id="tm02" onclick="sizeFonts(12),selectedFonts(\'tm02\'); return false">A</span>';
		strHTML += ' <span id="tm03" onclick="sizeFonts(13),selectedFonts(\'tm03\'); return false">A</span>';
		strHTML += ' <span id="tm04" onclick="sizeFonts(14),selectedFonts(\'tm04\'); return false">A</span>';
		oTarget.innerHTML = strHTML;

		sizeFonts = function(s) {
			var objTarget = $('#corpoNoticia *').not($('.grupoC1')).not($('.grupoC1 *'));
			objTarget.css("fontSize",s+'px');

			var objTarget = $('#corpoNoticia *').not($('.grupoC1')).not($('.grupoC1 *'));
			objTarget.css("fontSize",s+'px');

			var objTargetTemp = document.getElementById('corpoNoticia');
			objTargetTemp.style.fontSize = s+'px';
		};
		selectedFonts = function(o) {
			for(var i=1;i<=4;i++) $('#tm0'+i).css("color",'#155E91');
			$('#'+o).css("color",'#7F7F7F');
		};
		selectedFonts('tm03');
	};

	this.montarInfoNoticia = function(editoria, subeditoria, idPagina, GUIDPagina, target){
		var guid = GUIDPagina.substring(0,8) + "-" + GUIDPagina.substring(8,12) + "-" + GUIDPagina.substring(12,16) + "-" + GUIDPagina.substring(16,20) + "-" + GUIDPagina.substring(20);
		var url = "/estadao/includes/comentarios/qtde.php?guid="+guid;
		getXMLJson(url, true, function( objData ){
			include('/include/info_noticia/info_noticia.html', target , null, objData);
		}, this);
	};

	this.montarBannerX5 = function (editoria, subeditoria, idPagina, target){
		if (isVisibleBanner){
			OAS_AD('x05');
		}else{
			document.write('<div style="width: 140px; height: 90px; background-color: #DDDDDD;">x05</div>');
		}
	};
	
	/* LinksPatrocinados*/
	this.montarLinksPatrocinados = function(editoria, subeditoria, idPagina, target){
		var qtdeAd = 6;
		google_ad_channel = "cidades_eleicoes2008";

		google_ad_request_done = function(google_ads) {
			var first_ad_unit = '';
			if (google_ads.length == 0) {
				return;
			} else if (google_ads.length > 0) {
				for(i = 0; i < google_ads.length; ++i) {
					if (i == 0) first_ad_unit += '<table style="font-size:10pt"><tr height=5%><td><a href=\"' + google_info.feedback_url + '\"><div class="AdTituloLinksPatrocinados">An&uacute;ncios Google</div></a><br></td></tr>'; first_ad_unit += '<tr><td><a style="text-decoration:none" href="' +google_ads[i].url + '"><div class="AdTitulo">' + google_ads[i].line1 + '</div><div class="AdTexto">' + google_ads[i].line2 + '<br>' + google_ads[i].line3 + '</div><div class="ASurl">' + google_ads[i].visible_url + '</div></a><br></td></tr>';
					if (i == qtdeAd) first_ad_unit += '</table>';
				};
			};
			document.getElementById("first_ad_unit").innerHTML += first_ad_unit;
		};
		google_ad_client = "ca-pub-5597832529264086";
		google_ad_output = 'js';
		google_max_num_ads = qtdeAd;
		google_feedback = "on";
		google_ad_type = "text";
	};

	this.montarAbaNoticiasRelacionadas = function(editoria, subeditoria, idPagina, GUIDPagina, target, aba, keywords){
		var html = '';
		html += '<div class="box1 selected" id="boxEstadaoHoje">';
		html += '	<div class="contentAba">&nbsp;<br></div>';
		html += '	<div class="footerAba">&nbsp;<br></div>';
		html += '</div>';
		if(String(typeof(target)).toLowerCase() == 'string'){
			target = document.getElementById(target);
		};
		target.innerHTML = html;
		$('#sbestadaohoje a').removeClass('selected');
		aba.className = 'selected';
		if(!keywords) return;
		var url = serverBusca + '/JSearch/NR!nR.action?s='+ GUIDPagina+";" + escape(keywords.replace(/; +/g, ';')) +'&e=';
		include(url, target, 'html');
	};
	
	this.chamarTag = function(tag, editoria, subeditoria){
		var urlBusca = serverBusca + "/JSearch/TQM!tQM.action";
		objQstring = {};
		if (editoria) objQstring.e = editoria; else objQstring.e = "";
		objQstring.s = tag.replace(/^ +| +$/g,'');
		window.location = urlBusca + this.montaQString(objQstring);
	};

	this.enviarEmail = function(editoria, subeditoria, idPagina, GUIDPagina){
		var objQString = {};
		var titulo = getTitle(document.title);
		objQString.linkNoticia = window.location;
		objQString.tituloNoticia = titulo;
		var url = "http://int.estadao.com.br/EnvioNoticia/envioNoticia.jsp" + this.montaQString(objQString);
		window.open(url, 'envio_email', 'toolbars=no,status=no,width=656,height=620,location=no');
	};

	this.imprimir = function(editoria, subeditoria, idPagina, GUIDPagina){
		var pathName = window.location.toString().match(/\/(not_.*?)\..*?$/)[1];
		window.open("http://int.estadao.com.br/Multimidia/ShowImpressao.action?xmlPathname="+ pathName +".xml", 'impressao', 'width=640, height=480, scrollbars=yes');
	};

	this.montarTagsNoticia = function(editoria, subeditoria, idPagina, keywords, target){
		if (keywords){
			keywords = keywords.split("; ").join(";").split(" ;").join(";");
			var aKeywords = keywords.split(';');
			var oTarget = document.getElementById(target);
			var strHTML = '<div class="hr1 hrtop"><hr/></div><div>';
			strHTML += '<ul class="section"><li><a href="javascript: void(0);">TAGS</a></li></ul>';
			for (var i=0; i<aKeywords.length; i++){
				strHTML += ' <a href="javascript:Componentes.chamarTag(\''+ aKeywords[i].replaceAll("'","\\'") +'\')" title="'+aKeywords[i]+'">'+aKeywords[i]+'</a>,';
			};
			strHTML = strHTML.substring(0, strHTML.length-1);
			strHTML += '</span></div><br />';
			oTarget.innerHTML = strHTML;
		};
	};
};
var Componentes = new ComponentesClass();

/* Meu estadao */
Componentes.MeuEstadao = new (function(){})();
Componentes.MeuEstadao.server = "/interatividade";
Componentes.MeuEstadao.inserirNoticia = function(editoria, subeditoria, idPagina, GUIDPagina){
	var qstring = "?idNoticia=estadao:"+escape(editoria)+":noticia:"+escape(GUIDPagina);
	var url = "/MeuEstadao/INS.do" + qstring;

	include(this.server + url, "pResposta", 'html', null, function(){
		$(".xsnazzy").show();
	});
};
target = '';
/* /Meu estadao */


$(document).ready( function(){
	$("a").bind("focus", function(){ this.blur(); });
	
	if(Cookies.get('PORTAL_AUTH')){
		$("#linkLogin a").attr("href", "javascript: void(0)").bind("click",function(){
			Cookies.remove('PORTAL_AUTH', null, '/', '.estadao.com.br');
			document.location.reload();
		}).children('img').attr("src", "/img/topbar/bt_logoff.gif");
	}else{
		$("#linkLogin a").unbind("click").attr("href", serverRender + "/interatividade/ME/home.action").children("img").attr("src", "/img/topbar/bt_login.gif");
	};
});
