//////////////////// EXIBE / ESCONDE DIV ////////////////////
function showHide(show, hide) {
	$(show).show('fast');
	$(hide).hide('fast');
}

function goPage(pag, div){
	$.ajax({
		type: "GET",
		url: pag,
		beforeSend: function() {
			$("#"+div).html('Carregando...');
		},
		success: function(texto){
			$("#"+div).html(texto);
		},
		error: function(txt) {
			$("#"+div).html('Erro');
		}
	});
}

$(document).ready(function() {
	$('input#fpreco_min').setMask('decimal');
	$('input#fpreco_max').setMask('decimal');
	$('input#fcpf_news').setMask('cpf');

	$.mask.masks = $.extend($.mask.masks,{ // atribuir no alt
		contabancaria:{ mask: '99999999-9' }
	});

	$('.imoveis_lista').mouseover(function() {
		$(this).css({backgroundColor: '#ffe'}).mouseout(function() {
			$(this).css({backgroundColor: ''});
		});
	});

	//////////////////// FANCY ////////////////////
	$("a.imagem").fancybox();
	
	$("a[rel=foto_galeria]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Imagem ' +  (currentIndex + 1) + ' de ' + currentArray.length + ' ' + title + '<\/span>';
		}
	});
	
	$("a.fancy_comente").fancybox({
		//'height': 	500,
		'width':	300,
		'type' :	'iframe',
		'showCloseButton' : true,
		'padding'		: 0
	});

	$("a.fancy_compartilhe").fancybox({
		'height': 	300,
		'width':	350,
		'type' :	'iframe',
		'showCloseButton' : true,
		'padding'		: 0
	});

	//////////////////// ENVIA CONTATO ////////////////////
	$("form#contato").submit(function() {
		var fnome = $('#fnome').attr('value');
		var femail = $('#femail').attr('value');
		var fmsgm = $('#fmsgm').attr('value');
		$.ajax({
			type: "POST",
			url: "submit/contato.php",
			data: "fnome="+fnome+"&femail="+femail+"&fmsgm="+fmsgm,
			beforeSend: function() {
				$('div#retorno_contato').html('<img src="imgs/layout/ajax-loader.gif" \/>');
			},
			error: function(txt) {
				$('div#retorno_contato').html('Erro ao enviar');
			},
			success: function(data){
//				$('form#contato').hide(function(){$('div#retorno_contato').fadeIn();});
				$('div#retorno_contato').html(data);
			}
		});
		return false;
	});
	
	//////////////////// ENVIA NEWSLETTER ////////////////////
	$("form#newsletter").submit(function() {
		var fnome = $('#fnome_news').attr('value');
		var femail = $('#femail_news').attr('value');
		var fcpf = $('#fcpf_news').attr('value');
		$.ajax({
			type: "POST",
			url: "submit/newsletter.php",
			data: "fnome="+fnome+"&femail="+femail+"&fcpf="+fcpf,
			beforeSend: function() {
				$('div#retorno_newsletter').html('Enviando...');
			},
			error: function(txt) {
				$('div#retorno_newsletter').html('Erro ao cadastrar');
			},
			success: function(data){
				$('div#retorno_newsletter').html(data);
			}
		});
		return false;
	});

	//////////////////// BUSCA CEP ////////////////////
	$('#fcep').blur(function(){
		setTimeout(function(){
			var cep = $('#fcep').attr('value');
			if(cep !='' && cep != undefined){
				$.ajax({
					type: 'POST',
					dataType: 'xml',
					url: "includes/ajax/cep.php",
					data: "cep="+cep,
					beforeSend: function() {
						$('div#loading').html('<img src="imgs/layout/ajax-loader-flower.gif" \/>');
					},
					success:function(data){
						$('#loading').css({display:'none'});
						$(data).find('webservicecep').each(function(){
							if($(this).find('resultado').text()==1){
								$("#fuf").attr({ value: $(this).find('uf').text() });
								$("#fcidade").attr({ value: $(this).find('cidade').text() });
								$("#fbairro").attr({ value: $(this).find('bairro').text() });
								$("#fendereco").attr({ value: $(this).find('tipo_logradouro').text()+" "+$(this).find('logradouro').text() });
							}//end resultado true
						});//end each webservicecep
					}//end success
				});//end ajax
			}//end if value
		},1);//end setTimeout of masked
	});//end cep blur
});

$(function($){
	$(function(){
		$('input:text').setMask();
	});
});
