function Forcer_Fermeture_Trie()
{
	var FX_Trie_Mvt = new Fx.Morph($('Menu_Deroulant_Trie_Wrapper'), {link:'cancel'});
	FX_Trie_Mvt.start( {
					'height' : 0
						});
	FX_Trie_Mvt.addEvent('complete' ,function () { $('Menu_Deroulant_Trie_Wrapper').setStyle('display' , 'block'); });
}

//////////////////////////////////////
// Flottant Espace Client	- DEBUT //
//////////////////////////////////////
function Flottant_Espace_Client()
{
	if ($('Espace_Client_Ouverture'))
	{ 
		FX_Espace_Client_Ouvre = new Fx.Morph($('Flottant_Espace_Client_Contenu'), {link:'cancel'});
		$('Espace_Client_Ouverture').addEvent('click' , function () {
			FX_Espace_Client_Ouvre.start({ 'margin-left': [0] });
			Validation_Formulaire_Ajax();
		});
		
		
		FX_Espace_Client_Ouvre.addEvent('start' , function () { 
			$('Flottant_Espace_Client').setStyle('display' , 'block'); 
			
			FX_Espace_Client_Ferme = new Fx.Morph($('Flottant_Espace_Client_Contenu'), {link:'cancel'});
			$('Espace_Client_Fermeture').addEvent('click' , function () {
				FX_Espace_Client_Ferme.start({ 'margin-left': [275] });
			});
			
			FX_Espace_Client_Ferme.addEvent('complete' , function () { document.location = document.location; $('Flottant_Espace_Client').setStyle('display' , 'none'); } );	
		});
		
		
	}
}

//////////////////////////////////////
// Flottant Espace Client	- FIN //
//////////////////////////////////////
  
  
//////////////////////////////////////
// Validation formulaire Ajax	- DEBUT //
//////////////////////////////////////
function Validation_Formulaire_Ajax()
{	
	if ($('login_espace_client'))
	{
		$('login_espace_client').addEvent('submit', function(e) {
			//On stop l'execution
			e.stop();
			//On vide le DIV qui réceptionne et on met l'image d'attente
			var log = $('FormBox');
			//Set the options of the form's Request handler. 
			//("this" refers to the $('myForm') element).
			this.set('send', {onComplete: function(response) { 
				log.set('html', response);
				// On rappel l'initialisation des éléments JS pour réactiver les ouvertures / fermetures
								
				// Mise à jour de l'espace d'accès à mon compte (masqué actuellement)
				MAJ_Espace_Connexion = function () {ajax( root_url + 'ajax/ajax_Encart_Recherche.php' , $('Encart_Recherche') );}
				MAJ_Espace_Connexion.delay(500);
				
				// On lance la fermeture delayée du flottant
				Fermeture_Flottant_Espace_Client = function () { 
					Flottant_Espace_Client();
					FX_Espace_Client_Ferme.start({ 'margin-left': [275] }); 
					FX_Espace_Client_Ferme.addEvent('complete' , function () { ajax( root_url + 'ajax/ajax_FormBox.php' , $('FormBox') ); });
				}
				Fermeture_Flottant_Espace_Client.delay(2000);
				
			}});
			
			this.send();
			
			
		});
	}
}
//Validation_Formulaire_Ajax();
//////////////////////////////////////
// Validation formulaire Ajax	- FIN //
//////////////////////////////////////
	
window.addEvent('domready', function(){

	Flottant_Espace_Client();

	var myTips = new Tips('.thisisatooltip' , {className:'BulleJS' , fixed:true , offsets:{'x':10 , 'y':-13}});
	
	//////////////////////////////////////
	// Trie des listes produits - DEBUT //
	//////////////////////////////////////
	if ($$('a.Menu_Deroulant_Trie_Ouvre'))
	{
		
		var FX_Trie_Mvt = new Fx.Morph($('Menu_Deroulant_Trie_Wrapper'), {link:'cancel'});
		
		$$('a.Menu_Deroulant_Trie_Ouvre').each(function(el){ 
				el.addEvent('click' , function () {
			
				FX_Trie_Mvt.start( {
							'display' : 'block' ,
							'height' : '50'
								});
				return false;
			});
			
		});
		
		$$('a.Menu_Deroulant_Trie_Close').each(function(el){ 
				el.addEvent('click' , function () {
			
				FX_Trie_Mvt.start( {
								'height' : 0
									});
				FX_Trie_Mvt.addEvent('complete' ,function () { $('Menu_Deroulant_Trie_Wrapper').setStyle('display' , 'block'); });
				return false;
				
			});
			
		});
	}
	
	//////////////////////////////////////
	// Trie des listes produits - FIN //
	//////////////////////////////////////
	
	
	// Sélection visuelle d'un nuancier
	if ($('liste_nuancier'))
	{
		$$('a.selectionNuancierElement').each(function(el){ 
			el.addEvent('click' , function () {
				$$('a.selectionNuancierElement').each(function(el2){ el2.setStyle('border' , '1px solid #333'); el2.setStyle('width' , '20px'); el2.setStyle('height' , '20px'); });
				el.setStyle('border' , '2px solid #FC0');  el.setStyle('width' , '18px'); el.setStyle('height' , '18px');
			});
		});
	}
	
	if ( $('pagePromo') )
	{
		$$('a.bandeauSpecial').each( function (el) {
											   el.addEvent('mouseenter', function () { $$('a.bandeauSpecial').each( function (el2) { el2.setStyle('opacity' , '0.8');}); el.setStyle('opacity' , '1'); });
											   el.addEvent('mouseleave', function () { $$('a.bandeauSpecial').each( function (el3) { el3.setStyle('opacity' , '1');}); });
		});
	}
	
	if ($('data'))
	{
		mysort = new Fx.Sort($$('div.Catalogue_Produit'), {
		  transition: Fx.Transitions.Back.easeInOut,
		  duration: 1000
		});
		
		// Pagination des commentaires	
		myPaginator = new TomPaginator($$('div.Catalogue_Produit') , {commsParPages: 12} );
		myPaginator.allerPage(1);
	}
	
	if ($('addFavorite'))
	{
		$('addFavorite').addEvent('click' , function (e) {
			e.stop();
			new Request.HTML({url:root_url+'ajax/ajaxSetFavorite.php'}).post('id='+$('addFavorite').get('rel'));
		});
	}
	
	if ($('flottant'))
	{
		var fxFlottant = new Fx.Morph($('flottant') , {link:'cancel'});
		$$('a.afficher-flottant').each( function (el) {
		
		
			el.addEvent('click' , function (e) {
				e.stop();
				$$('a.afficher-flottant').each( function (a) { a.setStyles({'background-color':'transparent','font-weight':'normal'}); });
				$$('#flottant div.content').each( function (myDiv) { 
					if (myDiv.get('id') != el.get('rel'))	myDiv.setStyle('display','none'); 
				});
				el.setStyle('background-color','#EDEDED');
				el.setStyle('font-weight','bold');
				fxFlottant.start( {'margin-right':'0'} );
			});
		});
		
		$('fermer-flottant').addEvent('click' , function (e) {
				e.stop();
				fxFlottant.start( {'margin-right':'-500px'} );
				$$('a.afficher-flottant').each( function (el) { el.setStyles({'background-color':'transparent','font-weight':'normal'}); });
			});
		
		$('link-favoris').addEvent('click' , function (e) { 
			e.stop(); 
			$('liste-favoris').setStyle('display','block'); 
		});
		$('link-commande-rapide').addEvent('click' , function (e) { 
			e.stop(); 
			$('commande-rapide').setStyle('display','block'); 
		});
		$('link-generer-panier').addEvent('click' , function (e) { 
			e.stop(); 
			$('generer-panier').setStyle('display','block'); 
		});
		$('link-contenu-panier').addEvent('click' , function (e) { 
			e.stop(); 
			$('contenu-panier').setStyle('display','block'); 
		});
		
		// Commande rapide
		$('rapide-ref').addEvent('click' , function (e) { 
			e.stop(); 
			$('rapide-ref').value = '';
			$('rapide-ref').setStyles( {'font-style':'normal' , 'color':'#000'} ); 
		});
		
		$('rapide-ref').addEvent('keyup' , function () {
  			if ($('rapide-ref').value.length > 0)
  			{
  				$('autosuggestion').setStyle('display','block');
  				$('autosuggestion').load(root_url + 'ajax/ajaxListeReferences.php?ref='+$('rapide-ref').value);
  			}
  			else
  			{
  				$('autosuggestion').setStyle('display','none');
  			}
  		});
  		
  		$('set-quantite-input').addEvent('keyup' , function () {
  			if ($('set-quantite-input').value.length > 0 && parseInt($('set-quantite-input').value) > 0) 	
  			{
  				$('rapide-valider').setStyle('display','block');
  				$('total-rapide').innerHTML = 'Total : '+ ($('puHidden').value * parseInt($('set-quantite-input').value)).toFixed(2) +' € HT' ;
  			}
  			else 	
  			{
  				$('rapide-valider').setStyle('display','none');
  				$('total-rapide').innerHTML = '';
  			}
  		});
  		
  		$('ajouter-panier-rapide').addEvent('click' , function (e) {
  			e.stop();
  			if ( $('idHidden').value != '' && $('refHidden').value != '' && $('set-quantite-input').value != '')
  			{
  				$('ajax-data').load(root_url + 'ajax/ajaxAjouterPanier.php?id_produit='+$('idHidden').value+'&reference_produit='+$('refHidden').value+'&quantite='+$('set-quantite-input').value);
  			}
  		});
  		
  		
	}
	
	if ($('head-commercial'))
	{
		$('head-commercial').set('morph', {duration: 'long', transition: 'bounce:out'});
		( function() { $('head-commercial').morph({'margin-top':'-30px'}); }).delay(1500);
	}
	
});

//////////////////////////////////////
// FONCTION AJAX	- DEBUT //
//////////////////////////////////////
function ajax(url_cible,id_cible)
{
	var req = new Request.HTML({url:url_cible, 
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$(id_cible).set('text', '');
			//if (noloading) { $(id_cible).addClass('ajax-loading'); }
			//Inject the new DOM elements into the results div.
			$(id_cible).adopt(html);
			
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$(id_cible).set('text', 'The request failed.');
		}
	});
	
	req.send();
}

function ajax_simple(url , id)
{ 
	var req = null; 
	
	if(window.XMLHttpRequest) req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				
				document.getElementById(id).innerHTML=req.responseText;
				
			}	
			else	
			{
				document.getElementById(id).innerHTML = req.status + " " + req.statusText;
			}	
		} 
	}; 
	req.open("GET", url, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null);

} 
//////////////////////////////////////
// FONCTION AJAX	- FIN //
//////////////////////////////////////


//////////////////////////////////////
// FONCTION VALIDER RECHERCHE	- DEBUT //
//////////////////////////////////////
function lancer_recherche()
{
	if ($('input_recherche').value.length < 4) 
	{
		alert('Votre recherche doit faire au minimum 4 caractères');
	}
	else
	{
		$('form_recherche').submit();
	}
}
//////////////////////////////////////
// FONCTION VALIDER RECHERCHE	- FIN //
//////////////////////////////////////


 // Débug de Mootools pour les navigateurs basés sur WebKit
    Request.HTML.implement({
 
        processHTML: function(text){
            var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
            text = (match) ? match[1] : text;
           
            var container = new Element('div');
           
            return $try(function(){
                var root = '<root>' + text + '</root>', doc;
                if (Browser.Engine.trident){
                    doc = new ActiveXObject('Microsoft.XMLDOM');
                    doc.async = false;
                    doc.loadXML(root);
                } else {
                    doc = new DOMParser().parseFromString(root, 'text/html');
                }
                root = doc.getElementsByTagName('root')[0];
                for (var i = 0, k = root.childNodes.length; i < k; i++){
                    var child = Element.clone(root.childNodes[i], true, true);
                    if (child) container.grab(child);
                }
                return container;
            }) || container.set('html', text);
        }
   
    });
 
 
 function changerPage( url )
 {
	 var location = document.location.toString()
	 location = location.replace("#","");
	 document.location = location + url;
 }
 
 function trierListeProduit( cle , ordre )
 {
 	new Request.HTML({url:root_url+'ajax/ajaxGetOrderProduits.php'}).post('data='+$('data').get('text')+'&cle='+cle+'&ordre='+ordre);
 }
 
 function ajouterPanier(id)
 {
 	var quantite = prompt('Merci d\'indiquer la quantité que vous souhaitez ajouter à votre panier : ');
 	$('ajax-data').load(root_url + 'ajax/ajaxAjouterPanier.php?id_produit='+parseInt(id));
 }
