$(document).ready(function() {
	
	
	var cufon_arr = new Array();
	cufon_arr.push("ul.nav li a");
	cufon_arr.push(".titulo");
	cufon_arr.push(".coment_button a");
	
	for(i=0; i<cufon_arr.length; i++){
		if($(cufon_arr[i]).length>0)
			Cufon.replace(cufon_arr[i]);
	}
	
	Cufon.replace('.vermais', {
		textShadow: '#8a8987 1px -1px'
	});



	if(pagina != "home"){
		$("select[name=sitemap]").change(function(){
			window.location.href = $(this).find("option:selected").val();
		});
	}
	
	if($(".banner_top").length > 0){
		setTimeout(function(){$(".banner_top").slideDown("slow");}, 2000);
		$(".banner_top .bt_fechar, .banner_top .bt_saiba_mais").click(function(){
			$.post(path+"banner_top_close.php");
			$(".banner_top").slideUp("slow");
		});
	}
	
	//INICIO
	switch(pagina){
		case "home":
			
			var banner_principal = 	{	
										json: path+"banners.php",
										elementWidth: 152,
										leftArrowEnabled: path+"/img/roleta_seta_left.png",
										rightArrowEnabled: path+"/img/roleta_seta_right.png",
										leftArrowDisabled: path+"/img/roleta_seta_left.png",
										rightArrowDisabled: path+"/img/roleta_seta_right.png"				
									}
			$(".roleta").banners_slider(banner_principal);
			$(".box_abas").abas();
			
			if(!Modernizr.touch){
				//$("body").popup({url:path+"popup_landscape.php", width:744, height:457, closebutton:".closebutton"})
			}
			
		break
		case "institucional":
		
			init_imgToBackground("ul.parceiros li span");
			
			$("ul.sections li").delegate("a[rel=section_sobre]", "click", function(){
				$("body").scrollTo($("#section_sobre"), 800,  { easing:'easeInOutCubic'});
			});
			$("ul.sections li").delegate("a[rel=section_parceiros]", "click", function(){
				$("body").scrollTo($("#section_parceiros"), 800,  { easing:'easeInOutCubic'});
			});
			$("ul.sections li").delegate("a[rel=section_curriculo]", "click", function(){
				$("body").scrollTo($("#section_curriculo"), 800,  { easing:'easeInOutCubic'});
			});
			
			$("input[type=file]").filestyle({ 
				 image: path+"img/btn_anexar_curriculo.png",
				 imageheight : 43,
				 imagewidth : 180,
				 width : 180
			 });
		
		break
		case "cartao_fidelidade":
		
			init_imgToBackground("ul.parceiros li span");			
		
		break
		case "busca":
		
			search_acordeon();
			$(".box_abas").abas();
		
		break
		case "imoveis":
			
			if(!session_revenda){
				if(!Modernizr.touch){
					popuprevenda({title:'REVENDA LAN&Ccedil;AMENTOS', url:path+'popup_revenda.php'});
				}
			}
		
			search_acordeon();
			$("a.eb").expand_button();
		
		break
		case "imovel":
		
			$(".box_abas").abas();
		
		break
		case "empreendimentos":
		
			search_acordeon();
			$("a.eb").expand_button();
		
		break
		case "empreendimento":
		
			$(".box_abas").abas();
		
		break
		case "vender_alugar":
			$("input[type=file]").filestyle({ 
				 image: path+"img/img_anexar_foto.png",
				 imageheight : 23,
				 imagewidth : 79,
				 width : 79
			 });
		break
		
	}
	
	
	///MENU
	$("ul.nav li").mouseenter(function(){
		var image = path+"img/"+$(this).find("a").attr("rel")+"_over.png";
		$(this).find("a:eq(0) img").attr("src",image);
	}).mouseleave(function(){
		var image = path+"img/"+$(this).find("a").attr("rel")+".png"; 
		$(this).find("a:eq(0) img").attr("src",image);
	});
	
	
	
	if($(".coment_response").length > 0){
		$(".coment_data").delegate("a.coment_response", "click", function(){
			$(".formulario_coments input[name=idResposta]").val(this.rel);
			$("body").scrollTo($(".formulario_coments"), 800,  { easing:'easeInOutCubic'});
		});
	}
	
	
	$(".redes_sociais span a").mouseenter(function(){
		$(this).parents("span").animate({
			marginTop: '-5px'
		}, 300);
	}).mouseleave(function(){
		$(this).parents("span").animate({
			marginTop: '0'
		}, 300);
	});
	
	
	//$(".latest_tweets").latest_tweets({searchQuery:"from:imoveis_adao",rpp:2});
	$(".latest_flickr ul.imgs").latest_flickr({elementToCreate:"<li></li>"});
	
	controleEnvioFormularios();
	
	$("#txtBusca").autocomplete(path+"search.php", {
		width: 308,
		autoFill: true,
		max:100
	}).result(function(event, data, formatted){
		$("input[name=txtQuery]").val(data[1]);
	}).keyup(function(e){
		if(e.keyCode != 13)
			$("input[name=txtQuery]").val("");
	});
	
});


$.easing.easeInOutCubic = function (x, t, b, c, d) {
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}

function search_acordeon(){
	$(".search_filters ul li a").click(function(){
		$(this).parents("li").find("ul").slideToggle(function(){
			$(".search_filters ul li ul:visible").parents("li").find("a:eq(0)").attr("style","background:url("+path+"img/bg_filter_arrow_up.png) no-repeat 135px 5px;");	
			$(".search_filters ul li ul:hidden").parents("li").find("a:eq(0)").attr("style","background:url("+path+"img/bg_filter_arrow_down.png) no-repeat 135px 5px;");
		});
		$(this).parents("li").find("ul li a").unbind("click");
	});
}

function controleEnvioFormularios(){
	
	if($(".form_submit").length>0){
		$(".form_submit").each(function(){
			$(this).css("cursor","pointer");
			$(this).unbind("click");
			$(this).click(function(){
				$(this).parents("form").submit();
			});
		});
	}
	
	if($("form").length>0){
		$("form").unbind("submit");
		$("form").submit(function(){
			switch(this.name){
				case "form_imovel_email":
					Envia_Email(this);
				break;
				case "form_agendar_visita":
					Agendar(this);										
				break;
				case "form_coments":
					Envia_Comentario(this);			
				break;
				case "form_coments2":				
					Envia_Contato2(this);				
				break;
				case "form_imovel_contato":				
					Envia_Contato(this);				
				break;
				case "form_ligamos_p_voce":
					Ligamos_Voce(this);
				break;
				case "form_proposta":				
					Proposta(this);
				break;
				case "form_curriculo":
					Curriculum(this);
				break;
				case "form_contato":
					Fale_Conosco(this);				
				break;
				case "busca_simples":
				 	Busca_Simples(this);
				break;
			}
		});
	}
}

function Proposta(){
	var form			=	document.getElementById("form_proposta");
	var file_upload		=	form.file_upload.value;
	var pagina 			= 	"prosposta";
	ajaxUpload(form, path+"scripts/proposta.php",pagina,2);
}

function Curriculum(){
	var form			=	document.getElementById("form_curriculo");
	var file_upload		=	form.file_upload.value;
	var pagina 			= 	"institucional";
	ajaxUpload(form, path+"scripts/trabalhe_conosco.php",pagina,1);
}

function Ligamos_Voce(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var telefone=form.txtFone.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (telefone==""){
		alert("Digite o Telefone.");													
		form.txtFone.focus();
		return false;
	} else {
		$.post(path+'scripts/ligamos.php',$(form).serialize(),function(data){
			_gaq.push(['_trackPageview', '/LIGAMOS']); 
			$(".popup_formulario").fadeOut(function(){
				$(".agradecimento").fadeIn();
			});
		});
	}
}

function Envia_Email(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var destinatario=form.txtDestinatario.value;
	var comentario=form.txtComentario.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (destinatario==""){
		alert("Digite o Nome do Destinatario.");													
		form.txtDestinatario.focus();
		return false;
	} else {
		$.post(path+'scripts/envia_email.php',$(form).serialize(),function(data){
			_gaq.push(['_trackPageview', '/ENVIAREMAIL']);
			$(".popup_formulario").fadeOut(function(){
				$(".agradecimento").fadeIn();
			});
		});
	}
}

function Agendar(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var txtData=form.txtData.value;
	var txtHora=form.txtHora.value;
	var txtFone=form.txtFone.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (txtFone==""){
		alert("Digite a Telefone.");													
		form.txtFone.focus();
		return false;
	}else if (txtData==""){
		alert("Digite a Data.");													
		form.txtData.focus();
		return false;
	} else if (txtHora==""){
		alert("Digite o Horario.");													
		form.txtData.focus();
		return false;
	}else {
		$.post(path+'scripts/agendar.php',$(form).serialize(),function(data){
			_gaq.push(['_trackPageview', '/AGENDARVISITA']);
			$(".popup_formulario").fadeOut(function(){
				$(".agradecimento").fadeIn();
			});
		});
	}
}

function Envia_Contato(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var telefone=form.txtFone.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (telefone==""){
		alert("Digite o Telefone.");													
		form.txtFone.focus();
		return false;
	} else {
		$.post(path+'scripts/contato.php',$(form).serialize(),function(data){
			form.txtNome.value = '';
			form.txtEmail.value = '';
			form.txtFone.value = '';
			alert("Entraremos em contato em breve");
		});
	}
}

function Envia_Contato2(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var telefone=form.txtFone.value;
	var mensagem=form.txtMensagem.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (telefone==""){
		alert("Digite o Telefone.");													
		form.txtFone.focus();
		return false;
	} else if (mensagem==""){
		alert("Digite a Mensagem.");													
		form.txtMensagem.focus();
		return false;
	} else {
		$.post(path+'scripts/envia_contato.php',$(form).serialize(),function(data){
			form.txtNome.value = '';
			form.txtEmail.value = '';
			form.txtFone.value = '';
			form.txtMensagem.value = '';
			alert("Cadastro Efetuado com Suceso");
		});
	}
}

function Fale_Conosco(form){
	var nome=form.txtNome.value;
	var email=form.txtEmail.value;
	var telefone=form.txtFone.value;
	var cidade=form.txtCidade.value;
	var txtMensagem=form.txtMensagem.value;
	if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail.focus();
		return false;
	} else if (telefone==""){
		alert("Digite o Telefone.");													
		form.txtFone.focus();
		return false;
	} else if (cidade==""){
		alert("Digite a Cidade.");													
		form.txtCidade.focus();
		return false;
	}else if (txtMensagem==""){
		alert("Digite a Mensagem.");													
		form.txtMensagem.focus();
		return false;
	}else {
		$.post(path+'scripts/fale_conosco.php',$(form).serialize(),function(data){
			_gaq.push(['_trackPageview', '/CONTATO']);
			form.reset();
			alert("Entraremos em contato em breve");
		});
	}
}

function Envia_Comentario(form){
	var nome=form.txtNome2.value;
	var email=form.txtEmail2.value;
	var mensagem=form.txtMensagem.value;
		if (nome==""){
		alert("Digite o Nome.");											
		form.txtNome2.focus();
		return false;
	}else if (email==""){
		alert("Digite o Email.");													
		form.txtEmail2.focus();
		return false;
	}else if(!validaEmail(email)){
		alert("Formato do Email Incorreto.");													
		form.txtEmail2.focus();
		return false;
	} else if (mensagem==""){
		alert("Digite a Mensagem.");													
		form.txtMensagem.focus();
		return false;
	} else {
		$.post(path+'scripts/comentario.php',$(form).serialize(),function(data){
			_gaq.push(['_trackPageview', '/COMENTOU']);
			form.txtNome2.value = '';
			form.txtEmail2.value = '';
			form.txtTwitter.value = '';
			form.txtFacebook.value = '';
			form.txtMensagem.value = '';
			form.idResposta.value = 0;
			alert("Comentario Enviado Para Analise");
		});
	}
}

// TROCA IMAGENS NO HTML EV ACIONA NO BACKGROUND
function init_imgToBackground(element){
	$(element+" img").each(function(i){
		$(this).parent().css("background","url("+$(this).attr("src")+") no-repeat center center");
		$(this).hide();
	})
}

// TROCA IMAGENS NO HTML E ACIONA NO BACKGROUND
function init_logoToBackground(element){
	$(element+" img").each(function(i){
		$(this).parent().css("background","url("+$(this).attr("src")+") no-repeat left center");
		$(this).hide();
	})
}


function init_gallery_events(){
	
	
	//ADICIONA UM ID PARA CADA GALERIA
	$(".galeria").each(function(i){
		
		var galeria = "g"+i;
		$(this).attr("id",galeria);
		
		//PRIMEIRA IMAGEM DE CADA GALERIA
		var objImage = new Image();
		objImage.src = $(this).find("ul li:first a").attr("href");
		objImage.alt = $(this).find("ul li:first img").attr("alt");
		$(this).find(".desc").html("<img src='"+path+"/img/loadin1.gif' />");
		changeImg(objImage, galeria);
		
	});
	
	var arr_imgs = new Array();
	var arr_desc = new Array();
	
	$(".galeria ul li").each(function(i){
		
		arr_imgs.push($(this).find("a").attr("href"));
		arr_desc.push($(this).find("img").attr("alt"));
		
		$(this).find("a").attr("href", "javascript:;").click(function(){
			$(".galeria .img img").fadeOut("fast");
			var objImage = new Image();
			objImage.src = arr_imgs[i];
			objImage.alt = arr_desc[i];
			var galeria = $(this).parents(".galeria").attr("id");
			changeImg(objImage, galeria);
			
		});
	});
	
}


// FAZ A TROCA DA IMAGEM
function changeImg(objImage, galeria){
	
	if(objImage.complete){
		$("#"+galeria+" .img").empty();
		$("<img src='"+objImage.src+"' alt='"+objImage.alt+"' />").appendTo(".galeria .img");
		$("#"+galeria+" .desc").html(objImage.alt);
	}else{
		setTimeout(function(){changeImg(objImage,galeria);},500);
	}
	
}

//------------------------TODAS REQUISIÇÕES AJAX---------------------------
//-------------------------------------------------------------------------


function ajaxGeral(form, url){
	$.post(url, $(form).serialize(),
		function(data){
			
			$(form).find(".resposta").html(data);
			form.reset();
			form_titles_init();
			
		}
	);
}

function ajaxUpload(form, url){
	$("<iframe src='javascript:;' name='ajaxUpload' id='ajaxUpload'></iframe>").appendTo("body");
	$(form).attr({action: url, target: "ajaxUpload"});
	$.event.trigger("ajaxStart");
	upLoading(1);
}

function upLoading(num){
	iframe = document.getElementById("ajaxUpload");
	
	if($.browser.msie){
		if(iframe.readyState != "complete"){
			$("#ajaxMessage").text(iframe.readyState+" "+num);
			setTimeout(function(){upLoading(num+1)}, 500);
		}else{
			uploadReturn();
		}
	}else {
		iframe.onload = function(){uploadReturn();};
	}
	
}

function uploadReturn(){
	ajaxMessage = $("iframe[name='ajaxUpload']").contents().children().find("body").text();
	alert(ajaxMessage);
	$.event.trigger("ajaxSuccess");
	
	$(":input")
	 .not(":button, :submit, :reset, :hidden")
	 .val("")
	 .removeAttr("checked")
	 .removeAttr("selected");
	 
	 $("iframe[name='ajaxUpload']").attr("src","");
	 $("iframe[name='ajaxUpload']").remove();
}

function ajaxBox(url, atual){
	$(".ab_menu a").removeClass("atual");
	$(".ab_menu a:eq("+atual+")").addClass("atual");
	$(".ab_load").load(url);
}

function popupContato(id){
	
	
	$("#topo").before("<div class=\"popup_contato_form\" style=\"display:none;\"></div>");
	
	$(".popup_contato_form").load(path+"/inc_form_contato_seminovos.php?id="+id, function(){
																						  
		if($.browser.safari){
			scrollPosition = document.body.scrollTop;
		}else{
			scrollPosition = document.documentElement.scrollTop;
		}
		var half = $(this).height()/2;
		var posy = scrollPosition+(document.documentElement.clientHeight/2)-half;
		
		$(this).fadeIn();
		$(this).find(".fechar img").css("cursor","pointer").click(function(){$(".popup_contato_form").remove();});
		$(this).css("top",posy+"px");
	})
	
}


function popup1(settings){
	ajaxMessage = "Carregando Página";
	
	config = {
		width:"750",
		height:"570"
	};
	
	$.extend(config, settings);

	
	$("<div id=\"bg_black\" onClick=\"javascript:fecharpopup();\" style=\"display:none;\"></div>").appendTo("body");
	
	$("<div class=\"popup\"></div>").appendTo("body");
	
	if($.browser.safari){
		scrollPosition = document.body.scrollTop;
	}else{
		scrollPosition = document.documentElement.scrollTop;
	}
	
	
	$("#bg_black").fadeTo(01, 0).css({width:"100%",height:$(document).height()+"px",cursor:"pointer",top:0,position:"absolute",background:"#000",zIndex:"999"});
	$("#bg_black").fadeTo("slow", 0.7);
	
	var half = config.height/2;
	
	if(document.documentElement.clientHeight>config.width){
		var posy = (document.documentElement.clientHeight/2)-half;
	}else{
		var posy = 0;
	}
	
	var posx = config.width/-2;
	

	$(".popup").fadeTo(01, 0).css({top: (posy+scrollPosition)+"px", left:"50%",marginLeft:posx+"px", zIndex: "1000", width:config.width+"px", height:config.height+"px"});
	
	$(".popup").fadeTo("slow", 1);
	
	
	//POR AJAX
	$(".popup").load(path+"popup_atmosphere.php");
	
	//POR IFRAME
	//$("<iframe src='"+path+config.url+"' width='640' height='330' frameborder='0'></iframe>").appendTo(".popup .content");
	

	$(window).scroll(function(){
		if($(".popup").length>0){
			if($.browser.safari){
				scrollPosition = document.body.scrollTop;
			}else{
				scrollPosition = document.documentElement.scrollTop;
			}
			
			if(document.documentElement.clientHeight>config.width){
				posy = (document.documentElement.clientHeight/2)-half;
			}else{
				posy = 0;
			}
			
			$(".popup").css("top",(posy+scrollPosition)+"px");
		}
	});
	
}


//******************************************************************************/
// JF POPUP
/******************************************************************************/


function popup(settings){
	ajaxMessage = "Carregando Página";
	
	config = {
		width:"687",
		height:"502",
		title:"Popup",
		url:""
	};
	
	$.extend(config, settings);

	
	$("<div id=\"bg_black\" onClick=\"javascript:fecharpopup();\" style=\"display:none;\"></div>").appendTo("body");
	
	$("<div class=\"popup\"><div class=\"top\"><h1>"+config.title+"</h1><a href=\"javascript:fecharpopup();\"><img src=\""+path+"img/btn_fechar.png\" alt=\"Fechar\" /></a></div><div class=\"content\"></div></div>").appendTo("body");
	
	if($.browser.safari){
		scrollPosition = document.body.scrollTop;
	}else{
		scrollPosition = document.documentElement.scrollTop;
	}
	
	
	$("#bg_black").fadeTo(01, 0).css({width:"100%",height:$(document).height()+"px",cursor:"pointer",top:0,position:"absolute",background:"#000",zIndex:"999"});
	$("#bg_black").fadeTo("slow", 0.7);
	
	var half = config.height/2;
	
	if(document.documentElement.clientHeight>config.width){
		var posy = (document.documentElement.clientHeight/2)-half;
	}else{
		var posy = 0;
	}
	
	var posx = config.width/-2;
	

	$(".popup").fadeTo(01, 0).css({top: (posy+scrollPosition)+"px", left:"50%",marginLeft:posx+"px", zIndex: "1000", width:config.width+"px", height:config.height+"px"});
	
	$(".popup").fadeTo("slow", 1);
	
	
	//POR AJAX
	$(".popup .content").load(config.url, function(){
		formlicious_init();
	});
	
	//POR IFRAME
	//$("<iframe src='"+path+config.url+"' width='640' height='330' frameborder='0'></iframe>").appendTo(".popup .content");
	

	$(window).scroll(function(){
		if($(".popup").length>0){
			if($.browser.safari){
				scrollPosition = document.body.scrollTop;
			}else{
				scrollPosition = document.documentElement.scrollTop;
			}
			
			if(document.documentElement.clientHeight>config.width){
				posy = (document.documentElement.clientHeight/2)-half;
			}else{
				posy = 0;
			}
			
			$(".popup").css("top",(posy+scrollPosition)+"px");
		}
	});
	
}


function popuphome(settings){
	ajaxMessage = "Carregando Página";
	
	config = {
		width:"687",
		height:"270",
		title:"Popup",
		url:""
	};
	
	$.extend(config, settings);

	
	$("<div id=\"bg_black\" onClick=\"javascript:fecharpopuphome();\" style=\"display:none;\"></div>").appendTo("body");
	
	$("<div class=\"popuphome\"><div class=\"top\"><h1><b>BREVES</b> LAN&Ccedil;AMENTOS</h1><a href=\"javascript:fecharpopuphome();\"><img src=\""+path+"img/btn_fechar_home.png\" alt=\"Fechar\" /></a></div><div class=\"content\"></div></div>").appendTo("body");
	
	if($.browser.safari){
		scrollPosition = document.body.scrollTop;
	}else{
		scrollPosition = document.documentElement.scrollTop;
	}
	
	
	$("#bg_black").fadeTo(01, 0).css({width:"100%",height:$(document).height()+"px",cursor:"pointer",top:0,position:"absolute",background:"#000",zIndex:"999"});
	$("#bg_black").fadeTo("slow", 0.7);
	
	var half = config.height/2;
	
	if(document.documentElement.clientHeight>config.width){
		var posy = (document.documentElement.clientHeight/2)-half;
	}else{
		var posy = 0;
	}
	
	var posx = config.width/-2;
	

	$(".popuphome").fadeTo(01, 0).css({top: 100, left:"50%",marginLeft:posx+"px", zIndex: "1000", width:config.width+"px", height:config.height+"px"});
	
	$(".popuphome").fadeTo("slow", 1);
	
	
	//POR AJAX
	$(".popuphome .content").load(config.url, function(){
		formlicious_init();
	});
	
	//POR IFRAME
	//$("<iframe src='"+path+config.url+"' width='640' height='330' frameborder='0'></iframe>").appendTo(".popup .content");
	

	$(window).scroll(function(){
		if($(".popuphome").length>0){
			if($.browser.safari){
				scrollPosition = document.body.scrollTop;
			}else{
				scrollPosition = document.documentElement.scrollTop;
			}
			
			if(document.documentElement.clientHeight>config.width){
				posy = (document.documentElement.clientHeight/2)-half;
			}else{
				posy = 0;
			}
			
			$(".popuphome").css("top",(posy+scrollPosition)+"px");
		}
	});
	
}





function popuprevenda(settings){
	ajaxMessage = "Carregando Página";
	
	config = {
		width:"720",
		height:"470",
		title:"Popup",
		url:""
	};
	
	$.extend(config, settings);

	
	$("<div id=\"bg_black\" onClick=\"javascript:fecharpopuphome();\" style=\"display:none;\"></div>").appendTo("body");
	
	$("<div class=\"popuprevenda\"><div class=\"top\"><h1><b>REVENDA</b></h1><a href=\"javascript:fecharpopuprevenda();\"><img src=\""+path+"img/btn_fechar_home.png\" alt=\"Fechar\" /></a></div><div class=\"content\"></div></div>").appendTo("body");
	
	if($.browser.safari){
		scrollPosition = document.body.scrollTop;
	}else{
		scrollPosition = document.documentElement.scrollTop;
	}
	
	
	$("#bg_black").fadeTo(01, 0).css({width:"100%",height:$(document).height()+"px",cursor:"pointer",top:0,position:"absolute",background:"#000",zIndex:"999"});
	$("#bg_black").fadeTo("slow", 0.7);
	
	var half = config.height/2;
	
	if(document.documentElement.clientHeight>config.width){
		var posy = (document.documentElement.clientHeight/2)-half;
	}else{
		var posy = 0;
	}
	
	var posx = config.width/-2;
	

	$(".popuprevenda").fadeTo(01, 0).css({top: 100, left:"50%",marginLeft:posx+"px", zIndex: "1000", width:config.width+"px", height:config.height+"px"});
	
	$(".popuprevenda").fadeTo("slow", 1);
	
	
	//POR AJAX
	$(".popuprevenda .content").load(config.url, function(){
		formlicious_init();
	});
	
	//POR IFRAME
	//$("<iframe src='"+path+config.url+"' width='640' height='330' frameborder='0'></iframe>").appendTo(".popup .content");
	

	$(window).scroll(function(){
		if($(".popuprevenda").length>0){
			if($.browser.safari){
				scrollPosition = document.body.scrollTop;
			}else{
				scrollPosition = document.documentElement.scrollTop;
			}
			
			if(document.documentElement.clientHeight>config.width){
				posy = (document.documentElement.clientHeight/2)-half;
			}else{
				posy = 0;
			}
			
			$(".popuprevenda").css("top",(posy+scrollPosition)+"px");
		}
	});
	
}







function fecharpopup(){
	$(".popup").fadeOut(function(){$(this).remove();});
	$("#bg_black").fadeOut(function(){$(this).remove();});
	$.browser.msie ? $("body").removeAttr("scroll") : $("body").css("overflow", "auto");	
}

function fecharpopuphome(){
	$(".popuphome").fadeOut(function(){$(this).remove();});
	$("#bg_black").fadeOut(function(){$(this).remove();});
	$.browser.msie ? $("body").removeAttr("scroll") : $("body").css("overflow", "auto");	
}

function fecharpopuprevenda(){
	$(".popuprevenda").fadeOut(function(){$(this).remove();});
	$("#bg_black").fadeOut(function(){$(this).remove();});
	$.browser.msie ? $("body").removeAttr("scroll") : $("body").css("overflow", "auto");	
}



/******************************************************************************/

//---------------------PRÓXIMO CAMPO---------------------------------------------------------
function jrs_Valida(campo, prox, total)
{
	if(campo.value.length>total-1)document.getElementById(prox).focus();
}

//MÁSCARA DE VALORES
function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	} else {
		nTecla = evtKeyPress.which;
		if (nTecla == 8 || nTecla == 0) {
			return true;
		}
	}
    sValue = objeto.value;
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++;
	  }else{
        sCod += sValue.charAt(nCount);
        nCount++;
      }
      i++;
    }
    objeto.value = sCod;

	if (nTecla != 8) { // backspace ou tab
		if(sMask.charAt(i-1) == "9"){ // apenas números...
			return ((nTecla > 47) && (nTecla < 58));
		}else{ // qualquer caracter...
			return true;
		} 
	}else{
		return true;
	}
}

function validaEmail(email){
	ER = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64})*\.[a-z]{2,4}$");
	if (ER.test(email)){
		return true;
	}else{
		return false;
	}
}

/******************************************************************************/
// FILTROS
/******************************************************************************/
$("input[name=categoria]").live("change",function(){
	var categoria = $("input[name=categoria]:checked").val();
	$("select[name=regiao]").empty();
	$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria);
	$("select[name=imovel]").empty();
	$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria);
	$("select[name=quartos]").empty();
	$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria);
})

function Submeter_Busca() {
	var categoria = $("input[name=categoria]:checked").val();
	var regiao = $("select[name=regiao] option:selected").val();
	var imovel = $("select[name=imovel] option:selected").val();
	var quartos = $("select[name=quartos] option:selected").val();
	if(regiao != "" && imovel != "" && quartos != "") {
		window.location = path+"busca?categoria="+categoria+"&regiao="+regiao+"&imovel="+imovel+"&quartos="+quartos;		
	} else if(regiao != "" && imovel != "") {
		window.location = path+"busca?categoria="+categoria+"&regiao="+regiao+"&imovel="+imovel;				
	}else if(regiao != "" && quartos != "") {
		window.location = path+"busca?categoria="+categoria+"&regiao="+regiao+"&quartos="+quartos;				
	}else if(imovel != "" && quartos != "") {
		window.location = path+"busca?categoria="+categoria+"&imovel="+imovel+"&quartos="+quartos;				
	}else if(regiao != "") {
		window.location = path+"busca?categoria="+categoria+"&regiao="+regiao;				
	}else if(imovel != "") {
		window.location = path+"busca?categoria="+categoria+"&imovel="+imovel;				
	}else if(quartos != "") {
		window.location = path+"busca?categoria="+categoria+"&quartos="+quartos;				
	}else {
		window.location = path+"busca?categoria="+categoria;
	}
}

function Busca_Simples(form) {
	var busca = $("input[name=txtBusca]").val();
	if(busca == "") {
		alert("Informe um valor para buscar");
	} else {
		_gaq.push(['_trackPageview', '/busca?busca='+busca]);
		_gaq.push(['_trackPageview', '/BUSCOU']);
		form.submit();
	}
}

//-----------------------------FUNÇÕES UPLOAD--------------------------------------------------------
function ajaxUpload(form, url, urlretorno,tipo){
	$("<iframe src='javascript:;' name='ajaxUpload' id='ajaxUpload' style='display:none;'></iframe>").appendTo("body");
	$(form).attr({action: url});
	upLoading(urlretorno,tipo);	
}

function upLoading(urlretorno,tipo){
	iframe = document.getElementById("ajaxUpload");
	
	if($.browser.msie){
		if(iframe.readyState != "complete"){
			setTimeout(function(){upLoading(urlretorno,tipo)}, 500);
		}else{
			uploadReturn(urlretorno,tipo);
		}
	}else {
		iframe.onload = function(){uploadReturn(urlretorno,tipo);};
	}
	
}
function uploadReturn(urlretorno,tipo){
	var data = $("iframe[name='ajaxUpload']").contents().children().find("body").text();
	if(tipo == 1) {
		var form	=	document.getElementById("form_curriculo");
		if(data == 'vazio') {
			alert("Anexe o Curriculum");
		} else if(data == 'ext') {
			alert("Somente aceitos formatos pdf, doc, docx");			
		}else if(data == 'erro') {
			alert("Nao foi possivel gravar o curriulum tente novamente!");			
		}else {
			$(".retorno_form").fadeIn();
			form.reset();
		}
	} else if(tipo == 2){
		var form	=	document.getElementById("form_proposta");
		if(data == "todos") {
			alert("Preencher todos os campos, inclusive anexar foto");
		}else if(data == 'ext') {
			alert("Fotos aceitas somente formatos jpg ou jpeg");
		}else if(data == 'erro') {
			alert("Nao foi possivel enviar os dados tente novamente!");
		} else {
			$(".retorno_form").fadeIn();
			form.reset();
		}
	}
	 $("iframe[name='ajaxUpload']").attr("src","");
	 $("iframe[name='ajaxUpload']").remove();
}

/*$("select[name=regiao]").live("change",function(){
	var categoria = $("input[name=categoria]:checked").val();
	var regiao = $("select[name=regiao] option:selected").val();
	var imovel = $("select[name=imovel] option:selected").val();
	var quartos = $("select[name=quartos] option:selected").val();
		$("select[name=imovel]").empty();
		if(imovel != "" && quartos != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&regiao="+regiao+"&quartos="+quartos+"&imovel="+imovel);
		}else if(imovel != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&regiao="+regiao+"&imovel="+imovel);		
		}else if(quartos != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&regiao="+regiao+"&quartos="+quartos);		
		} else {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&regiao="+regiao);
		}	
		$("select[name=quartos]").empty();
		if(imovel != "" && quartos != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&regiao="+regiao+"&quartos="+quartos+"&imovel="+imovel);
		}else if(imovel != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&regiao="+regiao+"&imovel="+imovel);		
		}else if(quartos != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&regiao="+regiao+"&quartos="+quartos);		
		} else {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&regiao="+regiao);
		}
})

$("select[name=imovel]").live("change",function(){
	var categoria = $("input[name=categoria]:checked").val();
	var regiao = $("select[name=regiao] option:selected").val();
	var imovel = $("select[name=imovel] option:selected").val();
	var quartos = $("select[name=quartos] option:selected").val();
		$("select[name=regiao]").empty();
		if(regiao != "" && quartos != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&imovel="+imovel+"&quartos="+quartos+"&regiao="+regiao);
		}else if(imovel != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&imovel="+imovel+"&regiao="+regiao);		
		}else if(quartos != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&imovel="+imovel+"&quartos="+quartos);		
		} else {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&imovel="+imovel);
		}
		$("select[name=quartos]").empty();
		if(regiao != "" && quartos != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&imovel="+imovel+"&quartos="+quartos+"&regiao="+regiao);
		}else if(regiao != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&imovel="+imovel+"&regiao="+regiao);		
		}else if(quartos != "") {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&imovel="+imovel+"&quartos="+quartos);		
		} else {
			$("select[name=quartos]").load(path+"/lista_quartos.php?categoria="+categoria+"&imovel="+imovel);
		}	
})

$("select[name=quartos]").live("change",function(){
	var categoria = $("input[name=categoria]:checked").val();
	var regiao = $("select[name=regiao] option:selected").val();
	var imovel = $("select[name=imovel] option:selected").val();
	var quartos = $("select[name=quartos] option:selected").val();
		$("select[name=regiao]").empty();
		if(imovel != "" && regiao != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&quartos="+quartos+"&regiao="+regiao+"&imovel="+imovel);
		}else if(imovel != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&quartos="+quartos+"&imovel="+imovel);		
		}else if(regiao != "") {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&quartos="+quartos+"&regiao="+regiao);		
		} else {
			$("select[name=regiao]").load(path+"/lista_regiao.php?categoria="+categoria+"&quartos="+quartos);
		}
		$("select[name=imovel]").empty();
		if(imovel != "" && regiao != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&quartos="+quartos+"&quartos="+quartos+"&regiao="+regiao);
		}else if(imovel != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&quartos="+quartos+"&imovel="+imovel);		
		}else if(regiao != "") {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&quartos="+quartos+"&regiao="+regiao);		
		} else {
			$("select[name=imovel]").load(path+"/lista_imovel.php?categoria="+categoria+"&quartos="+quartos);
		}	
})
*/
function Sessao(){
	$.get('scripts/sessao.php',function(data){});
}
