/***************************/
// @implementing and optimizing for ACA: Alex Baskov, 2010
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusLoginArtist = 0;


//loading popup with jQuery magic!
function loadPopupLoginArtist()
{
	//loads popup only if it is disabled
	if(popupStatusLoginArtist == 0)
	{
		$("#popupLoginArtistBackground").css({
			"opacity": "0.4" // set to zero, if you don't want to have a background
		});
		$("#popupLoginArtistBackground").fadeIn("slow");
		$("#popupLoginArtist").fadeIn("slow");
		popupStatusLoginArtist = 1;
	}
	
	$.validator.addMethod("customRequired", function(value, element) { 
		if (element.value == element.title) { return false; } 
		else return true; }, "Please input something.");
	jla_validator = $("#form_login_artist").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_artist .popup_error").length == 0) {
				$("#form_login_artist .popup_error_holder").append("<div class='popup_error'></div>").show();
			}
			$("#form_login_artist .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_artist .popup_error .checked").length == $("#form_login_artist .popup_error .error").length) {
				$("#form_login_artist .popup_error").hide();
			}
			else {
				$("#form_login_artist .popup_error").show();
			}
		},
		highlight: function(element, errorClass) {
		},
		invalidHandler: function() {
		},
		submitHandler: function(form) {
			form.submit();
		}
	});
}

//disabling popup with jQuery magic!
function disablePopupLoginArtist()
{
	//disables popup only if it is enabled
	if(popupStatusLoginArtist == 1)
	{
		$("#popupLoginArtistBackground").fadeOut("slow");
		$("#popupLoginArtist").fadeOut("slow");
		popupStatusLoginArtist = 0;
	}
}

//centering popup
function centerPopupLoginArtist()
{
	//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 = $("#popupLoginArtist").height();
	var popupWidth = $("#popupLoginArtist").width();

	//centering
	$("#popupLoginArtist").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupLoginArtistBackground").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#buttonLoginArtist").click(function() {
		disablePopupLogin();
		
		centerPopupLoginArtist();
		loadPopupLoginArtist();
	});
	
	$("#popupLoginArtistClose").click(function() {
		disablePopupLoginArtist();
	});
			
	//Click out event!
	$("#popupLoginArtistBackground").click(function() {
		disablePopupLoginArtist();
	});
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if (e.keyCode == 27 && popupStatusLoginArtist == 1) {
			disablePopupLoginArtist();
		}
	});
	

});


