$(document).ready(function() {
	
	//se for ie6, carrega menu por js e roda o pngfix
	if($.browser.msie && $.browser.version=="6.0") {
		$.getScript("inc/dropdown/jquery.dropdown.js");
		$.getScript("inc/js/jquery.pngfix.js",function(xhr){
			var aplicar_pngfix = "#animacao .box";
			try {
				$(aplicar_pngfix).pngfix();
			} catch(err) {
				eval(xhr);
				$(aplicar_pngfix).pngfix();
			}
		});
		
	}

	//labelify
	$.getScript("inc/js/jquery.labelify.js",function(xhr){
		var aplicar_labelify = "#busca .codigo, #fnewsletter input[Name=nome], #fnewsletter input[Name=email]";
		try {
			$(aplicar_labelify).labelify();
		} catch(err) {
			eval(xhr);
			$(aplicar_labelify).labelify();
		}
	});

	//manter hover do menu principal ativo quando vai pro submenu
	$("#header #menu li.dir ul").mouseover(function(){
		$(this).parents("li.dir").find("a:first").addClass("active");
	});
	$("#header #menu li.dir ul").mouseout(function(){
		$(this).parents("li.dir").find("a:first").removeClass("active");
	});
	
	/*animacao
	if ($("#animacao").length>0) {
		$('#animacao').loopedSlider({
			autoStart: 4000,
			restart: 5000,
			slidespeed: 1000, 
			containerClick: false,
			addPagination: true
		});
	}*/
	
	//animacao
	if ($("#animacao").length>0) {
		$("#animacao .slides").after('<div id="anav">').cycle({
			fx:		'growY', //http://jquery.malsup.com/cycle/browser.html
			randomizeEffects: true,
			pager:	'#anav'
		});
		$("#animacao .slide").click(function(){
			var lnk = $(this).find("a").attr("href");
			window.location = lnk;
		});
		setTimeout("anav()",1000);
	}	
	
	//tooltips
	if ($(".tooltip").length>0) {
		$(".tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			showBody: " - ", 
			fade: 250 
		});
	}

	//over e tooltip dos imoveis
	if ($(".imoveis").length>0) {
		$(".imoveis .imovel").mouseover(function(){ 
			$(this).addClass("ovr");
			//lnk = $(this).find(".foto a").attr("href");
			//label = $(this).find(".foto").attr("id");
			//$(".imoveis .imovel").attr("title",label+'<br />Clique para ver mais detalhes').tooltip({track: true, delay: 0, showURL: false, showBody: " - " });		
			//$(this).click(function(){
			//	if (lnk!='' && lnk!='#' && lnk!==undefined) window.location = lnk;
			//	return false;
			//});
		});
		$(".imoveis .imovel").mouseout(function(){ 
			$(this).removeClass("ovr");
		});
	}

	//campo bairros na busca
	letras_bairros();
	ctrl=0;
	$("#osbairros").click(function(){
		if(ctrl==0){	
			ctrl=1; $("#busca .divbairros .bairros").slideDown("slow");
		}else{
			ctrl=0; $("#busca .divbairros .bairros").slideUp("slow");
		}
		c = conta_bairros();
		if (c>0) {
			if (c==1) s = ""; else s = "s";
			$(this).html(c+' bairro'+s+' selecionado'+s);
		} else {
			$(this).html('Nenhum selecionado');
		}
		
	});
	function conta_bairros(){
		c = 0;
		$("#busca .divbairros .bairros input").each(function(){
			if ($(this).is(":checked")) c++;
		});
		return c;
	}

	//fotos do imovel
	if ($("#fotos").length>0) {
		$('#fotos .items a').lightBox();
		$("#fotos div.scrollArea").scrollable({
			loop: true, 
			speed: 1200,
			size: 3
		});
	}

	//enviar por email
	if ($("#enviar-por-email").length>0) {
		$("#enviar-por-email").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 330,
			resizable: false
		});
		$(".icones .email, .label .email").click(function(){
			$("#enviar-por-email").dialog('open');
			return false;
		});
	}

	//video modal
	if ($("#video").length>0) {
		$("#video").dialog({
			autoOpen: false,
			bgiframe: true,
			minHeight: 140,
			modal: true,
			width: 695,
			resizable: false
		});
		$(".label .video").click(function(){
			$("#video").dialog('open');
			return false;
		});
	}
	
	//twitter
	if ($("#twitter").length>0) {
		$("#twitter .tweets").getTwitter({
			userName: "peiterimoveis",
			numTweets: 2,
			loaderText: "Aguarde, carregando...",
			slideIn: false,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: false,
			beforeEachTweet: "Peiter:"
		});
	}
		
	//rolagem
	$('.subir').click(function(){
		$('html, body').animate({scrollTop:0}, 1000);
	});
	
	$('.voltar').click(function(){
		history.go(-1);
	});		
	
	//rolagem ao entrar nos detalhes
	if ($("#detalhes").length>0) {
			$("html, body").animate({
				scrollTop: $("#mainContent").offset().top
			}, 1000);
			//return false;
	}
	
	$.getScript("inc/js/fav.js");
	
	carregaBairros();
	$("#busca_cidade").change(function(){ carregaBairros(); });		
	
	if ($("#cidades").length>0) {
		carregaBairrosAlerta();
		$("#cidades").change(function(){ carregaBairrosAlerta(); });	
	}

	//submit
	$("#fbusca").submit(function(){
		var livre = $.trim($("#fbusca input[Name=codigo]").val());
		if (isNaN(livre)) { $("#fbusca").attr('action','imoveis.php'); }
		else 			  { $("#fbusca").attr('action','detalhes.php'); }	 
	});		
	
	//busca
	if ($('#busca').length>0) {
		//if($.browser.msie && $.browser.version=="6.0") {} else {
			//$("#busca select").selectbox();
			$("#busca #busca_finalidade").selectbox();
			$("#busca #busca_tipo").selectbox();
			$("#busca #busca_cidade").selectbox();
			$("#busca #busca_dorm").selectbox();
			$("#busca #busca_vaga").selectbox();
			$("#busca #de_venda").selectbox();
			$("#busca #ate_venda").selectbox();
		//}
		letras_bairros();
	}	
	
	/*$("#de_locacao").hide();
	$("#ate_locacao").hide();
	trocar_finalidade();
	$("#finalidade").change(function()	{ trocar_finalidade(); });	*/
	
});

function mapa_imovel(l1,l2,tipo,controles,mapid) {
	if ( tipo === undefined ) tipo = 'ponto';
	if ( controles === undefined ) controles = true;
	if ( mapid === undefined ) mapid = "map";
	if ($("#"+mapid).length>0) {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById(mapid));
			map.setCenter(new GLatLng(l1,l2), 15);
			var latlng = new GLatLng(l1,l2);
			if (controles) {
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
			}
			if (tipo=='circulo') {
				var marcador = GPolygon.Circle(latlng, 500,"#4D6C6B",3,1,"#000",0.2)
				map.addOverlay(marcador);
			} else {
				var icone = new GIcon(G_DEFAULT_ICON);
				icone.image = "img/design/gmaps_icon.png";
				icone.iconSize = new GSize(21, 31);
				markerOptions = { icon:icone };
				map.addOverlay(new GMarker(latlng, markerOptions));
			}
		}
	}
}

function anav() {
	i = 0;
	$("#animacao .slide").each(function(){
		var title = $(this).attr("title");
		$(this).attr("title","");
		$("#anav a:eq("+i+")").html(title);
		i++;
	});
}

function letras_bairros() {
	if($.browser.msie && $.browser.version=="6.0") {
		$("#busca .divbairros .bairros p").hide();
	} else {
		$("#busca .divbairros .bairros").prepend("<p></p>");
		letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		letras_usadas = "";
		$("#busca .divbairros .bairros label").each(function(){
			str = $(this).html();
			v = str.split("> ");
			nome_bairro = v[1];
			letras_usadas += nome_bairro.substr(0,1);
		});
		letras_links = '<a href="#" class="todos">Todos</a>&nbsp;';
		for (i=0;i<=letras.length-1;i++) {
			tem = letras_usadas.indexOf(letras.substr(i,1));
			if (tem>=0) {
				letras_links += '<a href="#" class="letra">'+letras.substr(i,1)+'</a>';
			} else {
				letras_links += letras.substr(i,1);
			}
		}
		$("#busca .divbairros .bairros p").html(letras_links);
		$("#busca .divbairros .bairros p a.letra").click(function(){
			letra = $(this).html();
			$("#busca .divbairros .bairros label").each(function(){
				str = $(this).html();
				v = str.split("> ");
				nome_bairro = v[1];
				bletra = nome_bairro.substr(0,1);
				if (bletra==letra) $(this).show(); else $(this).hide();
			});
			return false;
		});
		$("#busca .divbairros .bairros p a.todos").click(function(){
			$("#busca .divbairros .bairros label").show();
			return false;
		});
	}
}

function carregaBairros() {
	var cidade = $('#busca_cidade').val();
	var lista  = $('#listabairros').val();
	$(".bairros").load("inc/lib/fnc_load-busca.php?act=bairros", {cidade: cidade, lista: lista},
		function(data){
		letras_bairros();
	})			
}		

function carregaBairrosAlerta() {
	var cidade = $('#cidades').val();

	$("#bairros-alerta").html('<div id="bairros-alerta">Carregando...</div>');

	 $("#bairros-alerta").load("inc/lib/fnc_load-alerta.php?act=bairros", {cidade: cidade},
		function(data){
			$("#bairros-alerta").html(data)
		})			
}	

function trocar_finalidade() {
	if ($("#finalidade").val()=='locacao') {
		$("#de_venda").css('display','none');
		$("#ate_venda").css('display','none');
		$("#de_locacao").css('display','block');
		$("#ate_locacao").css('display','block');
	}else{
		$("#de_locacao").css('display','none');
		$("#ate_locacao").css('display','none');
		$("#de_venda").css('display','block');
		$("#ate_venda").css('display','block');
	}	
}
