$(function(){
	$('a[href=""]').click(function(){
		return false;
	});
	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=1,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')]);
		}
															
	});
	
	$("#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 válido!",
				email:"Favor informar um E-mail válido!"
			},			
			telefone: "Favor informar o Telefone!",
			mensagem: "Favor informar a Mensagem!"
		}	
	});

	$('.lightbox a').lightBox({fixedNavigation:false});

     $(".direita ul").bxSlider({
     ticker: true,
     mode:"vertical",
     tickerHover: true,
     displaySlideQty:5,
        tickerSpeed: 2500
    });
	
		$('.banner').cycle({
		fx: 'fade'
	});	
	$('#bt_ok').click(function(){
            if($('#codigo').val() != '' && $('#codigo').val() != 'Busca por código'){
		abrirJanela(802,590,'imovel/'+$('#codigo').val()+'/popup');
            } else {
		alert('Favor informar o código!');	
		$('#codigo').focus();
            }
            return false;
	});
        $("#codigo").keypress(function(event) {
               if ( event.which == 13 ) {
                   $('#bt_ok').click();
                   return false;
                }
        });
	
	// Abrir detalhes do imóvel no PopUp
	$('a.linkImovel').live('click',function(){
		abrirJanela(802,590,$(this).attr('href')+'/popup');
		return false;
	});
	$('a.linkFotos').click(function(){
		abrirJanela(500,590,$(this).attr('href'));
		return false;								
	});
	
	$('.add').click(function(){
		ajaxFavoritos($(this).attr('id'));
	});
	$('.excluir').click(function(){
		ajaxExcluirFavoritos($(this).attr('id'));
	});	
	$("#vlrini").maskMoney({showSymbol:false,decimal:",",thousands:"."});
	$("#vlrfin").maskMoney({showSymbol:false,decimal:",",thousands:"."});
	$('a.linkVideo').click(function(){
		var url = $(this).attr('href');
		var div = document.createElement("div");
        div.title = $(this).attr('rel');
        div = $(div);
		div.append('<iframe src="'+url+'" width="560" height="345" frameborder="0" scrolling="no"></iframe>');				
		div.dialog({modal:true, resizable:false, width: 595, height: 500, position: ['auto',100], 
			  buttons: {
				Fechar: function (){
				$(this).dialog("destroy");
					 }
			  }
		});
		return false;
	});
	$('#carousel-detalhes-lista').jcarousel({
        vertical: true,
        scroll: 2
    });	
});

function ajaxFavoritos(codigo){
	$.ajax({
	  url: 'ajaxFavoritos.php',
	  data: "codigo="+ codigo+"&acao=inserir" ,
	  success: function(html) {
		if(html == 0){
			$('#conta_favoritos').html('Favoritos(0)');
		} else if(html == 1){
			$('#conta_favoritos').html('Favoritos(1)');
		} else {
			$('#conta_favoritos').html('Favoritos('+html+')');
		}
		alert('Imóvel adicionado a favoritos!');
	  },error: function() {
			alert("Ocorreu um erro!");
			$('#conta_favoritos').html('Favoritos(0)');
	  }
   });
	return false;
}
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';
	  },error: function() {
			alert("Ocorreu um erro!");
			$('#conta_favoritos').html('0 imóveis');
	  }
   });
}
