// written by Dean Edwards, 2005 with input from Tino Zijdel
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	if (!element.events) element.events = {};
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = {};
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	handlers[handler.$$guid] = handler;
	element["on" + type] = handleEvent;
};
addEvent.guid = 1;
function removeEvent(element, type, handler) {
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
};
function handleEvent(event) {
	var returnValue = true;
	event = event || fixEvent(window.event);
	var handlers = this.events[event.type];
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};
function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

// Main Regular Expressions 
var re = {
	imgResult: /\bimgresult\b/,
	fixMinWidth: /\bfixIeMinWidth\b/g,
	textResult: /\btextresult\b/,
	detalhada: /\bdetalhada\b/g,
	tipo: /\btipo(\w*)\b/,
	homes: /\bchome\b/,
	recPages: /\btextpages\b/,
	recStars: /\bstar(?:0)?(\d)\b/
}

// Busca Simples
var bSimples = {
	www: {
		action: "http://busca.uol.com.br/www/index.html",
		param: ["q","ad"]
	},
	br: {
		action: "http://busca.uol.com.br/br/index.html",
		param: ["q","ad"]
	},
	uol: {
		action: "http://busca.uol.com.br/uol/index.html",
		param: ["q","ad"]
	},
	img: {
		action: "http://busca.uol.com.br/imagem/index.html",
		param: ["q","ad"]
	},
	vid: {
		action: "http://busca.uol.com.br/video/index.html",
		param: ["q","ad"]
	},
	not: {
		action: "http://noticias.busca.uol.com.br/uol/index.html",
		param: ["q","id","rd","ad"]
	},
	pre: {
		action: "http://shopping.busca.uol.com.br/busca.html",
		param: ["q","ad"]
	},
	rec: {
		action: "http://dir.busca.uol.com.br/MDIbot.html",
		param: ["keyword","condition","RequestType","ad"]
	},
	rad: {
		action: "http://musica.busca.uol.com.br/radio/index.php",
		param: ["busca","param1","check","ad"]
	},
	mai: {
		action: "http://busca.uol.com.br/mais/",
		param: ["q","ad"]
	},
	check: function(tipo, enviar){
		if(!document.getElementById("simples")) return;
		var oSimples = document.getElementById("simples");
		if(document.getElementsByTagName("body").item(0).id == "bpre" || document.getElementsByTagName("body").item(0).id == "bprec") {
			oSimples.q.value = document.precos.q.value;
		}
		if(document.getElementsByTagName("body").item(0).id == "bmais") {
			oSimples.q.value = document.maisbuscas.q.value;
		}
		if(enviar){
			if(oSimples.q.value == ""){
				enviar = 0;
				var bHomeCheck = bSimples.homeCheck(tipo);
				if (!bHomeCheck) return;
			}
			oSimples.q.value = oSimples.q.value.replace(/site\:http(\:\/\/|\%3A\%2F\%2F)/,"site:");
		}
		if(document.getElementsByTagName("body").item(0).id=="bhome" && tipo == "mai") {
			var sURL = "http://busca.uol.com.br/mais/" + ((oSimples.q.value == "")? "" : "&q="+ oSimples.q.value);
			document.location = sURL;
		}
		document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.tipo,"") + " tipo" + lib.sBuscaAtiva;
		var oElements = document.getElementById("simples").elements;
		for(var i=0, oElm; oElm=oElements[i]; i++){
			if (oElm.type.toLowerCase() != "submit") oElm.disabled="disabled";
			for(var j=0, sName; sName = bSimples[tipo].param[j]; j++){
				if(oElm.name == sName) oElm.disabled="";
			}
		}
		//aqui entram as regras especiais
		switch(tipo){
			case "rec":
				oSimples.keyword.value = oSimples.q.value;
				if(!enviar && document.getElementsByTagName("body").item(0).id=="bhome") {
					oSimples.q.disabled = "";
				}
				break;
			case "rad":
				document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.detalhada,"");
				oSimples.busca.value = oSimples.q.value.replace(/\"/g,'');
				if(!enviar && document.getElementsByTagName("body").item(0).id=="bhome") {
					oSimples.q.disabled = "";
				}
				if (oSimples.check && oSimples.check.nodeName && (oSimples.check.nodeName.toLowerCase() == "input")){
					oSimples.check.value = "musica";
				} else if (oSimples.check) {
					for (var i=0, oInputRadio; oInputRadio=oSimples.check[i]; i++){
						if(oInputRadio.value == "musica"){
							oInputRadio.checked = "checked";
						}
					}
				}
				break;
			case "not":
				document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.detalhada,"");
				oSimples.id.value = "1";
				break;
			case "uol":
				if(document.getElementById("uol-id") && document.getElementById("uol-id").checked) {
					oSimples.id.value = document.getElementById("uol-id").value;
					oSimples.id.disabled = "";
				};
				break;
			case "img":
				document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.detalhada,"");
				break;
			case "vid":
				document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.detalhada,"");
				break;
		}
		oSimples.action = bSimples[tipo].action;
		if(enviar){
			oSimples.submit();
			oSimples.q.disabled = "";
		}
	},
	homeCheck: function(tipo) {
		var sBodyId = document.getElementsByTagName("body").item(0).id;
		switch(sBodyId){
			case "bhome":
				enviar = 0;
				if(tipo != "pre" && tipo != "mai" && tipo != "rec"){
					document.location.hash = "#op" + tipo;
				}
				if(tipo == "pre"){
					document.location = "http://precos.busca.uol.com.br/";
				}
				if(tipo == "mai"){
					document.location = "http://busca.uol.com.br/mais/";
				}
				if(tipo == "rec"){
					document.location = "http://mdi.busca.uol.com.br/";
				}
				break;
			case "bprec":
				if(tipo != "pre" && tipo != "mai" && tipo != "rec"){
					document.location = "http://busca.uol.com.br/#op" + tipo;
				}
				if(tipo == "mai"){
					document.location = "http://busca.uol.com.br/mais/";
				}
				if(tipo == "rec"){
					document.location = "http://mdi.busca.uol.com.br/";
				}
				break;
			case "bmais":
				if(tipo != "pre" && tipo != "mai" && tipo != "rec"){
					document.location = "http://busca.uol.com.br/#op" + tipo;
				}
				if(tipo == "pre"){
					document.location = "http://precos.busca.uol.com.br/";
				}
				if(tipo == "rec"){
					document.location = "http://mdi.busca.uol.com.br/";
				}
				break;
			case "breco":
				if(tipo != "pre" && tipo != "mai" && tipo != "rec"){
					document.location = "http://busca.uol.com.br/#op" + tipo;
				}
				if(tipo == "pre"){
					document.location = "http://precos.busca.uol.com.br/";
				}
				if(tipo == "mai"){
					document.location = "http://busca.uol.com.br/mais/";
				}
				break;
			case "brec":
				if(document.getElementsByTagName("body").item(0).className.match(re.recPages)){
					if(tipo != "pre" && tipo != "mai" && tipo != "rec"){
						document.location = "http://busca.uol.com.br/#op" + tipo;
					}
					if(tipo == "pre"){
						document.location = "http://precos.busca.uol.com.br/";
					}
					if(tipo == "mai"){
						document.location = "http://busca.uol.com.br/mais/";
					}
					if(tipo == "rec"){
						document.location = "http://mdi.busca.uol.com.br/";
					}
				}
				break;
			default: 
				lib.fnFillField();
				return false;
		}
		return true;
	}
}

// Busca no UOL, opções
var buscaNoUOL = {
	ids: [" > ","Notícias","Esporte","Guerra no Iraque","Sites pessoais","Viagem","Folha Online","Saúde","Tecnologia","Economia","Jogos","Educação","Gay","Humor","Sexo","Televisão","Música","Pelé.Net","Lição de Casa"]
}


// Global Library 
lib = {
	sBuscaAtiva: "",
	iUlSize: 0,
	fnFillField: function(){
		alert("O campo da busca deve ser preenchido")
	},
	fnSetFocus: function(obj){
		if(typeof obj.value != "undefined" && typeof obj.defaultValue != "undefined" && (obj.defaultValue == obj.value) && obj.type == "text"){
			obj.focus();
		}
	},
	fnBodyLoad: function(){
		document.getElementsByTagName("body").item(0).className +=" loaded";
		if(!document.getElementById("simples")) return;
		var aForms = ["simples","bottom","precos"];
		for (var i=0, sForm; sForm = aForms[i]; i++){
			if(document.getElementById(sForm)){
				addEvent(document.getElementById(sForm),"submit",lib.fnCheckEmpty);
			}
		}
	},
	fnCheckEmpty: function(e){
		var target = window.event? window.event.srcElement : e ? e.target : null;
		if (!target) return;

		target = lib.fnAscendDOM(target, "form");
		if (!target) return;
		if(target.q && target.q.value !="") {
			if(document.getElementsByTagName("body").item(0).id == "brad"){
				target.busca.value = target.q.value.replace(/\"/g,"");
				target.q.disabled = "disabled";
			}
			if(document.getElementsByTagName("body").item(0).className.match(/\btiporec\b/)){
				target.keyword.value = target.q.value;
				target.q.disabled = "disabled";
			}
			if(document.getElementsByTagName("body").item(0).className.match(/\btiporad\b/)){
				target.busca.value = target.q.value;
				target.q.disabled = "disabled";
			}
			target.q.value = target.q.value.replace(/site\:http(\:\/\/|\%3A\%2F\%2F)/,"site:");
			return true;
		} else {
			lib.fnFillField();
			return false;
		}
	},
	fnAscendDOM: function (obj, targetNode) {
		while (obj.nodeName.toLowerCase() != targetNode && obj.nodeName.toLowerCase() != 'html') {
			obj = obj.parentNode;
		}
		return (obj.nodeName.toLowerCase() == 'html') ? null : obj;
	},
	fnSelecionaTipo: function(sTipo,bSubmit){
		lib.sBuscaAtiva = sTipo.substring(1,sTipo.length);
		bSimples.check(lib.sBuscaAtiva,bSubmit)
	},
	fnTotalResults: function() {
		/* acerta frase informativa sobre os resultados */
		if(!document.getElementById('info') || !document.getElementById('total-results').firstChild) return;
		var sNumResults = document.getElementById('total-results').firstChild.nodeValue;
		if(sNumResults.length > 3){
			var aNumResults = new Array();
			for(var i=1, iNum = sNumResults.length; i<=iNum; i++){
				aNumResults.push(sNumResults.substring(i-1, i));
				if((iNum - i > 0) && ((iNum - i)%3 == 0)) {aNumResults.push(".")};
			}
			document.getElementById('total-results').firstChild.nodeValue = aNumResults.join("");
		}
		if(!document.getElementById('resultados')) return;
		var oDL = document.getElementById('resultados');
		var colDT = oDL.getElementsByTagName('dt');
		var oInfo = document.getElementById('info');
		if(oInfo.hasChildNodes){
			var colChilds = oInfo.childNodes;
			if(!document.createDocumentFragment) return;
			var oFragment = document.createDocumentFragment();
			var oStrong = document.createElement('strong');
			var sText = "";
			var oSpanFirst = colDT[0].getElementsByTagName("span").item(0);
			sText += parseInt(oSpanFirst.firstChild.nodeValue);
			if(colDT.length > 1){
				var oSpanLast = colDT[colDT.length-1].getElementsByTagName("span").item(0);
				sText += " a " + parseInt(oSpanLast.firstChild.nodeValue);
			}
			for(var i=0, iCIlen = colChilds.length; i<iCIlen; i++){
				if(colChilds[i].nodeType == 3){
					if(sNumResults=="1" && colChilds[i].nodeValue.match(/ encontrados/)) {
						colChilds[i].nodeValue = colChilds[i].nodeValue.replace(/ encontrados/," encontrado");
						break;
					}
					if(colDT.length > 1){
						colChilds[i].nodeValue = colChilds[i].nodeValue.replace(/\bResultados:/," Resultados de ");
					} else {
						colChilds[i].nodeValue = colChilds[i].nodeValue.replace(/\bResultados:/," Resultado ");
					}
				}
			}
			var oText = document.createTextNode(sText);
			oStrong.appendChild(oText);
			var oDeText = document.createTextNode(" de ");
			oFragment.appendChild(oStrong);
			oFragment.appendChild(oDeText);
			document.getElementById('info').insertBefore(oFragment,document.getElementById('total-results'));
		}
	},
	fnPaginacao: function() {
		/* limita paginação */
		if(!document.getElementById('paginas')) return;
		var oUL = document.getElementById('paginas');
		var colLi = oUL.getElementsByTagName('li');
		var iLiLen = colLi.length;
		if(iLiLen <= 0) return;
		var iActiveLi = -1;
		for(var i=0, oLi; oLi = colLi[i]; i++){
			if (oLi.className.match(/\bactive\b/)){
				activeLi = i;
				break;
			}
		}
		if(activeLi == -1) return;
		for (var j=iLiLen-1; j>=activeLi+10; j--){ $$temp = oUL.removeChild(colLi[j]);}
		for (var k=0; k<=activeLi-11; k++){ $$temp = oUL.removeChild(colLi[0]);}
	},
	fnOpenNewWindow: function(){
		/* cria ícone de abrir em nova janela */
		if(!document.getElementById('resultados')) return;
		var oDL = document.getElementById('resultados');
		var colDT = oDL.getElementsByTagName('dt');
		for(var i=0, oDT; oDT = colDT[i]; i++){
			var oLink = document.createElement("a");
			var oImg = document.createElement("img");
			var oSrc = oImg.src = "http://r.i.uol.com.br/c/ico_novajanela.gif";
			oLink.href = oDT.getElementsByTagName("a").item(0).href;
			oLink.appendChild(oImg);
			oDT.appendChild(oLink);
			addEvent(oLink,"click",lib.fnClickNewWin);
		}
	},
	fnTraduzir: function(){
		if(!document.getElementById('resultados')) return;
		var sQuery = document.location.search;
		var oDL = document.getElementById('resultados');
		var colDD = oDL.getElementsByTagName('dd');
		for (var i=0, oDD; oDD = colDD[i]; i++){
			var colSpan = oDD.getElementsByTagName("span");
			for (var j=0, oSpan; oSpan = colSpan[j]; j++){
				if(oSpan.className.match(/\btraduzir\b/)){
					var oLink = oSpan.getElementsByTagName("a")[0];
					oLink.href += "&uolq=" + encodeURIComponent(sQuery.replace("?","&"));
				}
			}
		}
	},
	fnClickNewWin: function(e){
		/* abre link em nova janela */
		e.stopPropagation();
		e.preventDefault();
		var target = window.event? window.event.srcElement : e ? e.target : null;
		if (!target) return;
		target = lib.fnAscendDOM(target, "a");
		if (!target) return;
		var oWin = window.open(target.href)
	},
	fnSendDetalhada: function(e){
		/* verifica detalhada */
		var target = window.event? window.event.srcElement : e ? e.target : null;
		if (!target) return false;

		target = lib.fnAscendDOM(target, "form");
		if (!target) return;
		if (target.fe && (target.fe.value != "")){
			if(target.q) {
				target.q.value += ' "'+target.fe.value+'"'; 
			}
			if(target.as_q) {
				target.as_q.value += ' "'+target.fe.value+'"';
			}
			target.fe.value = "";
			target.fe.disabled = "disabled";
			if (lib.sBuscaAtiva == "br") {
				lib.fnCheckSiteBR(target);
			}
			return true;
		}
		if (lib.sBuscaAtiva == "br") {
			lib.fnCheckSiteBR(target);
		}
		return true;
	},
	fnCheckSiteBR: function(oForm){
		if((oForm.site) && (oForm.site.value != "")){
			if(oForm.q) {
				oForm.q.value += " site:"+ oForm.site.value.replace("http://","");
				oForm.site.value = "";
				oForm.site.disabled = "disabled";
			}
		}
	},
	fnAddDetalhada: function(){
		if(!document.getElementById("detalhada")) return;
		var oDet = document.getElementById("detalhada");
		var colForms = oDet.getElementsByTagName("form");
		for(var i=0, oForm; oForm = colForms[i]; i++){
			addEvent(oForm,"submit",lib.fnSendDetalhada)
			if(oForm.fe){oForm.fe.disabled = ""}
		}
	},
	fnDetalhada: function(){
		/* eventos para abrir e fechar buscas detalhadas */
		lib.fnAddDetalhada();
		if(!document.getElementById("det-link")) return;
		addEvent(document.getElementById("det-link"),"click",lib.fnShowDetalhada);
		var colDetForm = document.getElementById("detalhada").getElementsByTagName("form");
		for(var i=0, oForm; oForm=colDetForm[i]; i++){
			var colP = oForm.getElementsByTagName("p");
			for(var j=0, oP; oP=colP[j]; j++){
				if(oP.className.match(/\bdsubmit\b/)){
					var oLink = oP.getElementsByTagName("a").item(0);
					if(oLink){
						addEvent(oLink,"click",lib.fnHideDetalhada);
					}
				}
			}
		}
	},
	fnShowDetalhada: function (e){
		/* insere estilo de busca detalhada */
		e.stopPropagation();
		e.preventDefault();
		document.getElementsByTagName("body").item(0).className += (document.getElementsByTagName("body").item(0).className.match(re.detalhada))? "": " detalhada";
	},
	fnHideDetalhada: function (e){
		/* remove estilo de busca detalhada */
		e.stopPropagation();
		e.preventDefault();
		document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.detalhada,"");
	},
	fnOpcoesBusca: function(){
		/* ativa eventos nas opções principais da busca */
		if(!document.getElementById("opcoes")) return;
		if(!document.getElementById("opcoes").getElementsByTagName("a")) return;
		var colLink = document.getElementById("opcoes").getElementsByTagName("a");
		for(var i=0, oLink; oLink=colLink[i]; i++){
			addEvent(oLink,"click",lib.fnSelecionaBusca)
		}
		if(!document.getElementById("weboptions") || !document.getElementById("weboptions").getElementsByTagName("input")) return;
		var colInput = document.getElementById("weboptions").getElementsByTagName("input");
		for(var i=0, oInput; oInput=colInput[i]; i++){
			if(oInput.type.toLowerCase() == "radio") {
				addEvent(oInput,"focus",lib.fnSelecionaRadio);
				if(navigator.userAgent.indexOf("Safari") > -1) {
					addEvent(oInput,"click",lib.fnSelecionaRadio);
				}
			}
		}
	},
	fnSelecionaBusca: function(e){
		/* seleciona a busca, e manda formulário para validação */
		e.stopPropagation();
		e.preventDefault();
		var target = window.event? window.event.srcElement : e ? e.target : null;
		if (!target) return;
		var oLi = lib.fnAscendDOM(target,"li");
		if(!oLi) return;
		lib.fnSelecionaTipo(oLi.id,true)
	},
	fnSelecionaRadio: function(e){
		/* seleciona a busca, mas não manda formulário para validação */
		e.stopPropagation();
		e.preventDefault();
		var target = window.event? window.event.srcElement : e ? e.target : null;

		if (!target) return;
		var oInput = lib.fnAscendDOM(target,"input");
		lib.fnSelecionaTipo(oInput.value,false)
	},
	fnAdjustUl: function() {
		/* alinhamento para resultados de imagem e vídeo */
		if(!document.getElementById("result-set")) return;
		var oUl = document.getElementById("result-set");
		if(lib.iUlSize != oUl.offsetWidth){
			lib.iUlSize = oUl.offsetWidth;
			var colLi = oUl.getElementsByTagName("li");
			lib.fnZeraLi(colLi);
			for(var i=1; i<colLi.length; i++){
				if (colLi[i].offsetTop > colLi[i-1].offsetTop) {
					lib.fnCreateClearLi(colLi[i])
					i++;
				}
			}
		}
	},
	fnAdjustImgLink: function() {
		/* alinhamento para resultados de imagem e vídeo */
		if(!document.getElementById("result-set")) return;
		var oRS = document.getElementById("result-set");
		var colP = oRS.getElementsByTagName("p");
		for (var i=0, oP; oP = colP[i]; i++){
			if(oP.className == "link") {
				var oLink = oP.getElementsByTagName("a")[0];
				if(oLink) {
					var oText = oLink.innerHTML;
					if (oText){
						var aTextSplit = oText.split("");
						var aTemp = [];
						for(var j=0, aNewText; aNewText = aTextSplit[j]; j++){
							aTemp.push(aNewText);
							if(j%26 == 25) {aTemp.push("<br>")};
						}
						oLink.innerHTML = aTemp.join("");
					}
				}
			}
			if(oP.className == "nome") {
				var oNomeText = oP.innerHTML;
				if (oNomeText){
					var aTextSplit = oNomeText.split("");
					var aTemp = [];
					for(var j=0, aNewText; aNewText = aTextSplit[j]; j++){
						aTemp.push(aNewText);
						if(j%26 == 25) {aTemp.push("<br>")};
					}
					oP.innerHTML = aTemp.join("");
				}
			}
		}
	},
	fnIeMinWidth: function(){
		/* simula MinWidth para IE forçando estilo CSS (e max-width para um caso específico */
		if(typeof document.all == 'undefined') return;
		if(document.getElementsByTagName("body").item(0).clientWidth < 761){
			document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.match(re.fixMinWidth)? document.getElementsByTagName("body").item(0).className : document.getElementsByTagName("body").item(0).className + " fixIeMinWidth";
		} else {
			document.getElementsByTagName("body").item(0).className = document.getElementsByTagName("body").item(0).className.replace(re.fixMinWidth,"");
		}
	},
	fnCreateClearLi: function(obj){
		/* cria elementos li extras para quebra de linha nas páginas de thumbnails */
		var li = document.createElement('li');
		var txt = document.createTextNode('&nbsp;');
		li.className="newcol";
		li.appendChild(txt);
		obj.parentNode.insertBefore(li,obj);
	},
	fnZeraLi: function(col){
		/* remove elementos li extras para quebra de linha nas páginas de thumbnails */
		for (var i=0; i<col.length; i++){
			if(col[i].className.match(/\bnewcol\b/)){
				col[i].parentNode.removeChild(col[i]);
			}
		}
	},
	fnMarcaEstacaoUOL: function(){
		var sLocation = document.location.toString();
		if (!sLocation.match(/[\?\&]id=(\d+)/)) return;
		numerodoid = RegExp.$1;
		if(!numerodoid || numerodoid == 0) return;
		if(!buscaNoUOL.ids[numerodoid] || !document.getElementById("area")) return;
		lib.fnMarcarId(numerodoid);
		var oText = document.createTextNode(buscaNoUOL.ids[0] + buscaNoUOL.ids[numerodoid])
		document.getElementById("area").appendChild(oText)
	},
	fnMarcarLinkUOL: function(){
		/* acrescenta class linkuol para páginas de conteúdo UOL */
		if(!document.getElementById('resultados')) return;
		var oDL = document.getElementById('resultados');
		var colDD = oDL.getElementsByTagName('dd');
		for (var i=0, oDD; oDD=colDD[i]; i++){
			if(oDD.className.match(/\blink\b/) && oDD.firstChild.firstChild.nodeValue.match(/\.uol\.com\.br/)){
				if(/(\.bol|\.sites|\.vilabol|\.fotoblog|\.blog)\.\uol.com\.br/.test(oDD.firstChild.nodeValue) == 0){
					oDD.className+=" linkuol";
				}
			}
		}
	},
	fnMarcarLinkBlog: function(){
		/* acrescenta "[blog] " antes de links para alguns blogs */
		if(!document.getElementById('resultados')) return;
		var oDL = document.getElementById('resultados');
		var colDD = oDL.getElementsByTagName('dd');
		for (var i=0, oDD; oDD=colDD[i]; i++){
			if(oDD.className.match(/\blink\b/) && oDD.firstChild.firstChild.nodeValue.match(/\.fotoblog\.uol|\.blog\.uol|theblog|weblogger(\.terra?)|blogger|blig|fotologs|photolog|flogs|photopage|floog|fotoaki|blogs|blog\.aol\.com\.br|\.zip\.net|\.blogspot\.com|\.turmadobar\.com|\.blogger\.com/g)){
				var oSpan = document.createElement("span")
				var sSpan = document.createTextNode("[blog] ")
				oSpan.className+="linkblog";
				oSpan.appendChild(sSpan);
				oDD.insertBefore(oSpan,oDD.firstChild);
				oDD.className=oDD.className.replace("linkuol","");
			}
		}
	},
	fnMarcarDomain: function(){
		if (!document.getElementById("domain-options")) return;
		if (document.getElementById("squery") && document.getElementById("squery").value){
			 document.getElementById("squery").value = document.getElementById("squery").value.replace(/(?: |\%20)site:([\w\%-\.\:\/]+)/g,"");
			 if (document.getElementById("bottom-query") && document.getElementById("bottom-query").value) {
			 	document.getElementById("bottom-query").value = document.getElementById("squery").value;
			 }
		}
		var sLocation = document.location.toString();
		if (!sLocation.match(/(?:[\&\?]site=|(?:\+| |\%20)site(?::|\%3A))([\w\%\-\.\:\/]+)/)) return;
		var sDomain = RegExp.$1;
		if(!sDomain || sDomain == "" || !sDomain.match(/[\.\w]/)) return;
		sDomain = sDomain.replace(/http(?:\:|\%3A)(\/|\%2F){2}/,"");
		var sDomainDecoded = decodeURIComponent(sDomain);
		if (!sLocation.match(/http:\/\/(blog|fotoblog|sites)\.busca\.uol\.com\.br/)) return;
		var sTipo = RegExp.$1;
		switch (sTipo) {
			case "fotoblog":
				sTipo = "fbl";
				break;
			case "sites":
				sTipo = "sit";
				break;
			default:
				sTipo = "blg";
		}
		if(document.getElementById("det-"+sTipo) && document.getElementById("det-"+sTipo).site && (document.getElementById("det-"+sTipo).site.value != null) ) {
			document.getElementById("det-"+sTipo).site.value = sDomain;
		}
		if (document.getElementById("det-"+sTipo) && document.getElementById("det-"+sTipo).q && (document.getElementById("det-"+sTipo).q.value != null) ) {
			document.getElementById("det-"+sTipo).q.value = document.getElementById("squery").value;
		}
		if(!document.createDocumentFragment) return;
		var oFragment = document.createDocumentFragment();
		var oInput = document.createElement("input");
		var oLabel = document.createElement("label");
		var oLabelText = document.createTextNode(sDomainDecoded);
		var sId = "site-" + (typeof sTipo != "undefined")? sTipo:"";
		oInput.name = "site";
		oInput.type = "checkbox";
		oInput.id = sId;
		oInput.value = sDomain;
		oLabel.appendChild(oLabelText);
		oFragment.appendChild(oInput);
		oFragment.appendChild(oLabel)
		document.getElementById("domain-options").insertBefore(oFragment,document.getElementById("domain-options").firstChild);
		oInput.checked = "checked";
		oLabel.setAttribute("for",sId)
		if(!document.getElementById("area")) return;
		var oText = document.createTextNode(" > " + sDomainDecoded);
		document.getElementById("area").appendChild(oText);
	},
	fnMarcarId: function(iID){
		if (!document.getElementById("weboptions") && !document.getElementById("detalhar")) return;
		if(!document.createDocumentFragment) return;
		var oSpan = document.createElement("span");
		var oInput = document.createElement("input");
		var oLabel = document.createElement("label");
		var oLabelText = document.createTextNode("no UOL, com textos em " + buscaNoUOL.ids[iID]);
		oInput.name = "tempid";
		oInput.type = "checkbox";
		oInput.id = "uol-id";
		oInput.value = iID;
		oLabel.appendChild(oLabelText);
		oSpan.appendChild(oInput);
		oSpan.appendChild(oLabel);
		oSpan.id = "uol-op-id";
		document.getElementById("weboptions").appendChild(oSpan);
		oInput.checked = "checked";
		oLabel.setAttribute("for","uol-id");
		bSimples.check("uol",0);
		addEvent(oInput,"change",function(){bSimples.check("uol",0)})
		if(document.getElementById("det-uol-id") && document.getElementById("det-uol-id").options){
			var colOptions = document.getElementById("det-uol-id").options;
			for(var i=0; i<colOptions.length; i++){
				if(colOptions[i].value == iID){
					colOptions[i].selected = "selected";
					break;
				}
			}
		}
	},
	fnCreateInputHidden: function(sName,sValue){
		var oInput = document.createElement("input");
		oInput.type = "hidden";
		oInput.name = sName;
		oInput.value = sValue;
		return oInput;
	},
	fnObjInnerHTML: function(oID, sCode){
		if(!document.getElementById(oID)) return;
		document.getElementById(oID).innerHTML = sCode;
	},
	fnChecarCensura: function(){
		var sLocation = document.location.toString();
		if(sLocation.match(/fi\=3/)){
			if(document.simples.fi) lib.fnMarcarRadio(document.simples.fi,"3");
			if(document.getElementById("det-img") && document.getElementById("det-img").fi) lib.fnMarcarRadio(document.getElementById("det-img").fi,"3");
			if(document.getElementById("det-vid") && document.getElementById("det-vid").fi) lib.fnMarcarRadio(document.getElementById("det-vid").fi,"3");
		} else {
			lib.fnMarcarRadio(document.simples.fi,"1");
			if(document.getElementById("det-img") && document.getElementById("det-img").fi) lib.fnMarcarRadio(document.getElementById("det-img").fi,"1");
			if(document.getElementById("det-vid") && document.getElementById("det-vid").fi) lib.fnMarcarRadio(document.getElementById("det-vid").fi,"1");
		}
	},
	fnSetRecomendados: function(){
		/* parte 1: marcar opção */
		var sLocation = document.location.toString();
		if(sLocation.match(/&condition\=(\d)/)){
			var sCondition = "tipo-condition-" + RegExp.$1;
			if(document.getElementById(sCondition)) document.getElementById(sCondition).checked = "checked";
		}
		/* parte 2: inserir frame de exibição do resultado */
		if(!document.getElementById("results") && !document.getElementById("resultados")) return;
		if(document.getElementById("results")) {colLinks = document.getElementById("results").getElementsByTagName("a");}
		if(document.getElementById("resultados")) {colLinks = document.getElementById("resultados").getElementsByTagName("a");}
		if(!colLinks || colLinks.length<1) return;
		for (var i=0, oLink; oLink = colLinks[i]; i++){
			var iStar = 0;
			if(oLink.className.match(re.recStars)){
				iStar = RegExp.$1;
			}
			if(iStar && oLink.title && oLink.title != ""){
				sLinkInner = oLink.innerHTML; // for IE
				oLink.href = "http://mdi.busca.uol.com.br/framemdi.htm?name=" + encodeURI(oLink.title) + "&star=" + iStar + "&url=" + oLink.href;
				oLink.innerHTML = sLinkInner; // for IE
				//addEvent(oLink,"click",lib.fnClickNewWin);
			}
		}
	},
	fnMarcarRadio: function(oRadio,sValue){
		for(var i=0, oRad; oRad = oRadio[i]; i++){
			if (oRad.value == sValue){
				oRad.checked = "checked";
				break;
			}
		}
	},
	fnOpcoesOrdenacao: function(){
		if(!document.getElementById("resultados")) return;
		var oResults = document.getElementById("resultados");
		var oTxtOrdem = document.createTextNode("Ordenação: ");
		var oTxtPipe = document.createTextNode(" | ");
		var oTxtData = document.createTextNode("mais recentes primeiro");
		var oTxtRelev = document.createTextNode("mais importantes primeiro");
		var sLocation = document.location.toString();
		var oLink = document.createElement("a");
		var sLink = sLocation.replace(/(?:\&|\?)rd=1/,"").replace(/(?:\&|\?)start=(?:\d*)/,"");
		sLink = sLink.replace("/index.html&","/index.html?");
		var oP = document.createElement("p");
		var oStrong = document.createElement("strong");
		oP.id = "ordenacao";
		oP.appendChild(oTxtOrdem);
		if(sLocation.match(/(?:\&|\?)rd=1/)){
			oLink.href = sLink;
			oLink.appendChild(oTxtRelev);
			oP.appendChild(oLink);
			oP.appendChild(oTxtPipe);
			oStrong.appendChild(oTxtData)
			oP.appendChild(oStrong);
		} else {
			oLink.href = sLink.replace(/(?:\&|\?)rd=0/,"") + "&rd=1";
			oLink.appendChild(oTxtData);
			oStrong.appendChild(oTxtRelev);
			oP.appendChild(oStrong);
			oP.appendChild(oTxtPipe);
			oP.appendChild(oLink);
		}
		oResults.parentNode.insertBefore(oP,oResults);
	},
	fnHideFooter: function(){
		if(document.getElementById("results") && document.getElementById("results").firstChild && (document.getElementById("results").firstChild.nodeType == 3) && document.getElementById("results").firstChild.nodeValue.match(/Nenhum resultado encontrado para/)){
			document.getElementsByTagName("body").item(0).className += " semresultados";
		}
	}
}


var bMais = {
	blg: {
		action: "http://blog.busca.uol.com.br/uol/index.html",
		param: ["q","ad"]
	},
	fbl: {
		action: "http://fotoblog.busca.uol.com.br/uol/index.html",
		param: ["q","ad"]
	},
	sit: {
		action: "http://sites.busca.uol.com.br/uol/index.html",
		param: ["q","ad"]
	},
	esp: {
		action: "http://busca.uol.com.br/uol/index.html",
		param: ["q","id","ad"]
	},
	mus: {
		action: "http://busca.uol.com.br/uol/index.html",
		param: ["q","id","ad"]
	},
	sex: {
		action: "http://busca.uol.com.br/uol/index.html",
		param: ["q","id","ad"]
	},
	tel: {
		action: "http://busca.uol.com.br/uol/index.html",
		param: ["q","id","ad"]
	},
	tvu: {
		action: "http://app.uol.com.br/tvuol/search.php",
		param: ["pdescri","desc","titu","copy","ad"]
	},
	check: function(){
		if(!document.getElementById("maisbuscas")) return;
		var oMais = document.getElementById("maisbuscas");
		var tipo = oMais.tipo[oMais.tipo.selectedIndex].value;
		if(oMais.q.value == ""){
			lib.fnFillField();
			return false;
		}
		var oElements = document.getElementById("maisbuscas").elements;
		for(var i=0, oElm; oElm=oElements[i]; i++){
			if (oElm.type.toLowerCase() != "submit") oElm.disabled="disabled";
			for(var j=0, sName; sName = bMais[tipo].param[j]; j++){
				if(oElm.name == sName) oElm.disabled="";
			}
		}
		switch(tipo){
			case "esp":
				oMais.id.value = "2";
				break;
			case "mus":
				oMais.id.value = "16";
				break;
			case "sex":
				oMais.id.value = "14";
				break;
			case "tel":
				oMais.id.value = "15";
				break;
			case "tvu":
				oMais.pdescri.value = oMais.q.value;
				break;
		}
		oMais.action = bMais[tipo].action;
		oMais.submit();
		oMais.q.disabled = "";
	}
}


var overture = {
	cab: '<h2>Links Patrocinados</h2>',
	noa: '',
	fot: '',
	fnNoAds: function(){
		if(!document.getElementById("links-patrocinados")) return;
		document.getElementById("links-patrocinados").innerHTML = overture.noa;
	},
	fnShow: function(){
		if(!document.getElementById("links-patrocinados")) return;
		var sCode = "";
		if(showAd>0){
			sCode += overture.cab;
			sCode += "\n<dl>";
			for (var i = iniTRnum; i < adArray.length; i++) {
				if (adArray[i]!=null){
					sCode += '<dt><a href="' + adArray[i][0] + '" onmouseover="window.status=\'' + adArray[i][1].replace(/^http:\/\//,'') + '\'; return true;" onmouseout="window.status=\'\'" target="_blank">' + adArray[i][2] + '</a></dt>\n';
					sCode += '<dd class="descricao descricaolp">' + adArray[i][3] + '</dd>\n';
					sCode += '<dd class="link linklp"><a href="' + adArray[i][0] + '" onmouseover="window.status=\'' + adArray[i][1].replace(/^http:\/\//,'') + '\'; return true;" onmouseout="window.status=\'\'" target="_blank">' + adArray[i][1].replace(/^http:\/\//,'') + '</a></dd>\n';
				}
			}
			sCode += '</dl>\n';
			sCode += overture.fot;
			document.getElementById("links-patrocinados").innerHTML = sCode;
		}
	}
}

