UI.SITCarousel = Class.create(UI.Carousel, {
		
		/**
		 *  Surcharge du constructeur pour dupliquer les premiers et derniers éléménts afin de faire la boucle
		 * */
		initialize: function($super, element, options)
		{
			this.autoScrollPE = null;
			
			this.element = $(element);
			
			container   = this.element.down(this.options.container).firstDescendant();
			elements    = container.childElements();
			
			if (elements.length < 2)
			{
				return $super(element, options);
			}
			
			clonedFirst = elements[0].cloneNode(true);
			clonedFirst.clonedFirst = true;
			
			clonedLast = elements[elements.length-1].cloneNode(true);
			clonedFirst.clonedLast = true;
			
			container.insert(clonedFirst);
			elements[0].insert({before:clonedLast});
			
			// Appel du constructeur parent
			$super(element, options);
			
			// Repositionnement de la liste au niveau du véritable premier élément
			this.checkScroll(this.computeElementSize()*-1, true);
			this.updateButtons();
			
			// Ajout de la gestion de fin de scroll pour éventuellement repositionner la scene
			this.observe('scroll:ended', this.handleSrollEnd.bind(this));
			
			// Ajout d'un handler pour stopper l'autoscroll lors d'un click sur les boutons
			[ this.previousButton, this.nextButton ].each(function(button) 
				{
					button.observe("click", this.stopAutoScroll.bind(this));
				},
				this
			);
			
		},
		
		/**
		 * Surcharge de la fonction pour cacher les boutons si il n'y a que 1 element
		 * */
		updateButtons: function($super)
		{
			if (this.elements.length == 1)
			{
				this.previousButton.style.visibility = 'hidden';
				this.nextButton.style.visibility = 'hidden';
			}
			else
			{
				return $super();
			}
		},
		
		
		/**
		 * Fonction pour lancer l'auto scroll
		 * */
		autoScroll: function(timeInSec) {
			if (this.elements.length > 1)
				this.autoScrollPE = new PeriodicalExecuter(this.scroll.bind(this, -1 * this.options.scrollInc * this.computeElementSize()), timeInSec);
  		},
  		
  		/**
		 * Fonction pour arreter l'auto scroll
		 * */
		stopAutoScroll: function() {
			if (typeof(this.autoScrollPE) == 'object' && this.autoScrollPE != null)
			{
				this.autoScrollPE.stop();
				this.autoScrollPE = null;
			}
  		},
  		
  		/**
  		 * Handler de l'évènement scrollend
  		 * */
  		handleSrollEnd: function()
  		{
  			
  			// On est positionné sur le dernier élément qui est en fait une copie du premier => je me repositionne sur le premier
  			if (this.currentIndex() == (this.elements.length-1))
  			{
  				this.checkScroll(this.computeElementSize()*-1, true);
				this.updateButtons();
			}
			// On est positionné sur le premier élément qui est en fait une copie du dernier => je me repositionne sur le dernier
			else if (this.currentIndex() == 0)
			{
				this.checkScroll(this.computeElementSize()*(this.elements.length-2)*-1, true);
				this.updateButtons();
			}
			
  			//alert('handleSrollEnd, this.currentIndex() : '+this.currentIndex()+', this.elements.length:'+this.elements.length);
		},
		
		/**
		 * 
		 *  Surchage de scroll pour booster la vitesse
		 *  + patch pour appliquer l'opacité sous IE
		 * */
		scroll: function(deltaPixel) {
			if (this.animating)
			  return this;

			// Compute new position
			var position =  this.currentPosition() + deltaPixel;

			// Check bounds
			position = this.checkScroll(position, false);

			// Compute shift to apply
			deltaPixel = position - this.currentPosition();
			if (deltaPixel != 0) {
			  this.animating = true;
			  this.fire("scroll:started");

			  var that = this;
			  // Move effects
			  
			  
			  this.container.morph({opacity:'0.5'}, {duration: 0.1, 
			  	beforeSetup: function(){if (Prototype.Browser.IE){that.container.childElements().each(function(liElement){liElement.style.filter='progid:DXImageTransform.Microsoft.alpha(opacity=50)';})}},
			  	afterFinish: function() {
				that.container.morph(that.posAttribute + ": " + position + "px", {
				  duration: 0.4,
				  delay: 0.1,
				  afterFinish: function() {
					if (Prototype.Browser.IE){that.container.childElements().each(function(liElement){liElement.style.filter='progid:DXImageTransform.Microsoft.alpha(opacity=100)';})}
					that.container.morph({opacity:'1'}, {
					  duration: 0.1,
					  afterFinish: function() {
						that.animating = false;
						that.updateButtons()
						  .fire("scroll:ended", { shift: deltaPixel / that.currentSize() });
					  }
					});
				  }
				});
			  }});
			}
			return this;
		  }
  	}	
);
