google.load("prototype", "1.6");
google.load("scriptaculous", "1.8.1");

/***************************************************************************************/
/***************************************************************************************/
function baobazOnLoad(){
		
		$$('body').invoke('removeClassName','nojs');	
		
		
		if($('home')) {
			
			//main nav
			$$('#mainNav ul li a').invoke('observe','click',popupLinkClicked);
			$$('#mainNav .popup').invoke('hide');
			$$('#mainNav .popup .close').invoke('observe','click',popupCLoseClicked);
		
		
			//slider
			window.nbSlide = $$('#rightSlider .slide').size();
		
			window.currentSlide = 1;
			updateCompteur();
		
			$$('#rightSlider .slide').invoke('hide');
			$$('#rightSlider .slide').first().show();
			
			$('slideprev').observe('click',slidePrev);
			$('slidenext').observe('click',slideNext);
		
			window.pSlide = new PeriodicalExecuter(slideNext, 15);	
			
			if($('ad_overlay')) {
				$('ad_overlay').setStyle({opacity: 0, display: 'block'});
				$('ad_overlay').fade({ duration: 0.5, from: 0, to: 1})
				$('ad_screen').setStyle({'height':document.viewport.getHeight()+'px'});
				$('ad_screen').observe('click',function(){$('ad_overlay').fade({ duration: 0.5, from: 1, to: 0})});
				$$('#ad_overlay .close').invoke('observe','click',function(){$('ad_overlay').fade({ duration: 0.5, from: 1, to: 0})});
				
				 var fade=setTimeout("$('ad_overlay').fade({ duration: 0.5, from: 1, to: 0})",20000);
			}
		
		
		
		//navigation ajax
		$('bigpopupclose').observe("click", bigPopupClose);
		$$('.aj').invoke('observe',"click", ajaxLinkClicked);
		
		
		if(History.get('page') != "") {
			lnk = $$('a[rel='+History.get('page')+']');
			if(lnk.length > 0) {	
				var ajF = ajaxLinkClicked.bind(lnk[0]);
				ajF(null);
			}
		}
	}
		else {
		
		//hack pour les pages "cloakés"
		$$('.aj').invoke('observe',"click", ajaxLinkRedirectClicked);
		
		
		}

		
}

function reinitContent() {
	$$('.ajform').invoke('observe',"submit", ajaxFormSubmit);
}
/***************************************************************************************/
/***************************************************************************************/
function popupLinkClicked(evt) {
	evt.preventDefault();
	var popup = $(this.readAttribute('rel'))

	new Effect.Parallel(
    [new Effect.Move(popup, {x: 0, y: 100, sync: true }),
     new Effect.Fade(popup, { sync: true, from:0, to: 1 }) ],
		{ duration: 1, beforeSetup: function(effect) {
          effect.effects[0].element.setStyle({ top: '-100px', 'display' : 'block', 'opacity':0 });
        }
    });
}
/***************************************************************************************/
/***************************************************************************************/
function popupCLoseClicked(evt) {
  this.up('.popup').dropOut({ duration: 1.0});
}
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
function slidePrev(evt) {
	var next = (window.currentSlide-1 >= 1)?window.currentSlide-1:window.nbSlide;
	slide(window.currentSlide,next);
  evt.preventDefault();
}
/***************************************************************************************/
/***************************************************************************************/
function slideNext(evt) {
	var next = (window.currentSlide+1 <= window.nbSlide)?window.currentSlide+1:1;
	slide(window.currentSlide,next);
  evt.preventDefault();
}
function slideNextPeriod() {
	var next = (window.currentSlide+1 <= window.nbSlide)?window.currentSlide+1:1;
	slide(window.currentSlide,next)
}
/***************************************************************************************/
/***************************************************************************************/
function slide(current, next) {
	
	if(window.currentlySlidding != true) {
		window.currentlySlidding = true;
		window.pSlide.stop();
		window.currentSlide = next;
		updateCompteur();
		$('slide'+current).fade({duration:0.5});
		$('slide'+next).appear({duration:0.5, afterFinish:function(){window.currentlySlidding = false; window.pSlide = new PeriodicalExecuter(slideNext, 15);	 }});
	}
}
/***************************************************************************************/
/***************************************************************************************/
function updateCompteur() {
	$('slidecompteur').update(window.currentSlide+'/'+ window.nbSlide);
}
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
function bigPopupClose(e) {
	$('bigpopup').fade({ duration: 0.5 , afterFinish:function(){$('bigpopupcontent').update('');}});
	if(e != null) Event.stop(e);
	History.unset('page');
}
/***************************************************************************************/
/***************************************************************************************/
function ajaxLinkClicked(e) {
	//console.log(this);
	ajaxHistoryRequest(this.readAttribute('href'), this.readAttribute('rel'));	
	if(e != null) Event.stop(e);
	return false;
}

function ajaxLinkRedirectClicked(e) {
	//console.log(this);
	//console.log($('homelnk').readAttribute('href'));
	window.location = $('homelnk').readAttribute('href')+'/#page='+this.readAttribute('rel');
	//ajaxHistoryRequest(this.readAttribute('href'), this.readAttribute('rel'));	
	if(e != null) Event.stop(e);
	return false;
}

function ajaxFormSubmit(e) {

	if(e != null) Event.stop(e);
	
	this.request({
  method: 'post',
  onSuccess: function(transport){ 
	$('bigpopupcontent').update(transport.responseText);
		reinitContent();
  }
})
	
	
	return false;
}
/***************************************************************************************/
/***************************************************************************************/
function ajaxHistoryRequest(url, myState) 
{
			if(window.currentlyAjax != true) {
				window.currentlyAjax = true;
				
				window.ajaxUrl = url;
					window.myState = myState;
				if($('bigpopup').visible()) {
					//$('bigpopup').setStyle({opacity: 0, display: 'block'});
					$('bigpopupcontent').fade({ duration: 0.1, from: 1, to: 0, afterFinish:properRequest });
					$('bigpopuploader').setStyle({display: 'block'});
				} else {
					$('bigpopup').setStyle({opacity: 0, display: 'block'});
					$('bigpopupcontent').setStyle({opacity: 0, display: 'block'});
					$('bigpopup').fade({ duration: 0.5, from: 0, to: 1, afterFinish:properRequest });
					$('bigpopuploader').setStyle({display: 'block'});
				}
				//$('bigpopup').hide();
					
				
			
		}
}

function properRequest() {
		/*new Ajax.Request(window.ajaxUrl, {
						onSuccess: function(transport) {
								$('bigpopupcontent').update(transport.responseText);
								$('bigpopuploader').setStyle({display: 'none'});
								$('bigpopupcontent').setStyle({opacity: 0, display: 'block'});
								$('bigpopupcontent').fade({ duration: 0.2, from: 0, to: 1, afterFinish:function(){window.currentlyAjax = false; } });
								// some stuff
						}
				});*/
		
		new Ajax.History.Request(window.ajaxUrl, {
						history : {
								id    : 'page',
								state : window.myState,
								cache : false,
								onStateChange: historyObserver
						},
						onSuccess: function(transport) {
								$('bigpopupcontent').update(transport.responseText);
								$('bigpopuploader').setStyle({display: 'none'});
								$('bigpopupcontent').setStyle({opacity: 0, display: 'block'});
								$('bigpopupcontent').fade({ duration: 0.5, from: 0, to: 1,afterFinish:function(){window.currentlyAjax = false; } });
								reinitContent();
						}
				});
}

function historyObserver(value) {

}
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
/***************************************************************************************/
google.setOnLoadCallback(baobazOnLoad);



