// JavaScript Document
lecture = false;
numero_photo=0;
cadre_photo="photo0";
fin_lecture = false;


function cache(){
	$$('.liste_realisation').each(function(z,indexz){
		z.hide();
	});
	$$('.rubrique_rea').each(function(z,indexz){
		Element.setStyle(z,{'color':'#b7b591'});
		/*Element.setStyle(eval('$(\''+z.id+':hover\')'),{'color':'#5d471a'});*/
	});
}


function change_photo(){
	
	if(photo_active.length==0 || photo_active==eval('tableau'+rubrique_active).length-1){
		/*photo_active = 0;*/
		if(!fin_lecture){
			mon_executer.stop();
			lecture = false;
			fin_lecture=true;
			return;
		}else{
			photo_active = 0;
		}
	}else{
		photo_active = parseInt(photo_active)+1;
	}
	
	if(eval('tableau'+rubrique_active).length>1){
		if(cadre_photo=="photo0"){
			cadre_photo_suivant="photo1";
		}else{
			cadre_photo_suivant="photo0";
		}
		if($(cadre_photo_suivant)){
			$(cadre_photo_suivant).remove();
		}
		$('photo_realisation').insert({bottom: '<div id="'+cadre_photo_suivant+'"><img src="./upload/realisations/'+eval('tableau'+rubrique_active)[photo_active]+'" alt="" /></div>'});
		new Effect.SlideUp(cadre_photo,{duration: 0.5});
		/*new Effect.Move(cadre_photo_suivant, {
			  x: 325, y: -242, mode: 'relative',
			  transition: Effect.Transitions.full
		});
		

		new Effect.Parallel([
			new Effect.Move(cadre_photo, {
			  x: -329, y: 0, mode: 'relative',
			  transition: Effect.Transitions.spring
			}),
			new Effect.Move(cadre_photo_suivant, {
			  x: -329, y: 0, mode: 'relative',
			  transition: Effect.Transitions.spring
			})
		], { duration: 2 });*/
		cadre_photo = cadre_photo_suivant;
	}
}

function lire(){
	fin_lecture = false;
	mon_executer = new PeriodicalExecuter(change_photo, 3);
	lecture = true;
}

function charger_photo(){
	$('photo_realisation').update('<div id="photo0"><img src="./upload/realisations/'+eval('tableau'+rubrique_active)[0]+'" alt="" /></div>');
	cadre_photo="photo0";
	if(eval('url'+rubrique_active)!=''){
		$('site_internet').update('<a href="'+eval('url'+rubrique_active)+'" target="_blank">Voir le site Internet</a>');
	}else{
		$('site_internet').update('');
	}
	photo_active = 0;
}

Event.observe(window,'load',function(event){
	$$('.rubrique_rea').each(function(z,indexz){
		Event.observe(z,'click',function(event){
			if(lecture){
				mon_executer.stop();
				lecture = false;
			}
			$('photo_realisation').update('');
			rubrique_active = $(z.id.replace('btn_','')).down().id.replace('rea_','')
			charger_photo();
			lire();
			cache();
			$(z.id.replace('btn_','')).show();
			
			Element.setStyle(z,{'color':'#5d471a'});
			/*rubrique_active = z.id.replace('btn_','');*/
		});
	});
	
	$$('.une_realisation').each(function(z,indexz){
		Event.observe(z,'click',function(event){
			if(lecture){
				mon_executer.stop();
				lecture = false;
			}
			rubrique_active = z.id.replace('rea_','');
			charger_photo();
			lire();
		});
	});
	
	Event.observe('play','click',function(event){
		if(!lecture){
			change_photo();
			lire();
		}
	});
	Event.observe('stop','click',function(event){
		if(lecture){
			mon_executer.stop();
			lecture = false;
		}
	});
	//lire();
});
