(function(){"use strict";u8.Base.Widgets.ImageRotator=function(i,t){this.Widget(i,$.extend({},u8.Base.Widgets.ImageRotator.defaults,t));if(this.valueToFrame&&typeof this.valueToFrame=="function"){this._valueToFrameCalculator=this.valueToFrame}if(!this._valueToFrameCalculator||typeof this._valueToFrameCalculator!="function"){this._valueToFrameCalculator=this._valueToFrameFromRange}if(this.range.min==0&&this.range.max==100&&this._valueToFrameCalculator==this._valueToFrameFromRange&&this.numberOfFrames==1){this.isSwitch=true}if(!this._isValid(this.getValue())){this.setValue(this.range.min)}this._$imageRotator=null;this._imageSize=null;this._backgroundOriginal=null;this._backgroundPosition=null;this._frameWidth=null;this._activeFrame=0;this._isCreated=false;this._isDisposed=false;if(this.value){delete this.value}this.bind(t);this._create()};u8.Base.Widgets.ImageRotator.makeClass({name:"ImageRotator",deriveFrom:u8.Base.Widgets.Widget,events:["Create","ValueChange","FrameChange","Click"]});u8.Base.Widgets.ImageRotator.defaults={numberOfFrames:1,orientation:"h",range:{min:0,max:100}};$.extend(u8.Base.Widgets.ImageRotator.prototype,{getValue:function(){return this._value},setValue:function(i){if(!this._isValid(i)){i=-1}if(this._value===i){return}this._value=parseInt(i,10);if(this._isCreated){this.raise("ValueChange",{value:this._value});this._updateActiveFrame()}},update:function(){this._create()},dispose:function(){if(this._isDisposed){return}delete this._$imageRotator;this.Widget_dispose()},_create:function(){if(this._isCreated){this.children.clear();this._$imageRotator.empty()}var i=this.$("<span />").addClass(this.image).addClass("img").css({position:"relative",visibility:"hidden"}).appendTo("body");this._imageSize={width:i.width(),height:i.height()};i.remove();var t=this.orientation=="h"?this._imageSize.width:this._imageSize.height;var e=t/this.numberOfFrames;if(this.sizeOfFrame){this._frameWidth=this.sizeOfFrame;if(this.sizeOfFrame>e){this._frameStep=(t-this.sizeOfFrame)/(this.numberOfFrames-1)}else{this._frameStep=e+(e-this.sizeOfFrame)/(this.numberOfFrames-1)}}else{this._frameWidth=this._frameStep=e}this._$imageRotator=this.$(this.element).css(this.orientation=="h"?"width":"height",this._frameWidth).addClass(this.image).addClass("img").click(u8.tools.raiseFn(this,"Click"));this._backgroundPosition=this._$imageRotator.css("background-position");if(this._backgroundPosition){this._backgroundPosition=this._backgroundPosition.split(" ");this._backgroundPosition={left:parseInt(this._backgroundPosition[0],10),top:parseInt(this._backgroundPosition[1],10)}}this._backgroundOriginal={top:this._backgroundPosition.top.valueOf(),left:this._backgroundPosition.left.valueOf()};this.withoutEvents(this._updateActiveFrame);u8.tools.created(this,{value:this._value})},_updateActiveFrame:function(){var i=this.getValue();if(typeof i=="undefined"){return}var t=this._valueToFrameCalculator(i);if(t==this._activeFrame){return}if(t>-1){this._activeFrame=t;this._setVisible(true);if(this.orientation=="h"){this._backgroundPosition.left=this._backgroundOriginal.left-this._frameStep*t}else if(this.orientation=="v"){this._backgroundPosition.top=this._backgroundOriginal.top-this._frameStep*t}var e=this._backgroundPosition.top;var a=this._backgroundPosition.left;this._$imageRotator.css({backgroundPosition:a+"px "+e+"px"})}else{this._activeFrame=-1;this._setVisible(false)}this.raise("FrameChange",{activeFrame:t})},_setVisible:function(i){this._$imageRotator.css("visibility",i===true?"visible":"hidden")},_valueToFrameFromRange:function(i){var t=this.range.max-this.range.min;var e=!!this.isSwitch;var a=this.numberOfFrames+(e?1:0);for(var s=1;s<=a;s+=1){if(i<t/(this.numberOfFrames+(e?1:0))*s){return s-(e?2:1)}}return a-1},_isValid:function(i){if(isNaN(i)||i===null){return false}if(i<this.range.min||i>this.range.max){return false}return true}})})();