$(document).ready(function() {
	// Initialize app
	start();
	menu();
	email();
});

function start() {
	$(".headbox").hide();
	$(".headbox").eq(0).fadeIn("normal", function() {
		$(".headbox").eq(1).fadeIn("normal", function() {
			$(".headbox").eq(2).fadeIn("normal");
		});
	});

	$(".picthumb").bind("mouseover", function() {
		if (!$(this).hasClass("selected"))
			if ($(this).children("img").length) {
				$(this).css("borderColor", "black");
			}
	});

	$(".picthumb").bind("mouseout", function() {
		if (!$(this).hasClass("selected"))
			$(this).css("borderColor", "#bbb");
	});
		
	$(".picthumb img").bind("click", function() {
		if (!$(this).parent().hasClass("selected")) {
			$(".picthumb").removeClass("selected").css("borderColor", "#bbb");
			$(this).parent().addClass("selected").css("borderColor", "#0068ab");
			getPicture($(this).attr("alt"), $(this).attr("title"));
		}
	});
	
	$("input, textarea").not(".button").bind("focus", function() {
		$(this).css("backgroundColor", "#ffff88");
	}).bind("blur", function() {
		$(this).css("backgroundColor", "white");
	});

	$("#reset").bind("click", function() {
		if (confirm("Reset the form?")) {
			$("input, textarea").not(".button").val("");
		}
	});
	
	$("#submit").bind("click", function() {
		if ($(this).attr("disabled") != "disabled") {
			var firstName = jQuery.trim($("#firstName").val());
			var lastName = jQuery.trim($("#lastName").val());
			var phone = jQuery.trim($("#phone").val());
			var email = jQuery.trim($("#email").val());
			var message = jQuery.trim($("#message").val());
	
			if (firstName.length == 0) {
				alert("Please enter your first name.");
				$("#firstName").focus();
			} else if (lastName.length == 0) {
				alert("Please enter your last name.");
				$("#lastName").focus();
			} else {
				var rePhone = /\(?\d{3}\)?[-\s.]?\d{3}[-\s.]?\d{4}$/;
				var reEmail = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
				if (!phone.match(rePhone)) {
					alert("Please enter a valid phone number at which you can be reached at.");
					$("#phone").focus();
				} else if (!email.match(reEmail)) {
					alert("Please enter a valid email at which you can be reached at.");
					$("#email").focus();
				} else {
					$("#submit").attr("disabled", "disabled").css("color", "#aaa");
					sendMail();
				}
			}
		}
	});
	
	if ($("#portfolio").length) {
		$('#portfolio').innerfade({
			speed: 'slow',
			timeout: 3000,
			type: 'sequence',
			containerheight: '200px'
		});
	}
}

function sendMail() {
	var firstName = jQuery.trim($("#firstName").val());
	var lastName = jQuery.trim($("#lastName").val());
	var phone = jQuery.trim($("#phone").val());
	var email = jQuery.trim($("#email").val());
	var message = jQuery.trim($("#message").val());
	var success = false;
	var returnText;
	
	$.ajax({
		type: "POST",
		url: "/ajax/mail.php",
		data: "firstName="+firstName+"&lastName="+lastName+"&phone="+phone+"&email="+email+"&message="+message,
		dataType: "text",
		success: function(e) {
			returnText = e;
			success = true;
		},
		error: function(a, b, c) {
			alert("There was an error processing the AJAX request.\n\n\nDebug data:\n\nXMLHttpRequest object: " + a + "\nDescription: " + b + "\nException object: " + c);
		},
		complete: function() {
			if (success) {
				if (returnText.indexOf("sent") != -1) {
					alert("Message sent! Thank you for your inquiry. Someone will contact you ASAP.");
				} else {
					alert("Message delivery failed...Please try again.");
					$("#submit").removeAttr("disabled").css("color", "#000");
				}
			} else {
				alert("Message delivery failed...Please try again.");
				$("#submit").removeAttr("disabled").css("color", "#000");
			}
		}
	});
}

function getPicture(picture, description) {
	var success = false;
	if (description == undefined) description = "";
	
	$("#picture").empty().css("background-image", "url(/img/ajax-loader.gif)");

	$.ajax({
		type: "POST",
		url: "/ajax/picture.php",
		data: "picture="+picture+"&description="+description,
		dataType: "html",
		success: function(e) {
			$("#picture").append(e);
			success = true;
		},
		error: function(a, b, c) {
			alert("There was an error processing the AJAX request.\n\n\nDebug data:\n\nXMLHttpRequest object: " + a + "\nDescription: " + b + "\nException object: " + c);
		},
		complete: function() {
			if (success) {
				$("#main").fadeOut("fast", function() {
					$("#picture img, #picture span").hide();
					$("#picture").show();
					$("#picture img, #picture span").fadeIn("normal");
				});
				
				$("#picture").unbind().bind("click", function() {
					$("#picture img, #picture span").fadeOut("slow", function() {
						$("#main").fadeIn("fast");
						$("#picture").hide();
					});
					$(".picthumb").removeClass("selected").css("borderColor", "#bbb");
				});
			}
			$("#picture").css("background-image", "none");
		}
	});
}

function menu() {
	$("#navh ul li").hover(function() {
			$(this).css("background", "#0072bc");
			$(this).children("ul").fadeIn("normal");
		}, function() {
			$(this).css("background", "none");
			$(this).children("ul").hide();
		}
	);

	$("#navh ul ul li").hover(function() {
			$(this).children("ul").fadeIn("normal");
		}, function() {
			$(this).children("ul").hide();
		}
	);
}

function email() {
	var email = [105,110,102,111,64,98,105,110,99,108,101,97,110,46,98,105,122];
	var emailInfo = "";
	for (var i = 0; i < email.length; i++)
		emailInfo += String.fromCharCode(email[i]);
	
	$(".emailInfo").html('<a href="mailto:' + emailInfo + '?subject=Website Inquiry">' + emailInfo + '</a>');
}