/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var	morphAction	= Class.create({

	options			: null,
	width			: 0,
	currentEffect	: null,
	elAnimate		: null,
	state			: 0,

	initialize	: function(elAction, elAnimate, options) {

		elAction		= $(elAction);
		this.elAnimate	= $(elAnimate);

		this.options = Object.extend({
			transitionOpen	: Effect.Transitions.spring,
			transitionClose	: Effect.Transitions.linear,
			durationOpen	: 1,
			durationClose	: 0.3,
			fixedSize		: null,
			eventClose		: 'mouseout',
			eventOpen		: 'mouseover',
			callBackOpen	: Prototype.emptyFunction,
			callBackClose	: Prototype.emptyFunction
		}, options || {});

		if(this.options.fixedSize !== null) {
			this.width		= this.options.fixedSize;
		} else {
			this.width		= this.elAnimate.getWidth();
		}
		
		this.elAnimate.setStyle({width: '0px', overflow: 'hidden'});

		elAction.observe(this.options.eventOpen, this.open.bind(this));
		elAction.observe(this.options.eventClose, this.close.bind(this));

	},

	open		: function() {
		
		if( this.options.eventOpen !== this.options.eventClose || this.state == 0) {
			if(this.currentEffect != null) {
				this.currentEffect.cancel();
			}

			this.currentEffect	= new Effect.Morph(this.elAnimate,
					{
						duration: this.options.durationOpen,
						style: 'width: ' + this.width + 'px',
						transition: this.options.transitionOpen,
						afterFinish: function(){this.currentEffect = null; this.state = 1;}.bind(this)
					}
				);

			this.options.callBackOpen.bind(this)();
		}
		
	},

	close		: function() {

		if( this.options.eventOpen !== this.options.eventClose || this.state == 1) {
			if(this.currentEffect != null) {
				this.currentEffect.cancel();
			}

			this.currentEffect	= new Effect.Morph(this.elAnimate,
					{
						duration: this.options.durationClose,
						style: 'width: 0px',
						transition: this.options.transitionClose,
						afterFinish: function(){this.currentEffect = null; this.state = 0;}.bind(this)
					}
				);

			this.options.callBackClose.bind(this)();
		}
	}

});


/***************************************************************
 *
 *	Initialisation spécifique par page
 *
 ***************************************************************/

/**
 * initialise un menu laterale
 */
function initSideMenu(button, el, width, close) {

	close			= close || false;

	button.onclick	= function(){return false;};

	button.observe( 'click',

		function(event) {
			if(el.getWidth() == 0) {
				el.morph('width: ' + width + 'px;');
				el.appear();
//				$(el.firstChild.firstChild).appear();
			} else {
				el.morph('width: 0px;');
				el.fade();
//				$(el.firstChild.firstChild).fade();
			}
		}.bind(button, el, width)

	);

	if(close == false) {
		el.morph('width: 0px');
	}
}

/**
 * initMenuReseau
 */
function initMenuReseau() {	
	initSideMenu($('plus'), $$('#ssmenu-reseau ul')[0], 282);

	$$('#ssmenu-reseau ul li a').each(
		function(el){
			el.onclick	= function(){return false;};
			el.observe('click',
				function() {
					ajaxLayerArticle(el.href + '&ajax=oui');
				}.bind(el)
			);
		}
	);
}

/**
 * initialise le menu campaillette
 */
function initMenuCampaillette() {

	new morphAction($('plus'), $('contentListe'), {eventOpen: 'click', eventClose: 'click', transitionOpen: Effect.Transitions.linear, durationOpen: 0.5});
	new morphAction($('btn-suivant'), $('suivantContent'), {eventOpen: 'click', eventClose: 'click', transitionOpen: Effect.Transitions.linear, fixedSize: 134, durationOpen: 0.5});

//	initSideMenu($('plus'), $('contentListe'), 240);
//	initSideMenu($('btn-suivant'), $('suivantContent'), 134);

	$$('ul.pictos li a').each(
		function(el){
			el.onclick	= function(){return false;};
			el.observe('click',
				function() {
					ajaxLayerArticle(el.href + '&ajax=oui');
				}.bind(el)
			);
		}
	);
}

/**
 * initialise le menu campaillou
 */
function initMenuCampaillou() {
	new morphAction($('plus'), $('contentListe'), {eventOpen: 'click', eventClose: 'click', transitionOpen: Effect.Transitions.linear, durationOpen: 0.5, fixedSize: 480});
	new morphAction($('btn-suivant'), $('suivantContent'), {eventOpen: 'click', eventClose: 'click', transitionOpen: Effect.Transitions.linear, fixedSize: 134, durationOpen: 0.5});
//	initSideMenu($('plus'), $('contentListe'), 480);
//	initSideMenu($('btn-suivant'), $('suivantContent'), 134);

	$$('ul.pictos li a').each(
		function(el){
			el.onclick	= function(){return false;};
			el.observe('click',
				function() {
					ajaxLayerArticle(el.href + '&ajax=oui');
				}.bind(el)
			);
		}
	);

}

/***************************************************************
 *
 *	Initialisation commune à toutes les pages
 *
 ***************************************************************/

/**
 * initialisation commune
 */
function initCommon() {

	$('layerLoader').setStyle({opacity: 0.5, zIndex: 100});

	Ajax.Responders.register({
		onCreate: displayLoader,
		onSuccess: hideLoader,
		onComplete: hideLoader
	});

	initMenu();
	initGoogleMap();
	initFooter();
	//initGame();
}

/**
 * initialisation du menu
 */
function initMenu() {
}

var	address	= 'paris france';
/**
 * initialisation du formulaire de recherche googlemap
 */
function initGoogleMap() {

	$('address').observe('click', function(){$('address').value='';});
	$('address').observe('blur', function(){ if($('address').value==''){$('address').value='Entrez vos informations ici';}});

//	closeGMAP();
	$('localiser').fade({queue: 'end', duration: 0.1});
	$('localiser').morph({height: '0px', top: '671px'}, {duration: 0.1});
	$('map1').morph({height: '0px'}, {duration: 0.1});


	$('valideGMAP').observe('click', openGMAP);
	$('valideGMAP').onclick	= function(){return false;};

	$('closeMap').onclick	= function(){return false;};
	$('closeMap').observe('click', closeGMAP);
}

/**
 * initialise les liens du footer
 */
function initFooter() {
	var	linkMention	= $('linkMention');
	var	urlMention	= linkMention.href + '&ajax=oui';

	linkMention.onclick	= function(){return false;};
	linkMention.observe('click',

		function(){
			ajaxLayerGlobal(urlMention, initMentions);
		}.bind(urlMention)
	);

	var	linkContact	= $('linkContact');
	var	urlContact	= linkContact.href + '&ajax=oui';

	linkContact.onclick	= function(){return false;};
	linkContact.observe('click',
		function(){
			ajaxLayerGlobal(urlContact, initContact);
		}.bind(urlContact)
	);

	var	linkEspace	= $('linkEspace');
	var	urlEspace	= linkEspace.href + '&ajax=oui';

	linkEspace.onclick	= function(){return false;};
	linkEspace.observe('click',

		function(){
			ajaxLayerGlobal(urlEspace);
		}.bind(urlEspace)
	);
}

/**
 * Initialisation du jeu
 */
function initGame() {
	new Ajax.Updater('page', 'spip.php?page=jeu',
		{
			insertion	: 'bottom',
			onComplete	: initLinkGame
		}
	);
}

/**
 * initialise les liens du jeu
 */
function initLinkGame() {
	initSideMenu($('btn-jeu'), $('contenu_jeu'), 134, true);

	var	linkGame	= $('link-jeu');
	var	urlGame		= linkGame.href + '&ajax=oui';
	
	linkGame.onclick	= function(){return false;};
	linkGame.observe('click',
		function(){
			ajaxLayerFlash(urlGame, initFlashGame);
		}.bind(urlGame)
	);
}

/**
 * Initialise le flash du jeu
 */
function initFlashGame() {
	var flashvars = {};
	var params = {
		menu: "false",
		scale: "noScale",
		bgColor: "#3c1101",
		allowScriptAccess: 'always',
		align: "middle",
		showMenu: "false",
		wmode:"transparent"
	};
	var attributes = {id: 'website', name:'website', style:'margin-top: -21px;'};
	swfobject.embedSWF( 'preloader.swf', 'website', '100%', '630px', '9.0.0', 'js/expressinstall.swf', flashvars, params, attributes );
}


/**
 * initialisation de la page contact
 */
function initContact() {

	$$('#menu-contact a').each(

		function(el) {
			el.onclick	= function(){return false;};
			
			el.observe('click', 	function() {new Ajax.Updater('page-annexe', el.href + '&ajax=oui',
					{
						onComplete: function(){initLayerGlobal();initContact();}
					}
				)

			}.bind(el)
			);
		}
	);

	initFormContact();
}

/**
 * initialisation du formulaier de contact
 */
function initFormContact() {

	$('senContact').onclick	= function(){return false;};

	$('senContact').observe( 'click',

		function() {
			$('formContact').request(
				{
					onComplete: function(transport) {$('page-annexe').update(transport.responseText);initLayerGlobal();initContact();}
				}
			)

			return	false;
		}
	);

}

/***************************************************************
 *
 *	Fonctions AJAX
 *
 ***************************************************************/

/**
 * ajaxLayer
 */
function ajaxLayerArticle(url) {

	if(!$('layerArticle')) {
		createLayerArticle();
	}

	new Ajax.Updater('layerArticle', url,
		{
			onComplete: initLayerArticle
		}
	);

}

/**
 * ajaxLayer global
 */
var	currentURLGlobal	= '';
var	layerGlobalAnime	= false;

function ajaxLayerGlobal(url, callBack) {

	if(!$('page-annexe')) {
		createLayerGlobal();
	}

	if(!callBack) {
		callBack	= Prototype.emptyFunction;
	}

	if(	currentURLGlobal != url) {
		closeLayerGlobal();

		function testCallBack() {
			initLayerGlobal();
			callBack();
		}
		
		testCallBack.bind(callBack);

		new Ajax.Updater('page-annexe', url,
			{
				onComplete	: testCallBack
			}
		);
	}

	currentURLGlobal	= url;

}

function ajaxLayerFlash(url, callBack) {

	if(!$('page-annexe')) {
		createLayerGlobal();
	}

	if(	currentURLGlobal != url) {
		closeLayerGlobal();

		function testCallBack() {
			initLayerFlash(callBack);
		}

		testCallBack.bind(callBack);

		new Ajax.Updater('page-annexe', url,
			{
				onComplete	: testCallBack
			}
		);
	}

	currentURLGlobal	= url;

}
/***************************************************************
 *
 *	Les layers
 *
 ***************************************************************/

/**
 * Création du layer article
 */
function createLayerArticle() {
	var	elLayer	= new Element('div', {id: 'layerArticle', style: 'display: none;'});
	elLayer.addClassName('col-texte');
	$('page').insert(elLayer);
}

/**
 * Initialise le contenu du layer article
 */
function initLayerArticle() {

	var	btnClose		= $$('#layerArticle div.fermer a');
	
	if(!btnClose[0]) {
		setTimeout(initLayerArticle, 100);
	} else {
		btnClose			= btnClose[0];
		btnClose.onclick	= function() {return false;};
	
		btnClose.observe('click', closeLayerArticle);
		
		openLayerArticle();
	}
}

/**
 * affiche le layer d'article
 */
function openLayerArticle() {
	$('layerArticle').appear();
	$('layerArticle').morph({left: '0px'});
}

/**
 * affiche le layer d'article
 */
function closeLayerArticle() {
	$('layerArticle').fade();
	$('layerArticle').morph({left: '-364px'});
}

/**
 * Création du layer global
 */
function createLayerGlobal() {
	var	elLayer	= new Element('div', {id: 'page-annexe', style: 'display: none;'});
	$('page').insert(elLayer);
}

/**
 * Initialise le contenu du layer global
 */
function initLayerGlobal() {
	var	btnClose		= $$('#page-annexe div.fermer a')[0];

	btnClose.onclick	= function() {return false;};

	btnClose.observe('click', closeLayerGlobal);
	openLayerGlobal();
}

/**
 * affiche le layer global
 */
function openLayerGlobal() {
	if(layerGlobalAnime === true) {
		setTimeout(openLayerGlobal, 100);
	} else {
		layerGlobalAnime	= true;
		$('page-annexe').appear({queue: 'end', afterFinish: function(){layerGlobalAnime = false;}});
		$('page-annexe').morph({left: '17px'}, {afterFinish: function(){layerGlobalAnime = false;}});
	}
}

/**
 * affiche le layer global
 */
function closeLayerGlobal() {

	if(layerGlobalAnime === true) {
		setTimeout(closeLayerGlobal, 100);
	} else {
		currentURLGlobal	= '';
		layerGlobalAnime	= true;
		$('page-annexe').fade({queue: 'end', afterFinish: function(){layerGlobalAnime = false;}});
		$('page-annexe').morph({left: '-987px'}, {afterFinish: function(){layerGlobalAnime = false;}});
	}
}


/**
 * Initialise le contenu du layer flash
 */
function initLayerFlash() {
	var	btnClose		= $$('#page-annexe div.fermer a')[0];

	btnClose.onclick	= function() {return false;};

	btnClose.observe('click', closeLayerFlash);
	openLayerFlash();
}

/**
 * affiche le layer global
 */
function openLayerFlash() {
	if(layerGlobalAnime === true) {
		setTimeout(openLayerFlash, 100);
	} else {
		layerGlobalAnime	= true;
		$('page-annexe').appear({queue: 'end', afterFinish: function(){layerGlobalAnime = false;}});
		$('page-annexe').morph({left: '17px'}, {afterFinish: function(){layerGlobalAnime = false;initFlashGame();}});
	}
}

/**
 * affiche le layer global
 */
function closeLayerFlash() {

	if(layerGlobalAnime === true) {
		setTimeout(closeLayerFlash, 100);
	} else {
		currentURLGlobal	= '';
		layerGlobalAnime	= true;

		$('website').remove();

//		$('website').fade(
//			{
//				afterFinish : function() {
						$('page-annexe').fade({queue: 'end', afterFinish: function(){layerGlobalAnime = false;}});
						$('page-annexe').morph({left: '-987px'}, {afterFinish: function(){layerGlobalAnime = false;}});
//				},
//
//				duration	: 0.2
//			}
//		);
	}
}


/**
 * ouvre le layer Google map
 */
function openGMAP() {
	address	= $('address').getValue();
	
	$('localiser').appear({queue: 'end'});
	$('localiser').morph({height: '409px', top: '262px'});
	$('map1').morph({height: '409px'}, {afterFinish: load1});

//        setTimeout(function(){initLiensBoulanger();}, 3000);
//        initLiensBoulanger.defer();

      	var scrollBoulanger = new Control.ScrollBar('listLocation', 'scrollbar_track_list');
        setTimeout(function(){scrollBoulanger.recalculateLayout();}, 2000);

}

/**
 * ferme le layer Google map
 */
function closeGMAP() {
	$('localiser').fade({queue: 'end'});
	$('localiser').morph({height: '0px', top: '671px'});
	$('map1').morph({height: '0px'});

}


/**
 * initialisation du fil d'actu
 */
function initFilActu() {

	var	actus	= $$('#contenuActu div.actuItem');

	if(actus.size() > 1) {

		new Effect.ScrollHorizontal('divActu',
			{
				duration: 10,
				queue: {scope: 'filActu'},
				from: (actus[0].positionedOffset().left),
				to: (actus[1].positionedOffset().left),
				afterFinish: nextActu,
				transition: Effect.Transitions.linear
			}
		);
	}
}

/**
 * prochain element fil d'actu
 */
function nextActu() {
	var	actus	= $$('#contenuActu div.actuItem');
	var first	= actus[0];
	
	first.remove();
	$('scrollActu').insert(first);

	actus	= $$('#contenuActu div.actuItem');
	if(actus.size() > 1) {

		new Effect.ScrollHorizontal('divActu',
			{
				duration: 10,
				queue: {scope: 'filActu'},
				from: (actus[0].positionedOffset().left),
				to: (actus[1].positionedOffset().left),
				afterFinish: nextActu,
				transition: Effect.Transitions.linear
			}
		);
	}

}



/**
 * affiche le layer de loader ajax
 */
function displayLoader() {
	var	layerLoader	= $('layerLoader');
	
//	$$('#layerLoader img')[0].setStyle({marginTop: 50%});

	layerLoader.appear({to: 0.5, duration: 0.2});

}

/**
 * cache le layer de loader ajax
 */
function hideLoader() {
	$('layerLoader').fade({to: 0, duration: 0.2});
}



/**
 * initialisation de la page mentions légale
 */
function initMentions() {
	
	var scrollMention = new Control.ScrollBar('contentMentions', 'scrollbar_track_mentions');

	setTimeout(function(){scrollMention.recalculateLayout();}, 2000);
}

/**
 * charge une fiche boulanger
 */
function loadFiche(url) {

    new Ajax.Updater('ficheBoulanger', url,
            {
                method: 'get',
                  onComplete: initFicheBoulanger
            }
    );
  
}

/**
 * initialise la fiche boulanger
 */
function initFicheBoulanger() {
	var	btnClose		= $$('#fiche-preview div.fermer a');

	if(!btnClose[0]) {
		setTimeout(initFicheBoulanger, 100);
	} else {
		btnClose			= btnClose[0];
		btnClose.onclick	= function() {return false;};

		btnClose.observe('click', function(){$('fiche-preview').fade({afterUpdate: function(){$('fiche-preview').setStyle({zIndex: 90});}});});

	    $('fiche-preview').appear({afterUpdate: function(){$('fiche-preview').setStyle({zIndex: 90});}});
		
		initScrollFiche();

//		new Control.ScrollBar('contenu-scroll', 'scrollbar_track1');

	}
}

function initScrollFiche() {

	if($('contentFiche')) {
//		alert('ok');
		var scrollMention = new Control.ScrollBar('contentFiche', 'scrollbar_track_fiche');
		setTimeout(function(){scrollMention.recalculateLayout();}, 2000);
	} else {
		setTimeout(initScrollFiche, 500);
	}
}

/**
 * Comment
 */
function initLiensBoulanger() {

	$$('a.linkFiche').each(
		function(el){
			el.onclick	= function(){return false;};

			el.observe('click',
				function() {
					loadFiche(el.href + '&ajax=oui');
				}.bind(el)
			);
		}
	);
}

