jQuery.fn.supportersclub = function() {

	return this.each(function() {

		$(this).find("div.widget").each(function() {
			$(this).addClass("ui-widget").addClass("ui-corner-all");
			$(this).find("div.title").addClass("ui-widget-header").addClass("ui-corner-all");
			$(this).find("div.content").addClass("ui-widget-content").addClass("ui-corner-all");
		});


		$(this).find(".info")
			.addClass("ui-state-highlight")
			.addClass("ui-corner-all")
			.prepend($("<span class='ui-icon ui-icon-info'></span>"));

		$(this).find(".error")
			.addClass("ui-state-error")
			.addClass("ui-corner-all")
			.prepend($("<span class='ui-icon ui-icon-alert'></span>"));

		$(this).find(".button").button();
		$(this).find("button").button();


		$(this).find("input.datePicker").each(function() {
			var options = {
				dateFormat	:	"dd/mm/yy"
			};
			if($(this).data("changeYear")) {
				$.extend(options,{
					changeYear	:	true
				});
			}

			$(this).datepicker(options);
		});

		$(this).find("a.popup").colorbox({
			onComplete	:	function() {
				$("#colorbox").supportersclub();
			}
		});

		$(this).find("div.padded, div.infoBox").wrapInner("<div class='inner' />");

		$(this).find("#cboxWrapper")
			.addClass("ui-corner-all")
			.css("color","#000000");

		$(this).find("form.ajax").submit(function() {

			var submitButton = $(this).find("input[type='submit']");
			$(submitButton).attr("disabled","disabled");

			$.ajax({
				url		:	$(this).attr("action"),
				type		:	"post",
				data		:	$(this).parseForm(),
				dataType	:	"json",

				error		:	function(http, status, error) {
					var error = "An error has occured, please refresh your browser and try again\n\n";
					error += "If the problem persists please contact the helpdesk with the below details:\n\n";
					error += "(" + status + " - " + error + "): " + http.responseText;
					alert(error);

					$(submitButton).removeAttr("disabled");
				},

				success		:	function(data) {
					// Catchable error
					if(data.status == 2) {
						$.fn.colorbox({
							html		:	data.error,
							onComplete	:	function() {
								$("#colorbox").supportersclub();
								$("#cboxWrapper").addClass("ui-state-error");
							},
							onClosed	:	function() {
								$("#cboxWrapper").removeClass("ui-state-error");
							}
						});
						return true;
					}

					// User info
					if(data.status == 3) {
						$.fn.colorbox({
							html		:	data.info,
							onComplete	:	function() {
								$("#colorbox").supportersclub();
								$("#cboxWrapper").addClass("ui-state-highlight");
							},
							onClosed	:	function() {
								location.reload();
							}
						});
						return true;
					}

					location.reload();

				}

			});

			return false;

		});

		$.colorbox.resize();

	});

};


jQuery.fn.parseForm = function(asObject) {

	if(asObject) {
		var data = {};
	} else {
		var data = "";
	}

	$(this).children().each(function() {

		// Recurse
		if(asObject) {
			$.extend(data,$(this).parseForm(true));
		} else {
			data += $(this).parseForm();
		}

		if(!$(this).is("input") && !$(this).is("select") && !$(this).is("textarea")) {
			return true;
		}

		var name = $(this).attr("name");

		if(!name) {
			return true;
		}

		var value = $(this).val().trim();
		$(this).val(value);

		if($(this).attr("type") == "checkbox") {
			if(!$(this).attr("checked")) {
				return true;
			}
		}

		if($(this).attr("type") == "radio") {
			if(!$(this).attr("checked")) {
				return true;
			}
		}

		if(asObject) {
			data[name] = value;
		} else {
			data += $(this).serialize() + "&";
		}

	});

	return data;

};

