/***************************/
//@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 popupStatusRegister = 0;

//loading popup with jQuery magic!
function loadPopupRegister(){
	//loads popup only if it is disabled
	if(popupStatusRegister==0){
		$("#backgroundRegisterPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundRegisterPopup").fadeIn("slow");
		$("#register-box").fadeIn("slow");
		popupStatusRegister = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopupRegister(){
	//disables popup only if it is enabled
	if(popupStatusRegister==1){
		$("#backgroundRegisterPopup").fadeOut("slow");
		$("#register-box").fadeOut("slow");
		popupStatusRegister = 0;
		$('.fout').remove();
		$('.field').val("");
		$('input:checkbox').removeAttr('checked');
	}
}

//centering popup
function centerPopupRegister(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = $(document).height();
	var popupHeight = $("#register-box").height();
	var popupWidth = $("#register-box").width();
	//centering
	$("#register-box").css({
		"position": "absolute",
		"top": 20,//windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#backgroundRegisterPopup").css({
		"height": windowHeight
	});

}

function hideRegs() {
	$("#register-view").hide();
	$("#preregister-view").hide();
	$("#presucces-view").hide();
}

function hidevCenterLoad() {
	//hide all other popups
	disablePopup();
	disablePopupOverview();
	disablePopupSucces();
	disablePopupPassword();
	//centering with css
	centerPopupRegister();
	//load popup
	loadPopupRegister();
	hideRegs();	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	// URL
	var urlParameters = document.location;
	urlParameters = (urlParameters.href).split('#')

	if(urlParameters[1] != undefined && urlParameters[1] == 'popRegister')
	{
		hidevCenterLoad();
		$("#register-view").show();
	}
	else if(urlParameters[1] != undefined && urlParameters[1] == 'popPreRegister')
	{
		hidevCenterLoad();
		$("#register-view").show();
	}

	//LOADING POPUP
	//Click the button event!
	$(".popRegister").click(function(){
		hidevCenterLoad();
		$("#register-view").show();
	});
	
	//LOADING POPUP
	//Click the button event!
	$(".popPreRegister").click(function(){
		hidevCenterLoad();
		$("#preregister-view").show();
	});

	//CLOSING POPUP
	//Click the x event!
	$("#close-register").click(function(){
		disablePopupRegister();
	});
	
	//Click out event!
	$("#backgroundRegisterPopup").click(function(){
		disablePopupRegister();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatusRegister==1){
			disablePopupRegister();
		}
	});

});
