var debug = true;

var alw_status;
var alw_loginForm;
var alw_loginMessage;
var forbidden_pages = new Array('wp-activate.php');
var redirect_on_forbidden_page;
jQuery(document).ready(function(){
	try{
		alw_init();
		redirect_on_forbidden_page = alw_base_uri;
	}catch(ex){ if(debug) alert(ex); }
});


function openSignIn(){
	try{
		alw_showLogin();
		var btn = jQuery("#login_btn")[0];
		jQuery("#login_box").animate({
			height:"toggle"
		}, 600, "easeOutCirc");
		setTimeout(function(){
			var value = jQuery('#login_box [name=log]').attr('value');
			jQuery('#login_box [name=log]').focus().attr('value', value).select();
		},600);
		jQuery(btn.parentNode).addClass("selected");
	}catch(ex){
		if(debug) alert(ex);	
	}
}
function closeSignIn(){
	try{
		jQuery("#login_box").animate({
				height:"toggle"
			}, 600, "easeOutCirc"
		);
		jQuery(jQuery("#login_btn")[0].parentNode).removeClass("selected");
	}catch(ex){
		if(debug) alert(ex);	
	}
}

function alw_init() {
	try{
		alw_status = 0;
		alw_loginForm = document.getElementById("alw_loginForm");	
		alw_loginMessage = document.getElementById("alw_loginMessage");		
		var pwd = jQuery('[name=pwd]', alw_loginForm);
			var pwd_text = jQuery('[name=pwd_text]', alw_loginForm);
			
			if(pwd.length){
			pwd_text.focus(function(){
				jQuery(this).hide();
				jQuery(pwd).show().focus();
			});
			pwd.blur(function(){
				if(jQuery.trim(this.value) == ""){
					jQuery(this).hide();
					jQuery(pwd_text).show();
					jQuery(pwd_text)[0].value = jQuery(pwd_text)[0].defaultValue;
				}
			});
			if(jQuery.trim(pwd[0].value) != ""){
				jQuery(pwd).show();
				jQuery(pwd_text).hide();
			}
		}
	}catch(ex){
		if(debug) alert(ex);	
	}
}
function alw_login() {
	try{
		jQuery('#alw_loginMessage').hide();
		if (0 != alw_status) {
			return;
		}
		if (jQuery.estilo.tools.form.hasDefaultValue(alw_loginForm.log)) {
			alw_loginForm.log.select();
			alw_loginForm.log.focus();
			setLoginMessage("Please enter your username");
			jQuery('#alw_loginMessage').fadeIn();
			return;
		}
		if (jQuery.estilo.tools.form.hasDefaultValue(alw_loginForm.pwd)) {
			alw_loginForm.pwd.select();
			alw_loginForm.pwd.focus();
			setLoginMessage("Please enter your password");
			return;
		}			
		is_working(true);
		jQuery.ajax({
			type: "POST",
			url: alw_base_uri + "/wp-content/mu-plugins/estilo-login-widget/login.php",
			data: jQuery(alw_loginForm).serialize(),
			success: alw_loginHandleResponse
		});
		alw_status = 1;
	}catch(ex){
		if(debug) alert(ex);	
	}
}
function setLoginMessage(message, autohide){
	jQuery(alw_loginMessage).empty().append(message).show();

	if(autohide == true){
		setTimeout(function(){
			jQuery(alw_loginMessage).fadeOut();
		}, 1500);
	}
}
function alw_loginHandleResponse(data) {
	try{
		is_working(false);
		alw_status = 0;
	
		var responselines = data.split("\n",2);
		if (responselines[0] == alw_failure) {
			setLoginMessage(responselines[1], true);
			return;
		}
		if (responselines[0] == alw_success) {
			var redirection_address = alw_redirectOnLogin;
			if(isForbiddenPageOpen()){
				redirection_address = redirect_on_forbidden_page;
			}
			if (redirection_address == '')
				window.location.reload(true);
			else
				window.location.href = redirection_address;
			return;
		}
		if(debug) alert("Unknown login response.");
	}catch(ex){
		if(debug) alert(ex);	
	}
}
function isForbiddenPageOpen(){
	try{
		var href = window.location.href;
		for(var i=0; i<forbidden_pages.length; i++){
			if(href.indexOf(forbidden_pages[i]) > -1)
				return true;
		}
		return false;
	}catch(ex){ if(debug) alert(ex); }
}
function alw_loginOnEnter(e) {
	try{
		if(window.event) // IE
			keynum = e.keyCode;
		else if(e.which) // Netscape/Firefox/Opera
			keynum = e.which;
		else
			keynum = 0;
	
		if (keynum==13)
			alw_login();
	}catch(ex){ if(debug) alert(ex); }
}





function alw_showLostPassword(){
	try{
		jQuery('#alw_loginForm').hide();
		jQuery('#alw_lostPasswordForm').show();
		var value = jQuery('[name=username_or_email]').attr('value');
		jQuery('[name=username_or_email]').focus().attr('value', value).select();
	}catch(ex){ if(debug) alert(ex); }
}

function alw_showLogin(){
	try{
		jQuery('#alw_lostPasswordMessage').hide();
		jQuery('#alw_lostPasswordText').show();	
			
		jQuery('#alw_loginForm').show();
		jQuery('#alw_lostPasswordForm').hide();
		var value = jQuery('[name=log]').attr('value');
		jQuery('[name=log]').focus().attr('value', value).select();
	}catch(ex){ if(debug) alert(ex); }
}
var alw_working = false;
function alw_getNewPassword(){
	try{
		if(!alw_working){
			alw_working = true;
			jQuery('#alw_lostPasswordMessage').hide();
			if (0 != alw_status) {
				return;
			}
			var username_or_email = jQuery('[name=username_or_email]')[0];
			if (jQuery.estilo.tools.form.hasDefaultValue(username_or_email)) {
				jQuery(username_or_email).focus().attr('value', username_or_email.defaultValue).select();
				jQuery('#alw_lostPasswordText span:first').css("color", "red");
				setTimeout(function(){
					jQuery('#alw_lostPasswordText span:first').css("color", "white");
				}, 1000);
				return;
			}
			is_working(true);
			jQuery.post(alw_base_uri + "/wp-content/mu-plugins/estilo-login-widget/newpassword.php", jQuery("#alw_lostPasswordForm").serialize(), function(data, textStatus){
				is_working(false);
				alw_working = false;
				var xotree = new XML.ObjTree()
				var json = xotree.parseXML(data).Root;
				if(json.status['@successful'] != 'true'){
					var validator_errors = new Object();
					if(json.errors.error.length == undefined)
						json.errors.error = new Array(json.errors.error);
					
					var error_msg = "";
					for(var i=0; i<json.errors.error.length; i++){
						var error = json.errors.error[i];
						error_msg += error['#text'];
					}
					jQuery('#alw_lostPasswordMessage').empty().append(error_msg).fadeIn();
					jQuery('#alw_lostPasswordText').hide();	
					setTimeout(function(){
						jQuery('#alw_lostPasswordMessage').hide();
						jQuery('#alw_lostPasswordText').fadeIn();	
					}, 2500);
				}else{
					jQuery('#alw_lostPasswordMessage').empty().append("A new password have been sent to your e-mailaddress.").fadeIn();
					jQuery('#alw_lostPasswordText').hide();	
					setTimeout(function(){
						closeSignIn();
						alw_showLogin();
					}, 2500);
					jQuery(username_or_email).focus().attr('value', username_or_email.defaultValue).select();
				}
			}, "html");
		}
	}catch(ex){ if(debug) alert(ex); }
}

function alw_newPasswordOnEnter(e) {
	try{
		if(window.event) // IE
			keynum = e.keyCode;
		else if(e.which) // Netscape/Firefox/Opera
			keynum = e.which;
		else
			keynum = 0;
	
		if (keynum==13)
			alw_getNewPassword();
	}catch(ex){ if(debug) alert(ex); }
}

















