
//slide right pke ça existe pas chez scriptaculoous
Effect.SlideRight = function(element) {
  element = $(element);
  Element.cleanWhitespace(element);
  // SlideDown need to have the content of the element wrapped in a container element with fixed height!

  var oldInnerRight = 510;
  var elementDimensions = Element.getDimensions(element);
  return new Effect.Scale(element, 100, Object.extend({ 
    scaleContent: false, 
    scaleY: false, 
    scaleFrom: window.opera ? 0 : 1,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makePositioned();
      effect.element.down().makePositioned();
      if (window.opera) effect.element.setStyle({top: ''});
      effect.element.makeClipping().setStyle({width: '0px'}).show(); 
    },
    
    afterUpdateInternal: function(effect) {
      effect.element.down().setStyle({right:
        (effect.dims[1] - effect.element.clientWidth) + 'px' }); 
    },
  	afterFinishInternal: function(effect) {
      effect.element.undoClipping().undoPositioned();
      effect.element.down().undoPositioned().setStyle({right: oldInnerRight}); }
    }, arguments[1] || { })
  );
}




var Player = new Class.create();

Player.prototype = {
	initialize: function() {
		/* @author : Alexandre Leblanc
			em@il : alexandre.leblanc@articmedia.net
		*/
		//tous les documents array enumerables
		this.documents=Array();
		this.current_id=null;
		this.current_obj=null;
		this.effect=null;
		this.timeout=null;
		this.width=510;
		this.height=305;
		this.play=true;
		this.diapo_images=Array();
		this.timer_diaporama=null;
		this.diapo_id=0;
		this.nb_diapo=0;
		this.diapo_id_after=null;
		this.id_css="dp_0";
	},
	init_docs: function(docs) {
		var this_object=this;
		this_object.documents=docs;

		this_object.play=true;
		this_object.current_id=0;
		this_object.current_obj=this_object.documents[0];
		$(this_object.id_css+'anime_select').selectedIndex=this_object.current_id;
		this_object.dispatcher();
	},
	dispatcher: function() {
		var this_object=this;	
		this_object.lance_diaporama();
	},
	
	lance_diaporama: function() {
		var this_object=this;
		//pour respecter l'ordre
		$(this_object.id_css+'prevlabel').show();
		$(this_object.id_css+'nextlabel').show();
	  	if(this_object.play){
	  		this_object.relance_diaporama();
	  	}else{
	  		this_object.relance_diaporama();
	  		this_object.pause_diapo();
	  	}

	},
	prev_diapo: function(){
		var this_object=this;
		window.clearTimeout(this_object.timer_diaporama);
		this_object.defil_diaporama_precedent();
		//this_object.timer_diaporama=window.setInterval("player.relance_diaporama()",20000);
		//on met en pause plutot
		this_object.pause_diapo();
		
	},
	next_diapo: function(){
		var this_object=this;
		window.clearTimeout(this_object.timer_diaporama);
		this_object.defil_diaporama();
		//this_object.timer_diaporama=window.setInterval("player.relance_diaporama()",20000);
		//on met en pause plutot
		this_object.pause_diapo();
		
	},
	relance_diaporama: function(){
		var this_object=this;
		window.clearTimeout(this_object.timer_diaporama);
		this_object.timer_diaporama=window.setInterval("player_"+this_object.id_css+".defil_diaporama()",5000);
	},
	pause_diapo: function(){
		var this_object=this;
		window.clearTimeout(this_object.timer_diaporama);
	},
	play_diapo: function(){
		var this_object=this;
		$(this_object.id_css+'playlabel').hide();
		$(this_object.id_css+'pauselabel').show();
	  	this_object.relance_diaporama();
	},
	set_zindex: function(id,zindex){
		$(id).style.zIndex=zindex;
		$(id).down().style.zIndex=zindex;
		/*$(id).down().down().style.zIndex=zindex;
		$(id).down().down().down().style.zIndex=zindex;
		$(id).down().down().down().down().style.zIndex=zindex;*/
	},
	defil_diaporama_precedent: function() {
		var this_object=this;
		if($(this_object.id_css+'diapo_player_'+this_object.diapo_id)!=undefined){
			var id_after=this_object.diapo_id_after;
			var id_avant_assigner=this_object.diapo_id;
			//reorganisation de l'ordre
			this_object.set_zindex(this_object.id_css+'diapo_player_'+this_object.diapo_id,33);
			this_object.diapo_id--;
			if($(this_object.id_css+'diapo_player_'+this_object.diapo_id)==undefined){
				this_object.diapo_id=this_object.nb_diapo-1;
			}
			if(id_after){
				//reorganisation de l'ordre
				this_object.set_zindex(this_object.id_css+'diapo_player_'+id_after,31);
			}
			$(this_object.id_css+'diapo_player_'+this_object.diapo_id).hide();
			//reorganisation de l'ordre
			this_object.set_zindex(this_object.id_css+'diapo_player_'+this_object.diapo_id,40);
			new Effect.SlideRight(this_object.id_css+'diapo_player_'+this_object.diapo_id, {duration:0.5});
			if(this_object.e2){
				this_object.e1.cancel();
				this_object.e2.cancel();
			}
			$(this_object.id_css+'fond_bande_boutons').hide();
			$(this_object.id_css+'bande_boutons').hide();
			this_object.e1=new Effect.Appear(this_object.id_css+'fond_bande_boutons', {duration:1, from:0.0, to:0.8});
			this_object.e2=new Effect.Appear(this_object.id_css+'bande_boutons', {duration:1, from:0.0, to:1});
			$(this_object.id_css+'diapo_texte_'+id_avant_assigner).hide();
			$(this_object.id_css+'diapo_texte_'+this_object.diapo_id).show();
			this_object.diapo_id_after=id_avant_assigner;
		}
	},				
	defil_diaporama: function() {
		var this_object=this;
		//console.log('rrr'+this_object.diapo_id);
		if($(this_object.id_css+'diapo_player_'+this_object.diapo_id)!=undefined){
			var id_after=this_object.diapo_id_after;
			var id_avant_assigner=this_object.diapo_id;
			//reorganisation de l'ordre
			this_object.set_zindex(this_object.id_css+'diapo_player_'+this_object.diapo_id,33);
			this_object.diapo_id++;
			if($(this_object.id_css+'diapo_player_'+this_object.diapo_id)==undefined){
				this_object.diapo_id=0;
			}
			if(id_after){
				//reorganisation de l'ordre
				this_object.set_zindex(this_object.id_css+'diapo_player_'+id_after,31);
			}
			$(this_object.id_css+'diapo_player_'+this_object.diapo_id).hide();
			//reorganisation de l'ordre
			this_object.set_zindex(this_object.id_css+'diapo_player_'+this_object.diapo_id,40);
			new Effect.SlideRight(this_object.id_css+'diapo_player_'+this_object.diapo_id, {duration:0.5});
			if(this_object.e2){
				this_object.e1.cancel();
				this_object.e2.cancel();
			}
			$(this_object.id_css+'fond_bande_boutons').hide();
			$(this_object.id_css+'bande_boutons').hide();
			this_object.e1=new Effect.Appear(this_object.id_css+'fond_bande_boutons', {duration:1, from:0.0, to:0.8});
			this_object.e2=new Effect.Appear(this_object.id_css+'bande_boutons', {duration:1, from:0.0, to:1});
			$(this_object.id_css+'diapo_texte_'+id_avant_assigner).hide();
			$(this_object.id_css+'diapo_texte_'+this_object.diapo_id).show();
			this_object.diapo_id_after=id_avant_assigner;
		}
	},
	//nettoie les variable d'un player a un autre
	clear: function()
	{
		var this_object=this;
		if(this_object.effect){
			this_object.effect.cancel();
		}
		$(this_object.id_css+"loading_player").hide();
		$(this_object.id_css+"prevlabel").hide();
		$(this_object.id_css+"nextlabel").hide();
		$(this_object.id_css+"pauselabel").hide();
		$(this_object.id_css+"playlabel").hide();
		$(this_object.id_css+"image_presentation").innerHTML="";
		$(this_object.id_css+"texte_presentation").innerHTML="";
		
		this_object.diapo_id=0;
		this_object.diapo_id_after=null;
		
		window.clearTimeout(this_object.timeout);
		window.clearTimeout(this_object.timer_diaporama);
	},				
	//destructeur
	destruct: function()
	{
		var this_object=this;
		window.clearTimeout(this_object.timeout);
		window.clearTimeout(this_object.timer_diaporama);
		this_object.documents=Array();
		this_object.current_id=null;
		this_object.current_obj=null;
		this_object.effect=null;
		this_object.timeout=null;
		this_object.play=true;
		this_object.diapo_images=Array();
	}
};

