(function($){$.widget("bw.scroller",{options:{scrollableArea:"div.scrollableArea",scrollWrapper:"div.scrollWrapper",prevButton:"a#nav-prev",nextButton:"a#nav-next",startAtElementId:""},_create:function(){var self=this,o=this.options,el=this.element;el.data("scrollWrapper",el.find(o.scrollWrapper));el.data("scrollableArea",el.find(o.scrollableArea));el.data("prevButton",el.find(o.prevButton));el.data("nextButton",el.find(o.nextButton));el.data("motherElementOffset",el.offset().left);el.data("scrollXPos",0);el.data("scrollableAreaWidth",0);el.data("startingPosition",0);el.data("getNextElementWidth",true);el.data("visible",true);el.data("initialAjaxContentLoaded",false);el.data("enabled",true);self.recalculateScrollableArea();$(window).bind("resize",function(){self.checkButtons();self._trigger("windowResized")});},moveToDirection:function(direction,step){var self=this,el=this.element,o=this.options,tempScrollableAreaWidth=0;var count=el.data("scrollableArea").children().length;var indexThumb=self.getActiveIndex(direction);if(indexThumb===false){return false;}
if(!(isNaN(indexThumb))){if(direction=='right'){indexThumb=indexThumb+(step-1);}
else{indexThumb=indexThumb-(step+1);}
if(indexThumb<0){indexThumb=0}
while(indexThumb>(count-1)){indexThumb=indexThumb-1;}
el.data("scrollableArea").children().each(function(index){if(index===(indexThumb)){if(direction=='right'){el.data("scrollXPos",tempScrollableAreaWidth-el.data("scrollWrapper").width()+$(this).outerWidth(true))}
else{el.data("scrollXPos",tempScrollableAreaWidth)}}
tempScrollableAreaWidth=tempScrollableAreaWidth+$(this).outerWidth(true);});}
el.data("scrollWrapper").stop().animate({scrollLeft:el.data("scrollXPos")},500,'easeInSine');self.checkButtons();},moveToElement:function(indexThumb,direction){var self=this,el=this.element,o=this.options,tempScrollableAreaWidth=0;new_xpos=0,xpos=0,width=0;var count=el.data("scrollableArea").children().length;if(!(isNaN(indexThumb))){if(indexThumb<0){indexThumb=0}
while(indexThumb>(count-1)){indexThumb=indexThumb-1;}
el.data("scrollableArea").children().each(function(index){if(index===(indexThumb)){if(direction=='right'){new_xpos=tempScrollableAreaWidth-el.data("scrollWrapper").width()+$(this).outerWidth(true);}
else{new_xpos=tempScrollableAreaWidth;}
xpos=tempScrollableAreaWidth;width=$(this).outerWidth(true);}
tempScrollableAreaWidth=tempScrollableAreaWidth+$(this).outerWidth(true);});}
if((xpos<el.data('scrollWrapper').scrollLeft())||((xpos+width)>(el.data('scrollWrapper').scrollLeft()+el.data("scrollWrapper").width()))){el.data("scrollXPos",new_xpos)
el.data("scrollWrapper").scrollLeft(el.data("scrollXPos"));}
self.checkButtons();},recalculateScrollableArea:function(){var tempScrollableAreaWidth=0,foundStartAtElement=false,o=this.options,el=this.element,self=this;el.data("scrollableArea").children().each(function(){if((o.startAtElementId.length>0)&&(($(this).attr("id"))===o.startAtElementId)){el.data("startingPosition",tempScrollableAreaWidth);foundStartAtElement=true}
tempScrollableAreaWidth=tempScrollableAreaWidth+$(this).outerWidth(true)});if(!(foundStartAtElement)){el.data("startAtElementId","")}
el.data("scrollableAreaWidth",tempScrollableAreaWidth);el.data("scrollableArea").width(el.data("scrollableAreaWidth"));el.data("scrollWrapper").scrollLeft(el.data("startingPosition"));el.data("scrollXPos",el.data("startingPosition"));self.checkButtons();},checkButtons:function(){var o=this.options,el=this.element,self=this;el.data("prevButton").removeClass('inactive');el.data("nextButton").removeClass('inactive');if(el.data("scrollXPos")==0){el.data("prevButton").addClass('inactive');}
if(el.data("scrollXPos")==(el.data("scrollableAreaWidth")-el.data("scrollWrapper").width())){el.data("nextButton").addClass('inactive');}},getActiveIndex:function(direction){var self=this,el=this.element,o=this.options,tempScrollableAreaWidth=0,indexThumb=false;el.data("scrollableArea").children(o.countOnlyClass).each(function(index){if(direction=='right'){if((tempScrollableAreaWidth<=(el.data('scrollWrapper').scrollLeft())+el.data("scrollWrapper").width())&&((tempScrollableAreaWidth+$(this).outerWidth(true))>(el.data('scrollWrapper').scrollLeft()+el.data("scrollWrapper").width()))){indexThumb=index;}}
else{if((tempScrollableAreaWidth<el.data('scrollWrapper').scrollLeft())&&((tempScrollableAreaWidth+$(this).outerWidth(true))>=el.data('scrollWrapper').scrollLeft())){indexThumb=index;}}
tempScrollableAreaWidth=tempScrollableAreaWidth+$(this).outerWidth(true);})
return indexThumb;},show:function(){var el=this.element;el.data("visible",true);el.show()},hide:function(){var el=this.element;el.data("visible",false);el.hide()},enable:function(){var el=this.element;el.data("enabled",true)},disable:function(){var el=this.element;clearInterval(el.data("rightScrollInterval"));clearInterval(el.data("leftScrollInterval"));el.data("enabled",false)},destroy:function(){var el=this.element;clearInterval(el.data("rightScrollInterval"));clearInterval(el.data("leftScrollInterval"));el.data("scrollableArea").removeAttr("style");el.data("scrollWrapper").scrollLeft(0);$.Widget.prototype.destroy.apply(this,arguments)}})})(jQuery);
