window.addEvent('domready', function(){
		UIloadInterface();
	});

	function UIloadInterface() {
		// FUNCTION TO LOAD THE INTERFACE COMPONENTS
		ajaxforms = new AjaxForms();
		ajaxlinks = new AjaxLinks();
	}

	// AjaxForms

	// All forms with class 'ui-ajaxform' are submitted via AJAX and
	// the AJAX response is placed in the element with the ID that
	// is specified in the target attribute of the form.

	var AjaxForms = new Class({
		initialize: function(){
			// Initialize Events
			$$("form.ui-ajaxform").removeEvents('submit');
			$$("form.ui-ajaxform").addEvent('submit',this.submitform.bind(this));
		},
		submitform: function(event){

			var form = event.target;

			// Stop the form form submitting...
			event.preventDefault();
			event.stop();

			// If a user hits return in a text field, the text field is the target!
			while (form.nodeName != "FORM") { form = $(form).parentNode; }

			// Check if validation is required
			if (form.validate) {
				response = form.validate();
				if (response != true) {
					if (response != false) alert(response);
					return false;
				}
			}

			// Check if confirmation is required
			confirmation = form.getProperty('confirmation');
			if (confirmation && confirmation != "") {
				if (!confirm(confirmation)) return false;
			}

			// Figure out where the response is going (if anywhere)...
			var target_id = form.getProperty('target');
			if (target_id != "") {
				var target = $$("#"+target_id);
				target.addClass("ui-loading");
				target.fireEvent("ui-loading");
			}

			// Fire off ajax request...
			form.set('send',{
				evalScripts:true,
				onComplete: function(){
					if (target_id != "") {
						// Put content in destination text...
						target.set('html',this.response.text); 
						target.removeClass("ui-loading");
						UIloadInterface();
						target.fireEvent("ui-complete");
					}
				},
				onFailure: function(){
					if (target_id != "") {
						// Put content in destination text...
						target.setHTML("<p class=\"ui-error\">Ajax Request Failed</p>"); 
						target.removeClass("ui-loading");
					}
				}
			});

			form.send();

			// Reset the form...
			// form.reset();

			// Don't let the form submit!
			return false;
		}
	});




	// AjaxLinks

	// All links with class 'ui-ajaxlink' are submitted via AJAX and
	// the AJAX response is placed in the element with the ID that
	// is specified in the target attribute of the link.

	var AjaxLinks = new Class({
		initialize: function(){
			// Initialize Events
			$$(".ui-ajaxlink").removeEvents('click');
			$$(".ui-ajaxlink").addEvent('click',this.submitlink.bind(this));
		},
		submitlink: function(event){

			var link = $(event.target);

			if (link.tagName != "A") {
				while (!link.hasClass('ui-ajaxlink') && link.tagName != 'HTML') {
					link = $(link).getParent();
				}
			}

			url = $(link).getProperty("href");
			url = url.match(/^[^?]*/);
			url = url.toString();

			querystring = $(link).getProperty("href");
			querystring = $(link).getProperty("href");
			querystring = querystring.match(/[^?]*$/);
			querystring = querystring.toString();
			query = querystring.parseQueryString();

			// Stop the link from clicking...
			event.preventDefault();
			event.stop();

			// Check if confirmation is required
			confirmation = link.getProperty('confirmation');
			if (confirmation && confirmation != "") {
				if (!confirm(confirmation)) return false;
			}

			// Figure out where the response is going (if anywhere)...
			var target_id = link.getProperty('target');
			if (target_id != "") {
				var target = $$("#"+target_id);

				if(target.getChildren().length > 1)
				{
					console.log(target.getChildren());
					return false;
				}

				target.addClass("ui-loading");
				target.fireEvent("ui-loading");
			}

			

			// Fire off ajax request...
			var req = new Request.HTML({
				'url':url,
				'method':'post',
				'data':query,
				'evalScripts':true,
				'onComplete':function(){
					if (target_id != "") {
						// Put content in destination text...
						target.set('html',this.response.text); 
						target.removeClass("ui-loading");
						UIloadInterface();
						target.fireEvent("ui-complete");
					}
				},
				'onFailure': function(){
					if (target_id != "") {
						// Put content in destination text...
						target.setHTML("<p class=\"ui-error\">Ajax Request Failed</p>"); 
						target.removeClass("ui-loading");
					}
				}

			});
	
			req.send();

			// Don't let the link click!
			return false;
		}
	});

