var SlideShow = Class.create();

	SlideShow.prototype = {
	    
	  initialize: function(mainContainer, datas, direction) {

	  	var nextHandler = mainContainer.select('.next')[0];
	  	var previousHandler = mainContainer.select('.previous')[0];
	  	
	  	this.currentView = { param : null, obj : null};
			this.previousView = { param : null, obj : null};
			this.clickController = false;
			this.mainContainer = mainContainer;
			this.container = null;
			this.datas = datas;
			if(direction)
				this.direction = direction;
			else
				this.direction = "horizontal";
			
			this.currentView.param = "s1";

	  	this.container = this.mainContainer.select('.container')[0];
	  			
	  	this.currentView.obj = this.container.select('.'+this.currentView.param)[0];
	  	
	  	this.writeTitle();
	  	
	  	if(this.datas.slides){
	  		this.slidesQty = this.datas.slides.length;
			}
			else{this.slidesQty = 0;}
	  	
	  	if(this.slidesQty > 0){
		  	if(this.datas.mode == "linear") this.currentView.num = 0;
		  	else if(this.datas.mode == "random") this.currentView.num = Math.floor(Math.random()*this.slidesQty);
					
				this.writeContent(this.currentView.obj, this.datas.slides[this.currentView.num]);
			}
			
			this.removeMask();
			
			
			if(this.slidesQty > 1){
				this.createSlideView();
				if(nextHandler){
					Event.observe(
						nextHandler,
						'click',
						function(evt){
							Event.stop(evt);
							if(this.clickController == false){
								this.setViews();
								this.setNextSlideNumber('next');
								this.clearContent(this.currentView.obj, this.datas.slides[this.currentView.num]);
								this.writeContent(this.currentView.obj, this.datas.slides[this.currentView.num]);
								if(this.direction == 'horizontal')
									this.doHorizontalSlide('next');
								else
									this.doVerticalSlide('next');
							}
						}.bind(this)
					);
				}
				
				if(previousHandler){
					Event.observe(
						previousHandler,
						'click',
						function(evt){
							Event.stop(evt);
							if(this.clickController == false){
								this.setViews();
								this.setNextSlideNumber('previous');
								this.clearContent(this.currentView.obj, this.datas.slides[this.currentView.num]);
								this.writeContent(this.currentView.obj, this.datas.slides[this.currentView.num]);
								if(this.direction == 'horizontal')
									this.doHorizontalSlide('previous');
								else
									this.doVerticalSlide('previous');
							}
						}.bind(this)
					);
				}
			}
			else{
				if(nextHandler) nextHandler.style.visibility = "hidden";//nextHandler.hide();
				if(previousHandler) previousHandler.style.visibility = "hidden";//hide();
			}
	  },
	  
	  createSlideView : function(){
	  	var templateView = this.currentView.obj.cloneNode(true);
	  	templateView.removeClassName('s1');
	  	templateView.addClassName('s2');
	  	
	  	if(this.direction == 'horizontal'){
				templateView.style.top = "0px";
				templateView.style.left = this.mainContainer.getWidth() + "px";
			}
			else
			{
				templateView.style.left = "0px";
				templateView.style.top = this.mainContainer.getHeight() + "px";
			}

	  	var parentContainer = this.currentView.obj.parentNode;
	  	if(parentContainer){
	  		parentContainer.appendChild(templateView);
	  	}
	  },
	  
	  setViews : function(){
			if(this.currentView.param == "s1"){
				this.currentView.param = "s2";
				this.previousView.param = "s1";
			}
			else if(this.currentView.param == "s2"){
				this.currentView.param = "s1";
				this.previousView.param = "s2";
			}
			
			this.currentView.obj = this.container.select('.'+this.currentView.param)[0];
			this.previousView.obj = this.container.select('.'+this.previousView.param)[0];
		},
		
		doHorizontalSlide : function(direction){
			var xPos = 0;
			var containerWidth = this.container.getWidth();
			if(direction == "next"){
				this.currentView.obj.setStyle({'left' : containerWidth+'px'});
				xPos = containerWidth*(-1);
			}
			else if(direction == "previous"){
				this.currentView.obj.setStyle({'left' : '-'+containerWidth+'px'});
				xPos = containerWidth;
			}
			
			new Effect.Parallel([
				  new Effect.Move(this.previousView.obj, { sync: true, x: xPos, y: 0, mode: 'absolute' }), 
				  new Effect.Move(this.currentView.obj, { sync: true, x: 0, y: 0, mode: 'absolute' })
				], { 
				  duration: 1,
				  delay: 0,
				  beforeStart : function(){
				  	this.clickController = true;
				  }.bind(this),
				  afterFinish : function(){
				  	this.clickController = false;
				  }.bind(this)
				});
		},
		
		doVerticalSlide : function(direction){
			var yPos = 0;
			var containerHeight = this.container.getHeight();
			if(direction == "next"){
				this.currentView.obj.setStyle({'top' : containerHeight+'px'});
				yPos = containerHeight*(-1);
			}
			else if(direction == "previous"){
				this.currentView.obj.setStyle({'top' : '-'+containerHeight+'px'});
				yPos = containerHeight
			}
			
			new Effect.Parallel([
				  new Effect.Move(this.previousView.obj, { sync: true, x: 0, y: yPos, mode: 'absolute' }), 
				  new Effect.Move(this.currentView.obj, { sync: true, x: 0, y: 0, mode: 'absolute' })
				], { 
				  duration: 1,
				  delay: 0,
				  beforeStart : function(){
				  	this.clickController = true;
				  }.bind(this),
				  afterFinish : function(){
				  	this.clickController = false;
				  }.bind(this)
				});
		},
		
		setNextSlideNumber : function(direction){
			if(direction == "next"){
				var newSlideNumber = this.currentView.num + 1;
				if(newSlideNumber > (this.slidesQty -1)) newSlideNumber = 0;
				this.currentView.num = newSlideNumber;
			}
			else if(direction == "previous"){
				var newSlideNumber = this.currentView.num - 1;
				if(newSlideNumber < 0) newSlideNumber = this.slidesQty -1;
				this.currentView.num = newSlideNumber;
			}
		},
		
		writeTitle : function(){
			var title = this.mainContainer.select('.titrecarrousel')[0];
			if(title){
				if(this.datas.titrecarrousel[1]){
					if(this.datas.titrecarrousel[1] != ""){
						var titleLink = document.createElement('a');
						titleLink.innerHTML = this.datas.titrecarrousel[0];
						titleLink.href = this.datas.titrecarrousel[1];
						if(this.datas.titrecarrousel[2] && this.datas.titrecarrousel[2] != "") titleLink.target = this.datas.titrecarrousel[2];
						title.appendChild(titleLink);
					}
				} 
				else{title.update(this.datas.titrecarrousel[0]);}
			}
		},
		
		removeMask : function(){
			var mask = this.mainContainer.select('.attente')[0];
			if(mask) mask.style.display="none";
		},
		
		writeContent : function(_parent, content){
		
			var sClass = "";
			var oHtmlElement = null;
			var oValues = null;
			
			for(var mapKey in content){
				tClass = mapKey;
				oHtmlElement = this.getSlideElementFromClass(_parent, '.'+tClass);
				if(oHtmlElement){
					oValues = content[mapKey];
					
					switch(oValues[0]){
						case 'text' :
							this.writeText(oHtmlElement, oValues[1]);
						break;
						
						case 'link' : 
							if(oValues[1][0] == "" && oValues[1][1] != "") if(oHtmlElement.hasClassName('hidden')) {oHtmlElement.removeClassName('hidden');}
							this.writeText(oHtmlElement, oValues[1][0]);
							this.writeHref(oHtmlElement, oValues[1][1]);
							this.writeTarget(oHtmlElement, oValues[1][2]);
						break;
						
						case 'list' : 
							var lis = oHtmlElement.select('li.liste');
							var nbValues = oValues[1].length;
							if(lis && lis.length > 0 && lis.length >= nbValues){
								for(var i = 0; i< nbValues; i++){
									this.clearContent(lis[i], oValues[1][i]);
									this.writeContent(lis[i], oValues[1][i]);
									if(lis[i].hasClassName('hidden')) {lis[i].removeClassName('hidden');}
								}
							}
							if(oHtmlElement.hasClassName('hidden')) {oHtmlElement.removeClassName('hidden');}
						break;
						
						case 'src' :
							this.writeSrc(oHtmlElement, oValues[1]);
						break;
						
						default : 
						break;
					}
				}
			}
		},
		
		writeText : function(elt, data){
			if(elt){
				if(data != ""){
					elt.update(data);
					if(elt.hasClassName('hidden')) {elt.removeClassName('hidden');}
				}
			}
		},
		
		writeHref : function(elt, data){
			if(elt){
				if(data != ""){
					elt.href=data;
				}
				else{elt.href="#";}
			}
		},
		
		writeTarget : function(elt, data){
			if(elt){
				if(data != "") elt.target=data;
			}
		},
		
		writeSrc : function(elt, data){
			if(elt){
				if(data != ""){
					elt.src=data;
					if(elt.hasClassName('hidden')) {elt.removeClassName('hidden');}
				}
				else{elt.src="/px.gif";}
			}
		},
		
		/* Clearing functions */
		clearContent : function(_parent, content){
			var sClass = "";
			var oHtmlElement = null;
			var oValues = null;
			
			for(var mapKey in content){
				tClass = mapKey;
				oHtmlElement = this.getSlideElementFromClass(_parent, '.'+tClass);
				if(oHtmlElement){
					oValues = content[mapKey];
					
					switch(oValues[0]){
						case 'text' :
							this.clearText(oHtmlElement);
						break;
						
						case 'link' : 
							if(oValues[1][0] == "" && oValues[1][1] != ""){
								this.clearHref(oHtmlElement);
								this.clearTarget(oHtmlElement);
							}
							else if(oValues[1][0] != ""){
								this.clearText(oHtmlElement);
								this.clearHref(oHtmlElement);
								this.clearTarget(oHtmlElement);
							}
						break;
						
						case 'list' : 
							var lis = oHtmlElement.select('li.liste');
							if(lis && lis.length > 0){
								for(var i = 0; i< lis.length; i++){
									this.clearContent(lis[i], oValues[1][i]);
									if(!lis[i].hasClassName('hidden')) {lis[i].addClassName('hidden');}
								}
							}
						break;
						
						case 'src' :
							this.clearSrc(oHtmlElement);
						break;
						
						default : 
						break;
					}
					
					if(!oHtmlElement.hasClassName('hidden')) {oHtmlElement.addClassName('hidden');}
				}
			}
		},
		
		clearTarget : function(elt){
			if(elt){
				elt.target="";
			}
		},
		
		clearSrc : function(elt){
			if(elt){
				elt.src="/px.gif";
			}
		},
		
		clearHref : function(elt){
			if(elt){
				elt.href="#";
			}
		},
		
		clearText : function(elt){
			if(elt){
				elt.update("");
			}
		},
		/* End Clearing Functions */
		
		getSlideElementFromClass : function(_parent, classe){
			var elt = _parent.select(classe);
			if(elt && elt.length > 0) return elt[0];
			else return false;
		}
	}
	
	// Initialisation des slideshow present dans la page. A mettre de preference dans le JS principal de la page.
	/*document.observe(
		'dom:loaded',
		function(){
			var listeSlideshows = $$('.slideshow');
			if(listeSlideshows && listeSlideshows.length > 0){
				listeSlideshows.each(function(element){
					if(typeof(carrousels) != "undefined"){
						if(carrousels[element.id]){	
							element.slidehow = new SlideShow(element, carrousels[element.id]);
						}
					}
				}); 
			}
		}
	);*/
