var Wamllp =  Wamllp || {};

Wamllp.init = function(){

	// hide popup
    $('.LoginOuter').hide();

	// close popup
	$('a#close').click(Wamllp.hidePopup);

	// load popup
	$('#popupTrigger').click(Wamllp.showPopup);

	// indication for lost password
	$('a#lostPasswordTrigger').click(Wamllp.lostPassword);

	// create account
	$('a#createAccountTrigger').click(Wamllp.createAccount);

	// login processor
	$('a#submitLogin').click(Wamllp.doLogin);

	// create account processor
	$('#submitAccount').click(Wamllp.doRegister);
	
	$('#login input').bind('keypress', function(e) {
		    if(e.keyCode==13){
				Wamllp.doLogin();
				return false;
		    }
	});

};

Wamllp.showPopup = function(obj){
	hrf = $(obj).attr("href");
	txt = $(obj).text();

	$(".section").text(txt);
	$(".sectionUrl").attr("href",hrf);

	$('#pageWrapper').show();

	$('.LoginOuter, form#login').show();
	$('form#register, .message').hide();

	return false;
}

Wamllp.hidePopup = function(){
	$('div.LoginOuter').hide();

	return false;
}

Wamllp.thankYou = function() {
	$('form#register').hide();
	$("div.thankyou").show();

	return false;
}

Wamllp.showIndication = function(){
	$('#howTo').show();

	return false;
}

Wamllp.showLoginErr = function(){
	$('#loginErr').show();

	return false;
}

Wamllp.hideIndication = function(){
	$('#howTo').hide();

	return false;
}

Wamllp.createAccount = function(){
	$('#howTo, form#login, div.thankyou').hide();
	$('form#register').show();

	return false;
}

Wamllp.lostPassword = function(){
	$.post("/user/lostpass.html",$(this).parents("form").serialize(),function data(data) { if(data) { Wamllp.showIndication(); $('#howTo').text(data); } else { Wamllp.hideIndication(); } });
	// post to AJAX here

	return false;
}

Wamllp.doLogin = function(){
	hrf = $(".sectionUrl").attr("href");
	$.post("/user/login.html",$("#login").serialize(),function data(data) { if(data) { Wamllp.showLoginErr(); $('#loginErr').text(data); } else { Wamllp.hideIndication(); window.location = hrf; } });
	// post to AJAX here

	return false;
}

Wamllp.doRegister = function(){
	// post to AJAX here
	$.post("/user/register.html",$(this).parents("form").serialize(),function data(data) { if(data) { Wamllp.showIndication(); $('#howTo').text(data); } else { Wamllp.hideIndication(); Wamllp.thankYou(); } });

	return false;
}

$(document).ready(Wamllp.init);

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};
