$(document).ready(function() {
	$("a.next").click(function() {
		var img = $(this).parent().find("img.show");
		if(img.next().attr("alt") == "screenshot") {
			img.hide();
			img.next().fadeIn("slow").addClass("show").removeClass("notshow");
			img.removeClass("show").addClass("notshow");
		}
		else {
			img.hide();
			img.parent().find(".img-0").fadeIn("slow").addClass("show").removeClass("notshow");
			img.removeClass("show").addClass("notshow");
		}
		return false;
	})
	$(".backtop").click(function() {
		$.scrollTo( '#container', 800 );
	});
	$(".facebox").facebox();
	
	$(".contact a").click(function() {
		$("#contact-content .hidden").empty().append("<input type='hidden' name='notspam' value='1' />");
	});
	
	$("#contact-content form").live("submit", function() {
		$("#contact-content p").empty().append("<img src='assets/images/loading.gif' alt='' /> Please wait, validating your form first");
		
		var inputs = [];
		var error = 0;
		$(":input",this).each(function() {
			if(this.value == "") error = 1;
			if(this.name == "email" && !this.value.match(/@/)) error = 1;
			inputs.push(this.name + "=" + escape(this.value));
		});
		
		if(error == 1) {
			$("#contact-content p").empty().append("<strong style='color:#CC0000;'>Please complete all the fields below</strong>");
		}
		
		if(error == 0) {
			$.ajax({
				url: "submit.php",
				data: inputs.join("&"),
				type: "POST",
				success: function(callback) {
					data = callback.split(",");
					if(data[0] == "key") {
						$("#contact-content p").empty().append("<img src='assets/images/loading.gif' alt='' /> Validation success. Please wait while sending the message...");
						$("#contact-content .hidden").empty().append("<input type='hidden' name='id' value='"+data[1]+"' />");
						inputs.push("id="+data[1]);
						
						$.ajax({
							url: "submit.php",
							data: inputs.join("&"),
							type: "POST",
							success: function(c) {
								if(c == 1) {
									$("#contact-content p").empty().append("Your message has been sent. Thank you!");
									$("#contact-content p").css({fontWeight:"bold", color: "#749522"});
								}
								else if(c == 0) {
									$("#contact-content p").empty().append("Somehow, your message cannot be send. Please try refreshing your page.");
									$("#contact-content p").css({fontWeight:"bold", color: "#CC0000"});
								}
								else if(c == 2) {
									$("#contact-content p").empty().append("Somehow, your message has been marked as spam.");
									$("#contact-content p").css({fontWeight:"bold", color: "#CC0000"});
								}
								
								$("#contact-content form").remove();
							}
						});
					}
				}
			});
		}

		
		return false;
	});
})
