function update_status() {
	jQuery.getJSON(BASE_URL+"ajax/authentication/status/", {}, function(json) {
		jQuery("ul.nav-login-signup").show();
		
		if (json['authenticated']) {
			insert_account_info(json['shop']['list']);
		} else {
			prepare_login();
            prepare_signup();
			prepare_change_password();
		}
	});
}

function insert_account_info(shops) {
	jQuery("#loginForm").hide();
	
	jQuery("#loginMenuItem").hide();
	jQuery("#signupMenuItem").hide();
	jQuery("#yourAccountMenuItem").show();
	jQuery("#logoutMenuItem").show();
	
	if (!shops || shops.length == 0) {
		// This is now a shop owner. We forward the user to their account
		jQuery("#myAccountMenuLink").click(function() {
			window.location = BASE_URL+'account/';
			return false;
		});
		jQuery("#myAccountMenuLink").html("My account");
	} else if (shops.length == 1) {
		jQuery("#myAccountMenuLink").click(function() {
			window.location = BASE_URL+'shop/' + shops[0]['id'] + '/';
			return false;
		});
		jQuery("#myAccountMenuLink").html("My shop");
	} else {
		jQuery("#myAccountMenuLink").click(function() {
			displayShopSelector(shops);
			showBubble('shopSelector', jQuery(this));
			return false;
		});
		jQuery("#myAccountMenuLink").html("My shops");
	}
}

function displayShopSelector(shops) {
	var shopDiv = jQuery("#shopSelector");
	shopDiv.find("p:first").text("You're registered as owner of multiple shops. Please select one in the following:");
	
	var dd = jQuery("#shopSelectorDropdown");
	var options = '<option value="0" disabled="disabled" style="font-weight: bold;">-- please select a shop --</option>';
	for (var i = 0; i < shops.length; i++) {
		var shop = shops[i];
		options += '<option value="' + shop['id'] + '">' + shop['name'] + '</option>'; 
	}
	dd.html(options);
	
	jQuery("#shopSelectForm").submit(function() {
		var shop_id = jQuery("#shopSelectorDropdown option:selected").attr("value");
		
		if (shop_id > 0) 
			document.location = BASE_URL+"shop/" + shop_id + "/";
		
		return false;
	});

	showBubbleContent("shopSelector");
}

function display_error(error_str, shake, email) {
	if (shake == undefined) {
		shake = true;
	}
	
	if(error_str.length > 0) {
		jQuery("#login_bubble_container .bubbleFormErrorMessage").html(error_str).show().find("#forgotPasswordLink").click(function() {
			display_error("");
			showBubbleContent("accountForgotPassword");
			jQuery("#id_forgot_password_email").val(email ? email : "").focus();
			return false;
		});
		if (shake) {
			jQuery("#login_bubble_container").shake();
		}
	} else {
		jQuery("#login_bubble_container .bubbleFormErrorMessage").html("").hide();
	}
}


/* prepare functions {{{ */
function prepare_signup() {
    jQuery("#signupMenuItem").show();
	
	jQuery("#signupMenuLink").click(function() {
		display_error("");
		showBubble("accountSignupBox", jQuery(this));
		showBubbleContent("accountSignupBox");
		return false;
	});
	
	jQuery("#signupForm").submit(function() {
		var email = jQuery("#id_signup_email").val();
		var password = jQuery("#id_signup_password").val();
		var repeat_password = jQuery("#id_signup_repeat_password").val();
		var newsletter = jQuery("#id_signup_newsletter").get(0).checked ? jQuery("#id_signup_newsletter").val() : "";
		var terms = jQuery("#id_signup_terms").get(0).checked ? jQuery("#id_signup_terms").val() : "";
		
		jQuery.post(BASE_URL+"ajax/authentication/signup/", {email: email, password: password, repeat_password: repeat_password, newsletter: newsletter, terms: terms}, function(json) {
			if(json['status'] == 'error') {
				errmsgs = json['messages'];
				for(i in errmsgs) {
					display_error(errmsgs[i][0]);
					jQuery("#id_signup_"+i).focus().pulsateOpacity(function(){});
					break;
				}
			} else if(json['status'] == 'error-occupied') {
				display_error("The e-mail is already registered.<br /><br />Did you forgot your password or mabye not recieve an activation e-mail? Head on over to the <a href=\"#\" id=\"forgotPasswordLink\">forgot password</a> dialog and you'll be on your way before you know it.", null, email);
				jQuery("#id_signup_email").focus().pulsateOpacity(function(){});
			} else {
				showBubbleContent("accountSignupReponseBox");
				display_error("");
				
				jQuery("#id_signup_email, #id_signup_password, #id_signup_repeat_password").val("");
				jQuery("#id_signup_newsletter").get(0).checked = false;
				jQuery("#id_signup_terms").get(0).checked = false;
			}
			
			// Re-enable the signup button and stop the pulsating
            pulsateButton = null;
            jQuery("#signup_box_submit").get(0).disabled = false;
		}, "json");
		return false;
	});
}

function prepare_login() {
	jQuery("#loginMenuItem").show();
	jQuery("#yourAccountMenuItem").hide();
	jQuery("#logoutMenuItem").hide();
	
	jQuery("#loginMenuLink").click(function() {
		display_error("");
		showBubble("accountLoginBox");
		showBubbleContent("accountLoginBox");
		return false;
	});
	jQuery("#loginForm").submit(function() {
		var email = jQuery("#id_email").val();
		var password = jQuery("#id_password").val();
		jQuery.post(BASE_URL+"ajax/authentication/login/", {email: email, password: password}, function(json) {
			display_error("");
			shops = json['shops'];
			if (json['error']) {
				jQuery("#login_bubble_container #id_password").get(0).focus();
				display_error(json['message']);
			} else {
				if (!shops || shops.length == 0) {
					// This is not a shop owner. We forward the user to their account
					window.location = '/account/';
				} else if (shops.length == 1) {
					window.location = '/shop/' + shops[0]['id'] + '/';
				} else {
					displayShopSelector(shops); 
				}
			}
			
			// Re-enable the login button and stop the pulsating
            pulsateButton = null;
            jQuery("#login_box_submit").val("Login").get(0).disabled = false;
		}, "json");
		
		return false;
	});
}


function prepare_change_password() {
	jQuery("#accountForgotPasswordLink").click(function() {
		display_error("");
		showBubbleContent("accountForgotPassword");
		jQuery("#id_forgot_password_email").focus();
		return false;
	});
	jQuery("#forgotPasswordForm").submit(function() {
		var email = jQuery("#id_forgot_password_email").val();
		jQuery.post(BASE_URL+"ajax/authentication/forgot_password/", {email: email}, function(json) {
			shops = json['shops'];
			if (json['error']) {
				jQuery("#login_bubble_container #id_forgot_password_email").focus()
				display_error(json['message']);
			} else {
				jQuery("#accountForgotPassword").hide();
				jQuery("#forgotPasswordMessage").html(json['message']); 
				jQuery("#forgotPasswordMessage").show();
			}
		}, "json");
		
		return false;
	});
}
/* }}} prepare functions */


/* Pulsate/disable login and signup {{{ */
var pulsateButton = null;
var pulsateColor = "#ffffff";
function beginLogin() {
	$("#login_box_submit").get(0).disabled = true;
	pulsateButton = $("#login_box_submit");
	setTimeout(pulsateButtonLoop, 1000);
	return true;
}


function beginSignup() {
	$("#signup_box_submit").get(0).disabled = true;
	pulsateButton = $("#signup_box_submit");
	setTimeout(pulsateButtonLoop, 1000);
	return true;
}


function pulsateButtonLoop() {
	if(pulsateButton != null)
		$(pulsateButton).pulsateOpacity(pulsateButtonLoop, pulsateColor);
}
/* }}} Pulsate/disable login and signup */



