/* ----------------------------------------------------------------- *\
   M2BRNET - Criatividade e Tecnologia
\* ----------------------------------------------------------------- */

$(function(){
	// DOM Ready!

	// Limpando values dos inputs -------------------------------------

	$("#contact-form input, #contact-form textarea").m2_clearInput();

	// Verificando formulário -----------------------------------------

	$("#cf-phone").focus(function(){
		$(this).mask("(99) 9999-9999",{placeholder:""});
	}).blur(function(){
		$(this).unmask();
		var valor = $(this).val();
		if (valor.length<14) {
			$(this).attr("value","Seu telefone");
		};
	});

	$("#contact-form").submit(function(){

		var validForm = validateForm(this);
		if (!validForm) {
			return false;
		} else {
			var nome  = $("#cf-name").val();
			var email = $("#cf-mail").val();
			var fone  = $("#cf-phone").val();
			var msg   = $("#cf-msg").val();
			var btnSubmit = $("#cf-submit");
			var loader = $("#cf-loader");
			var result = $("#cf-result");
			
			if (fone == "Seu telefone") {
				fone = "";
			};
			
			$.ajax({
				url: "mail.php",
				type: "POST",
				dataType: "html",
				data: "nome="+nome+"&email="+email+"&fone="+fone+"&mensagem="+msg,
				beforeSend: function() {
					$(btnSubmit).hide();
					$(loader).html("Enviando mensagem...").show();
				},
				success: function(data){
					pageTracker._trackPageview('/enviado/contato/');
					$(loader).hide();
					$(result).addClass("notice").html(data).show();
					setTimeout('clearMsg($("#cf-result"),$("#cf-submit"))', 5000);
					$("#contact-form")[0].reset();					
				},
				error: function() {
					$(result).addClass("alert").html("Ocorreu um erro ao enviar o seu e-mail. Tente novamente!").show();
				}
			});
			return false;			
		};
	});

	// Mostrar label restrito -----------------------------------------

	$("dt").each(function() {
		$(this).mouseover(function() {
			$(this).find(".restrict").show();
		}).mouseout(function(){
			$(this).find(".restrict").hide();
		});
	});
	

	// Abrindo links externos -----------------------------------------
	
	$("a[rel=external]").click(function(){
		$(this).attr("target","_blank");
	});
	
	// Menu scroll ----------------------------------------------------

	$("a[href*=#]").click(function() {
		var target = $(this).attr("href");
		if (target == "#") {
			return false;
		} else {
			var targetOffset = $(target).offset().top;
			$("html,body").animate({ scrollTop: targetOffset }, 800);
			return false;
		}
	});

	// Lightbox -------------------------------------------------------
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal',
		padding: 40,
		opacity: 0.35,
		showTitle: false,
		allowresize: true,
		counter_separator_label: ' de '
	});
	$("a[rel^='prettyPhoto']").click(function(){
		var rel = $(this).attr('rel');
		var rel = rel.replace(/prettyPhoto\[/, '');
		var rel = rel.replace(/\]/, '');
		//alert(rel);
		pageTracker._trackPageview('/portfolio/'+rel+'/');
		return false;
	});

	// Sidebar Select -------------------------------------------------

    $(".services-jump").change(function() {
        var val = $(this).val();
        if (val != '') {
            location.href = val;
        }
    });
    
});

/* ----------------------------------------------------------------- *\
   Helpers
\* ----------------------------------------------------------------- */

function clearMsg(msg,btn){
	$(msg).hide();
	$(btn).show();		
}

function validateEmail(email){
	var reg = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (reg.test(email));
}	

function validateForm(form){
	var erro  = "";
	var nome  = $("#cf-name").val();
	var email = $("#cf-mail").val();
	var msg   = $("#cf-msg").val();
	var validEmail = validateEmail(email);

	if (nome == "" || nome == "Seu nome") { 
		erro += "- Por favor, digite seu nome.\n";
	};
	if (email == "" || email == "seu@email.com.br") { 
		erro += "- Por favor, digite seu e-mail.\n";
	} else if(!validEmail) {
		erro += "- Por favor, digite um e-mail válido.\n";
	}
	if (msg == "" || msg == "Sua mensagem") { 
		erro += "- Por favor, deixe sua mensagem.\n";
	};
	if (erro) {
		alert("Aconteceram alguns errors no envio do seu e-mail!\n"+erro);
		return false;
	} else {
		return true;
	};
}

$.fn.m2_clearInput = function() {
	return $(this).focus(function(){
		if(this.value == this.defaultValue) {
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length) {
			this.value = this.defaultValue;
		}
	});
};