/**
 *	An image fader.
 *	Depends on Prototype.js and Effects.js (Scriptaculous)
 *
 *  @author Brook Elgie
 */


 var Fader=Class.create({initialize:function(rotatingImages,containerID,options){this.rotatingImageObjs=rotatingImages;this.containerElement=$(containerID);this.preloader=new Preloader();this.newTag=null;this.retryDuration=1.0;var img=Fader._getImageElement(this.containerElement);this.imageHeight=img.getHeight();this.imageWidth=img.getWidth();this.options=Object.extend({fadeInDuration:1.0,fadeOutDuration:1.0,seconds:2.0,crossfade:false,pause:false},options||{});if(this.options.crossfade){this.containerElement.setStyle({height:this.imageHeight+'px',width:this.imageWidth+'px'});this.containerElement.makePositioned();this.containerElement.firstDescendant().setStyle({position:'absolute',top:0,left:0});}
 var preloadPaths=[];for(var i=0;i<rotatingImages.length;i++){preloadPaths.push(rotatingImages[i].imgPath);}
 this.preloader.addToQueue(preloadPaths);this.preloader.startLoading();if(!this.options.pause)this.pe=new PeriodicalExecuter(this.next.bind(this),this.options.seconds);},next:function(pe){if(this.pe!=null)this.pe.stop();var nextRotateImage=this.rotatingImageObjs.shift();this.rotatingImageObjs.push(nextRotateImage);if(!this.preloader.hasPathLoaded(nextRotateImage.imgPath)){this.rotatingImageObjs.unshift(this.rotatingImageObjs.pop());this.pe=new PeriodicalExecuter(this.next.bind(this),this.retryDuration);return false;}
 this.fadeToImage(nextRotateImage);},prev:function(pe){if(this.pe!=null)this.pe.stop();this.rotatingImageObjs.unshift(this.rotatingImageObjs.pop());var nextRotateImage=this.rotatingImageObjs.pop();this.rotatingImageObjs.push(nextRotateImage);if(!this.preloader.hasPathLoaded(nextRotateImage.imgPath)){this.rotatingImageObjs.push(this.rotatingImageObjs.shift());this.pe=new PeriodicalExecuter(this.prev.bind(this),this.retryDuration);return false;}
 this.fadeToImage(nextRotateImage);},fadeToImage:function(nextRotateImage){var img=Fader._getImageElement(this.containerElement);this.newTag=Element.extend(document.createElement('img'));this.newTag.writeAttribute('src',nextRotateImage.imgPath);this.newTag.writeAttribute('alt',nextRotateImage.imgAlt);this.newTag.writeAttribute('width',this.imageWidth);this.newTag.writeAttribute('height',this.imageHeight);Element.setOpacity(this.newTag,0);var aTag;if(nextRotateImage.linkUrl!=null){aTag=Element.extend(document.createElement('a'));aTag.writeAttribute('href',nextRotateImage.linkUrl);this.newTag=aTag.update(this.newTag);}
 if(this.options.crossfade){this.newTag.setStyle({position:'absolute',top:0,left:0});var newImg=(this.newTag.tagName!='IMG')?Fader._getImageElement(this.newTag):this.newTag;this.containerElement.insert(this.newTag);new Effect.Opacity(newImg,{duration:this.options.fadeInDuration,from:0.0,to:1.0,afterFinish:this.onCrossfadeFinish.bind(this)});}else{new Effect.Opacity(img,{duration:this.options.fadeOutDuration,from:1.0,to:0.0,afterFinish:this.onFadeOutFinish.bind(this)});}},onFadeOutFinish:function(obj){this.containerElement.childElements().first().replace(this.newTag);var img=Fader._getImageElement(this.containerElement);new Effect.Opacity(img,{duration:this.options.fadeInDuration,from:0.0,to:1.0,afterFinish:this.onFadeInFinish.bind(this)});},onFadeInFinish:function(obj){if(!this.options.pause)this.pe=new PeriodicalExecuter(this.next.bind(this),this.options.seconds);},onCrossfadeFinish:function(obj){this.containerElement.childElements().first().remove();if(!this.options.pause)this.pe=new PeriodicalExecuter(this.next.bind(this),this.options.seconds);}});Fader._getImageElement=function(containerElement){var img=containerElement.select('img')[0];return img;};
