window.addEvent('domready', function() {
	callAfterAjax();
	
	if ($('NewsVertical')) {
		newsTicker = tickerBoxInit('TickerVertical',65,'v','li', 400, 6000);
	}
	
});

//fonction d'affectation des événements (accordéon, scrollbar...)
function callAfterAjax() {
	//smooth scroll sur les ancres
	var mySmoothScroll = new Fx.SmoothScroll();

	/**************************************************************/
	/***************** Gestion du menu en accordéon ***************/
	/**************************************************************/
	if ($('menu')) {
		var accordion1 = new Accordion($$('h1.toggler'),$$('div.niv1'),{
			"onComplete": function() {
				this.elements.each(function(el) {
					if(el.getStyle('height').toInt() != 0) {
						el.setStyle('height','auto');
					}
				});
			},
			"onActive": function() {
			   this.elements.each(function(el) {
					if(el.getStyle('height') == 'auto') {
						el.setStyle('height',el.getSize().y+"px");
					}
				});
			},
			opacity: false,
			alwaysHide: false,
			display: -1
		});
		
		/*//hover effect
		$$('h1.toggler').each(function(e){
			e.addEvent('mouseenter', function() { this.fireEvent('click'); }); 
		});*/

		//réouverture de la catégorie et sous catégorie
		$$('.menuFamActiv').each(function(acc) {
			//récup du numéro d'accordéon niv1 à réouvrir -> id = "menu2", on recup le 2
			var nb1 = $(acc).parentNode.parentNode.id.substring(4, $(acc).parentNode.parentNode.id.length); //alert(nb1);
			accordion1.display(nb1-1);
		});
		
		
		//navigation, on recharge la div "cntnrPage"
		$$('h1.toggler a').each(function(e){
			var url = e.id;
			if (url!="") {
				e.addEvent('click', function() { 
					affPage(url);
				});
			}
		});
		
		//navigation, on recharge la div "cntnrPage"
		$$('.ssnav1 a').each(function(e){
			var url = e.id;
			if (url!="") {
				e.addEvent('click', function() { 
					affPage(url);
					$$('.ssnav1 a').each(function(el){
						el.className=""; 
					});
					e.className="menuFamActiv";
				});
			}
		});
	}


	/**************************************************************/
	/*************** Gestion de l'accordéon annonces **************/
	/**************************************************************/
	if ($('accordion')) {
		var monTxt = $('accordion').className; // le nom de la class correspond en fait au texte affiché quand on clique sur les accordéons
		var accordion2 = new Accordion($$('h3.toggler'),$$('div.element'),{
			opacity: false,
			alwaysHide: true,
			display: -1,
			onActive: function(toggler, element){
				toggler.innerHTML='<span class="puceUpDown"></span>MASQUER LES '+monTxt;
				toggler.getElement('span').setStyle('background-position', '0 -12px');
			},
			onBackground: function(toggler, element){
				toggler.innerHTML='<span class="puceUpDown"></span>AFFICHER LES '+monTxt;
				toggler.getElement('span').setStyle('background-position', '0 0');
			}
		});
	}

	
	/**************************************************************/
	/**************** Gestion du scrollbar vertical  **************/
	/**************************************************************/
	if ($('content1')) {
		makeScrollbar( $('content1'), $('scrollbar1'), $('handle1') );
		callAfterAjaxBook();
	}

	if ($('distriListing')) {
		makeScrollbar( $('distriListing'), $('scrollbar1'), $('handle1') );
	}
	
	/**************************************************************/
	/****************   Gestion des formulaires      **************/
	/**************************************************************/
	if ($('exA')){
    	var exValidatorA = new fValidator("exA", {language: "fr"});  
	}
	if ($('exB')){
    	var exValidatorB = new fValidator("exB", {language: "fr"});  
	}
	if ($('formContact')){
    	var exValidatorC = new fValidator("formContact", {language: "fr"});  
		$('formContact').addEvent('submit', function(e){
			e.stop();
			var req = new Request.HTML({
				method: 'post',
				url: 'agence/envoiContact.php',
				onRequest: function() { $('ajax-loading').innerHTML='<img src="images/commun/ajax-loader.gif" alt="loading" />'; },
				update: $('ajax-reponse'),
				onComplete: function() { $('ajax-loading').innerHTML=''; callAfterAjax(); }
			}).send(this.toQueryString());
		});
	}
	/*if ($('formDevis')){
    	var exValidatorC = new fValidator("formDevis", {
			language: "fr",  
			"onValid" : function(e){
				e.stop();
				var req = new Request.HTML({
					method: 'post',
					url: 'agence/envoiDevis.php',
					onRequest: function() { $('ajax-loading').innerHTML='<img src="images/commun/ajax-loader.gif" alt="loading" />'; },
					update: $('ajax-reponse'),
					onComplete: function() { $('ajax-loading').innerHTML=''; callAfterAjax(); }
				}).send(this.toQueryString());
			}
		});
	}*/

	if ($('formDevis')){
    	var exValidatorC = new fValidator("formDevis", {language: "fr"});  
		$('formDevis').addEvent('submit', function(e){
			e.stop();
			var req = new Request.HTML({
				method: 'post',
				url: 'agence/envoiDevis.php',
				onRequest: function() { $('ajax-loading').innerHTML='<img src="images/commun/ajax-loader.gif" alt="loading" />'; },
				update: $('ajax-reponse'),
				onComplete: function() { $('ajax-loading').innerHTML=''; callAfterAjax(); }
			}).send(this.toQueryString());
		});
	}


	/**************************************************************/
	/****************   Gestion des adresses email   **************/
	/**************************************************************/
	// on remplace <span id="joanny-piste-noire"></span>
	// par <a href='mailto:joanny@piste-noire.com>joanny@piste-noire.com</a>
	if ($('joanny-piste-noire')){
    	$('joanny-piste-noire').innerHTML="<a href='mailto:joanny@piste-noire.com>joanny@piste-noire.com</a>";  
	}
	if ($('info-akkro')){
    	$('info-akkro').innerHTML="<a href='mailto:info@akkro.com>info@akkro.com</a>";  
	}
	if ($('info-piste-noire')){
    	$('info-piste-noire').innerHTML="<a href='mailto:info@piste-noire.com>info@piste-noire.com</a>";  
	}
	if ($('aline-piste-noire')){
    	$('aline-piste-noire').innerHTML="<a href='mailto:aline@piste-noire.com>aline@piste-noire.com</a>";  
	}
	if ($('david-piste-noire')){
    	$('david-piste-noire').innerHTML="<a href='mailto:david@piste-noire.com>david@piste-noire.com</a>";  
	}
	if ($('sophie-piste-noire')){
    	$('sophie-piste-noire').innerHTML="<a href='mailto:sophie@piste-noire.com>sophie@piste-noire.com</a>";  
	}
	if ($('jerome-piste-noire')){
    	$('jerome-piste-noire').innerHTML="<a href='mailto:jerome@piste-noire.com>jerome@piste-noire.com</a>";  
	}
	if ($('contactez-nous')){
    	$('contactez-nous').innerHTML="<a href='mailto:joanny@piste-noire.com>contactez-nous</a>";  
	}
}

//fonction d'affectation des événements (book)
function callAfterAjaxBook() {

		//effet roll over opacité sur les images
		// + rechargement du visuel en ajax
		$('content1').getElements('a').each(function(el) {
			var visu = el.getElement('img'); //alert (visu);
			var quel = el.id.substring(4, el.id.length);
			el.addEvents({
				'mouseenter': function(){
					visu.className = 'visuOn';
				},
				'mouseleave': function(){
					visu.className = 'visuOff';
				},
				'click': function(){
					//affBook(quel);		//LA CA BUG GRAVE !! je sais pas pourquoi !!
					$$('#content1 a').each(function(e){
						e.className=""; 
					});
					el.className="lienVisuActif";
				}
			});	
		});
		
		//idem pour les boutons suivant / précédent
		$('bookPagin').getElements('a').each(function(el) {
			var quel = el.id.substring(4, el.id.length); 
			var lien = 'visu'+quel;
			el.addEvent(
				'click', function(){
					affBook(quel);
					$$('#content1 a').each(function(e){
						e.className=""; 
					});
					$(lien).className="lienVisuActif";
				}
			);	
		});
}

/**************************************************************/
//Création d'un scrollbar sur une DIV
/**************************************************************/
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}



/**************************************************************/
//NAVIGATION -> fonction pour afficher le contenu de la page dans la div principale "cntnrPage"
/**************************************************************/
function affPage(url) {
        //make the ajax call, replace text
        var req = new Request.HTML({
            method: 'get',
            url: 'agence/affPage.php',
			//evalScripts: true,
            data: { 'url' : url },
			//onRequest: function() { alert('Request made. Please wait...'); },
			update: $('cntnrPage'),
			onComplete: function() { callAfterAjax(); // on réaffecte les événements javascript 
			}
        }).send(); 
}


/**************************************************************/
//NAVIGATION -> fonction pour afficher les references et la pagination dans la div "book"
/**************************************************************/
function affBook(idBook) {
        //make the ajax call, replace text
       var req = new Request.HTML({
            method: 'get',
            url: 'agence/affBook.php',
            data: { 'idBook' : idBook },
			onRequest: function() { $('loadingBook').setStyle('visibility', 'visible'); },
			update: $('book'),
			onComplete: function() { $('loadingBook').setStyle('visibility', 'hidden'); }
        }).send(); 
		
        //make the ajax call, replace text
        var req2 = new Request.HTML({
            method: 'get',
            url: 'agence/affPagination.php',
            data: { 'idBook' : idBook },
			onRequest: function() { $('loadingBook').setStyle('visibility', 'visible'); },
			update: $('bookPagin'),
			onComplete: function() { 
				$('loadingBook').setStyle('visibility', 'hidden');
				callAfterAjaxBook(); // on réaffecte les événements javascript 
			}
        }).send();
}


/**************************************************************/
//Mis à jour du listing de distributeur des magazines dans la div "distriListing"
/**************************************************************/
function affSecteur(idSecteur) {
        //make the ajax call, replace text
        var req = new Request.HTML({
            method: 'get',
            url: 'agence/affSecteur.php',
            data: { 'idSecteur' : idSecteur }, 
			update: $('distriListing')
        }).send();
}



