if (typeof(XB) == "undefined") XB = {};

XB.Form = function() {
	var _this = this;
	
	this.init = function(args) {
		this.sel = args.selector;
		this.ajax_url = args.ajax_url;
		this.inputs = $(this.sel+" :input");
		this.labels = $(this.sel+" label");
	};
	
	this.setup = function() {
		this.labels.hide();
		this.inputs.each(function() {
			_this.toggleInput(this);
		});
		this.inputs.focus(function() {
			_this.toggleInput(this);
		});
		this.inputs.blur(function() {
			_this.toggleInput(this);
		});
		
		$(this.sel+" .button a").click(function() {
			_this.send();
			return false;
		});
	};
	
	this.send = function() {
		var sending = document.createElement("p");
		sending.innerHTML = "Your message is being sent...";
		sending.className = "sending";
		sending.style.display = "none";
		
		$(this.sel).ajaxStart(function() {
			_this.inputs.attr("disabled", "disabled");
			
			$(_this.sel+" .button").hide();
			$(this).append(sending);
			$(sending).fadeIn("slow");
		});

		$(this.sel).ajaxStop(function() {
			_this.inputs.attr("disabled", "");

			$(sending).empty();
			$(_this.sel+" .button").fadeIn("fast");
		});
		
		$.post(this.ajax_url,
			{
				name: $("#c_name").val(),
				email: $("#c_email").val(),
				message: $("#c_message").val()
			},
			function(data, textStatus) { 
				_this.handleResponse(data, textStatus);
			},
			"json"
		);
	};
	
	this.handleResponse = function(d, t) {
		if (t == "success" && d.sent == "1")
			this.success();
		else
			this.error();
	};
	
	this.success = function() {
		var msg = document.createElement("p");
		msg.innerHTML = "Thanks! We'll get back with you as soon as possible.";
		msg.className = "success";
		
		// Show the success message
		$(this.sel+" form").prepend(msg);
		
		// Get rid of the success message
		$(msg).click(function() { $(this).fadeOut("fast"); });
		setTimeout(function() {
			if ($(_this.sel+" form .success"))
				$(_this.sel+" form .success").fadeOut("fast");
		}, 7000);
		
		// Remove the error message
		if ($(_this.sel+" .error"))
			$(_this.sel+" .error").empty();

		// Clean the inputs
		this.inputs.each(function() {
			_this.toggleInput(this, true);
		});
	};
	
	this.error = function() {
		var msg = document.createElement("p");
		msg.innerHTML = "We were unable to send your message. Please try again later.";
		msg.className = "error";
		
		$(this.sel+" form").prepend(msg);
	};
	
	this.toggleInput = function(e, clean) {
		if ($(e).val() == $(e).attr("name") && !clean) {
			$(e).val("");
			$(e).attr("class", "");
		}
		else if ($(e).val() == "" || clean) {
			$(e).val($(e).attr("name"));
			$(e).attr("class", "alt");
		}
	};
};

XB.Utilities = function() {
	
	this.addArrows = function() {		
		$(".button a").each(function() {
			var span = document.createElement("span");
			span.className = "arrow";
			
			$(this).append(span);
		});
	};
	
	this.switchThumbnails = function() {
		$("#callout .thumbs a").click(function() {
			var current = $("#callout .screenshot img").attr("src");
			var clicked = $(this).attr("href");
			
			$("#callout .screenshot img").attr("src", clicked);
			$(this).children("img").attr("src", current);
			$(this).attr("href", current);
			
			return false;
		});
	};
};

// Font replacement
Cufon.replace("h1")("h2", { hover: true })("h3", { hover: true })("h4")("h5")("#logo")("#nav", { hover: true })(".tagline")(".button", { hover: true })(".platform .heading");

$(document).ready(function() {
	// Handle form inputs
	var form = new XB.Form();
	form.init({ 
		selector: "#contact",
		ajax_url: "/site_includes/send_contact.php"
	});
	form.setup();
	
	// Site utilities
	var util = new XB.Utilities();
	util.addArrows();
	util.switchThumbnails();
});
