
var PeriodicalExecuter=new Class({initialize:function(callback,frequency){this.callback=callback;this.frequency=frequency;this.currentlyExecuting=false;this.registerCallback()},registerCallback:function(){this.stop();this.timer=setInterval(this.onTimerEvent.bind(this),this.frequency*1000);return this},execute:function(){this.callback(this);return this},stop:function(){if(!this.timer)return this;clearInterval(this.timer);this.timer=null;return this},onTimerEvent:function(){if(!this.currentlyExecuting){try{this.currentlyExecuting=true;this.execute();}finally{this.currentlyExecuting=false;}}return this}});(function(){function style(el,style){var mrg=el.getStyle(style);return mrg=='auto'?0:mrg.toInt()}var Carousel=this.Carousel=new Class({Implements:[Options,Events],options:{mode:'horizontal',animation:'Move',scroll:4,distance:1,fx:{link:'cancel',transition:'sine:out',duration:500}},plugins:{},initialize:function(options){this.addEvent('change',function(current){this.current=current}.bind(this)).setOptions(options);['previous','next'].each(function(val){if($(this.options[val]))$(this.options[val]).addEvent('click',function(e){e.stop();this[val]()}.bind(this))},this);this.elements=$(options.container).getChildren(options.selector);this.current=0;this.anim=new this.plugins[this.options.animation](this);this.move(this.options.current||0);},isVisible:function(index){if($type($(index))=='element')index=this.elements.indexOf($(index));var length=this.elements.length,current=this.current,scroll=this.options.scroll;if(current<=index&&index<current+scroll)return true;if(this.options.circular)for(var i=1;i<scroll;i++){if((i+current)%length==index)return true;}return false},first:function(){return this.current},previous:function(direction){return this.move(this.current-this.options.distance,direction)},next:function(direction){return this.move(this.current+this.options.distance,direction)},move:function(index,direction){var elements=this.elements,current=this.current,length=elements.length,scroll=this.options.scroll;if($type($(index))=='element')index=elements.indexOf($(index));if(!this.options.circular){if(index>length-scroll)index=length-scroll}else{if(index<0)index+=length
index%=Math.max(length,1);}if(index<0||length<=scroll||index>=length)return this;if(direction==undefined){var forward=current<index?index-current:elements.length-current+index,backward=current>index?current-index:current+elements.length-index;direction=Math.abs(forward)<=Math.abs(backward)?1:-1}this.anim.move(this,index,direction);return this}});Carousel.prototype.plugins.Move=new Class({initialize:function(carousel){var up=this.up=carousel.options.mode=='vertical',options=this.options=carousel.options,parent=carousel.elements[0].getParent(),elements=this.elements=carousel.elements;parent.setStyles({height:parent.offsetHeight,position:'relative',overflow:'hidden'}).getStyle('padding'+(this.up?'Top':'Left'));elements.each(function(el){el.setStyles({display:'block',position:'absolute'})});this.property='offset'+(up?'Top':'Left');this.margin=up?['marginTop','marginBottom']:['marginLeft','marginRight'];this.reorder(0,1).fx=new Fx.Elements(elements,options.fx)},reorder:function(offset,direction){var options=this.options,panels=this.elements,panel,prev,ini=pos=style(panels[0].getParent(),'padding'+(this.up?'Top':'Left')),i,index,length=panels.length,horizontal=options.mode=='horizontal',side=horizontal?'offsetWidth':'offsetHeight';if(direction==-1){for(i=length;i>(options.scroll-1);i--){index=(i+offset+length)%length;prev=panel;panel=panels[index];if(horizontal)panel.setStyle('left',pos);else panel.setStyles({left:0,top:pos});pos-=(panel[side]+style(panel,this.margin[1]));if(prev)pos-=style(prev,this.margin[0]);}pos=ini+panel[side]+style(panel,this.margin[0]);for(i=1;i<options.scroll;i++){index=(i+offset+length)%length;prev=panel;panel=panels[index];if(horizontal)panel.setStyle('left',pos);else panel.setStyles({left:0,top:pos});pos+=panel[side]+style(panel,this.margin[0]);if(prev)pos+=style(prev,this.margin[1])}}else if(direction==1)for(i=0;i<length;i++){index=(i+offset+length)%length;prev=panel;panel=panels[index];if(horizontal)panel.setStyle('left',pos);else panel.setStyles({left:0,top:pos});pos+=panel[side]+style(panel,this.margin[0]);if(prev)pos+=style(prev,this.margin[1]);}return this},move:function(carousel,current,direction){var obj={},up=this.up,property=this.property,offset;if(this.options.circular)this.reorder(carousel.current,direction);offset=carousel.elements[current][property];carousel.elements.each(function(el,index){obj[index]=up?{top:el[property]-offset}:{left:el[property]-offset}});this.fx.cancel().start(obj).chain(function(){carousel.fireEvent('change',current)})}})})();Carousel.Extra=new Class({Extends:Carousel,Binds:['update','start','stop'],initialize:function(options){this.parent($merge({interval:10,delay:10},options));['previous','next'].each(function(val){if($(this.options[val]))$(this.options[val]).addEvent('click',function(e){e.stop();if(this.running)this.stop().start.delay(this.options.delay*1000)}.bind(this))},this);this.reverse=!!this.options.reverse;this.running=false;this.timer=new PeriodicalExecuter(this.update,this.options.interval);return this},update:function(){return this[this.reverse?'previous':'next']()},start:function(){this.timer.registerCallback();this.running=true;return this},stop:function(){this.timer.stop();this.running=false;return this}});
