Carousel=Class.create(Abstract,{initialize:function(scroller,slides,controls,options){createSmoothScrollEffect();this.scrolling=false;this.scroller=$(scroller);this.slides=slides;this.controls=controls;this.options=Object.extend({duration:1,auto:false,frequency:3,visibleSlides:1,controlClassName:'carousel-control',jumperClassName:'carousel-jumper',disabledClassName:'carousel-disabled',selectedClassName:'carousel-selected',circular:false,wheel:true,effect:'scroll',transition:'sinoidal'},options||{});if(this.options.effect=='fade'){this.options.circular=true;}
this.slides.each(function(slide,index){slide._index=index;});if(this.controls){this.controls.invoke('observe','click',this.click.bind(this));}
if(this.options.wheel){this.scroller.observe('mousewheel',this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll',this.wheel.bindAsEventListener(this));;}
if(this.options.auto){this.start();}
if(this.options.initial){var initialIndex=this.slides.indexOf($(this.options.initial));if(initialIndex>(this.options.visibleSlides-1)&&this.options.visibleSlides>1){if(initialIndex>this.slides.length-(this.options.visibleSlides+1)){initialIndex=this.slides.length-this.options.visibleSlides;}}
this.moveTo(this.slides[initialIndex]);}},click:function(event){this.stop();var element=event.findElement('a');if(!element.hasClassName(this.options.disabledClassName)){if(element.hasClassName(this.options.controlClassName)){eval("this."+element.rel+"()");}else if(element.hasClassName(this.options.jumperClassName)){this.moveTo(element.rel);if(this.options.selectedClassName){this.controls.invoke('removeClassName',this.options.selectedClassName);element.addClassName(this.options.selectedClassName);}}}
this.deactivateControls();event.stop();},moveTo:function(element){if(this.options.beforeMove&&(typeof this.options.beforeMove=='function')){this.options.beforeMove();}
this.previous=this.current?this.current:this.slides[0];this.current=$(element);var scrollerOffset=this.scroller.cumulativeOffset();var elementOffset=this.current.cumulativeOffset();if(this.scrolling){this.scrolling.cancel();}
switch(this.options.effect){case'fade':this.scrolling=new Effect.Opacity(this.scroller,{from:1.0,to:0,duration:this.options.duration,afterFinish:(function(){this.scroller.scrollLeft=elementOffset[0]-scrollerOffset[0];this.scroller.scrollTop=elementOffset[1]-scrollerOffset[1];new Effect.Opacity(this.scroller,{from:0,to:1.0,duration:this.options.duration,afterFinish:(function(){if(this.controls){this.activateControls();}
if(this.options.afterMove&&(typeof this.options.afterMove=='function')){this.options.afterMove();}}).bind(this)});}).bind(this)});break;case'scroll':default:var transition;switch(this.options.transition){case'spring':transition=Effect.Transitions.spring;break;case'sinoidal':default:transition=Effect.Transitions.sinoidal;break;}
this.scrolling=new Effect.SmoothScroll(this.scroller,{duration:this.options.duration,x:(elementOffset[0]-scrollerOffset[0]),y:(elementOffset[1]-scrollerOffset[1]),transition:transition,afterFinish:(function(){if(this.controls){this.activateControls();}
if(this.options.afterMove&&(typeof this.options.afterMove=='function')){this.options.afterMove();}
this.scrolling=false;}).bind(this)});break;}
return false;},prev:function(){if(this.current){var currentIndex=this.current._index;var prevIndex=(currentIndex==0)?(this.options.circular?this.slides.length-this.options.visibleSlides:0):currentIndex-1;}else{var prevIndex=(this.options.circular?this.slides.length-this.options.visibleSlides:0);}
if(prevIndex==(this.slides.length-this.options.visibleSlides)&&this.options.circular&&this.options.effect!='fade'){this.scroller.scrollLeft=(this.slides.length-this.options.visibleSlides)*this.slides.first().getWidth();this.scroller.scrollTop=(this.slides.length-this.options.visibleSlides)*this.slides.first().getHeight();prevIndex=this.slides.length-this.options.visibleSlides-1;}
this.moveTo(this.slides[prevIndex]);},next:function(){if(this.current){var currentIndex=this.current._index;var nextIndex=(this.slides.length-this.options.visibleSlides==currentIndex)?(this.options.circular?0:currentIndex):currentIndex+1;}else{var nextIndex=1;}
if(nextIndex==0&&this.options.circular&&this.options.effect!='fade'){this.scroller.scrollLeft=0;this.scroller.scrollTop=0;nextIndex=1;}
if(nextIndex>this.slides.length-(this.options.visibleSlides+1)){nextIndex=this.slides.length-this.options.visibleSlides;}
this.moveTo(this.slides[nextIndex]);},first:function(){this.moveTo(this.slides[0]);},last:function(){this.moveTo(this.slides[this.slides.length-1]);},toggle:function(){if(this.previous){this.moveTo(this.slides[this.previous._index]);}else{return false;}},stop:function(){if(this.timer){clearTimeout(this.timer);}},start:function(){this.periodicallyUpdate();},pause:function(){this.stop();this.activateControls();},resume:function(event){if(event){var related=event.relatedTarget||event.toElement;if(!related||(!this.slides.include(related)&&!this.slides.any(function(slide){return related.descendantOf(slide);}))){this.start();}}else{this.start();}},periodicallyUpdate:function(){if(this.timer!=null){clearTimeout(this.timer);this.next();}
this.timer=setTimeout(this.periodicallyUpdate.bind(this),this.options.frequency*1000);},wheel:function(event){event.cancelBubble=true;event.stop();var delta=0;if(!event){event=window.event;}
if(event.wheelDelta){delta=event.wheelDelta/120;}else if(event.detail){delta=-event.detail/3;}
if(!this.scrolling){this.deactivateControls();if(delta>0){this.prev();}else{this.next();}}
return Math.round(delta);},deactivateControls:function(){this.controls.invoke('addClassName',this.options.disabledClassName);},activateControls:function(){this.controls.invoke('removeClassName',this.options.disabledClassName);}});function createSmoothScrollEffect(){if(Effect.SmoothScroll){return;}
Effect.SmoothScroll=Class.create();Object.extend(Object.extend(Effect.SmoothScroll.prototype,Effect.Base.prototype),{initialize:function(element){this.element=$(element);var options=Object.extend({x:0,y:0,mode:'absolute'},arguments[1]||{});this.start(options);},setup:function(){if(this.options.continuous&&!this.element._ext){this.element.cleanWhitespace();this.element._ext=true;this.element.appendChild(this.element.firstChild);}
this.originalLeft=this.element.scrollLeft;this.originalTop=this.element.scrollTop;if(this.options.mode=='absolute'){this.options.x-=this.originalLeft;this.options.y-=this.originalTop;}},update:function(position){this.element.scrollLeft=this.options.x*position+this.originalLeft;this.element.scrollTop=this.options.y*position+this.originalTop;}});}
var WJCarousel=Class.create({initialize:function(id,options,slideselectors){this.id=id;this.wrapper=$(id+"_brandboxwrapper");this.container=$(id+"_brandboxcontainer");this.content=$(id+"_brandboxcontent");this.slideselectors=slideselectors;this.slidewrapper=this.content.down(this.slideselectors["slidewrapper"]);this._setOptions(options);this._getSlides();if(this.options.horizontal){this._fixWidth();}
this._addObservers();this._initializeControls();this._initializeCarousel();},_getSlides:function(){this.slides=this.slidewrapper.select(this.slideselectors["slides"]);this.first=this.slides.first();if(this.slides.size()>this.options.visibleSlides&&this.options.circular){$R(1,this.options.visibleSlides).each(function(index){this.last=this._addSlide(this.slides[index-1].cloneNode(true));this.last.clonePosition(this.slides[index-1],{setLeft:false,setTop:false});this._cleanIds(this.last);}.bind(this));}
this._activateSlide(this.first);return this.slides;},_activateSlide:function(slide){this.slides.invoke("removeClassName","carousel-slide-active");return slide.addClassName("carousel-slide-active");},_deactivateSlide:function(slide){return slide.removeClassName("carousel-slide-active");},_addSlide:function(slide){this.slides.first().up().appendChild(slide);this.slides.push(slide);return slide;},_cleanIds:function(element){element.id=null;element.select("*[id]").each(function(el){el.id=null;});return element;},_fixWidth:function(){var width=this.first.getDimensions().width;var count=this.slides.size();this.slidewrapper.setStyle({"width":(width*count)+"px"});},_setOptions:function(options){this.options=Object.extend({wheel:false,duration:1,auto:true,frequency:15,circular:true,horizontal:true,visibleSlides:1},(options||{}));return this.options;},_addOption:function(name,value){this.options[name]=value;},_addObservers:function(){this._addOption("beforeMove",this._beforeMove.bind(this));this._addOption("afterMove",this._afterMove.bind(this));},_initializeControls:function(){if(this.slides.size()<=this.options.visibleSlides){this.container.select("div.carousel-controls").invoke("remove");return;}
this.controls=this.container.select("a.carousel-control");if(this.container.select("a.carousel-jumper").size()>0){this.jumpers=this.container.select("a.carousel-jumper");this.jumpers.first().addClassName("carousel-jumper-active");this._addOption("selectedClassName","carousel-jumper-active");this.controls=(this.controls.size()>0)?this.controls.concat(this.jumpers):this.jumpers;}
return this.controls;},_initializeCarousel:function(){this.carousel=new Carousel(this.content,this.slides,this.controls,this.options);return this.carousel;},_afterMove:function(){if(this.jumpers){this.jumpers.invoke("removeClassName",this.carousel.options.selectedClassName);if(this.carousel.current.id){this.container.select("a[rel="+this.carousel.current.id+"]").invoke("addClassName",this.carousel.options.selectedClassName);}
else if(this.carousel.slides[0].id){this.container.select("a[rel="+this.carousel.slides[0].id+"]").invoke("addClassName",this.carousel.options.selectedClassName);}}
if(this.controls&&this.options.circular===false){this.controls.invoke("removeClassName","carousel-control-disabled");var disable=(this.carousel.current===this.slides.first())?"prev":"next";this.controls.each(function(disable,control){if(control.readAttribute("rel")===disable){control.addClassName("carousel-control-disabled");}}.curry(disable));}
this._activateSlide(this.carousel.current);this._fireEvent("end");},_beforeMove:function(){this._fireEvent("start");},_fireEvent:function(name){this.container.fire("brandbox:"+name,{"carousel":this});}});
