// JavaScript Document

var position = 0;

var color = [
	"#A08480",
	"#848095",
	"#809084",
	"#A09480"];

$(document).ready(function() {
	var menu = $("#main-menu a");
	for(var i = 0; i < menu.length; i++) {
		menu.eq(i).click(function(event) {
			event.preventDefault();
			
			flow($("#main-menu a").index($(this)));
		});
	}
	
	$(".navigation").click(function(event) {
		event.preventDefault();
		
		flow(position + ($(".navigation").index($(this)) * 2) - 1);
	});
	
	$("input.name").change(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, " + ($(this).val() ? "0.5" : "1.0") + ")"});});
	$("input.name").focusin(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, 0.5)"});});
	$("input.name").focusout(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, " + ($(this).val() ? "0.5" : "1.0") + ")"});});
	
	$("input.email").change(function(event) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$(this).css({"background-color" : "rgba(255, 255, 255, " + (filter.test($(this).val()) ? "0.5" : "1.0") + ")"});
    });
	$("input.email").focusin(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, 0.5)"});});
	$("input.email").focusout(function(event) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$(this).css({"background-color" : "rgba(255, 255, 255, " + (filter.test($(this).val()) ? "0.5" : "1.0") + ")"});
    });

	$("textarea.comment").change(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, " + ($(this).val() ? "0.5" : "1.0") + ")"});});
	$("textarea.comment").focusin(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, 0.5)"});});
	$("textarea.comment").focusout(function(event) {$(this).css({"background-color" : "rgba(255, 255, 255, " + ($(this).val() ? "0.5" : "1.0") + ")"});});
	
	$("form").submit(function(event) {
		event.preventDefault();
		
		var result = true;
		
		if(!$("input.name").val()) {
			$("input.name").css({"background-color" : "rgba(255, 255, 255, 1.0)"});
			result = false;
		}
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test($("input.email").val())) {
			$("input.email").css({"background-color" : "rgba(255, 255, 255, 1.0)"});
			result = false;
		}
		
		if(!$("textarea.comment").val()) {
			$("textarea.comment").css({"background-color" : "rgba(255, 255, 255, 1.0)"});
			result = false;
		}
		
		if(result) {
			$.ajax({
				type : "POST",
				url : "script/mail.php",
				data : $(this).serialize()
			});
			
			$("form").fadeOut(500);
			$("form").parent().children(".type")
				.html("Message Sent")
				.css({"color" : "rgba(255, 255, 255, 1.0)"});
		}
	});
});

var flow = function(location) {
	position = location;
	
	if(position != 0) {
		$(".navigation").eq(0).fadeTo(125, .125);
	} else {
		$(".navigation").eq(0).fadeOut(125);
	}
	if(position != 3) {
		$(".navigation").eq(1).fadeTo(125, .125);
	} else {
		$(".navigation").eq(1).fadeOut(125);
	}
	
	$("#body > .box").css({"left" : (position * 100 * (-1)) + "%"});
	$("#main-menu a.active").removeClass("active");
	$("#main-menu a").eq(position).addClass("active");
	
	$("body").css({"background-color" : color[position]});
}
