$(function(){
	$('a[href=""]').click(function(){
		return false;
	});


// codigo novo

	function abrirJanela(width, height, nome,barra) {
		var top; var left; 
		if (barra == "") {
			barra = 0;
		}
		top = ( (screen.height/2) - (height/2) )
		left = ( (screen.width/2) - (width/2) )
		window.open(nome, "","toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=1,width="+width+",height="+height+",left="+left+",top="+top);
	}
	var valores = new Array;
	$('input[type=text],textarea').each(function(){
			valores[$(this).attr('name')] = $(this).attr('value');
	});
	$('input[type=text],textarea').focus(function(){
	
		if ($(this).attr('value') == valores[$(this).attr('name')]){
			$(this).val('');
		}
															
	}); 
	$('input[type=text],textarea').blur(function(){
	
		if ($(this).attr('value') == ""){
			$(this).val(valores[$(this).attr('name')]);
		}
															
	});
	
	

	$('.multi_check label:odd').addClass('par');
	$('.multi_check label:even').addClass('impar');
	$('.multi_check label').hover(function(){
			$(this).toggleClass('hover');
	});

	$("#telefone").mask("(99) 9999-9999? / 9999-9999");
	$("#form_contato, #form_gostei_imovel").validate({
		rules: {
			nome: "required",
			email: {
				required: true,	
				email: true
			},
			telefone: "required",
			mensagem: "required"			
		},
		messages: {
			nome: "Favor informar o Nome!",
			email: {
				required:"Favor informar um E-mail valido!",
				email:"Favor informar um E-mail valido!"
			},			
			telefone: "Favor informar o Telefone!",
			mensagem: "Favor informar a Mensagem!"
		}	
	});

	// Busca pelo código
	$('#formBusca').submit(function(){
		if($('#formBusca #codigo').val() != '' && $('#formBusca #codigo').val() != 'Busca por código'){
			abrirJanela(1001,590,$('#formBusca').attr('action')+'/'+$('#formBusca #codigo').val()+'/popup');
		} else {
			alert('Favor informar o código!');	
			$('#formBusca #codigo').focus();
		}
		return false;
	});
	
	// Abrir detalhes do imóvel no PopUp
	$('a.linkImovel').click(function(){
		abrirJanela(1001,590,$(this).attr('href')+'/popup');
		return false;
	});
	$('a.linkFotos').click(function(){
		abrirJanela(500,590,$(this).attr('href'));
		return false;								
	});

	$("#vlrini").maskMoney({showSymbol:false,decimal:",",thousands:"."});
	$("#vlrfin").maskMoney({showSymbol:false,decimal:",",thousands:"."});
	$("#quaini").mask("?99");
	$("#quafin").mask("?99");	

	$('.lightbox a').lightBox({fixedNavigation:false});
	$('#imagens_emp a').lightBox({fixedNavigation:true});
	$('#slide').gallery({	
		interval: 5500,
		width: '739px',
		height: '306px',
		thumbWidth: 30,
		thumbHeight: 16
	});
	$('.add').click(function(){
		ajaxFavoritos($(this).attr('id'));
	});
	$('.excluir').click(function(){
		ajaxExcluirFavoritos($(this).attr('id'));
	});
	$('#formOrdemPesquisa').submit(function(){
		$(this).attr('action',$(this).attr('action')+'/'+$('#ordem').attr('value'));
	});

	// Carousel
	function mycarousel_initCallback(carousel)
	{
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
	 
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
	 
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	 
	$('.mycarousel').jcarousel({
		auto: 4,
		scroll: 1,
		animation: 2000,
		wrap: 'circular',
		initCallback: mycarousel_initCallback
	});
	ajaxEdificio();
});
function ajaxFavoritos(codigo){
	$.ajax({
	  url: 'ajaxFavoritos.php',
	  data: "codigo="+ codigo+"&acao=inserir" ,
	  success: function(html) {
		if(html == 0){
			$('#conta_favoritos').html('0 im&oacute;veis');
		} else if(html == 1){
			$('#conta_favoritos').html('1 im&oacute;veis');
		} else {
			$('#conta_favoritos').html(html+' im&oacute;veis');
		}
		alert('Imóvel adicionado a favoritos!');
	  },error: function() {
			alert("Ocorreu um erro!");
			$('#conta_favoritos').html('0 im&oacute;veis');
	  }
   });
}
function ajaxExcluirFavoritos(codigo){
	$.ajax({
	  url: 'ajaxFavoritos.php',
	  data: "codigo="+ codigo+"&acao=excluir" ,
	  success: function(html) {		
		alert('Imóvel excluido dos favoritos!');
		window.location = 'imoveis-favoritos.php';
	  },error: function() {
			alert("Ocorreu um erro!");
			$('#conta_favoritos').html('0 im&oacute;veis');
	  }
   });
}
