jQuery(document).ready(function(){
	jQuery('a[rel=external]').attr('target','_blank');
	jQuery('.clear-text').clearDefault();
	
	/* Rotação dos endereços na home */
	function rotate(){
		$('.banner-slider .banner:first').fadeOut(400, function(){
			$(this).next().hide().fadeIn(150);
			$(this).appendTo('.banner-slider').show();
		});
	};	
	setInterval(rotate, 4000);
	
	/* Slider principal */	
	$('.slider-pagination li').click(function(){
		$('.slider-pagination li').removeClass('active');
		$(this).addClass('active');
		
		var slideNumb = $(this).attr('rel');
	
		$('.slider li').not('.placeholder').stop().animate({'top':'312px'}, 700, function(){
			$('.slider li').not('.placeholder').hide();
			$('.slide' + slideNumb).show();
		}).parent().find('.placeholder').show().stop().animate({'top':'0px'}, 700);

		clearTimeout(timer);
		var timer = setTimeout(function(){
			$('.slider').find('.placeholder').stop(true, false).animate({'top':'-312px'}, 700).parent().find('.slide' + slideNumb).stop(true, false).animate({'top':'0px'}, 700);
		}, 800);		
		return false;
	});
	
	/* Galeria */
	$('.gallery-slider').easySlider({
		continuous: true
	});
	
	$('.gallery ul a').click(function(){
		var src = $(this).attr('href');
		
		$('.main-photo img').attr('src',src);
		return false;
	});
	
	/* Menu */
	$('.menu-tab').hide();
	$('.menu-tab:first').show();
	
	$('.menu-list a').click(function(){
		var tab = $(this).attr('rel');
		
		$('.menu-list li').removeClass('active');
		$(this).parent().addClass('active');
		
		$('.menu-tab').hide();
		$('.menu-tab.n' + tab).show();
		return false;
	});
	
});

/* Clear forms */
(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);


function validateEmail(email) {
	var ret = false;
	var em = new String();
	em = String(email);
	var RegExPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/; 
	if ( em.match(RegExPattern) ) {
		ret = true;
	}
	return ret;
}

function validarNews(){
	
	
	try{
		var nome = $('#nome');
		var email = $('#email');
		
		var valid = false;
		
		if(nome.val() == "" || nome.val() == "Nome"){
			valid = true;
		}
		
		if(!validateEmail(email.val())){
			valid = true;
		}
		
		
		if(!valid){
		
			$.ajax({
				type: "POST",
				url: "send_news.php",
				data: "nome=" + nome.val() + "&email=" + email.val(),
				success: function(msg){
					if ((msg) == "1") {
						$('form').get(0).reset();
						alert('E-mail Cadastrado.');
					} else {
						alert('Erro. Tente novamente mais tarde.');
					}
				}
			});
		}else{
			alert('Preencha os campos corretamente.');
		}
		
	}catch(e){
		alert(e);
	}
	
	return false;


}



function validarContato(){
	try{
		var nome = $('#nome');
		var email = $('#email');
		var mensagem = $('#mensagem');
		
		var valid = false;
		
		if(nome.val() == ""){
			valid = true;
		}
		
		if(email.val() == "" && !validateEmail(email.val())){
			valid = true;
		}
		
		if(mensagem.val() == ""){
			valid = true;
		}
		
		if(!valid){
		
			$.ajax({
				type: "POST",
				url: "send_mail.php",
				data: "nome=" + nome.val() + "&email=" + email.val() + "&mensagem=" + mensagem.val(),
				success: function(msg){
					if ((msg) == "1") {
						$('form').get(0).reset();
						$('#resposta').html('Mensagem Enviada.');
					} else {
						$('#resposta').html('Erro. Tente novamente mais tarde.');
					}
				}
			});
		}else{
			$('#resposta').html('Preencha os campos corretamente.');
		}
		
	}catch(e){
		alert(e);
	}
	
	return false;
	
}
