(function($,window,document,undefined){var move,ms=$.mobiscroll,classes=ms.classes,instances=ms.instances,util=ms.util,pr=util.jsPrefix,has3d=util.has3d,hasFlex=util.hasFlex,getCoord=util.getCoord,constrain=util.constrain,testTouch=util.testTouch;classes.Scroller=function(el,settings,inherit){var $markup,btn,isScrollable,itemHeight,s,trigger,click,moved,start,startTime,stop,p,min,max,target,index,lines,timer,that=this,$elm=$(el),iv={},pos={},pixels={},wheels=[];function onStart(ev){if(testTouch(ev,this)&&!move&&!click&&!btn&&!isReadOnly(this)){ev.preventDefault();ev.stopPropagation();move=true;isScrollable=s.mode!='clickpick';target=$('.dw-ul',this);setGlobals(target);moved=iv[index]!==undefined;p=moved?getCurrentPosition(target):pos[index];start=getCoord(ev,'Y');startTime=new Date();stop=start;scroll(target,index,p,0.001);if(isScrollable){target.closest('.dwwl').addClass('dwa');}
if(ev.type==='mousedown'){$(document).on('mousemove',onMove).on('mouseup',onEnd);}}}
function onMove(ev){if(move){if(isScrollable){ev.preventDefault();ev.stopPropagation();stop=getCoord(ev,'Y');if(Math.abs(stop-start)>3||moved){scroll(target,index,constrain(p+(start-stop)/itemHeight,min-1,max+1));moved=true;}}}}
function onEnd(ev){if(move){var time=new Date()-startTime,val=constrain(p+(start-stop)/itemHeight,min-1,max+1),speed,dist,tindex,ttop=target.offset().top;ev.stopPropagation();if(has3d&&time<300){speed=(stop-start)/time;dist=(speed*speed)/s.speedUnit;if(stop-start<0){dist=-dist;}}else{dist=stop-start;}
tindex=Math.round(p-dist/itemHeight);if(!moved){var idx=Math.floor((stop-ttop)/itemHeight),li=$($('.dw-li',target)[idx]),valid=li.hasClass('dw-v'),hl=isScrollable;if(trigger('onValueTap',[li])!==false&&valid){tindex=idx;}else{hl=true;}
if(hl&&valid){li.addClass('dw-hl');setTimeout(function(){li.removeClass('dw-hl');},100);}}
if(isScrollable){calc(target,tindex,0,true,Math.round(val));}
if(ev.type==='mouseup'){$(document).off('mousemove',onMove).off('mouseup',onEnd);}
move=false;}}
function onBtnStart(ev){btn=$(this);if(testTouch(ev,this)){step(ev,btn.closest('.dwwl'),btn.hasClass('dwwbp')?plus:minus);}
if(ev.type==='mousedown'){$(document).on('mouseup',onBtnEnd);}}
function onBtnEnd(ev){btn=null;if(click){clearInterval(timer);click=false;}
if(ev.type==='mouseup'){$(document).off('mouseup',onBtnEnd);}}
function onKeyDown(ev){if(ev.keyCode==38){step(ev,$(this),minus);}else if(ev.keyCode==40){step(ev,$(this),plus);}}
function onKeyUp(){if(click){clearInterval(timer);click=false;}}
function onScroll(ev){if(!isReadOnly(this)){ev.preventDefault();ev=ev.originalEvent||ev;var delta=ev.wheelDelta?(ev.wheelDelta/120):(ev.detail?(-ev.detail/3):0),t=$('.dw-ul',this);setGlobals(t);calc(t,Math.round(pos[index]-delta),delta<0?1:2);}}
function step(ev,w,func){ev.stopPropagation();ev.preventDefault();if(!click&&!isReadOnly(w)&&!w.hasClass('dwa')){click=true;var t=w.find('.dw-ul');setGlobals(t);clearInterval(timer);timer=setInterval(function(){func(t);},s.delay);func(t);}}
function isReadOnly(wh){if($.isArray(s.readonly)){var i=$('.dwwl',$markup).index(wh);return s.readonly[i];}
return s.readonly;}
function generateWheelItems(i){var html='<div class="dw-bf">',w=wheels[i],l=1,labels=w.labels||[],values=w.values,keys=w.keys||values;$.each(values,function(j,v){if(l%20===0){html+='</div><div class="dw-bf">';}
html+='<div role="option" aria-selected="false" class="dw-li dw-v" data-val="'+keys[j]+'"'+(labels[j]?' aria-label="'+labels[j]+'"':'')+' style="height:'+itemHeight+'px;line-height:'+itemHeight+'px;">'+
'<div class="dw-i"'+(lines>1?' style="line-height:'+Math.round(itemHeight/lines)+'px;font-size:'+Math.round(itemHeight/lines*0.8)+'px;"':'')+'>'+v+'</div></div>';l++;});html+='</div>';return html;}
function setGlobals(t){var multiple=t.closest('.dwwl').hasClass('dwwms');min=$('.dw-li',t).index($(multiple?'.dw-li':'.dw-v',t).eq(0));max=Math.max(min,$('.dw-li',t).index($(multiple?'.dw-li':'.dw-v',t).eq(-1))-(multiple?s.rows-1:0));index=$('.dw-ul',$markup).index(t);}
function formatHeader(v){var t=s.headerText;return t?(typeof t==='function'?t.call(el,v):t.replace(/\{value\}/i,v)):'';}
function getCurrentPosition(t){return Math.round(-util.getPosition(t,true)/itemHeight);}
function ready(t,i){clearTimeout(iv[i]);delete iv[i];t.closest('.dwwl').removeClass('dwa');}
function scroll(t,index,val,time,active){var px=-val*itemHeight,style=t[0].style;if(px==pixels[index]&&iv[index]){return;}
pixels[index]=px;if(has3d){style[pr+'Transition']=util.prefix+'transform '+(time?time.toFixed(3):0)+'s ease-out';style[pr+'Transform']='translate3d(0,'+px+'px,0)';}else{style.top=px+'px';}
if(iv[index]){ready(t,index);}
if(time&&active){t.closest('.dwwl').addClass('dwa');iv[index]=setTimeout(function(){ready(t,index);},time*1000);}
pos[index]=val;}
function getValid(val,t,dir,multiple){var cell=$('.dw-li[data-val="'+val+'"]',t),cells=$('.dw-li',t),v=cells.index(cell),l=cells.length;if(multiple){setGlobals(t);}else if(!cell.hasClass('dw-v')){var cell1=cell,cell2=cell,dist1=0,dist2=0;while(v-dist1>=0&&!cell1.hasClass('dw-v')){dist1++;cell1=cells.eq(v-dist1);}
while(v+dist2<l&&!cell2.hasClass('dw-v')){dist2++;cell2=cells.eq(v+dist2);}
if(((dist2<dist1&&dist2&&dir!==2)||!dist1||(v-dist1<0)||dir==1)&&cell2.hasClass('dw-v')){cell=cell2;v=v+dist2;}else{cell=cell1;v=v-dist1;}}
return{cell:cell,v:multiple?constrain(v,min,max):v,val:cell.hasClass('dw-v')?cell.attr('data-val'):null};}
function scrollToPos(time,index,manual,dir,active){if(trigger('validate',[$markup,index,time,dir])!==false){$('.dw-ul',$markup).each(function(i){var t=$(this),multiple=t.closest('.dwwl').hasClass('dwwms'),sc=i==index||index===undefined,res=getValid(that._tempWheelArray[i],t,dir,multiple),cell=res.cell;if(!(cell.hasClass('dw-sel'))||sc){that._tempWheelArray[i]=res.val;if(!multiple){$('.dw-sel',t).removeAttr('aria-selected');cell.attr('aria-selected','true');}
$('.dw-sel',t).removeClass('dw-sel');cell.addClass('dw-sel');scroll(t,i,res.v,sc?time:0.1,sc?active:false);}});that._tempValue=s.formatResult(that._tempWheelArray);if(that.live){that._hasValue=manual||that._hasValue;setValue(manual,manual,0,true);}
that._header.html(formatHeader(that._tempValue));if(manual){trigger('onChange',[that._tempValue]);}
trigger('onValidated',[]);}}
function calc(t,val,dir,anim,orig){val=constrain(val,min,max);var cell=$('.dw-li',t).eq(val),o=orig===undefined?val:orig,active=orig!==undefined,idx=index,dist=Math.abs(val-o),time=anim?(val==o?0.1:dist*s.timeUnit*Math.max(0.5,(100-dist)/100)):0;that._tempWheelArray[idx]=cell.attr('data-val');scroll(t,idx,val,time,active);setTimeout(function(){scrollToPos(time,idx,true,dir,active);},10);}
function plus(t){var val=pos[index]+1;calc(t,val>max?min:val,1,true);}
function minus(t){var val=pos[index]-1;calc(t,val<min?max:val,2,true);}
function setValue(fill,change,time,noscroll,temp){if(that._isVisible&&!noscroll){scrollToPos(time);}
that._tempValue=s.formatResult(that._tempWheelArray);if(util.isNumeric(that._tempValue)){that._tempValue=+that._tempValue;}
if(!temp){that._wheelArray=that._tempWheelArray.slice(0);that._value=that._hasValue?that._tempValue:null;}
if(fill){trigger('onValueFill',[that._hasValue?that._tempValue:'',change]);if(that._isInput){$elm.val(that._hasValue?that._tempValue:'');if(change){that._preventChange=true;$elm.change();}}}}
classes.Widget.call(this,el,settings,true);that.setVal=that._setVal=function(val,fill,change,temp,time){that._hasValue=val!==null&&val!==undefined;that._tempWheelArray=$.isArray(val)?val.slice(0):s.parseValue.call(el,val,that);setValue(fill,change===undefined?fill:change,time,false,temp);};that.getVal=that._getVal=function(temp){return that._hasValue?that[temp?'_tempValue':'_value']:null;};that.setArrayVal=that.setVal;that.getArrayVal=function(temp){return temp?that._tempWheelArray:that._wheelArray;};that.setValue=function(val,fill,time,temp,change){that.setVal(val,fill,change,temp,time);};that.getValue=that.getArrayVal;that.changeWheel=function(idx,time,manual){if($markup){var i=0,nr=idx.length;$.each(s.wheels,function(j,wg){$.each(wg,function(k,w){if($.inArray(i,idx)>-1){wheels[i]=w;$('.dw-ul',$markup).eq(i).html(generateWheelItems(i));nr--;if(!nr){that.position();scrollToPos(time,undefined,manual);return false;}}
i++;});if(!nr){return false;}});}};that.getValidCell=getValid;that._generateContent=function(){var lbl,html='',l=0;$.each(s.wheels,function(i,wg){html+='<div class="mbsc-w-p dwc'+(s.mode!='scroller'?' dwpm':' dwsc')+(s.showLabel?'':' dwhl')+'">'+
'<div class="dwwc"'+(s.maxWidth?'':' style="max-width:600px;"')+'>'+
(hasFlex?'':'<table class="dw-tbl" cellpadding="0" cellspacing="0"><tr>');$.each(wg,function(j,w){wheels[l]=w;lbl=w.label!==undefined?w.label:j;html+='<'+(hasFlex?'div':'td')+' class="dwfl"'+' style="'+
(s.fixedWidth?('width:'+(s.fixedWidth[l]||s.fixedWidth)+'px;'):(s.minWidth?('min-width:'+(s.minWidth[l]||s.minWidth)+'px;'):'min-width:'+s.width+'px;')+
(s.maxWidth?('max-width:'+(s.maxWidth[l]||s.maxWidth)+'px;'):''))+'">'+
'<div class="dwwl dwwl'+l+(w.multiple?' dwwms':'')+'">'+
(s.mode!='scroller'?'<div class="dwb-e dwwb dwwbp '+(s.btnPlusClass||'')+'" style="height:'+itemHeight+'px;line-height:'+itemHeight+'px;"><span>+</span></div>'+
'<div class="dwb-e dwwb dwwbm '+(s.btnMinusClass||'')+'" style="height:'+itemHeight+'px;line-height:'+itemHeight+'px;"><span>&ndash;</span></div>':'')+
'<div class="dwl">'+lbl+'</div>'+
'<div tabindex="0" aria-live="off" aria-label="'+lbl+'" role="listbox" class="dwww">'+
'<div class="dww" style="height:'+(s.rows*itemHeight)+'px;">'+
'<div class="dw-ul" style="margin-top:'+(w.multiple?0:s.rows/2*itemHeight-itemHeight/2)+'px;">';html+=generateWheelItems(l)+
'</div></div><div class="dwwo"></div></div><div class="dwwol"'+
(s.selectedLineHeight?' style="height:'+itemHeight+'px;margin-top:-'+(itemHeight/2+(s.selectedLineBorder||0))+'px;"':'')+'></div></div>'+
(hasFlex?'</div>':'</td>');l++;});html+=(hasFlex?'':'</tr></table>')+'</div></div>';});return html;};that._attachEvents=function($markup){$markup.on('DOMMouseScroll mousewheel','.dwwl',onScroll).on('keydown','.dwwl',onKeyDown).on('keyup','.dwwl',onKeyUp).on('touchstart mousedown','.dwwl',onStart).on('touchmove','.dwwl',onMove).on('touchend','.dwwl',onEnd).on('touchstart mousedown','.dwwb',onBtnStart).on('touchend','.dwwb',onBtnEnd);};that._markupReady=function(){$markup=that._markup;scrollToPos();};that._fillValue=function(){that._hasValue=true;setValue(true,true,0,true);};that._readValue=function(){var v=$elm.val()||'';that._hasValue=v!=='';that._tempWheelArray=that._wheelArray?that._wheelArray.slice(0):s.parseValue(v,that);setValue();};that._processSettings=function(){s=that.settings;trigger=that.trigger;itemHeight=s.height;lines=s.multiline;that._isLiquid=(s.layout||(/top|bottom/.test(s.display)&&s.wheels.length==1?'liquid':''))==='liquid';if(lines>1){s.cssClass=(s.cssClass||'')+' dw-ml';}};that._selectedValues={};if(!inherit){instances[el.id]=that;that.init(settings);}};classes.Scroller.prototype._class='scroller';classes.Scroller.prototype._defaults=$.extend({},{lang:'en',setText:'Set',selectedText:'Selected',closeText:'Close',cancelText:'Cancel',clearText:'Clear',disabled:false,closeOnOverlay:true,showOnFocus:true,showOnTap:true,display:'modal',scrollLock:true,tap:true,btnClass:'dwb',btnWidth:true,focusOnClose:false},{minWidth:80,height:40,rows:3,multiline:1,delay:300,readonly:false,showLabel:true,wheels:[],mode:'scroller',preset:'',speedUnit:0.0012,timeUnit:0.08,formatResult:function(d){return d.join(' ');},parseValue:function(value,inst){var val=value.split(' '),ret=[],i=0,keys;$.each(inst.settings.wheels,function(j,wg){$.each(wg,function(k,w){keys=w.keys||w.values;if($.inArray(val[i],keys)!==-1){ret.push(val[i]);}else{ret.push(keys[0]);}
i++;});});return ret;}});})(jQuery,window,document);