$(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!"
		}	
	});
	$("#cadastrar_comentario").submit(function(){
	
	});

	$("#cadastrar_comentario").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'Falta preencher 1 campo'
					: 'Falta preencher ' + errors + ' campos';
				$("div.error span").html(message);
				$("div.error").show();
				
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function(){
			
			var dados = $('#cadastrar_comentario').serializeArray();
			var dados2 = '';
			$.each(dados, function(key, value){
				var i = 0;
				$.each(value, function(key2, value2){
					if (i == 0){
						dados2 = dados2 + value2 +"=";
						i++;
					} else {
						dados2 = dados2 + escape(value2)+"&";
						i = 0;
					}
			   });
			});
			//alert(dados2);
			 var rand = Math.random();
			
			$.ajax({
				type: "POST",
				url: endereco_site + "cadastrar_comentario.php",
				dataType: "text",
				data: dados2,
				success: function(resultado){
					
					if(resultado == 1){
						alert('Comentario cadastrado com sucesso!');
						window.location = window.location;
					} else {
						alert('Erro ao cadastrar o comentario!');	
					}
				}
			});
			return false;
			
			
			
		},
		messages: {
			nome: {
				required: "Favor preencher seu nome"
			},
			email: {
				required: "Favor preencher seu e-mail",
				email: "E-mail inv&aacute;lido!"
			},
			mensagem: {
				required: "Favor digitar um coment&aacute;rio"
			},
			captcha:{
				required: "Favor digitar o c&oacute;digo",
				remote: "C&oacute;digo inv&aacute;lido!"
			}
		},
		rules: {
			nome: {
				required: true
			},
			email: {
				required: true,
				email: true
			},
			mensagem: {
				required: true
			},
			captcha:{
				required: true,
				remote: endereco_site + 'check.php'
			}
		},
		debug:false,
/*		errorPlacement: function(error, element) {
			error.appendTo( element.parent() );
		},*/
		onkeyup: false
	});	

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

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

	$('.ligthbox a').lightBox({fixedNavigation:false});
	$('#imagens_emp a').lightBox({fixedNavigation:true});
	$('#myGallery').galleryView();
	$('.banner ul li').cycle({
		fx: 'fade'
	});	
	$("#FormNews").validate({
		rules: {
			nome_news: "required",
			email_news: {
				required: true,	
				email: true
			}				
		},
		messages: {
			nome_news: "Favor informar o Nome!",
			email_news: {
				required:"Favor informar um E-mail valido!",
				email:"Favor informar um E-mail valido!"
			}
		}	
	});
	if($('#youtube').html() != ''){
		$('#player').youTubeEmbed({
			video : $('#youtube').html(),
			width : 570						  
		});
		$('#youtube').html('');
	}
	if($('#youtubeMenor').html() != ''){
		$('#playerMenor').youTubeEmbed({
			video : $('#youtubeMenor').html(),
			width : 330,
			height: 184
		});
		$('#youtubeMenor').html('');
	}
	$('#formEnquete').submit( function(){
		var resposta = $('input:radio[name=resposta]:checked').val();
		var enquete = $('input:hidden[name=enquete]').val();
		var comentario = escape($('textarea[name=comentario]').val());
		if(resposta == undefined){
			$('input:radio[name=resposta]').focus();
			alert('Selecione uma resposta!');
		} else {
			abrirJanela(350,300,'votar-enquete?enquete='+enquete+'&resposta='+resposta+'&comentario='+comentario);
		}
		return false;
	});
	$('a.resultadoEnquete').click(function(){
		abrirJanela(350,300,$(this).attr('href'));
		return false;
	});
	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();
		});
	}
	$('#barra_parceiros').jcarousel({
		scroll: 1,
		auto: 5,
		wrap: 'circular',
		initCallback: mycarousel_initCallback
	});
});
