window.addEvent('domready',function(){
	function clearClass(toThisClass){
		$(oldElement).set('class',toThisClass);
		$('what').set('class','what');
	}
	var countPict='';
	var actual='';
	var oldEntry='';
	var initialVisibility=0;
	var oldElement=$('folder77');
	var isPlaying=0;
	var myVerticalSlide=new Fx.Slide('swappable',{duration:100});
	var tempo=3000;
	var periodical;
	var nextImgPeriod=function(){periodical=nextImgGo.periodical(tempo);}
	$('minus').set('title','faster');
	$('minus').addEvent('click',function(){
		$clear(periodical);
		if(tempo>2000){tempo-=1000;}
		if(isPlaying==1){
			$('spinner').set('src','img/spinner.gif');
			nextImgPeriod();
		}
	});
	$('minus').addEvent('mouseenter',function(){$('titreBouton').set('html',this.title);});
	$('minus').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('malus').set('title','slower');
	$('malus').addEvent('click',function(){
		$clear(periodical);
		if(tempo<6000){tempo+=1000;}
		if(isPlaying==1){
			$('spinner').set('src','img/spinner.gif');
			nextImgPeriod();
		}
	});
	$('malus').addEvent('mouseenter',function(){$('titreBouton').set('html',this.title);});
	$('malus').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	myVerticalSlide.hide();
	$$('.folder').each(function(element,index){
		element.addEvent('click',function(){
			$('grigri').setStyle('background-color', '#EFEFEF');
			initialVisibility=1;
			myVerticalSlide.slideIn();
			actual=this.title;
			$('titreRubrique').set('html',this.title);
			clearClass('folder');
			$('pause').set('class','pause');
			$('play').set('class','play');
			this.set('class','opened');
			oldEntry=actual;
			oldElement=this.id;
			countPict=this.lang;
			counter=0;
			scrTo.toElement("I"+counter);
			$clear(periodical);
			injectPict(this.id);
			$('page').set('text','');
			myFxpage.start('opacity',0,1);
			myFxNoSwap.start('opacity',0,1);
			$('spinner').set('src','img/tr.gif');
			$('leftPut').setStyle('display', 'none');
			$('rightPut').setStyle('display', 'block');
			$('goesTogrey').setStyle('background-color', '#EFEFEF');
		});
		element.addEvent('mouseenter',function(){$('titreRubrique').set('html',this.title);});
		element.addEvent('mouseleave',function(){$('titreRubrique').set('html',oldEntry);});
	});
	var myFxtitreRub = new Fx.Tween($('titreRubrique'),{duration:100});
	var myFxtitre = new Fx.Tween($('titre'),{duration:100});
	var myFxtexte = new Fx.Tween($('texte'),{duration:100});
	var myFxcopy = new Fx.Tween($('copyright'),{duration:100});
	var myFxpage = new Fx.Tween($('page'),{duration:100});
	var myFxNoSwap = new Fx.Tween($('nonswappable'),{duration:100});
	var myFxUrl = new Fx.Tween($('urlu'),{duration:100});
	var myFxswappable = new Fx.Tween($('swappable'),{duration:100})


	$('play').addEvent('click',function(){
		isPlaying = 1;
		this.set('class','playOpen');
		$('spinner').set('src','img/spinner.gif');
		$('pause').set('class','pause');
		nextImgPeriod();
	});
	
	$('play').addEvent('mouseenter',function(){$('titreBouton').set('html','Play');});	
	$('play').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('pause').addEvent('click',function(){
		$('spinner').set('src','img/tr.gif');
		this.set('class','pauseOpen');
		$('play').set('class','play');
		$clear(periodical);
		isPlaying=0;
	});
	
	$('pause').addEvent('mouseenter',function(){$('titreBouton').set('html','Pause');});	
	$('pause').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('left').addEvent('click',function(){
		$('spinner').set('src','img/tr.gif');
		$('play').set('class','play');
		$('pause').set('class','pause');
		$clear(periodical);
		nextImgPrev();
		isPlaying=0;
	});
	
	$('left').addEvent('mouseenter',function(){$('titreBouton').set('html','Previous');});	
	$('left').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('right').addEvent('click',function(){
		$('spinner').set('src','img/tr.gif');
		$('play').set('class','play');
		$('pause').set('class','pause');
		$clear(periodical);
		nextImgGo();
		isPlaying=0;
	});
	
	$('right').addEvent('mouseenter',function(){$('titreBouton').set('html','Next');});	
	$('right').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('rewind').addEvent('click',function(){
		$('spinner').set('src','img/tr.gif');
		rewind();
		$('play').set('class','play');
		$('pause').set('class','pause');
		isPlaying=0;
	});
	
	$('rewind').addEvent('mouseenter',function(){$('titreBouton').set('html','Rewind');});	
	$('rewind').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('what').addEvent('click',function(){
			$('titreRubrique').set('html','About');
			$(oldElement).set('class','folder');
			$('pause').set('class','pause');
			$('play').set('class','play');
			this.set('class','wopened');
			oldEntry=actual;
			oldElement=this.id;
			countPict=this.lang;
			counter=0;
			scrTo.toElement("I0");
			$clear(periodical);
			injectPict('folder_999');
			myFxNoSwap.start('opacity',0,1);
			myFxpage.start('opacity',1,0);
			$('leftPut').setStyle('display', 'none');
			$('rightPut').setStyle('display', 'block');
			$('spinner').set('src','img/tr.gif');
			isPlaying=0;
	});
	
	$('what').addEvent('mouseenter',function(){$('titreBouton').set('html','About');});	
	$('what').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	$('aro').addEvent('mouseenter',function(){$('titreBouton').set('html','Email');});	
	$('aro').addEvent('mouseleave',function(){$('titreBouton').set('html','');});
	
	function injectPict(myGal){
		var req=new Request.HTML({url:'', 
			onSuccess:function(html){
				$('layout').set('text','');
				$('layout').adopt(html);
				injectText($('II0').title);
			},
			onFailure:function(){
				$('layout').set('text','_________________ The request failed :(');
			}
		}).post('imgGal.php?g='+myGal);
		req.send();
	}
	
	var injectText = function(blocToSplit){
        var myStringArray = blocToSplit.split("::"); 
		$('titre').set('html',blocToSplit); 
        $('texte').set('html',''); 
        $('copyright').set('html','');		
		$('titre').set('html','<span class="vert">'+myStringArray[0]+'</span> / '+myStringArray[1]); 
	    $('texte').load('textOnly.php?g='+myStringArray[2]);
		$('urlu').set('html','<a href="http://'+myStringArray[3]+'" target="_blank" class="petitText">'+myStringArray[3]+'</a>'); 
	    $('copyright').load('copyOnly.php?g='+myStringArray[2]);
		$('page').set('text',(counter+1)+'/'+countPict);
	}
	var scrTo=new Fx.Scroll('wrapper',{
		wait:false,
		duration:600,
		offset: {'x':0,'y':0},
		transition:Fx.Transitions.Quart.easeInOut
	})
	var counter=0;
	var nextImgGo=function(){ 
		if(counter<countPict-1){
			counter++;
			$('leftPut').setStyle('display', 'block');
		}else{
			$('spinner').set('src','img/tr.gif');
			$('play').set('class','play');
			$clear(periodical);
			counter=0;
			isPlaying=0;
			$('leftPut').setStyle('display', 'none');
		}
		scrTo.toElement("I"+counter);
		injectText($("II"+counter).title);
		// $('stalker').set('text',countPict);
	};
	var nextImgPrev=function(){ 
		if(counter>0){counter--;}else{
			counter=countPict-1;
		}
		scrTo.toElement("I"+counter);
		injectText($("II"+counter).title);
		isPlaying=0;
		if(counter==0){$('leftPut').setStyle('display','none');}
	}
	var rewind=function(){
		$clear(periodical);
		counter=0;
		scrTo.toElement("I0");
		injectText($("II"+counter).title);
		isPlaying=0;
		$('leftPut').setStyle('display', 'none');
	}

	$('leftArrow').addEvent( 'click' , function() { 									  
		$('spinner').set('src','img/tr.gif');
		$('play').set('class','play');
		$('pause').set('class','pause');
		$clear(periodical);
		nextImgPrev();
		isPlaying=0;
	});	
	
	$('rightArrow').addEvent( 'click' , function() { 
		$('spinner').set('src','img/tr.gif');
		$('play').set('class','play');
		$('pause').set('class','pause');
		$clear(periodical);
		nextImgGo();
		isPlaying=0;
	});

	
	$('textZone').addEvent('mouseleave', function(){
		if(initialVisibility==1){
			myFxtitreRub.start('opacity',1,0);
			myFxtitre.start('opacity',1,0);
			myFxtexte.start('opacity',1,0);
			myFxcopy.start('opacity',1,0);
			myFxpage.start('opacity',1,0);
			myFxNoSwap.start('opacity',1,0);
			myFxUrl.start('opacity',1,0);
			myFxswappable.start('opacity',1,0);
			myVerticalSlide.slideOut();
			$('grigri').setStyle('background-color', '#FFFFFF');
			//$('goesTogrey').setStyle('background-color', '#FFFFFF');
		}
	});

	
	$('textZone').addEvent('mouseenter',function(){
		if(initialVisibility==1){
			myFxtitreRub.start('opacity',0,1);
			myFxtitre.start('opacity',0,1);
			myFxtexte.start('opacity',0,1);
			myFxcopy.start('opacity',0,1);
			myFxpage.start('opacity',0,1);
			myFxNoSwap.start('opacity',0,1);
			myFxswappable.start('opacity',0,1);
			myFxUrl.start('opacity',0,1);
			myVerticalSlide.slideIn();
			$('grigri').setStyle('background-color', '#EFEFEF');
			//$('goesTogrey').setStyle('background-color', '#EFEFEF');
		}
	});

});
