// Ouverture et positionnement de la popup Print
function printPopup(id) {
	$(id).blur();
	var w_left = (screen.width - 260)/2;
	var w_top = (screen.height - 230)/2;
	var props = 'toolbar=no, location=no, status=no, scrollbars=no, menubar=no, resizable=no, copyhistory=no, width=260, height=230, left='+ w_left +', top='+ w_top;
	if(id == "print_button_p") var print_popup = "./print_p.php5"; // Si on est dans l'Espace privilège
	else var print_popup = "./print.php5";
	popupWin = window.open(print_popup,'print',props);
	popupWin.focus();
}

// Réinitialisation du formulaire de la popup de login à l'Espace privilège
function reset_form_privilege() {
	$('res_login').set('text', '');
	$('res_pwd').set('text', '');
	$('form_privilege').reset();
	return;
}

// Fonction Ajax pour le login à l'espace privilège
function ajaxPrivilege_login(toSend) {
	new Request({
		url: $('form_privilege').getProperty('action'),
		method: $('form_privilege').getProperty('method'),
		onComplete: function(response) {
			var jsonObj = JSON.decode(this.getHeader('X-JSON'));
			switch (jsonObj._status) {
				
				// Affichage de la popup de login
				case 'must_login':
					reset_form_privilege();
					$('back').setStyle('display', 'none');
					$('step_forgot_pwd').setStyle('display', 'none');
					$('step_login').setStyle('display', 'block');
					$('privilege_popup').setStyle('top', window.getSize().y / 2 - 88);
					privilege_popup(0, 1);
					break;
				
				// Login OK ou session Espace privilège déjà ouverte
				case 'request_redirect':
					window.location.replace(jsonObj._data);
					break;
				
				// Login NOK
				case 'request_error':
					$('res_login').set('text', jsonObj._data);
					$('pwd').set('value', '');
					$('login').focus();
					break;
				
				// Step_pwd : email_sent
				case 'email_sent':
					$('email').set('value', '');
					$('res_pwd').set('text', jsonObj._data);
					break;
				
				// Step_pwd : email_error
				case 'email_error':
					$('res_pwd').set('text', jsonObj._data);
					break;
				
				// Step_pwd : email_does_not_exist
				case 'email_does_not_exist':
					$('res_pwd').set('text', jsonObj._data);
					break;
			}
		}
	}).send(toSend);
}

// Privilege popup
function privilege_popup(from, to) {
	new Fx.Tween($('privilege_popup'), {
		duration: 2000,
		transition: Fx.Transitions.Cubic.easeOut,
		onStart: function() {
			var mask_height = $('top_container').getSize().y + 10 < window.getSize().y ? window.getSize().y : $('top_container').getSize().y + 10;
			$('mask').setStyle('height', mask_height);
			new Fx.Tween($('mask'), {
				duration: 1000,
				transition: Fx.Transitions.Cubic.easeOut
			}).start('opacity', from / 1.4, to / 1.4);
		},
		onComplete: function() {
			if(from == 0) $('login').focus();
			$('close').addEvent("click", function(e) {
				privilege_popup(1, 0);
			});
			$('form_privilege').addEvent("submit", function(e) {
				new Event(e).stop();
				if($('todo').get('value') == 'login') {
					if($('login').get('value') == "") {
						$('login').focus();
					} else if($('pwd').get('value') == "") {
						$('pwd').focus();
					} else {
						ajaxPrivilege_login($('form_privilege'));
					}
				} else if($('todo').get('value') == 'forgot_pwd') {
					if($('email').get('value') == "") {
						$('email').focus();
					} else {
						ajaxPrivilege_login($('form_privilege'));
					}
				}
			});
		}
	}).start('opacity', from, to);
}

// Slides 'bottom to top' des popup 'Legal wording' et 'Credits'
function slideBottomToTop(popup_name, fx_obj) {
	if(popup_name == 'legal_popup') {
		alt_popup_name = 'credits_popup';
		alt_fx_obj = myCreditsMorphFx;
	} else {
		alt_popup_name = 'legal_popup';
		alt_fx_obj = myLegalMorphFx;
	}
	
	if($(popup_name).getStyle('height') != '0px') { // Fermeture du slide
		fx_obj.start({
			'height': [108, 0],
			'top': [-120, -12]
		});
	} else if($(alt_popup_name).getStyle('height') != '0px') { // Fermeture du slide déjà ouvert puis ouverture du nouveau
		new Fx.Morph(alt_popup_name, {
			duration: 500,
			transition:  Fx.Transitions.Linear
		}).start({
			'height': [108, 0],
			'top': [-120, -12]
		}).chain(function() {
			fx_obj.start({
				'height': [0, 108],
				'top': [-12, -120]
			});
		});
	} else { // Ouverture du slide
		fx_obj.start({
			'height': [0, 108],
			'top': [-12, -120]
		});
	}
}

// *** DOM READY ***
window.addEvent("domready", function() {
	
	// Preload des images
	var loader = new Asset.images(myImages, {
		onComplete: function() {
			// ...
		}
	});
	
	// Slides 'bottom to top' des popup 'Legal wording' et 'Credits'
	myLegalMorphFx = new Fx.Morph('legal_popup', {
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeOut
	});
	myCreditsMorphFx = new Fx.Morph('credits_popup', {
		duration: 1000,
		transition: Fx.Transitions.Cubic.easeOut
	});
	
	// Lancement des slides 'bottom to top'
	$('legal_button').addEvent("click", function(e) {
		slideBottomToTop('legal_popup', myLegalMorphFx);
	});
	$('credits_button').addEvent("click", function(e) {
		slideBottomToTop('credits_popup', myCreditsMorphFx);
	});
	
	// Gestionnaire de l'événement 'click' sur 'Print'
	$$('a[id^=print_button]').addEvent("click", function(e) {
		printPopup(e.target.get("id"));
	});
	
	if($('privilege_popup')) {
		// Gestionnaire de l'événement 'click' sur 'Espace privilège'
		$$('a[id^=menu_private_]').addEvent("click", function(e) {
			new Event(e).stop();
			e.target.blur();
			ajaxPrivilege_login("todo=login");
		});
		// Gestionnaire de l'événement 'click' sur 'help_pwd' de 'Espace privilège'
		$('help_pwd').addEvent("click", function(e) {
			new Event(e).stop();
			reset_form_privilege();
			$('todo').set('value', 'forgot_pwd');
			$('step_login').setStyle('display', 'none');
			$('back').setStyle('display', 'block');
			$('step_forgot_pwd').setStyle('display', 'block');
		});
		// Gestionnaire de l'événement 'click' sur 'back' de 'Espace privilège'
		$('back').addEvent("click", function(e) {
			new Event(e).stop();
			reset_form_privilege();
			$('todo').set('value', 'login');
			$('back').setStyle('display', 'none');
			$('step_forgot_pwd').setStyle('display', 'none');
			$('step_login').setStyle('display', 'block');
		});
	}
	
	/* *** AJAX - Gestion de la langue *** */
	if($$('[id^=switch_lang_]')[0]) {
		$$('[id^=switch_lang_]')[0].addEvent("click", function(e) {
			new Event(e).stop();
			new Request.HTML({
				url: "./ajax/switch_lang.php5",
				method: "post",
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
					var jsonObj = JSON.decode(this.getHeader('X-JSON'));
					if(jsonObj) {
						switch (jsonObj._status) {
							case "lang_switched":
								document.location.href = './' + jsonObj._data[0].goto;
								break;
						}
					}
				}
			}).send("location=" + document.location.href);
		});
	}
	
	// Désactivation click droit
	window.document.addEvent("contextmenu", function(e) { // Ne pas oublier '.document' sinon ça ne marche pas sous IE6 !!!
		//e.stop();
	});
	
});
