$(document).ready(function() {
	
	//BT BACK 
	$(".bt-voltar").click(function(){
		history.back();
	});
	//BT BACK 
	$(".bt-imprimir").click(function(){
		window.print();
	});
	/* BT TOPO */
	$(".bt-topo").click(function(){
		$(document).scrollTo( {top:'0px',left:'0px'}, 200 );
	});
	
	//LINKS EXTERNOS
	$("a[rel=externo]").attr('target','_blank');
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");
	
	// SLIDES HOME
	$('.slide-show').cycle({ 
	    speed:       600, 
	    timeout:     5000, 
	    pager:      '', 
	    pagerEvent: 'mouseover', 
	    pauseOnPagerHover: true
	});
	
	//TRANSPARÊNCIA
	$(".opac").css("opacity","0.85");
	
	// DESABILITA BOTÕES
	$(".disabled").attr("href","javascript:void(0);");
	$(".disabled").attr("title","");

	// ABRE FORM TENHO INTERESSE
	$("#tenho-interesse").click(function(){
		$("#form-interesse").toggle("fast");
	});
	
	// ABRE BOX VIDEO
	$(".bt-assista-video").click(function(){
		$("#box-video").toggle("fast");
	});
	
	//AREAS DE ATIVACAO DO LIGHTBOX
	$('a[rel*=lightbox]').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 	'img/loading.gif',
		imageBtnClose: 	'img/close.gif',
		imageBtnPrev: 	'img/prev.gif',
		imageBtnNext: 	'img/next.gif',
		containerResizeSpeed: 300,
		txtImage: 'Imagem',
		txtOf: 'de'
	});
	
	// GALERIA DE IMAGENS HOME
	$('.thumbs a.thumb-alguma-coisa-sei-la').click(function(){
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		$('#alvo-gallery').html('<img src="'+href+'" alt="'+title+'" title="clique para ver toda a imagem" class="ativa-blocos" />');
		return false;
	});
	
	// GALERIA DE IMAGENS HOME
	$('#imovel-2 .f-right a').click(function(){
		var href = $(this).attr('href');
		var title = $(this).attr('title');
		$('#imovel-2 .f-left').html('<img src="'+href+'" alt="'+title+'" />');
		return false;
	});
	
	// MOSTRA E OCULTA PAINÉIS HOME
	$('#alvo-gallery').click(function(){
		var id = $('#alvo-gallery img').attr('class');
		if(id == 'ativa-blocos'){
			$('.marca-imovel-home').css("opacity","0.1");
			$('.thumbs-imovel-home').css("opacity","0.1");
			$('#alvo-gallery img').attr('class','desativa-blocos');
		}else{
			$('.marca-imovel-home').css("opacity","1");
			$('.thumbs-imovel-home').css("opacity","1");
			$('#alvo-gallery img').attr('class','ativa-blocos');
		}
	});
	
	$('.bt-apto-decorado').click(function(){
		$('.bt-apto-decorado').hide();
		$('.bt-fotos-empreendimento').show();
		$.ajax({
			type: "POST",
			url: "incs/fotos-apto-decorado.php",
			data: "id=" + this.title,
			beforeSend: function() {
				$('#alvo-ajax').html('<img style="width:16px;" src="img/ajax-loader.gif">');
			},
			success: function(txt) {
				$('#alvo-ajax').html(txt);
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	});
	
	if($.browser.safari){
		$('#tenho-interesse').css({padding:"0 15px"});
	}

});

function wopen(file){
	window.open('pop.php?f=' + file,'','status=no,width=722, height=435, scrollbars=yes');
}

function hide(id){
	$(id).hide(100);
}

//HABILITA FORM ENCAMINHAR POR EMAIL
function mostraFormEmail(id){
	$('.thumbs-imovel-home').fadeOut(100);
	$.ajax({
		type: "POST",
		url: "incs/form-envie-email.php",
		data: "id=" + id,
		beforeSend: function() {
			$('.thumbs-imovel-home').html('<img style="width:16px;" src="img/ajax-loader.gif">');
		},
		success: function(txt) {
			$('.thumbs-imovel-home').fadeIn(500);
			$('.thumbs-imovel-home').html(txt);
		},
		error: function(txt) {
			alert('Erro: '+txt);
		}
	});
}

//INDICAR
function funcaoIndicar(flag){
	if(flag){
		$.ajax({
			type: "POST",
			url: "lib/acao_indicar.php",
			data: 
			"id=" 		+ $('#id_galeria').val() 						+ "&" + 
			"nomede=" 	+ $('#nome_envie_email').val() 					+ "&" + 
			"emailde=" 	+ $('#email_envie_email').val() 				+ "&" + 
			"nome=" 	+ $('#nome_destinatario_envie_email').val() 	+ "&" + 
			"email=" 	+ $('#email_destinatario_envie_email').val() 	+ "&" + 
			"",
			beforeSend: function() {
			},
			success: function(txt) {
				alert(txt);
				$('.thumbs-imovel-home').fadeOut(100);
//Colocar para mostrar as thumbs do empreendimento
//				$('.thumbs-imovel-home').fadeIn(500);
//				$('.thumbs-imovel-home').html('CONTEUDO');
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	}
}

//INTERESSE
function funcaoInteresse(flag){
	if(flag){
		$.ajax({
			type: "POST",
			url: "lib/acao_interesse.php",
			data: 
			"id=" 		+ $('#id_galeria').val() 		+ "&" + 
			"nome=" 	+ $('#nome_interesse').val() 	+ "&" + 
			"email=" 	+ $('#email_interesse').val() 	+ "&" + 
			"fone=" 	+ $('#fone_interesse').val() 	+ "&" + 
			"msg=" 		+ $('#msg').val() 				+ "&" + 
			"id_assunto="		+ $('#assunto_interesse').val()			+ "&" + 
			"destino=" 	+ $('#destino').val() 			+ "&" + 
			"",
			beforeSend: function() {
			},
			success: function(txt) {
				alert(txt);
				$('#form-interesse').hide(1000);
			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	}
}

//CONTATO
function funcaoContato(flag){
	if(flag){
		$.ajax({
			type: "POST",
			url: "lib/acao_contato.php",
			data: 
			"nome=" 			+ $('#nome').val() 				+ "&" + 
			"email=" 			+ $('#email').val()				+ "&" + 
			"fone=" 			+ $('#fone').val()				+ "&" + 
			"id_assunto="		+ $('#assunto').val()			+ "&" + 
			"msg=" 				+ $('#msg').val() 				+ "&" + 
			"empreendimentos=" 	+ $('#empreendimentos').val() 	+ "&" + 
			"msg_retorno="		+ $('#msg_retorno').val()		+ "&" + 
			"destino=" 			+ $('#destino').val() 			+ "&" + 
			"",
			beforeSend: function() {
			},
			success: function(txt) {
				alert(txt);
//				$('#form-contato').reset();
				$('#form-contato').each(
						function(){
							this.reset();
						}
					);

			},
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	}
}

/* FUNÇÃO EMAIL ASSISTENCIA TÉCNICA */
function exibeFormAssistencia(valor){

	$('#form-assistencia-tecnica').toggle("fast");

}

function fotosAptoDecorado(href,title){
	$('#alvo-gallery').html('<img src="'+href+'" alt="'+title+'" title="clique para ver toda a imagem" class="ativa-blocos" />');
	return false;
}

function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj 	= document.getElementById(form).elements[i].id;
		var relObj 	= document.getElementById(form).elements[i].getAttribute('rel');
				
		if ((relObj == "email")) {
			expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
			if(!expressao.test(document.getElementById(form).elements[i].value)){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].value = "";
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}else if (relObj == "required") {
			if((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

/**
  * Função validar assistencia tecnica
  * @param 
  * @return Boolean
  **/
function validarAssistenciaTecnica() {	
	if (validaForm("form-assistencia-tecnica")) { 
		$.ajax( {
			type: "POST",
			url: "lib/acao_assistencia_tecnica.php",
			data: "nome=" + $("#nome_ass").val() + "&email=" + $("#email_news").val() + "&telefone=" + $("#fone_form").val() + "&empreendimento=" + $("#empreendimento_form").val() + "&n_apartamento=" + $("#n_apartamento").val() + "&torre=" + $("#torre").val() + "&bloco=" + $("#bloco").val() + "&mensagem=" + $("#mensagem_form").val(),
			beforeSend: function() {
			},
			success: function(txt) {
				alert("E-mail enviado com sucesso! Obrigado!");
				document.getElementById("form-assistencia-tecnica").reset();	
				hide('#form-assistencia-tecnica');
			},
			error: function(txt) {
				alert("Erro: " + txt);
			}
		});
	}
}