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


 var Rotator=Class.create({initialize:function(rotatingImages,containerID,options){this.rotatingImageObjs=rotatingImages;this.containerID=containerID;this.preloader=new Preloader();this.retryDuration=1.0;this.moveEffect=null;var img=$(this.containerID).childElements().first();if(img.tagName!='IMG')img=img.down('img');var clippingObj={height:String(img.getHeight())+'px',width:String(img.getWidth())+'px'};this.imageWidth=clippingObj.width;this.imageHeight=clippingObj.height;this.options=Object.extend({direction:"left",transitionEquation:Effect.Transitions.sinoidal,seconds:10,pause:false},options||{});var preloadPaths=[];for(var i=0;i<rotatingImages.length;i++){preloadPaths.push(rotatingImages[i].imgPath);}
 this.preloader.addToQueue(preloadPaths);this.preloader.startLoading();$(this.containerID).update("<div id='"+this.containerID+"-inner'>"+$(this.containerID).innerHTML+"</div>");$(this.containerID).makeClipping().setStyle(clippingObj);$(this.containerID).setStyle({overflow:'hidden',position:'relative'});if(!this.options.pause)this.pe=new PeriodicalExecuter(this.next.bind(this),this.options.seconds);},next:function(){if(this.pe!=null)this.pe.stop();if(this.moveEffect!=null)return false;var nextRotateImage=this.rotatingImageObjs.shift();this.rotatingImageObjs.push(nextRotateImage);if(!this.preloader.hasPathLoaded(nextRotateImage.imgPath)){new PeriodicalExecuter(this.next.bind(this),this.retryDuration);return false;}
 var innerContainer=this.containerID+"-inner";if(this.options.direction=="right"||this.options.direction=='left'){var containerWidth=String((parseInt(this.imageWidth)*2)+10)+"px";$(innerContainer).setStyle({width:containerWidth});}else if(this.options.direction=='up'||this.options.direction=='down'){var containerHeight=String((parseInt(this.imageHeight)*2)+10)+"px";$(innerContainer).setStyle({height:containerHeight});}
 var newTag=Element.extend(document.createElement('img'));newTag.writeAttribute('src',nextRotateImage.imgPath);newTag.writeAttribute('alt',nextRotateImage.imgAlt);newTag.writeAttribute('width',this.imageWidth);newTag.writeAttribute('height',this.imageHeight);var aTag;if(nextRotateImage.linkUrl!=null){aTag=Element.extend(document.createElement('a'));aTag.writeAttribute('href',nextRotateImage.linkUrl);newTag=aTag.update(newTag);}
 var scrollImageBy=0;if(this.options.direction=="left"){$(innerContainer).insert({bottom:newTag});scrollImageBy=-parseInt(this.imageWidth);}else if(this.options.direction=="right"){$(this.containerID+'-inner').setStyle({left:0});$(innerContainer).insert({top:newTag});scrollImageBy=parseInt(this.imageWidth);$(this.containerID).scrollLeft=scrollImageBy;}else if(this.options.direction=='up'){$(innerContainer).insert({bottom:newTag});scrollImageBy=-parseInt(this.imageHeight);}else if(this.options.direction=='down'){$(this.containerID+'-inner').setStyle({top:0});$(innerContainer).insert({top:newTag});scrollImageBy=parseInt(this.imageHeight);$(this.containerID).scrollTop=scrollImageBy;}
 if(this.options.direction=='left'||this.options.direction=='right')
 this.moveEffect=new Effect.Move($(this.containerID+'-inner'),{duration:1,x:scrollImageBy,afterFinish:this.onRotateFinish.bind(this),transition:this.options.transitionEquation});else if(this.options.direction=='up'||this.options.direction=='down')
 this.moveEffect=new Effect.Move($(this.containerID+'-inner'),{duration:1,y:scrollImageBy,afterFinish:this.onRotateFinish.bind(this),transition:this.options.transitionEquation});},onRotateFinish:function(obj){this.moveEffect=null;var childer=$(this.containerID+'-inner').childElements();if(this.options.direction=='left'||this.options.direction=='up'){childer.first().remove();$(this.containerID+'-inner').setStyle({left:0,top:0});}else if(this.options.direction=='right'||this.options.direction=='down'){childer.last().remove();}
 if(!this.options.pause)this.pe=new PeriodicalExecuter(this.next.bind(this),this.options.seconds);}});Rotator._BackEaseIn=function(pos){var s=1;return pos*pos*((s+1)*pos-s);};Rotator._BackEaseInOut=function(pos){var s=1.70158;if((pos/=0.5)<1)return 0.5*(pos*pos*(((s*=(1.525))+1)*pos-s));return 0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos+s)+2);};
