/***************************************************************************
****************************************************************************

	Name: swzSlideshow
	Requirements : prototype.js, scriptaculous.js, swzImageLoad.js

	x = new swzSlideshow({
		id 				: 'the id of the slideshow holder',
		randomizeArray 	: 'randomize the slide array',
		randomStart 	: 'randomize the array starting point',
		duration 		: 'the duration of animation',
		transition		: 'the transition delay between slides'
	})
	
	to add a slide -
		x.addSlide('YOUR HTML',['imagetoload1','imagetoload2']);
		
	to begin the slideshow - 
		x.start();

****************************************************************************
***************************************************************************/
	
	var swzSlideshow = Class.create({
	
		initialize:function(options){
		
			//create and extend the options
			this.options = {
				id	  			: 'Slideshow',
				randomizeArray 	: false,
				randomStart		: false,
				duration		: 1,
				transition		: 3
			}
			Object.extend(this.options,options || '');
			
			//create an array for the slides
			this.name 			= 'swzSlideshow';
			this.arrSlides 		= new Array();
			this.arrLoaded		= new Array();
			this.arrSlide		= new Array();
			this.initialSlide	= 0;
		
		},
		
		randomizeArray:function(){
			this.arrSlides.sort(Math.round(Math.random())-0.5);
		},
		
		addSlide:function(html,images){
			this.arrSlides[this.arrSlides.length] = new Array(html,images);
			this.arrLoaded[this.arrLoaded.length] = false;
		},
		
		loadSlide:function(obj,s){
		
			if(obj.arrLoaded[s] == true){
				obj.showSlide(s);
			} else {
								
				//load the images
				new swzImageLoad({
					src : obj.arrSlides[s][1],
					onSuccess:function(){
						slide = Builder.node('div',{style:'position:absolute;top:0;left:0;display:none;border:0px solid #000'},'');
						$(obj.options.id).appendChild(slide);
						slide.innerHTML = obj.arrSlides[s][0];
						obj.arrSlide[s] = slide;
						obj.arrLoaded[s] = true;
						obj.showSlide(s);
					}
				});
			}
		},
		
		start:function(){
		
			//randomize the array?
			if(this.options.randomizeArray){
				this.arrSlides.sort(function() {return 0.5 - Math.random()}) 
			}
			
			//select a random starting point?
			if(this.options.randomStart){
				this.initialSlide = Math.floor(Math.random() * this.arrSlides.length);
			}
			
			this.loadSlide(this,this.initialSlide);
		},
		
		showSlide:function(s){
		
			if(this.currentSlide == null){

				//set the current slide
				this.currentSlide = s;
				
				//set the next slide
				if(this.currentSlide == this.arrSlides.length-1){
					this.nextSlide = 0
				} else {
					this.nextSlide = this.currentSlide + 1;
				}
				
				//get the current slide
				slide = this.arrSlide[this.currentSlide];

				//show the current slide				
				animationOptions = {
					afterFinish:function(){
						setTimeout(function(){this.loadSlide(this,this.nextSlide)}.bind(this),this.options.transition * 1000);										
					}.bind(this),
					duration:this.options.duration,
					fps:50				
				}
				$(slide).appear(animationOptions);
			
			} else {
						
				//set the previous slide
				this.previousSlide = this.currentSlide;
				
				//set the current slide
				this.currentSlide = s;
				
				//set the next slide
				if(this.currentSlide == this.arrSlides.length-1){
					this.nextSlide = 0
				} else {
					this.nextSlide = this.currentSlide + 1;
				}
				
				//set up the effects!
				slideA = this.arrSlide[this.previousSlide];
				slideB = this.arrSlide[this.currentSlide];
				
				//hide slideA show slideB
				animationOptions = {
					duration:this.options.duration,
					fps:50
				}
				paralellOptions = {
					afterFinish:function(){
						setTimeout(function(){this.loadSlide(this,this.nextSlide);}.bind(this),this.options.transition * 1000);
					}.bind(this)
				}
				new Effect.Parallel([
					new Effect.Fade(slideA,animationOptions),
					new Effect.Appear(slideB,animationOptions)				
				],paralellOptions);
				
			}		
		}

	
	});

