/***************************/
// @implementing and optimizing for ACA: Alex Baskov, 2010
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusLoginClient = 0;


//loading popup with jQuery magic!
function loadPopupLoginClient()
{
	//loads popup only if it is disabled
	if(popupStatusLoginClient == 0)
	{
		$("#popupLoginClientBackground").css({
			"opacity": "0.4" // set to zero, if you don't want to have a background
		});
		$("#popupLoginClientBackground").fadeIn("slow");
		$("#popupLoginClient").fadeIn("slow");
		popupStatusLoginClient = 1;
	}
	
	$.validator.addMethod("customRequired", function(value, element) { 
		if (element.value == element.title) { return false; } 
		else return true; 
	  }, "Please input something.");
	jlc_validator = $("#form_login_client").validate({
		errorElement: "div",
		rules: {
			josso_username: {
				customRequired: true
			},
			josso_password: {
				customRequired: true
			}
		},
		messages: {
			josso_username: {
				customRequired: "<b>Username</b> is missing"
			},
			josso_password: {
				customRequired: "<b>Password</b> is missing"
			}
		},
		errorPlacement: function(error, element) {
			if ($("#form_login_client .popup_error").length == 0) {
				$("#form_login_client .popup_error_holder").append("<div class='popup_error'></div>").show();
			}
			$("#form_login_client .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_login_client .popup_error .checked").length == $("#form_login_client .popup_error .error").length) {
				$("#form_login_client .popup_error").hide();
			}
			else {
				$("#form_login_client .popup_error").show();
			}
		},
		highlight: function(element, errorClass) {
		},
		invalidHandler: function() {
		},
		submitHandler: function(form) {
			form.submit();
		}
	});
}

//disabling popup with jQuery magic!
function disablePopupLoginClient()
{
	//disables popup only if it is enabled
	if(popupStatusLoginClient == 1)
	{
		$("#popupLoginClientBackground").fadeOut("slow");
		$("#popupLoginClient").fadeOut("slow");
		popupStatusLoginClient = 0;
	}
}

//centering popup
function centerPopupLoginClient()
{
	//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 = $("#popupLoginClient").height();
	var popupWidth = $("#popupLoginClient").width();

	//centering
	$("#popupLoginClient").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupLoginClientBackground").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#buttonLoginClient").click(function() {
		disablePopupLogin();
		
		var url = $("#login_redirect_to_url").val();
		if (url != "") {
			$("#form_login_client input[name=josso_back_to]").val($("#form_login_client input[name=josso_on_error]").val().split('=')[0] + "=" + url);
		}
		
		centerPopupLoginClient();
		loadPopupLoginClient();
	});
	
	$("#popupLoginClientClose").click(function() {
		disablePopupLoginClient();
	});
			
	//Click out event!
	$("#popupLoginClientBackground").click(function() {
		disablePopupLoginClient();
	});
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatusLoginClient == 1) {
			disablePopupLoginClient();
		}
	});
	

});


