$(document).ready(function () {
	$('#pf_requestInvite').submit(PrimalFusion.virtualsignup);
	$('#pf_requestInviteSubmit').click(PrimalFusion.virtualsignup);
	$('#pf_email').focus(PrimalFusion.clearField);
	$('#pf_email').val("Your email address");
	
	PrimalFusion.resizeBody();
});

PrimalFusion = {
	// This method will measure the leftNav and rightNav and take the taller height of the two and set
	// the body content areas height to match it so that the body is never shorter than either of the sides
	resizeBody: function(){
		leftHeight = $("#pf_leftnav").outerHeight();
		rightHeight = $("#pf_rightnav").outerHeight();
		
		bodyHeight = (leftHeight > rightHeight ? leftHeight : rightHeight);
		if ($.browser.msie && jQuery.browser.version < 7){
			$("#pf_page").height(bodyHeight);
			$("#pf_about").height(bodyHeight);			
		} else {
			$("#pf_page").css("min-height", bodyHeight);
			$("#pf_about").css("min-height", bodyHeight);
		}
	},

    virtualsignup: function (e) {
        e.preventDefault();
        var ajaxuri = "/account_requests";
        var uri = BROKER_URL + "/account_requests/new";
        var email = $.trim($('#pf_email').val());

        if (email.length == 0) {
            alert("Please tell us your email address.");
            return false;
        } else if (!email.match(/[^@]+@[^@]+(\.[^@]+)+/)) {
            alert("Your email address is invalid. Please try again.");
            return false;
        }

        var args = {
            "email": email,
            "veremail": email,
            "invitationCode": ""
        }

        $('#pf_requestInvite').replaceWith(PrimalFusion.template.inProgress());

        jQuery.post(ajaxuri, args, function(resp) {
            if($(resp).find("h2:contains('Thank you')").length > 0) {
                $('#pf_requestInvite').replaceWith(PrimalFusion.template.signupSuccess(email));
            } else {
                $('#pf_requestInvite').replaceWith(PrimalFusion.template.signupFailure(uri));
            }
        });
        return false;
    },

    clearField: function(e){
        $(this).val("");
    },

    template: {
        inProgress: function () {
			var uiText = '';
			uiText += '<div id="pf_requestInvite">';
			uiText +=   '<p class="Load"><img src="'+RESOURCE_ROOT+'/images/generatedSites/spinner_fffeea.gif" alt="Loading..." /></p>';
			uiText +=   '<p class="Load">Sending your request for an invite...</p>';
			uiText += '</div>';
			return uiText;
        },

        signupSuccess: function (email) {
			var uiText = '';
			uiText += '<p>We\'ve received your request for an invitation to Primal Fusion\'s alpha. Although we\'re limiting access at this time, we\'ll be sure you get hands-on experience before Primal Fusion opens to a wide audience.</p>';
			uiText += '<p>Monitor your email Inbox for our next message, which will invite you to create an account and get started. Until then, be sure to <a href="http://corp.primalfusion.com/blog">read our blog</a> or <a href="http://www.getsatisfaction.com/primalfusion">visit our forums</a> for the latest news.</p>';
		    return uiText;
        },

        signupFailure: function (uri) {
			var uiText = '';
			uiText += '<div id="pf_requestInvite">';
			uiText +=   '<p>Uh-oh! We couldn\'t send you an invite. Perhaps you can try again by going to our <a href="'+uri+'">Account Request</a> page?</p>';
			uiText += '</div>';
            return uiText;
        }
    }
}

