/********************
 *
 * Librairie JFLAD Studio V5
 *
 * Version 0.1
 *
 * Author: LAVAULT Julien
 * License: JFLAD Studio
 * Date de création : 13/09/2007
 * Date de mise à jour : 14/09/2007
 *
 * Description:
 *		- Gère tout les elements de la page
 *		- 
 *
 * Méthode:
 *		- init(rubrique1, rubrique2): initialise la librairie
 *		- initContenu: initialise les évenements du div contenu
 *		- pageHeader
 *		- pageFooter
 *		- pageAll
 *		- pageDefault
 *		- pageAccueil
 *		- pageReference
 *		- pageContact 
 *		- fx_form
 *		- fx_titreAlpha
 *		- fx_hautPage
 *		- tipsFixe
 *		- tipsMobile 
 *		- ssmenu
 *		- ssmenuScroll
 *		- ajaxRequest
 *		- ajax_showSucces
 *		- ajax_showFailure
 *		- ajax_showRequest
 *
 **********************/
 
	var JFLAD = new Class({
		
		initialize : function(rubrique1, rubrique2) {

			this.rubrique1 = rubrique1;
			this.rubrique2 = rubrique2;
			this.root = this;
			
			this.pageHeader();
			
			// Init de la page spécifique
			if(this.rubrique1 == 'accueil' || this.rubrique1 == '') this.pageAccueil();
			else 
			{
				this.pageAll();
				this.initContenu(this);
			};
			this.pageFooter();
		},

		initContenu : function(root) {
			switch(rubrique1)
			{
				case 'references' : root.pageReferences(root);
				break;
				case 'contact': root.pageContact(root);
				break;
				default: root.pageDefault(root);
			};
		},
		
		pageHeader : function() {
			this.tipsMobile('.tipsAcceuil', 0.9);
			
			// Fancy Menu
			new SlideList($E('ul', 'menu'), {
				duration: 700, 
				transition: Fx.Transitions.backOut
			});
			$('blog').addEvent('mouseenter', function(){ var myFx1 = new Fx.Style(this, 'opacity',{duration:300}).start(1,0.5); });
			$('blog').addEvent('mouseleave', function(){ var myFx2 = new Fx.Style(this, 'opacity',{duration:300}).start(0.5,1); });	
		},
		
		pageFooter : function() {
			$('espace_client').addEvent('mouseenter', function(){ var myFx1 = new Fx.Style(this, 'opacity',{duration:300}).start(1,0.5); });
			$('espace_client').addEvent('mouseleave', function(){ var myFx2 = new Fx.Style(this, 'opacity',{duration:300}).start(0.5,1); });
			$('newsletter').addEvent('mouseenter', function(){ var myFx3 = new Fx.Style(this, 'opacity',{duration:300}).start(1,0.5); });
			$('newsletter').addEvent('mouseleave', function(){ var myFx4 = new Fx.Style(this, 'opacity',{duration:300}).start(0.5,1); });
			$('goodies').addEvent('mouseenter', function(){ var myFx5 = new Fx.Style(this, 'opacity',{duration:300}).start(1,0.5); });
			$('goodies').addEvent('mouseleave', function(){ var myFx6 = new Fx.Style(this, 'opacity',{duration:300}).start(0.5,1); });	
		},
		
		pageAll : function() {

			// Bouton RSS
			if(this.rubrique1 == 'actualites' || this.rubrique1 == 'references' || this.rubrique1 == 'recrutement')
			{
				$$('.rss_link').addEvent('mouseenter', function(){ var myFx1 = new Fx.Style(this, 'opacity',{duration:250}).start(1,0.6); });
				$$('.rss_link').addEvent('mouseleave', function(){ var myFx2 = new Fx.Style(this, 'opacity',{duration:250}).start(0.6,1); });	
			};
			
			// Init de Moodalbox
			var myMOOdalBox = new MOOdalBox();
			
			// Recommandation, imprimer, favoris, rss
			this.tipsFixe('.tips_amis', 0.9);
			$$('.tips_amis').addEvent('click', function(){ myMOOdalBox.open('templates/tmp.goodies.php','Title','500 400'); });
			
			this.tipsFixe('.tips_favoris', 0.9);
			this.tipsFixe('.tips_imprimer', 0.9);
			if(this.rubrique1 == 'actualites' || this.rubrique1 == 'references' || this.rubrique1 == 'recrutement') this.tipsFixe('.tips_rss', 0.9);
			
			// Init du sous menu
			this.ssmenu();
			this.ssmenuScroll();
		},

		pageAccueil : function() {
			$('brochure').addEvent('mouseenter', function(){ var myFx1 = new Fx.Style(this, 'opacity',{duration:300}).start(1,0.6); });
			$('brochure').addEvent('mouseleave', function(){ var myFx2 = new Fx.Style(this, 'opacity',{duration:300}).start(0.6,1); });
			$('brochure').addEvent('click', function(){ alert('Téléchargement de la brochure ...'); });
		},
		
		
		//////////////////////////////////////////////////
		// Zone de contenu de la rubrique
		
		pageDefault : function(root) {
			alert('page default');
			root.fx_titreAlpha('titre_contenu',2000); 
			root.fx_hautPage();
		},

		pageReferences : function(root) {
			
			// Tips sur les images
			root.tipsMobile('.tips_img1',0.8);
			/*
			this.tipsMobile('.tips_img2',0.8);
			this.tipsMobile('.tips_img3',0.8);
			this.tipsMobile('.tips_img4',0.8);
			this.tipsMobile('.tips_img5',0.8);
			*/
			
			// Tips sur les prestations
			root.tipsMobile('.tipsReference1', 1);
			root.tipsMobile('.tipsReference2', 1);
			//this.fx_hautPage();
			Lightbox.init();
		},
		
		pageContact : function(root) {
			root.pageDefault(root);
			root.fx_form('input',2000,'200px','150px','#333333','#CCCCCC','#FFFFFF','#1E1E1E');
			root.fx_form('textarea',2000,'400px','150px','#333333','#CCCCCC','#FFFFFF','#1E1E1E');
		},


		////////////////////////////////////////////////////
		// Effet graphique sur les élements des pages (fx_)
		fx_form : function(type,duree,widthIn,widthOut,colorIn,colorOut,bgIn,bgOut) {
			var input = $$('#formulaire '+type);
			input.each(function(element) {
				if(element.getProperty('type') != 'chekbox')
				{
					var fxInput = new Fx.Styles(element, {
						duration:duree,    //2000
						wait:false,
						transition: Fx.Transitions.Back.easeOut
					});
					$(element).addEvent('focus', function(e) {
						e = new Event(e).stop();
						fxInput.start({
							'width': widthIn,            //200px
							'color': colorIn,            //#333333
							'background-color': bgIn     //#CCCCCC
						});
					});
					$(element).addEvent('blur', function(e) {
						e = new Event(e).stop();
						fxInput.start({
							'width': widthOut,            //150px
							'color': colorOut,            //#FFFFFF
							'background-color': bgOut     //#1E1E1E
						});
					});
				};
			});
		},
		
		fx_titreAlpha : function(id,time) {
	  		var myFx = new Fx.Style(id, 'opacity',{duration:time}).start(1,0.75);
		},
		
		fx_hautPage : function() {
			var size = $('contenu').getSize();
			var sizeY = size['size'].y;
			var limiteY = 560;
			
			if(sizeY >= limiteY)    // On test si le contenu est assez grand pour afficher le bt 'haut de page'
			{
				$('haut_page').setStyles('visibility:visible;');   // On affiche le bouton
				var scrollTop = new Fx.Scroll(window, {
					wait: false,
					duration: 2500,
					transition: Fx.Transitions.Expo.easeOut
				});
				$('haut_page').addEvent('click', function(e) {
					e = new Event(e).stop();
					scrollTop.toTop('conteneur');
				});
			}
			else $('haut_page').setStyles('visibility:hidden;'); // Sinon on cache le bt 'haut de page'
	  },


		tipsFixe : function(element,alpha) {
			new Tips($$(element), {
				fixed: true,
				initialize: function(){ this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 400, wait: false}).set(0); },
				onShow: function(toolTip){ this.fx.start(alpha); },
				onHide: function(toolTip) { this.fx.start(0); }
			});
		},

		tipsMobile : function(element,alpha){
			new Tips($$(element), {
				fixed: false,
				initialize:function(){ this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 300, wait: false}).set(0); },
				onShow: function(toolTip) { this.fx.start(alpha); },
				onHide: function(toolTip) { this.fx.start(0); }
			});
		},
		
		
		/////////////////////////////////////////////////////////////
		// Sous menu
		ssmenu : function() {
			
			// On stock le scope
			var cible = this;
			
			var list = $$('ul#sous_menu li a');
			list.each(function(element) {
				var urlElement = element.getProperty('href');  // On test si l'élément n'est pas déja selectinné
				var fx = new Fx.Styles(element, {
					duration:1300, 
					wait:false,
					transition: Fx.Transitions.Elastic.easeOut
				});
				element.addEvent('mouseenter', function(){
					if(urlElement != urlAjax) fx.start({
						'margin-left': 8,
						'color': '#fff'
					});
				});
				element.addEvent('mouseleave', function(){
					if(urlElement != urlAjax) fx.start({
						'margin-left': 0,
						'color': '#999'
					});
				});
				element.addEvent('click', function(e){
					e = new Event(e).stop();
					// Préchargement
					$('contenu').setHTML('<div class="ajax-loading"></div>');
					
					// Init du conteneur + Requette Ajax
					var url = 'ajax.php';
					var urlLien = element.getProperty('href');
					var requestVar = {
						mode: 'page',
						url: urlLien
					};
					cible.ajaxRequest(url,requestVar);
				});
				
			});
		},
			
		ssmenuScroll : function() {
			var scrolling = new Fx.Scroll('ssmenu_conteneur', {
				wait: false,
				duration: 1500,
				transition: Fx.Transitions.Expo.easeOut
			});
			var list2 = $$('ul#ssmenu_page li a');
			list2.each(function(element) {
				element.addEvent('click', function(e) {
					e = new Event(e).stop();
					var point = element.getProperty('href');
					point = point * 100;     //scrolling.toElement('ssmenu'+point);
					point = point.toInt();
					scrolling.scrollTo(0,point);
				});
			});
		 },
		
		
		 ////////////////////////////////////////////////////////////
		 // Ajax request (effectue une requette)
		 ajaxRequest : function(url,requestVar) {

			var conteneur = $('contenu');
		 	var myAjax = new Ajax(url, {
				method: 'post',
				encoding: 'iso-8859-1',
				//onComplete: this.ajax_showSucces(),
				//onFailure: this.ajax_showFailure(requestVar),
				update: conteneur
		 	}).addEvent('onComplete', function(){
				//$('contenu').removeClass('ajax-loading');
				//$('contenu').setHTML('');
				//$('contenu').setOpacity(0);
				var root = this;
				this.initContenu(this.root);
				
			}).request(requestVar);
		 },

		 ajax_showSucces : function() {
			var root = this.root;
			
			var conteneur = $('contenu');
			$('contenu').removeClass('ajax-loading');
			$('contenu').setOpacity(0);
			
			// Effet d'apparition du contenu
			var contenuFx = new Fx.Styles(conteneur, {
				duration: 1500,
				wait: false,
				transition: Fx.Transitions.Quad.easeOut
			});
			contenuFx.addEvent('onComplete', root.initContenu(root));
			contenuFx.start({
				'opacity': [0, 1]
			});
			
		  },
		
		 ajax_showFailure : function(requestVar){
		 	$('contenu').removeClass('ajax-loading');
			//alert('failure'+requestVar);
		 },
		
		 ajax_showRequest : function(requestVar){
			$('contenu').removeClass('ajax-loading');
			//alert('Request: '+requestVar);
		 },
		 
		 
		 ////////////////////////////////////////////////////////////
		 // Racourcis clavier
		 keyboardListener: function(event){
			switch (event.keyCode)
			{
				case 27: case 88: case 67: alert('Key close'); break;
				case 37: case 80: alert('Key prev'); break;	
				case 39: case 78: alert('Key next');
			}
		}
		
	});


