// JavaScript Document
var interval;
$(document).ready(function(){
	$("#formulario_newsletter").validate({
		rules: {
			newsletter: {
				required: true,
				email: true
			}
		},
		messages: {
			newsletter: {
				required: "",
				email: ""
			}
		}, 
		submitHandler: function(em) {
			var dados = $("#formulario_newsletter").serialize();  
			$.ajax({  
				type: "POST",  
				url: 'json/grava_newsletter.php', 
				data: dados,  
				success: function( data )  
				{  
					data = jQuery.parseJSON(data);
					if(data.result)
					{
						alert('Seu e-mail foi cadastrado com sucesso em nossa newsletter.');
						$("#newsletter").val("");
					}
					else
					{
						alert('Não foi possível cadastrar seu e-mail em nossa newsletter.');
					}
				}, 
				error: function( data )  
				{
					alert('Não foi possível cadastrar seu e-mail em nossa newsletter.');
				}
			});  

			return false;
		}
	});	

	// Checkbox style
	$('label.checkbox').live({
		click: function() {
						
			if(!$(this).hasClass('check')){
				$(this).addClass('check');
				$('input',this).attr('checked', true);
			}else{
				$(this).removeClass('check');
				$('input',this).attr('checked', false);
			}
			return false;
		}
	});
	
	// Formulario de contato (Parceria)
	jQuery.validator.addMethod("complete_url", function(val, elem) {
		// if no url, don't do anything
		if (val.length == 0) { return true; }
	 
		// if user has not entered http:// https:// or ftp:// assume they mean http://
		if(!/^(https?|ftp):\/\//i.test(val)) {
			val = 'http://'+val; // set both the value
			$(elem).val(val); // also update the form element
		}
		// now check if valid url
		// http://docs.jquery.com/Plugins/Validation/Methods/url
		// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
		return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
	});	
	
	$("#parTel").mask("99 9999-9999");
	$("#form_parcerias").validate({
		rules: {
			parNome: "required",
			parEmail: {
				required: true,
				email: true
			}, 
			parTel: {
				required: true, 
				minlength: 10
			}, 
			parCargo: "required", 
			parSobre: "required", 
			parUrl: {
				complete_url: true, 
				required: true
			}, 
			parSeg: "required", 
			parOnline: "required", 
			parQuant: "required"
		},
		messages: {
			parNome: "* Preencha seu nome", 
			parEmail: {
				required: "* Preencha seu e-mail",
				email: "* Preencha seu e-mail corretamente"
			}, 
			parTel: {
				required: "* Preencha seu telefone", 
				minlength: "* Preencha seu telefone corretamente"
			}, 
			parCargo: "* Preencha seu cargo", 
			parSobre: "* Preecha as informa&ccedil;&otilde;es de seu site", 
			parUrl: {
				complete_url: "* Preecha a URL de seu site corretamente" , 
				required: "* Preecha a URL de seu site" 
			}, 
			parSeg: "* Preecha o segmento de sua empresa", 
			parOnline: "* Desde quando sua empresa est&aacute; online?", 
			parQuant: "* Quantos usu&aacute;rios opt-in sua empresa possui?"
		}, 
		submitHandler: function(em) {
			var dados = $("#form_parcerias").serialize();  
			$.ajax({  
				type: "POST",  
				url: 'json/grava_contato_parceria.php', 
				data: dados,  
				success: function( data )  
				{  
					data = jQuery.parseJSON(data);
					if(data.result)
					{
						alert('Seu contato foi enviado com sucesso. Em breve entraremos em contato');
						
						$("#parNome").val("");
						$("#parEmail").val("");
						$("#parTel").val("");
						$("#parCargo").val("");
						$("#parSobre").val("");
						$("#parUrl").val("");
						$("#parSeg").val("");
						$("#parOnline").val("");
						$("#parQuant").val("");
					}
					else
					{
						alert('Não foi possível enviar seu contato. Por gentileza tente novamente daqui alguns minutos.');
					}
				}, 
				error: function( data )  
				{
					alert('Não foi possível enviar seu contato. Por gentileza tente novamente daqui alguns minutos.');
				}
			});  

			return false;
		}
	});	
	// Fim formulario de contato (Parceria)

	// Formulario de contato (Clientes)
	$("#cliTel").mask("99 9999-9999");
	$("#form_cliente").validate({
		rules: {
			cliNome: "required",
			cliEmail: {
				required: true,
				email: true
			}, 
			cliTel: {
				required: true, 
				minlength: 10
			}, 
			cliEmp: "required", 
			cliMsg: "required"
		},
		messages: {
			cliNome: "* Preencha seu nome", 
			cliEmail: {
				required: "* Preencha seu e-mail",
				email: "* Preencha seu e-mail corretamente"
			}, 
			cliTel: {
				required: "* Preencha seu telefone", 
				minlength: "* Preencha seu telefone corretamente"
			}, 
			cliEmp: "* Preencha sua empresa", 
			cliMsg: "* Preecha sua mensagem"
		}, 
		submitHandler: function(em) {
			var dados = $("#form_cliente").serialize();  
			$.ajax({  
				type: "POST",  
				url: 'json/grava_contato_cliente.php', 
				data: dados,  
				success: function( data )  
				{  
					data = jQuery.parseJSON(data);
					if(data.result)
					{
						alert('Seu contato foi enviado com sucesso. Em breve entraremos em contato');
						
						$("#cliNome").val("");
						$("#cliEmail").val("");
						$("#cliTel").val("");
						$("#cliEmp").val("");
						$("#cliMsg").val("");
					}
					else
					{
						alert('Não foi possível enviar seu contato. Por gentileza tente novamente daqui alguns minutos.');
					}
				}, 
				error: function( data )  
				{
					alert('Não foi possível enviar seu contato. Por gentileza tente novamente daqui alguns minutos.');
				}
			});  

			return false;
		}
	});	
	// Fim formulario de contato (Clientes)
	
	$('p.linkCase a').live({
		click: function() {
			var chosen = $(this).html();
			$(this).parent().find('a').removeClass('active');
			$(this).addClass('active');
			$(this).parent().parent().find('.infoCase').hide();
			$(this).parent().parent().find('.case'+chosen).show();
			return false;
		}
	}
	);
	$('p.linkBanner a').live({
		click: function() {
			var chosen = $(this).html();
			$('p.linkBanner a').removeClass('active');
			$(this).addClass('active');
			$('.bannerBack').hide();
			$('#b0'+chosen).show();
			return false;
		}
	});
	interval = setInterval(function(){
		if($('p.linkBanner a:last').hasClass('active')){
			$('p.linkBanner a:first').click();
		}else{
			$('p.linkBanner a.active').next().click();
		}
	}, 10000);
	
	$('a.btLimpar').live({
		click: function() {
			$(this).parent().parent().find('p input[type=text]').val('');
			$(this).parent().parent().find('p textarea').val('');
			$(this).parent().parent().find('p input[type=checkbox]').attr('checked', false);
			return false;
		}
	});
	
	if($('div.comment div').hasClass('scrollQuotes')){
		carouselQuotes();
	}
	if($('div').hasClass('mailMain')){
		carouselMail();
	}	
});

function limpaCampoNewsletter(v)
{
	if (v=="digite aqui seu e-mail")
	{
		$("#newsletter").val("");
	}
	else
	{
		if (v=="")
			$("#newsletter").val("digite aqui seu e-mail");
	}
}
// Fim Campo cadastro de newsletter 
                                             
// TROCA BANNER

var caminhoImoImg;
var imoImg;
function trocaImgSingle(ele){
	$('.galerry li:first a').addClass('actv');
	
	caminhoImoImg = ele;
	$('.galerry li a').click(function(){
		$('.galerry li a').removeClass('actv');
		$(this).addClass('actv');
		
		imoImg = $(this).attr('rel');
		//if (!$('.old').is(':animated') && !$('.new').is(':animated')) {
		$('<img src="'+caminhoImoImg+'/'+imoImg+'" name="'+imoImg+'" width="752" height="465" alt="" title="" />').insertBefore('.bigImgImovel img');
		$('.bigImgImovel img:last').remove();
		//}
		return false;
	});
}

// TROCA CONTEUDO

var caminhoImg;
var imgNext;
var imgPrev;

var oldWalk;
var newWalk;

var prevOrNext;

var panoramica;

function galeriadeImoveis(ele) {
	
	if($('.galeriaImovel li:first a').attr('name') == 'panoramica'){
		$('.old').append('<span class="labelPanoramica" style="display:none;">PANORÂMICA</span>');
		$('.labelPanoramica').fadeIn();
		movePanoramica();
	}
	caminhoImg = ele;
	$('.galeriaImovel li:first a').addClass('active');
	$('.galeriaImovel li').addClass('active');
	$('.new').css('left','720px');
	
		$('.galeriaImovel li a').click(function(event){
			event.preventDefault();
			if (!$('.old').is(':animated') && !$('.new').is(':animated')) {
				oldWalk = -720;
				prevOrNext = 'next';
				$('.galeriaImovel li a').removeClass('active');
				$(this).addClass('active');
				trocaImg($(this).attr('rel'), $(this).attr('name'));
			}
		});
		$('a.galNext').click(function(event){
			event.preventDefault();
			if (!$('.old').is(':animated') && !$('.new').is(':animated')) {
				oldWalk = -720;
				prevOrNext = 'next';
				imgNext = $('a.active').parent().next('li').find('a').attr('rel');
				panoramica = $('a.active').parent().next('li').find('a').attr('name');
				if(imgNext == undefined){
					$('.galeriaImovel li a').removeClass('active');
					$('.galeriaImovel li:first a').addClass('active');
					trocaImg($('.galeriaImovel li:first a').attr('rel'),$('.galeriaImovel li:first a').attr('name'));
				}else{
					trocaImg(imgNext,panoramica);
					$('a.active').parent().next('li').find('a').addClass('active');
					$('a.active:first').removeClass('active');
				}
			}
		});
		$('a.galPrev').click(function(event){
			event.preventDefault();
			if (!$('.old').is(':animated') && !$('.new').is(':animated')) {
				oldWalk = 720;
				prevOrNext = 'prev';
				imgPrev = $('a.active').parent().prev('li').find('a').attr('rel');
				panoramica = $('a.active').parent().prev('li').find('a').attr('name');
				if(imgPrev == undefined){
					$('.galeriaImovel li a').removeClass('active');
					$('.galeriaImovel li:last a').addClass('active');
					trocaImg($('.galeriaImovel li:last a').attr('rel'),$('.galeriaImovel li:last a').attr('name'));
				}else{
					trocaImg(imgPrev,panoramica);
					$('a.active').parent().prev('li').find('a').addClass('active');
					$('a.active:last').removeClass('active');
				}
			}
		});
		
}

function trocaImg(ele, status){
	$('.labelPanoramica').remove();
	var sobra = ele.substring(0,ele.length-4);
	if (ele != $('.old img').attr('name')) {
		$('.galeriaBigPic').append(
			'<div class="new">'
			+'	<img src="'+caminhoImg+'/'+ele+'" name="'+sobra+'" />'
			+	'<p style="position:absolute; top:0; left:0; color:#000000; font-weight:bold;"></p>'
			+'</div>'
		);
		if(prevOrNext == 'next'){
			$('.new').css('left','720px');
		}else{
			$('.new').css('left','-720px');
		}
		$('.old').animate({
		left: oldWalk
		}, 1000, function() {
			$(this).remove();
		});
		$('.new').delay(200).animate({
		left: 0
		}, 1000, function() {
			$(this).attr('class','old');
			
			if(status == 'panoramica'){
				$('.old').append('<span class="labelPanoramica" style="display:none;">PANORÂMICA</span>');
				$('.labelPanoramica').fadeIn();
				movePanoramica();
			}
		});
	}
}
// CHECKBOX


function carouselQuotes(){
	$('#carousel').cycle({ 
		fx:     'scrollHorz', 
		speed:  'slow', 
		timeout: 22000, 
		next:   '.next', 
		prev:   '.prev' 
	});
}

function carouselMail(){
	$('#mail_marketing_carousel').cycle({ 
		fx:     'scrollHorz', 
		speed:  'slow', 
		timeout: 22000, 
		next:   '.next', 
		prev:   '.prev', 
		width: 925
	});
}

