/*
/////////////////////////////////////////////////////////////////////////////////////

	SuperSlideShow
	by Carlos Lopez de Pablo 2009 © http://www.lopezdepablo.com
	
/////////////////////////////////////////////////////////////////////////////////////
*/


//FORCE NAMESPACE
var SuperSlideShow = new Class({

	initialize: function(el){
	
		//COLLECTION OF IMAGES TO LOAD
		this.images = new Array();
		this.index = 0;
		
		//IMAGES LOADER
		this.assetImages = null;
		
		//SLIDE SHOW ELEMENT 
		this.el = el;
		
		//SLIDE ELEMENT
		this.slide = null;

		//SLIDE SHOW CONTROLS
		this.overlay = null;
		
	},
	
	loadShow:function(){
	
		if(this.slide)this.slide.destroy();

		var n= this.index;
		//IF ISSUE N EXSIST LOAD ITS IMAGES
		if (this.images[n]){
			this.el.addClass('sss_loading');
			this.index=n;
			
			this.assetImages = new Asset.images(this.images,{
				onComplete: function(){
					this.loadShowCompleted();
				}.bind(this),
				onProgress: function(counter,index){
					//alert(counter+' : '+index);
				},
				onError: function(){
					alert('error! '+counter+' : '+index);
				}
				
			});
			
		}
	},
	
	loadShowCompleted:function(){
		this.el.removeClass('sss_loading');
		this.buildSlide();
		this.buildOverlay();
		this.showSlide();

	},
	
	showSlide:function(){
		this.slide.fx.onComplete= function(){
			this.overlay.fx.start({'opacity':1,'top':0});
		}.bind(this);
		this.slide.fx.duration=500;
		this.slide.fx.start({'opacity':1});
	},
	
	hideSlide:function(direction){
		var dir = (direction)? direction : 1;
		this.slide.fx.onComplete= function(){
			this.loadShow.delay(250,this);
		}.bind(this);
		this.slide.fx.duration=250;
		this.slide.fx.start({'opacity':0,'left':(direction/2)*this.el.getSize().x});	
	},
	
	buildSlide:function(){
		this.slide = new Element('div',{
			'styles':{
				'display':'block',
				'position':'absolute',
				'width':this.el.getSize().x,
				'height':this.el.getSize().y,
				'background':'url('+this.images[this.index]+') 50% 50% no-repeat',
				'opacity':0,
				'left':0, 'right':0
			}
		});
		this.slide.fx = new Fx.Morph(this.slide, {duration: 'short', link:'cancel', transition: Fx.Transitions.Sine.easeIn});
		this.slide.inject(this.el);
		
	},
	
	buildOverlay:function(){
		this.overlay = new Element('div',{
			'styles':{
				'display':'block',
				'position':'absolute',
				'width':this.el.getSize().x,
				'height':this.el.getSize().y,
				'top':-8,
				'opacity':0
			}
		});
		this.overlay.fx = new Fx.Morph(this.overlay, {duration: 'short',link:'cancel', transition: Fx.Transitions.Sine.easeOut});
		this.overlay.inject(this.el);


		//BUILD ISSUE NAVIGATION BUTTONS
		if (this.index<this.images.length-1){
			var next_button = new Element('div',{
				'class':'sss_slide_nav sss_slide_nav_next',
				'html':'<div class="button"></div>',
				'events':{
					'click':function(){
						this.changeSlide(this.index+1);
					}.bind(this)
				}
			});
			next_button.inject(this.overlay);
		}
		if (this.index>0){
			var prev_button = new Element('div',{
				'class':'sss_slide_nav sss_slide_nav_prev',
				'html':'<div class="button"></div>',
				'events':{
					'click':function(){
						this.changeSlide(this.index-1);
					}.bind(this)
				}
			});
			prev_button.inject(this.overlay);
		}
		
		
	},
	
	changeSlide:function(n){
		if(this.images[n]){
			this.el.addClass('sss_loading');
			if(this.overlay)this.overlay.destroy();
			if (this.index>n){
				this.index=n;
				this.hideSlide(1);
			}
			else{
				this.index=n;
				this.hideSlide(-1);
			}
		}
		
	}
	

});

