/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
//
//@implementing and optimizing for Lisa Mann Creative Management: Alex Baskov, 2009
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusLogin = 0;


//loading popup with jQuery magic!
function loadPopupLogin()
{
	//loads popup only if it is disabled
	if(popupStatusLogin == 0)
	{
		$("#popupLoginBackground").css({
			"opacity": "0.8"
		});
		$("#popupLoginBackground").fadeIn("slow");
		$("#popupLogin").fadeIn("slow");
		popupStatusLogin = 1;
	}
	
	login_validator = $("#form_login").validate({
		errorElement: "div",
		rules: {
			user: {
				required: true
			},
			pass: {
				required: true
			}
		},
		messages: {
			user: {
				required: "Missing user name"
			},
			pass: {
				required: "Missing your password"
			}
		},
		errorPlacement: function(error, element) {
				element.parent().prev().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.parent().hide();
		},
		highlight: function(element, errorClass) {
			$(element).parent().prev().show();
		},
		invalidHandler: function() {
			$("#form_login .field_error").show();
		}	
	});
	$("form_login input").keypress(function(){
		login_validator.element("#" + $(this).attr('id'));
	});
}

//disabling popup with jQuery magic!
function disablePopupLogin()
{
	//disables popup only if it is enabled
	if(popupStatusLogin == 1)
	{
		$("#popupLoginBackground").fadeOut("slow");
		$("#popupLogin").fadeOut("slow");
		popupStatusLogin = 0;
	}
	$("#form_login .field_error").hide();
}

//centering popup
function centerPopupLogin()
{
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	//var windowWidth = document.body.clientWidth;
	//var windowHeight = document.body.clientHeight;
	var popupHeight = $("#popupLogin").height();
	var popupWidth = $("#popupLogin").width();

	//
	// we need here to emulate fixed position... so we use offsetHeight and offsetWidth instead
	// (c) Alex Baskov
	//
	var topScrollerPosition = 0;
	
	// we may just scroll to top and see nice popup
	//scroll(0,0);

	// center vertically in the visible area...
	var topScrollerPosition = document.documentElement.scrollTop;


	//centering
	$("#popupLogin").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		//"top": parseInt((windowHeight-popupHeight)/2) + topScrollerPosition,
		//"top": topScrollerPosition + 140,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#popupLoginBackground").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

	//LOADING POPUP
	//Click the button event!
	
	$("#linkLogin").live('click', function() {
		//centering with css
		centerPopupLogin();
		//load popup
		loadPopupLogin();
	});
				
	//CLOSING POPUP
	$("#popupLoginClose").live('click', function() {
		disablePopupLogin();
	});
	
	//Click out event!
	$("#popupLoginBackground").live('click', function() {
		disablePopupLogin();
	});
	
	//Press Escape event!
	$(document).keypress(function(e) {
		if(e.keyCode == 27 && popupStatusLogin == 1) {
			disablePopupLogin();
		}
	});

});

