$(function(){
	$('div.filtro h3').click(function(){
		h3 = this;
		$(h3).toggleClass("bg_mais");
   		$(h3).parent().find('div').toggleClass("off");
	})	
	$('div.filtro .mais').click(function(){
		$(this).parent().parent().find('.ativo').addClass("off");
		$(this).parent().parent().parent().find('.inativo').toggleClass("on"); 
	})	
	$('div.filtro .menos').click(function(){
		$(this).parent().parent().find('.inativo').removeClass("on");
		$(this).parent().parent().parent().find('.ativo').removeClass("off"); 
	})	
	$('#resultados .ebit').mouseover(function(){
		$(this).parent().parent().find('.ouro').toggleClass("ouroon");
	}).mouseout(function(){
		$(this).parent().parent().find('.ouro').removeClass("ouroon");
	})
	$('#resultados .ebit').mouseover(function(){
		$(this).parent().parent().find('.prata').toggleClass("prataon");
	}).mouseout(function(){
		$(this).parent().parent().find('.prata').removeClass("prataon");
	})
	$('#resultados .ebit').mouseover(function(){
		$(this).parent().parent().find('.bronze').toggleClass("bronzeon");
	}).mouseout(function(){
		$(this).parent().parent().find('.bronze').removeClass("bronzeon");
	})
	$('#resultados .ebit').mouseover(function(){
		$(this).parent().parent().find('.diamante').toggleClass("diamanteon");
	}).mouseout(function(){
		$(this).parent().parent().find('.diamante').removeClass("diamanteon");
	})
	$('#resultados .ebit').mouseover(function(){
		$(this).parent().parent().find('.sob_avaliacao').toggleClass("semavlon");
	}).mouseout(function(){
		$(this).parent().parent().find('.sob_avaliacao').removeClass("semavlon");
	})
	$('#resultados .emp_reco').mouseover(function(){
		$(this).parent().parent().parent().find('.balaoselo').toggleClass("on");
	}).mouseout(function(){
		$(this).parent().parent().parent().find('.balaoselo').removeClass("on");
	})
	$('#resultados .pag_dig').mouseover(function(){
		$(this).parent().parent().parent().find('.balaopd').toggleClass("on");
		$(this).parent().parent().parent().parent().find('.baloes').css("display", "none");
	}).mouseout(function(){
		$(this).parent().parent().parent().find('.balaopd').removeClass("on");
		$(this).parent().parent().parent().parent().find('.baloes').css("display", "block");
	})
	$('.imginter').mouseover(function(){
		$(this).parent().find('span.boxinter').toggleClass("on");
	}).mouseout(function(){
		$(this).parent().parent().parent().find('span.boxinter').removeClass("on");
	})
	$('.imginterpd').mouseover(function(){
		$(this).parent().find('.boxinterpd').toggleClass("on");
	}).mouseout(function(){
		$(this).parent().parent().parent().find('.boxinterpd').removeClass("on");
	})
	$('#boxqtsprod .imginter').mouseover(function(){
		$(this).parent().parent().find('#boxinter').css("display", "block");
	}).mouseout(function(){
		$(this).parent().parent().parent().find('#boxinter').css("display", "none");
	})
	$('#resultados .mais').click(function(){
		$(this).toggleClass("off");		
		$(this).parent().find('.menos').toggleClass("on");
		$(this).parent().find('.alerta').toggleClass("on");
		$(this).parent().find('.video').toggleClass("on");
	})
	$('#resultados .menos').click(function(){
		$(this).removeClass("on");		
		$(this).parent().find('.mais').removeClass("off");
		$(this).parent().find('.alerta').removeClass("on");
		$(this).parent().find('.video').removeClass("on");
	})
	$('#nova_cprocura .mais').click(function(){
		$(this).toggleClass("off");		
		$(this).parent().find('.menos').toggleClass("on");
		$(this).parent().find('.alerta').toggleClass("on");
		$(this).parent().find('.video').toggleClass("on");
	})
	$('#nova_cprocura .menos').click(function(){
		$(this).removeClass("on");		
		$(this).parent().find('.mais').removeClass("off");
		$(this).parent().find('.alerta').removeClass("on");
		$(this).parent().find('.video').removeClass("on");
	})
	
})

function barra_avl(nota, divnome, inputnome) {
	$('.'+divnome).slider({orientation:'horizontal',value:nota, min:0,max:5,animate:false,change:function(event,ui){finalValue=(ui.value > 0)?ui.value-0.2 : ui.value;inputnome.value=finalValue;submit();}})	
}

function barra_avl(nota, inputnome){
	
	$('.barra_avl div').slider({
		orientation: 'horizontal', 
		value:nota, 
		min:0, 
		max:5, 
		animate:false,
		change: function(event, ui) {submit_avl(ui.value, inputnome)}
	});

	$('.barra_avl .star').click(function(){
		submit_avl($('.barra_avl .star').index(this), inputnome);
		return false;
	});
}

function submit_avl(ui, inputnome) {

	inputnome.value=(ui > 0)?ui-0.2 : ui;
	submit();
}	

function resultados_skins(elemento_nome, minsize) {
	$(elemento_nome).each(function(){
		if(parseInt($('.mtable').css('width'))<minsize) {
			var preco = $(this).find('td.sk_preco').html();
			var logo = $(this).find('td.sk_loja').html();
			$(this).find('td.sk_preco').remove();
			$(this).find('td.sk_loja').remove();
			$(this).find('td.sk_botao').prepend('<p>' + preco + '</p>');
			$(this).find('td.sk_info').append('<p class="img_skin">' + logo + '</p>');
		}
	});
}

/* Início Código Campanha BuscaPe 2009 */
var balaoDisableGlobal = null;
var balaoIsShowedGlobal = false;

function ocultahint(id, nome){
	if (document.getElementById(id).style.display == 'block') {
		$('#' + id).hide();
		balaoIsShowedGlobal = false;
	}		
}

function ocultahintflash(id, nome){
	document.getElementById(id).style.display = "none";
	fecharBalaoClique(nome);	
	balaoIsShowedGlobal = false;	
}

function mostrahintSem(id, nome) {

	if (balaoIsShowedGlobal || !abrirBalao(nome)) {
		return;
	}

	gravaBalao(nome);
	
	$('#' + id).removeAttr("onmouseout");
	$('#' + id).mouseleave(function(){ocultahint(id, nome);})	
	setTimeout(function (){ocultahint(id, nome);}, 10000);
	
	var elemento = document.getElementById(id);
	if (elemento == null)
		return;
	
	if(document.getElementById(id).style.display != 'block') {
		document.getElementById(id).style.display = "block";
		balaoIsShowedGlobal = true;			
		var balaoBaixo = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="350" height="345">'
		+ '<param name="movie" value="http://s.buscape.com.br/balao/'+nome+'.swf">'
		+ '<param name="FlashVars" value="idDiv=' + id +'">'
		+ '<param name="quality" value="high">'
		+ '<param name="wmode" value="transparent">'
		+ '<param name="allowScriptAccess" value="always">'
		+ '<embed allowScriptAccess="always" wmode="transparent" src="http://s.buscape.com.br/balao/'+nome+'.swf" FlashVars="idDiv=' + id + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="350" height="345"></embed>'
		+ '</object>';
		$("#"+id).html(balaoBaixo);
	}
}

function fecharBalaoClique(nomeBalao) {
	var cookieValue = Get_CookieCamp('nbalao');
	if (!isEmpty(cookieValue)) {
		if (!temParametro(cookieValue, nomeBalao)) {
			cookieValue = cookieValue + nomeBalao + "&";	
			createCookieCamp("nbalao",cookieValue,".buscape.com.br","");
		}
	}
	else {
		cookieValue = nomeBalao + "&";	
		createCookieCamp("nbalao",cookieValue,".buscape.com.br","");		
	}	
}

function abrirBalao(nomeBalao) {

	if (balaoDisableGlobal == null) balaoDisableGlobal = retornaBalaoDisable();
	
	if (balaoDisableGlobal)
		return false; // não mostra	
	
	if (verificaCookieParametro(Get_CookieCamp('nbalao'), nomeBalao) ||
		verificaCookieParametro(Get_CookieCamp('sbalao'), (nomeBalao + "x2"))) 
		return false; // não mostra	
	else
		return true;
}

function verificaCookieParametro(cookieValue, nomeBalao){
	if (!isEmpty(cookieValue)) {
		if (temParametro(cookieValue, nomeBalao)) {
			return true; 		
		}
	}
	return false;
}

function gravaBalao(nomeBalao) {
	if (nomeBalao == 'balao04' || nomeBalao == 'balao13' || nomeBalao == 'balao06') {
		var cookieValue = Get_CookieCamp('sbalao');
		
		if (!isEmpty(cookieValue)) {
			if (!temParametro(cookieValue, nomeBalao)) 
				cookieValue = cookieValue + nomeBalao + "&";	
			 else 
				cookieValue = cookieValue.replace(nomeBalao + "&", nomeBalao + "x2&");							
		}
		else {cookieValue = nomeBalao + "&";}	
		createCookieCamp("sbalao",cookieValue,".buscape.com.br","");	
	}
}

function retornaBalaoDisable() {
    var resultado = false;
	var cookieValue = Get_CookieCamp('nbalao');
	var dataControleCookie, tipoDataCookie;
	
	if (!isEmpty(cookieValue)) {
		dataControleCookie = getParametro(cookieValue, "dc_p1", false);
		tipoDataCookie = getParametro(cookieValue, "tdc_p2", false);	
		//tdc_p2=1 (2 dias)  -  tdc_p2=2 (30 dias)	
	} 
	else { cookieValue = "";}	

	var dataControle = new Date();
	
	if (isEmpty(dataControleCookie)) {
		dataControle.setDate(dataControle.getDate()+2);
		cookieValue = "tdc_p2=1&dc_p1=" + dataControle.getTime() + "&" + cookieValue; 
		createCookieCamp("nbalao",cookieValue,".buscape.com.br","");		
	} 
	else if (dataControle < dataControleCookie && tipoDataCookie == '2') {	
		resultado = true;
	} else if (dataControle > dataControleCookie) {
	    if (tipoDataCookie == '1') {
			dataControle.setDate(dataControle.getDate()+30);		
			cookieValue = cookieValue.replace("tdc_p2=1", "tdc_p2=2");
			cookieValue = cookieValue.replace("dc_p1=" + dataControleCookie, "dc_p1=" + dataControle.getTime());	
			createCookieCamp("nbalao",cookieValue,".buscape.com.br","");
			resultado = true;
		}
		else if (tipoDataCookie == '2') {
			dataControle.setDate(dataControle.getDate()+2);		
			cookieValue = cookieValue.replace("tdc_p2=2", "tdc_p2=1");
			cookieValue = cookieValue.replace("dc_p1=" + dataControleCookie, "dc_p1=" + dataControle.getTime());	
			createCookieCamp("nbalao",cookieValue,".buscape.com.br","");
		}
		else {
			eraseCookieCamp('nbalao');
			resultado = null;
		}
	}
	return resultado;
}

function getParametro(texto, parametro, parametro_mais_valor) {
	parametro = parametro + "=";
	var startIndex = texto.indexOf(parametro);
	if (startIndex == -1) return null;
	var endIndex = texto.indexOf("&", startIndex);
	if (endIndex == -1)	endIndex = texto.length;
	if (!parametro_mais_valor) startIndex = (startIndex + parametro.length);
	return texto.substring(startIndex, endIndex);
}

function temParametro(texto, parametro) {
	parametro = parametro + "&";
	var startIndex = texto.indexOf(parametro);
	if (startIndex == -1) return false;
	return true;	
}

function eraseCookieCamp(name) {createCookieCamp(name,"",".buscape.com.br",-1);}

function createCookieCamp(name,value,domainName,days) {
	var domain = "; domain="+domainName; 
	if (name == 'nbalao') {
		var date = new Date(2010, 01, 15);
		var expires = "; expires="+date.toGMTString();
	} else if (days) {
		var date = new Date();	
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+domain+"; path=/";
}

function isEmpty( inputStr ) {if (inputStr == null || inputStr.length == 0) return true;return false;}

function Get_CookieCamp(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {return null;}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/* Fim Codigo Campanha BuscaPe 2009 */

function rotateCNQBarato() {
	var elems = $('#comp_bot .qb_cn');
	var rand = Math.floor(Math.random() * elems.length); 
	if(rand % 2 == 0) {
		elems.each(function() {
			$(this).toggleClass('off');
		});
	}
}
/* #### Início Código Resultados Internacionais ####*/
$(function(){
	$('#resultadosMais p').click(function(){
		$(this).parent().toggleClass("r_off");
		$(this).parent().removeClass("r_on");
		$(this).parent().parent().find('#resultadosMenos').toggleClass("r_on");
		$(this).parent().parent().find('#resultadosMenos').removeClass("r_off");
		$(this).parent().parent().find('.paises_off').toggleClass("paises_on");
		$(this).parent().parent().find('.paises_on').removeClass("paises_off");
	})
	$('#resultadosMenos p').click(function(){
		$(this).parent().toggleClass("r_off");
		$(this).parent().removeClass("r_on");
		$(this).parent().parent().find('#resultadosMais').toggleClass("r_on");
		$(this).parent().parent().find('#resultadosMais').removeClass("r_off");
		$(this).parent().parent().find('.paises_on').toggleClass("paises_off");
		$(this).parent().parent().find('.paises_off').removeClass("paises_on");
	}) 
})
/* #### Fim Código Resultados Internacionais ####*/

/* #### Início Código Validação Campo de Busca ####*/
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function validaBusca(idbox){
	var box=document.getElementById(idbox);
	box.value = trim(box.value);
	if(box.value==''){
		alert('Por favor, digite um produto ou serviço para realizar a busca.');
		return false;
	}else
		return true;
}
/* #### Fim Código Validação Campo de Busca ####*/