function init_overlay_forms(hijackedform)
{
	//hijack the register form submit so we can do it with ajax
	$("form#ajax-register").submit(function(){
		var formdata = $("form#ajax-register").serialize();
		$.post("/mmlib/includes/mydaughter/ajax-register.php", formdata, function(response){
			if (response.success)
			{
				hijackedform.addClass("submittable").submit();
			}
			else
			{
				$("#ajax-register .errors").css('display', 'block').html("");
				for (var i = 0; i<response.errors.length ;i++)
				{
					$("#ajax-register .errors").append("<li>"+ response.errors[i] +"</li>");
				}
			}
		}, "json");
		return false;
	});

	$("form#ajax-login").submit(function(){
		var formdata = $("form#ajax-login").serialize();
		$.post("/mmlib/includes/mydaughter/ajax-login.php", formdata, function(response){
			if (response.success)
			{
				hijackedform.addClass("submittable").submit();
			}
			else
			{
				$("#ajax-login .errors").html("<li>Your login details were not correct, please try again</li>");
			}
		}, "json");
		return false;
	});
}
$(document).ready(function(){
	$("form.hijackform").submit(function(){

		var hijackedform = $(this);
		if ($(this).hasClass("submittable"))
		{
			return true;
		}
		else
		{
			$("#loginregister_overlay").overlay({
				"top": 0,
				onBeforeLoad: function() {

					// grab wrapper element inside content
					var wrap = this.getContent();
					var  d = new Date();
					var formurl = "/mmlib/includes/mydaughter/loginregister-form.php?t=" + d.getMilliseconds();
					// load the page specified in the trigger
					wrap.load(formurl, null, function(){
						//init the forms after the combined login/register form has loaded
						init_overlay_forms(hijackedform);
					});
				},
				// we want to use the programming API
				api: true,
				expose: {

					// you might also consider a "transparent" color for the mask
					color: '#000',

					// load mask a little faster
					loadSpeed: 200,

					// highly transparent
					opacity: 0.5
				}

			// load it immediately after the construction
			}).load();
		}
		return false;
	});
});
