/***************************/
// @implementing and optimizing for ACA: Alex Baskov, 2010
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusForgot = 0;


//loading popup with jQuery magic!
function loadPopupForgot()
{
	//loads popup only if it is disabled
	if(popupStatusForgot == 0)
	{
		$("#popupForgotBackground").css({
			"opacity": "0.4" // set to zero, if you don't want to have a background
		});
		$("#popupForgotBackground").fadeIn("slow");
		$("#popupForgot").fadeIn("slow");
		popupStatusForgot = 1;
	}
	
	$.validator.addMethod( 
	  "customRequired", 
	  function(value, element) { 
		if (element.value == element.title) { return false; } 
		else return true; 
	  }, 
	  "Please input something." 
	);
	validator = $("#form_forgot_password").validate({
		errorElement: "div",
		rules: {
			fusername: {
				customRequired: true
			},
			femail: {
				customRequired: true
			}
		},
		messages: {
			fusername: {
				customRequired: "<b>Username</b> is missing"
			},
			femail: {
				customRequired: "<b>Email</b> address is missing"
			}
		},
		errorPlacement: function(error, element) {
			if ($("#form_forgot_password .popup_error").length == 0) {
				$("#form_forgot_password .popup_error_holder").append("<div class='popup_error'></div>").show();
			}
			$(".popup_error").show().append(error);
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
			label.hide();

			if ($("#form_forgot_password .popup_error .checked").length == $("#form_forgot_password .popup_error .error").length) {
				$(".popup_error").hide();
			}
			else {
				$(".popup_error").show();
			}
		},
		highlight: function(element, errorClass) {
		},
		invalidHandler: function() {
		},
		submitHandler: function() {
		}
	});
	$("input[class^='it_popup_signin']").keyup(function(){
		validator.element("#" + $(this).attr('id'));
	});
	
	$("#form_forgot_password .popup_message_inprogress").hide();
	$("#form_forgot_password .popup_error_holder").empty();
	$("#form_forgot_password .popup_lbl").show();
	$("#form_forgot_password .popup_fld").show();
	$("#form_forgot_password .popup_forgot_desc").show();
	$(".popup_recover_btn").show();
}

//disabling popup with jQuery magic!
function disablePopupForgot()
{
	//disables popup only if it is enabled
	if(popupStatusForgot == 1)
	{
		$("#popupForgotBackground").fadeOut("slow");
		$("#popupForgot").fadeOut("slow");
		popupStatusForgot = 0;
	}
}

//centering popup
function centerPopupForgot()
{
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupForgot").height();
	var popupWidth = $("#popupForgot").width();

	//centering
	$("#popupForgot").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupForgotBackground").css({
		"height": windowHeight
	});
	
}

function showRecoverPasswordSuccess() {
	if(popupStatusForgot == 1) {
		disablePopupForgot();
		centerPopupThankYou();
		$("#thank_you_forgot").show();
		loadPopupThankYou();
	}
}

function showRecoverPasswordError() {
	$("#form_forgot_password .popup_message_inprogress").hide();
	$("#form_forgot_password .popup_error_holder").show();
	$("#form_forgot_password .popup_lbl").show();
	$("#form_forgot_password .popup_fld").show();
	$("#form_forgot_password .popup_forgot_desc").show();
	$(".popup_recover_btn").show();
}

function showRecoverPasswordProgress() {
	$("#form_forgot_password .popup_message_inprogress").show();
	$("#form_forgot_password .popup_error_holder").empty().hide();
	$("#form_forgot_password .popup_lbl").hide();
	$("#form_forgot_password .popup_fld").hide();
	$("#form_forgot_password .popup_forgot_desc").hide();
	$(".popup_recover_btn").hide();
}

var who = "";
function sendRecoverPassword() {
	
	if (!(validator.form())) {
		return false;
	}
	
	showRecoverPasswordProgress();

	var data = "username=" + escape($("#f_username").val()) +
			   "&email=" + escape($("#f_email").val()); 

	$.ajax({
		type: "POST",
		url: "/external/recoverPassword/" + who,
		data: data,
		success: function(msg) {
			var response = JSON.parse(msg);
			if (response.status == "error") {
				if (response.errors != undefined && response.errors.length > 0) {
					var description = "";
					for (var i = 0; i < response.errors.length; i++) {
						var code = response.errors[i].code;
						if (code == "global") code = "";
						else if (code == "username") code = "Username: ";
						else if (code == "email") code = "Email: ";
						else code = "";

						if (i != 0) description += "<br/>";
						description += "<b>" + code + "</b>" + response.errors[i].description;
					}
					$("#form_forgot_password .popup_error_holder").append("<div class='popup_error'>"  + description + "</div>");
				}
				else {
					$("#form_forgot_password .popup_error_holder").html("<div class='popup_error'>Error to recover your password. Please, try again later!</div>");
				}
				showRecoverPasswordError();
			}
			else {
				showRecoverPasswordSuccess();
			}
		},
		error: function (data, status, e) {
			$("#form_forgot_password .popup_error_holder").html("<div class='popup_error'>Error to recover your password. Please, try again later!</div>");
			showRecoverPasswordError();
		}
	});
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#buttonLoginAForgot").click(function() {
		disablePopupLoginArtist();
		
		who = "artist";
		centerPopupForgot();
		loadPopupForgot();
	});
	
	$("#buttonLoginCForgot").click(function() {
		disablePopupLoginClient();

		who = "client";
		centerPopupForgot();
		loadPopupForgot();
	});
	
	 
	
	$("#popupForgotClose").click(function() {
		disablePopupForgot();
	});
			
	//Click out event!
	$("#popupForgotBackground").click(function() {
		disablePopupForgot();
	});
	
	$("#buttonForgotRecover").click(sendRecoverPassword);
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatusForgot == 1) {
			disablePopupForgot();
		}
	});
	

});


