/**
	aSlideshow 14/02/2011
	Copyright (c) 2011 Alfieri Mauro; info [at] aportal [dot] eu; http://www.aportal.eu/

*/

var aSlideshow = Class.create({

	initialize: function(element, options) {		
		this.options = {
      			duration: 1,
			delay: 4.0,
			randomize: false,
			autostart:true,
			controls:false,
			mode: 'fade', // fade,puzzle,slidedxsx,slidesxdx,slideup,slidedown
			pRows: 3, 
			pCols: 5,
			pColor: '#FFFFFF'
    		}
		Object.extend(this.options, options || {});

    	this.element        = $(element);
		this.slides			= this.element.childElements();
		this.num_slides		= this.slides.length;		
		this.current_slide 	= 0;
		this.end_slide		= this.num_slides - 1;
		this.mode			= this.options.mode;

		this.sDimension	 	= this.element.getDimensions();
		this.sWidth  		= this.sDimension.width;
		this.sHeight 		= this.sDimension.height;
		
		this.slides.invoke('hide');
		this.slides[this.current_slide].show();
		
		/* Preload */
		this.preload_imgs = new Array();
	    for (i = 0; i < this.num_slides; i++) {
	      this.preload_imgs[i] = new Image();
	      this.img_elements = this.slides[i].childElements();
	      for (j = 0; j < this.img_elements.length; j++) {
	      	if ( this.img_elements[j].nodeName.toLowerCase() == 'img' ) {
	      		this.preload_imgs[i].src = this.img_elements[j].src;
	      		this.img_elements[j].className = 'slider-image-imgOn';
	      	}
	      	
	      	if ( this.img_elements[j].nodeName.toLowerCase() == 'a' ) {	
	      		var _img = this.img_elements[j].childElements();
	      		if ( _img[0].nodeName.toLowerCase() == 'img' ) {
	      			this.preload_imgs[i].src = _img[0].src;
	      			_img[0].className = 'slider-image-imgOn';
	      		}
	      	}
	      } 
	    }
		/* Fine Preload */
		
		if (this.options.autostart) { 
			this.startSlideshow();
		}

		if (this.mode == 'puzzle') {
			this.buildPuzzle();
		}	
	},
	
	buildPuzzle: function() {		
		this.Puzzle 	= [];
		var pDimension	 	= this.element.getDimensions();
		var pWidth  		= pDimension.width;
		var pHeight 		= pDimension.height;
				
		var _Width 		= pWidth / this.options.pCols;
		var _Height 		= pHeight / this.options.pRows;
	
		$R(0, this.options.pCols-1).each(function(col) {
			this.Puzzle[col] = [];							 	
			$R(0, this.options.pRows-1).each(function(row) {
				var _Left = col * _Width;
			    	var _Top  = row * _Height;
				this.Puzzle[col][row] = new Element('div').setStyle({
 											opacity: 0, backgroundColor: this.options.pColor,
											position: 'absolute', 'z-index': 5,
											left: _Left + 'px', top: _Top + 'px',
											width: _Width + 'px', height: _Height + 'px'		
											});
				this.element.insert(this.Puzzle[col][row]);				 							 										 
			}.bind(this))
		}.bind(this));			
	},

	startSlideshow: function(event) {
		if (event) { Event.stop(event); }
		if (!this.running)	{
			this.executer = new PeriodicalExecuter(function(){
	  			this.updateSlide(this.current_slide+1);
	 		}.bind(this),this.options.delay);
			this.running=true;
		}
	},
	
	stopSlideshow: function(event) {	
		if (event) { Event.stop(event); } 
		if (this.executer) { 
			this.executer.stop();
			this.running=false;
		}	 
	},

	updateSlide: function(next_slide) {
		if (next_slide > this.end_slide) { 
			next_slide = 0; 
		} 
		else if ( next_slide == -1 ) {
			next_slide = this.end_slide;
		}
		
		if ( this.mode == 'fade'|| this.mode == 'puzzle')
		{
			this.fadeInOut(next_slide, this.current_slide);
		}

		if ( this.mode == 'slidedxsx')
		{	
			this.slides[next_slide].setStyle('left: ' + this.sWidth + 'px; ');
			this.slides[next_slide].show();
			this.slideDxSx(next_slide, this.current_slide);
		}

		if ( this.mode == 'slidesxdx')
		{   
			this.slides[next_slide].setStyle('left: -' + this.sWidth + 'px; ');
			this.slides[next_slide].show();
			this.slideSxDx(next_slide, this.current_slide);
		}
		
		if ( this.mode == 'slideup')
		{
			this.slides[next_slide].setStyle('top: ' + this.sHeight + 'px; ');
			this.slides[next_slide].show();
			this.slideUp(next_slide, this.current_slide);
		}
		
		if ( this.mode == 'slidedown')
		{
			this.slides[next_slide].setStyle('top: -' + this.sHeight + 'px; ');
			this.slides[next_slide].show();
			this.slideDown(next_slide, this.current_slide);
		}
	
	},

 	fadeInOut: function (next, current) {		
		new Effect.Parallel([
			new Effect.Fade(this.slides[current], { sync: true }),
			new Effect.Appear(this.slides[next], { sync: true }) 
  		], { duration: this.options.duration });
		
		if (this.mode == 'puzzle') {			
			$R(0, this.options.pCols-1).each(function(col) {	 						 	
				$R(0, this.options.pRows-1).each(function(row) {
					var _Puzzle = this.Puzzle[col][row];
					var delay = Math.random() * 150;				
					setTimeout(this.delayedAppear.bind(this, _Puzzle), delay);
				}.bind(this))
			}.bind(this));	
		}
		
		this.current_slide = next;		
	},

	slideDxSx: function (next, current) {
		new Effect.Parallel([
			new Effect.Move(this.slides[current], { sync: true, x: -this.sWidth, y: 0, mode: 'absolute' }),
  			new Effect.Move(this.slides[next], { sync: true, x: 0, y: 0, mode: 'absolute' })
  		], { duration: this.options.duration });
		
		this.current_slide = next;		
	},

	slideSxDx: function (next, current) {
		new Effect.Parallel([
			new Effect.Move(this.slides[current], { sync: true, x: this.sWidth, y: 0, mode: 'absolute' }),
			new Effect.Move(this.slides[next], { sync: true, x: 0, y: 0, mode: 'absolute' })
  		], { duration: this.options.duration });
		
		this.current_slide = next;		
	},

	slideUp: function (next, current) {
		new Effect.Parallel([
			new Effect.Move(this.slides[current], { sync: true, x: 0, y: -this.sHeight, mode: 'absolute' }),
			new Effect.Move(this.slides[next], { sync: true, x: 0, y: 0, mode: 'absolute' })
  		], { duration: this.options.duration });
		
		this.current_slide = next;		
	},

	slideDown: function (next, current) {
		new Effect.Parallel([
			new Effect.Move(this.slides[current], { sync: true, x: 0, y: this.sHeight, mode: 'absolute' }),
			new Effect.Move(this.slides[next], { sync: true, x: 0, y: 0, mode: 'absolute' })
  		], { duration: this.options.duration });
		
		this.current_slide = next;		
	},
	
	delayedAppear: function(eSquare)	{
		var opacity = Math.random();
		new Effect.Parallel([ 
			new Effect.Appear ( eSquare, { from: 0, to: opacity, duration: this.options.duration/4 } ),
			new Effect.Appear ( eSquare, { from: opacity, to: 0, duration: this.options.duration/1.25} )
		], { sync: false });			
	}

});
