$(document).ready(function(){		
	bindSubmits();
	mainMenuBindings();
});

bindSubmits = function() {
	$("form.simple_ajax").simpleAjaxSubmit();
	
}




$.fn.simpleAjaxSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){		
		return submitAjaxForm(this);	
	});
	
	return this;
}

submitAjaxForm = function(form) {
	$("#form_status").load("form_loading.html");
	$("#form_status").filter(":hidden").show();
	
	var params = {};
	$(form)
	.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], select, textarea")
	.filter(":enabled")
	.each(function() {
		params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	});
	
	$.post(form.getAttribute("action"), params, function(xml) {		
		if ($("response", xml).size() < 1) {
			response = "<div class=\"errors\">";
			response += "<span class=\"heading\">An unknown error occurred while submitting the form. Please try again later</span>";
			response += "</div>";
			
			$("#form_status").html(response).filter(":hidden").show();	
		} else {
			errors = "";
			
			$("error", xml).each(function(i) {
				errors += "<li>" + this.getAttribute("message") + "</li>";						   
			});
			
			if (errors.length > 0) {
				response = "<div class=\"errors\">";
				response += "<span class=\"heading\">The following errors occurred</span>";			
				response += "<ul>";
				response += errors;
				response += "</ul>";
				response += "</div>";
			
				$("#form_status").html(response).filter(":hidden").show();
			} else if ($("redirect", xml).size() > 0) {		
				location.href = $("redirect", xml).get(0).getAttribute("page");	
			} 
		}		
	});

	return false;	
}



mainMenuBindings = function() {	closeAllMenus();	
	$('ol.navigation').find('li').each(function() {
		if ($(this).find('ol').size() > 0) {
			$(this).addClass('hasMore');	
		}
	});
	
	$('ol.navigation').find('li').hover(function(){
		$(this).addClass('hover');
		$(this).children('a').addClass('hover');
		$(this).children('ol').css("top", "0").css("left", $(this).width() + "px").show();		
	},function(){
		$(this).children('ol').hide();
		$(this).children('a').removeClass('hover');
		$(this).removeClass('hover');
	});
}	


otherMenuBindings = function() {
	$('ol.vert_menu').find('li').hover(function() {			
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
}


closeAllMenus = function() {
	$('ol.navigation').find('ol').hide();
}
