/*
 *  UPDATED: 04.05.07 | 10:00 AM
 *
 *  This class will create a fade in slide show that will preload the next image.
 *  It requires 2 parameters: the ID of the slide show image, and an array of the images.
 *  All images must be the same size.
 *  
 *  Exapmle: 
 *  var myShow = new SlideShow('show2',Slide2, {speed: 5, duration: 2});
 *  
 *  
 * ------------------------------------------------------------------------------------------------ */

var SlideShow = Class.create();
SlideShow.prototype = {
	version : '1.1',
	
	initialize : function(slideImage,array) {
		if(typeof Prototype=='undefined' || array.length<1){return; /* Could not find the Image exit the funtion */ }
		this.options = Object.extend({
			speed:    	9,
			duration: 	1
		}, arguments[2] || {});
		
		this.link = null;
		this.image = slideImage;
		this.slides = array;
		this.counter = 0;
		this.preloaded = false;
		Event.observe(window,'load',this._build.bindAsEventListener(this));
	},
  
	_build: function(){
		this.slideImage = $(this.image);
		if (!this.slideImage){ return; /* Could not find the Image exit the class */}
		var slideZ = parseInt(Element.getStyle(this.slideImage, 'z-index'));
		var nextZ = (isNaN(slideZ)) ? 1 : slideZ+1;
		this.nextImage = document.createElement('IMG');
		this.nextImage.setAttribute('border','0');
		Position.clone(this.slideImage, this.nextImage, {setLeft:false, setTop:false});
		Element.setStyle(this.nextImage, {opacity:'0',filter:'alpha(opacity=0)',MozOpacity:'0'});
		//this._setOpacity(this.nextImage, 0);
		/* check if the slides are an object and the object has a link */
		if (typeof this.slides[0]=='object' && this.slides[0].link != 'undefined'){
			var link = document.createElement('A');
			link.href = this.slides[0].link;
			Position.clone(this.slideImage, link);
			Element.setStyle(link, {position:'absolute', zIndex:nextZ});
			link.appendChild(this.nextImage);
			document.body.appendChild(link);
			this.link = link;
		}
		else {
			Position.clone(this.slideImage, this.nextImage);
			Element.setStyle(this.nextImage, {position:'absolute',zIndex:nextZ});  
			document.body.appendChild(this.nextImage);
		}
		/* Begin the Show */
		this._run();
	},
  
	_run: function(){
		this.nextImage.src = (this.slides[this.counter].img) ? this.slides[this.counter].img : this.slides[this.counter];
		this._fade();
	},
  
	_fade: function(){
		var FPS = 25;
		var time = (this.options.duration*1000)/FPS;
		var step = 1/FPS;
		var newOpacity = (this._getOpacity(this.nextImage) + step);
		if (newOpacity<1){
			//this._setOpacity(this.nextImage,newOpacity);
			Element.setStyle(this.nextImage,{opacity:newOpacity,filter:'alpha(opacity='+ (newOpacity*100) +')',MozOpacity:newOpacity});
			setTimeout(this._fade.bind(this), time);
		}
		else {
			if(this.link){this.link.href = this.slides[this.counter].link;} /* change the link if available */
			this.slideImage.src = (this.slides[this.counter].img) ? this.slides[this.counter].img : this.slides[this.counter];
			//this._setOpacity(this.nextImage, 0);
			Element.setStyle(this.nextImage, {opacity:'0',filter:'alpha(opacity=0)',MozOpacity:'0'});
			
			(this.counter<this.slides.length-1) ? this.counter++ : this.counter=0;
			
			if(!this.preloaded){
				(new Image()).src = (this.slides[this.counter].img) ? this.slides[this.counter].img : this.slides[this.counter];
				if (this.counter == (this.slides.length-1)) {this.preloaded=true;}
			}	  
			setTimeout(this._run.bind(this), this.options.speed*1000);
		}
	},

	_setOpacity: function(element,value){
			element= $(element);  
			if(value == 1){
				Element.setStyle(element,{
					opacity: (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 0.999999 : null
				});
			if(/MSIE/.test(navigator.userAgent))
				Element.setStyle(element,{
					filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')
				});  
			}else{  
				if(value < 0.00001) value = 0;  
				Element.setStyle(element, {opacity: value});
				if(/MSIE/.test(navigator.userAgent))  
					Element.setStyle(element,{
						filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity='+value*100+')'
					});  
			}
	},

	_getOpacity: function(element){  
  		var opacity;
  		if (opacity = Element.getStyle(element, 'opacity'))  
  	 		return parseFloat(opacity);  
  		if (opacity = (Element.getStyle(element, 'filter') || '').match(/alpha\(opacity=(.*)\)/))  
  	  	if(opacity[1]) return parseFloat(opacity[1]) / 100;  
  		return 1.0;  
	}
}