/***************************
 *@Author: Adrian "yEnS" Mato Gondelle
 *@website: www.yensdesign.com
 *@email: yensamg@gmail.com
 *@license: Feel free to use it, but keep this credits please!
 *			
 ***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var clientLoginPanelStatus = 0;

//loading popup with jQuery magic!
function loadClientLoginPanel(){
	//loads popup only if it is disabled
	if(clientLoginPanelStatus==0){
		$j("#panel_background").css({
			"opacity": "0.7"
		});
		$j("#panel_background").fadeIn("fast");
		$j("#panel_popup").fadeIn("fast");
		clientLoginPanelStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disableClientLoginPanel(){
	//disables popup only if it is enabled
	if(clientLoginPanelStatus==1){
		$j("#panel_background").fadeOut("fast");
		$j("#panel_popup").fadeOut("fast");
		clientLoginPanelStatus = 0;
	}
}

//centering popup
function centerClientLoginPanel(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $j("#panel_popup").height();
	var popupWidth = $j("#panel_popup").width();
	
	//Adjust for scrolling (keep centered even during scroll)
	var scroll_position = 0;
	if (window.innerHeight) //Firefox Support
		scroll_position = window.pageYOffset;
	else
		scroll_position = document.body.scrollTop;
		
	//centering
	$j("#panel_popup").css({
		"position": "absolute",
		"top": (windowHeight/2-popupHeight/1.5)+scroll_position,
		"left": (windowWidth/2-popupWidth/2)-30
	});
	//only need force for IE6
	
	$j("#panel_background").css({
		"height": windowHeight
	});
}


//CONTROLLING EVENTS IN jQuery
$j(document).ready(function(){
	//LOADING POPUP
	//Click the button event!
	$j("#client-login").click(function(){
		//centering with css
		centerClientLoginPanel();
		//load popup
		loadClientLoginPanel();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$j("#panel_close_button").click(function(){
		disableClientLoginPanel();
	});
	//Click out event!
	$j("#panel_background").click(function(){
		disableClientLoginPanel();
	});
	//Press Escape event!
	$j(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1)
			disableClientLoginPanel();
	});
	
	//Make it draggable
	$j("#panel_popup").draggable({ handle: '#panel_popup h1' });
	
	//Fade in Form
	$j('#panel_form').hide();
	
	$j('#plesk-panel').click( function(){
		$j('#panel_form').fadeIn('normal');
		$j("input.focus:first").focus(); //set focus on input field
	}); //End fading form
	$j('#back_to_selection').click( function(){
		$j('#panel_form').fadeOut('normal');
	});
});

//Keep centered during scroll and resize of window
$j(window).scroll( centerClientLoginPanel );
$j(window).resize( centerClientLoginPanel );