/**
 *  DIAPORAMA
 */
	/** PARAMETERS **/
	var effect = 1;
	var time = 3000;
	
	var current_image = new Array();
	var next_image = new Array();
	var image = new Array();
	var timer;
	var previous_txt_bloc; //permet de savoir le bloc txt precedent qui doit etre masque
	
	var nb_bloc = 1;
	var cycle = 3;
	var next_pos = 1;
	/****************/
	
	/**
	 * Init the diaporama
	 * 
 	 * @author Richard Marc, <marc@belenvol.fr>
	 * @param class name of the img markup
	 * @param nb_bloc nomber of bloc which manage the diaporama process
	 */
	function init_diaporama(class_name,nb_bloc) {
		var imageBuf;
		
		if( nb_bloc ){
			nb_bloc = nb_bloc;
		}
		
		for(i=1; i<=nb_bloc; i++){
			if(document.getElementsByClassName(class_name+i)){
				imageBuf = document.getElementsByClassName(class_name+i);
				image[i-1] = new Array();

				image[i-1][0] = imageBuf[0];
				current_image[i-1] = 0;
				next_image[i-1] = 1;
				
				for(j=1; j < imageBuf.length; j++){
					image[i-1][j] = imageBuf[j];
					image[i-1][j].style.display = "none";
				}
			}
		}

		
		if(image.length > 0){
			galerie();
			
			//change the text
			//displayAdText("text_img_"+image[0].getAttribute('id'));
		}
	}
	
	/**
	 * Auto diaporama
	 */
	function galerie() {
		
		timer = self.setTimeout("nextimage(true)",time);	
	}
	
	/**
	 * Display the next picture
	 * 
 	 * @author Richard Marc, <marc@belenvol.fr>
	 * @param play boolean to stop  or play the diaporama (manuel mode)
	 */
	function nextimage(play) {
		var effects = new Array();
		var cur;
		var next;
		
		if(effect == 1) {
			for(i=0; i<image.length; i++){
				if( i== 0 || ((i==1 || i==3) && next_pos==1) || ((i==2 || i==4) && next_pos==2) || (i==5 && next_pos==3) ){
					cur = current_image[i];
					next = next_image[i];
					new Effect.Fade(image[i][cur]); 
					new Effect.Appear(image[i][next]);
				}
			}
		}
		
		if(effect == 2) { 
			for(i=0; i<image.length; i++){
				if( i== 0 || ((i==1 || i==3) && next_pos==1) || ((i==2 || i==4) && next_pos==2) || (i==5 && next_pos==3) ){
					cur = current_image[i];
					next = next_image[i];
					new Effect.Fade(image[i][cur]); 
					new Effect.Appear(image[i][next]);
				}
			}
		}
		
		for(i=0; i<image.length; i++){
			if( i== 0 || ((i==1 || i==3) && next_pos==1) || ((i==2 || i==4) && next_pos==2) || (i==5 && next_pos==3) ){
				if( next_image[i] == (image[i].length-1) ){
					current_image[i] = next_image[i];
					next_image[i] = 0;
				}else{
					current_image[i] = next_image[i];
					next_image[i]++;
				}
			}
		}
		
		next_pos++;
		
		if( next_pos > cycle ){
			next_pos = 1;
		}
		
		//change the text
		//displayAdText("text_img_"+image[current_image].getAttribute('id'));
		
		if( play ){
			galerie();
		}else{
			if( timer ){
				self.clearTimeout(timer);
			}
		}
	}
	
	/**
	 * Display the previous picture
	 * 
 	 * @author Richard Marc, <marc@belenvol.fr>
	 * @param stop boolean stop to stop the diaporamo (manuel mode)
	 */
	function previousimage(play) {
		for(i=0; i<image.length; i++){
			if( i== 0 || ((i==1 || i==3) && next_pos==1) || ((i==2 || i==4) && next_pos==2) || (i==5 && next_pos==3) ){
				if(next_image[i] == 0 ) {
					current_image[i] = 0;
					next_image[i] = image.length[i]-1;
				}else{
					current_image[i] = next_image[i];
					next_image[i]--;
				}
			}
		}
		
		var effects = new Array();
		var cur;
		var next;
		
		if(effect == 1) {
			for(i=0; i<image.length; i++){
				if( i== 0 || ((i==1 || i==3) && next_pos==1) || ((i==2 || i==4) && next_pos==2) || (i==5 && next_pos==3) ){
					if(next_image[i] == 0 ) {
						current_image[i] = 0;
						next_image[i] = image.length[i]-1;
					}else{
						current_image[i] = next_image[i];
						next_image[i]--;
					}
				}
			}
		}
		
		if(effect == 2) { 
			for(i=0; i<image.length; i++){
				cur = current_image[i];
				next = next_image[i];
				effects.push(new Effect.BlindUp(image[i][cur])); 
				effects.push(new Effect.BlindDown(image[i][next]));
			}
		}
		
		next_pos++;
		
		if( next_pos > cycle ){
			next_pos = 1;
		}
			
		for(i=0; i<image.length; i++){
			if( next_image[i] == (image[i].length-1) ){
				current_image[i] = next_image[i];
				next_image[i] = 0;
			}else{
				current_image[i] = next_image[i];
				next_image[i]++;
			}
		}
		
		if( play ){
			galerie();
		}else{
			if( timer ){
				self.clearTimeout(timer);
			}
		}
	}
	
	
	
	/**
	 * Display the opacity text
 	 * @author Richard Marc, <marc@belenvol.fr>
 	 * @param id bloc txt to display
	 */
	function displayAdText(id){
		var myFx1;
		var myFx2;
	    var layerOpacity = $('bloc_opaque');
	    var txtOpacity;
		
		if( !previous_txt_bloc ){
	        var dimensions = $(id).getCoordinates();
	        layerOpacity.setStyles({'width': dimensions.width +'px', 'height': dimensions.height +'px'});
	        layerOpacity.setOpacity(0);
	        layerOpacity.setStyle('visibility','visible');
	        
	        txtOpacity = $(id);
	        txtOpacity.setOpacity(0);
	        txtOpacity.setStyle('visibility','visible');
	        
	        myFx1 = new Fx.Style(layerOpacity, 'opacity',{'duration':1500});
	        myFx1.start(0,0.4);
	        
	        myFx2 = new Fx.Style(txtOpacity, 'opacity',{'duration':1500});
	        myFx2.start(0,1);
        
        	previous_txt_bloc = id;
        }else{ //mask previous bloc
        	var buf = previous_txt_bloc;
        	previous_txt_bloc = null;
        	
	        myFx1 = new Fx.Style(layerOpacity, 'opacity',{'duration':1500,'onComplete':'displayAdText('+id+')'});
	        myFx1.start(0.4,0);
	        
	        myFx2 = new Fx.Style($(buf), 'opacity',{'duration':1500});
	        myFx2.start(1,0);
	        
        }
	}
	