$(document).ready(function() {
	$("form.check-form").visionSubmit();
	
	// Make UI Message Removable
	$(".js-delete-uimessage").live("click", function() {
		$(this).parent(".message").fadeOut("fast", function() {
			$(this).remove()
			if($("#messageWrapper div.message").length == 0) {
				$("#messageWrapper").hide();
			}
		});
	});
});

// jquery plugin
$.fn.visionSubmit = function() {
	// make sure we find something first
	if (this.length) {
		this.submit(function() {
			var form = $(this);
			
			// suppress message class tells the forms handler to 
			// tell the envelope class to suppress the default 
			// message handling behavior and let the calling code
			// deal with displaying the ui message how it wants.  Work
			// around for forms in thickboxes.
			var suppressMsg = form.hasClass("check-form-suppressMsg");
			
			// before submit
			form.trigger("beforeSubmit");
			if(form.data("stopSubmit")) {
				return false;
			}
		
			// remove message
			$("#messageWrapper div.message").remove();
			
			// Disable submits and add waiting cursor
			$("body").append('<div id="loadingMessage">Loading</div>').css("cursor", "wait");
			form.find("button:submit").attr("disabled", "disabled");
		
			// build params
			var params = form.serialize(), submit = form.data("submitButton");
			if(submit) {
				params += (params.length ? "&" : "") + submit.name + "=" + submit.value;
				form.data("submitButton", false); // reset
			}
		
			// post
			$.post(form.attr("action"), params, function(data) {
				var focus = true, envelope = new Envelope(data,suppressMsg);

				data = typeof(envelope.payload.failed) == "undefined" ? [] : envelope.payload.failed;
				for(var i in data) {
					var name = data[i], id = form.find("input[name=%s],textarea[name=%s],select[name=%s]".replace(/%s/g, name)).attr("id");
					if(id) {
						form.find("label[for=%s]".replace(/%s/g, id)).addClass("inputFieldError");
						
						if(focus === true) {
							$("#" + id).focus(); // focus on the first error
							focus = false;
						}
					}
				}
				
				data = typeof(envelope.payload.succeeded) == "undefined" ? [] : envelope.payload.succeeded;
				for(var i in data) {
					var name = data[i], id = form.find("input[name=%s],textarea[name=%s],select[name=%s]".replace(/%s/g, name)).attr("id");
					if(id) {
						form.find("label[for=%s]".replace(/%s/g, id)).removeClass("inputFieldError");
					}
				}
			
				// remove loading message
				$("body").css("cursor", "default").find("#loadingMessage").remove();

				// re-enable button & trigger after submit
				form.find("button:submit").removeAttr("disabled").trigger("afterSubmit", envelope);

			}, "text"); // type (text/json) to skip global eval in jquery 1.4

			return false;

		}).find("button:submit").click(function() {
			var self = $(this);
			var form = self.parents("form");
			var name = self.attr("name");
			if (name) {
				form.data("submitButton",{"name": name, "value": $(this).val() || true});
			}
			return true;

		}).removeAttr("disabled"); // release the submit buttons on doc ready
	}

	return this;
};
