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,fx:{link:"cancel",transition:"sine:out",duration:500}},plugins:{},initialize:function(options){this.addEvent("change",function(current){this.current=current}.bind(this)).setOptions(options);$each({left:"previous",right:"next"},function(val,key){if(this.options[key]){$(this.options[key]).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.scroll,direction)},next:function(direction){return this.move(this.current+this.options.scroll,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.isVisible(index)){return this}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,elements=this.elements=carousel.elements.map(function(el){return el.setStyles({display:"block",position:"absolute"})}),parent=elements[0].getParent(),pos=parent.setStyles({height:parent.offsetHeight,position:"relative",overflow:"hidden"}).getStyle("padding"+(this.up?"Top":"Left"));this.property="offset"+(up?"Top":"Left");this.margin="margin"+(up?"Top":"Left");this.reorder(0,1).fx=new Fx.Elements(elements,options.fx)},reorder:function(offset,direction){var options=this.options,panels=this.elements,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;panel=panels[index];if(horizontal){panel.setStyle("left",pos)}else{panel.setStyles({left:0,top:pos})}pos-=(panel[side]+style(panel,this.margin))}pos=ini+panel[side]+style(panel,this.margin);for(i=1;i<options.scroll;i++){index=(i+offset+length)%length;panel=panels[index];if(horizontal){panel.setStyle("left",pos)}else{panel.setStyles({left:0,top:pos})}pos+=panel[side]+style(panel,this.margin)}}else{if(direction==1){for(i=0;i<length;i++){index=(i+offset+length)%length;panel=panels[index];if(horizontal){panel.setStyle("left",pos)}else{panel.setStyles({left:0,top:pos})}pos+=panel[side]+style(panel,this.margin)}}}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.start(obj).chain(function(){carousel.fireEvent("change",current)})}})})();
