
/************************************************************************
 *  IcyCarousel - Powered by Brokenice Interactive
 *  (c) 2008-2009 Bechetti Luca
 *
 *  IcyGallery is freely distributable under the terms of an GPL license.
 *  For details, see brokenice.it website http://www.brokenice.it
 *
 *  based on fantastic prototype framework
 *
 ************************************************************************/

var IcyCarousel = Class.create({
	
	version			: 	'1.0',	
	
	initialize	: function(elem, options){
		options = options || {};
		this.options= {
			larghezzaCar 	: 	600,
			altezzaCar		:	450,
			numeroDiv		:	0,
			container		:	'gallery',
			containerSlide	: 	'ContainerSlide',
			controlSx		: 	'sinistra',
			controlDx		:	'destra',
			classSlide		:	'slide',
			autoStart		:	true,
			slideInterval	:	5000,
			onSlideLoad		:   Prototype.EmptyFunction
		};
		Object.extend(this.options, options);
		
		this.currentSlide = 0;
		
		//Setto proprietà carosello
		$(elem).setStyle({'width' : this.options.larghezzaCar + 'px', height :  this.options.altezzaCar + 'px', 'position' : 'relative', 'text-align' : 'center', 'overflow' : 'hidden'});
		
		//Preparo i div contenitori delle slide
		this.carousel = new Element('div', {'class' : this.options.container});
		this.carousel.setStyle({'width' : this.options.larghezzaCar+'px', 'height' : '100%', 'overflow' : 'hidden', 'float' : 'left', 'position':'relative','margin' : '0', 'padding' : '0'});
		this.containerSlide = new Element('div', {'class' : this.options.containerSlide});
		this.carousel.insert(this.containerSlide);
				
		//Preparo le slide del carosello
		$(elem).descendants('div').each(function(f){
			if(f.nodeName == 'DIV'){
		
				f.addClassName(this.options.classSlide);
				f.setStyle({'textAlign' : 'left'});
				
				this.containerSlide.insert(f);
				//Aggiorno evento click sullo slide
			}	
		}.bind(this));
		$(elem).insert(this.carousel);
		
			
		//Aggiungo i controlli se non sono presenti
		if(!$(this.options.controlSx)){
			this.controlSX = new Element('div', {'id' : this.options.controlSx});
			this.controlSX.setStyle({'backgroundImage' : 'url(\'css/images/left.png\')', 'position' : 'absolute', 'left' : '0', 'bottom' : '0', 'width':'25px', 'height' : '28px', 'cursor' : 'pointer'});
			$(elem).insert(this.controlSX);
		}else{
			this.controlSX = $(this.options.controlSx);
		}
		if(!$(this.options.controlDx)){
			this.controlDX = new Element('div', {'id' : this.options.controlDx});
			this.controlDX.setStyle({'backgroundImage' : 'url(\'css/images/right.png\')', 'position' : 'absolute', 'right' : '0', 'bottom' : '0', 'width':'25px', 'height' : '28px', 'cursor' : 'pointer'});
			$(elem).insert(this.controlDX);
		}else{
			this.controlDX = $(this.options.controlDx);
		}
		this.options.numeroDiv = 0;
		//Metto le slide fluttuanti
		$$('.'+this.options.classSlide).each(function(x){
			this.options.numeroDiv++;
			$(x).setStyle({'width' : this.options.larghezzaCar+'px', 'height' : '100%', 'float' : 'left'});
			
		}.bind(this));
		this.containerSlide.setStyle({'width' : (this.options.larghezzaCar * this.options.numeroDiv)+'px', 'height' : '100%', 'left' : '0px', 'position' : 'relative'});

		//Aggiorno controlli freccie
		this.controlSX.show();
		this.controlDX.show();
		Event.observe(this.controlDX, "click", function(){
		  this.moveDx();
		}.bind(this));
		
		Event.observe(this.controlSX, "click", function(){
		  this.moveSx();  	
		}.bind(this));
		
		
		
		if(this.options.autoStart)
			window.setInterval(this.slideAuto.bind(this), this.options.slideInterval);
	},
	
	//Funziona per lo slide automatico
	slideAuto : function(){
		this.moveDx();
	},
	
	//Funzione per il movimento a sinistra
	moveSx : function(){

	      //Controllo che non venga avviata più volte l'animazione cliccando i pulsanti di controllo
	      this.count = Math.abs($(this.containerSlide).getStyle('left').replace('px',''));
	      //Se non è mosso scorro
	      if(this.count == this.options.larghezzaCar * (this.options.numeroDiv - 1)){
	      				if( this.containerSlide.getStyle('left').replace('px','') < 0  ){
	      	this.containerSlide.animate({left :(1*this.containerSlide.getStyle('left').replace('px','')) + this.options.larghezzaCar},500);
	      }else{
	     	this.containerSlide.animate({left : 0}, 500);
	      }
	      	this.move = false;
	      	this.currentSlide--;
	      	if(this.options.onSlideLoad)
	      		this.options.onSlideLoad(this.currentSlide);
	      }
	      //Controllo il resto della divisione
	      else {
	      	var c = (this.options.larghezzaCar * (this.options.numeroDiv - 1)) % this.count;
	      	if(c % this.options.larghezzaCar == 0){
	      					if( this.containerSlide.getStyle('left').replace('px','') < 0  ){
	      	this.containerSlide.animate({left :(1*this.containerSlide.getStyle('left').replace('px','')) + this.options.larghezzaCar},500);
	      }else{
	     	this.containerSlide.animate({left : 0}, 500);
	      }
	      		this.move = false;
	      		this.currentSlide--;
	      		if(this.options.onSlideLoad)
	      			this.options.onSlideLoad(this.currentSlide);
	      	}
	      }
	},
	
	//Funzione per il movimento a destra
	moveDx : function(){
	
	      //Controllo che non venga avviata più volte l'animazione cliccando i pulsanti di controllo
	      this.count = Math.abs(this.containerSlide.getStyle('left').replace('px',''));
	      //Se non è mosso scorro
	      	      
	      if(this.count == 0){
	      	if( this.containerSlide.getStyle('left').replace('px','') > -(this.options.larghezzaCar * (this.options.numeroDiv-1) ) ){
	      	this.containerSlide.animate({left : '-=' + this.options.larghezzaCar},500);
	      	this.rewind = false;
	      }else{
	     	this.containerSlide.animate({left : 0},500);
	     	this.rewind = true;
	      }
	      	this.move = false;
	      	if(this.rewind)
	      		this.currentSlide=0;
	      	else
	      		this.currentSlide++;
	      	if(this.options.onSlideLoad)
	      		this.options.onSlideLoad(this.currentSlide);
	      	
	      }
	      //Controllo il resto della divisione
	      else {
	      	var c = (this.options.larghezzaCar * (this.options.numeroDiv - 1)) % this.count;
	      	if(c % this.options.larghezzaCar == 0){
	      		if( this.containerSlide.getStyle('left').replace('px','') > -(this.options.larghezzaCar * (this.options.numeroDiv-1) ) ){
	      	this.containerSlide.animate({left : '-='+ this.options.larghezzaCar},500);
	      	this.rewind = false;

	      }else{
	     	this.containerSlide.animate({left : 0},500);
	     	this.rewind = true;
	      }
	      		this.move = false;
		      	if(this.rewind)
		      		this.currentSlide=0;
		      	else
		      		this.currentSlide++;
		      	if(this.options.onSlideLoad)
	      		this.options.onSlideLoad(this.currentSlide);
	      	}
	      	
	      }
	},
	
	moveToIndex : function(x){
	
		 //Controllo che non venga avviata più volte l'animazione cliccando i pulsanti di controllo
	      this.count = Math.abs(this.containerSlide.getStyle('left').replace('px',''));
	      //Se non è mosso scorro
	 
	      	      
	      if(this.count == 0){
	      	this.containerSlide.animate({left : -x*this.options.larghezzaCar},500);
	      	this.move = false;

	      	this.currentSlide=x;
	  
	      	if(this.options.onSlideLoad)
	      		this.options.onSlideLoad(this.currentSlide);
	      	
	      }
	      //Controllo il resto della divisione
	      else {
	      	var c = (this.options.larghezzaCar * (this.options.numeroDiv - 1)) % this.count;
	      	if(c % this.options.larghezzaCar == 0){
	      		this.containerSlide.animate({left : -x*this.options.larghezzaCar},500);
	      		this.move = false;
		      	
		      	this.currentSlide=x;
		      
		      	if(this.options.onSlideLoad)
	      		this.options.onSlideLoad(this.currentSlide);
	      	}
	      	
	      }
	}
});


