(function(b){function s(B,h,y){var z=this,D=y,A,C,m,x=false;this.settings=D;this.values=null;this.val=null;this.temp=null;this.setDefaults=function(E){b.extend(f,E)};this.enable=function(){D.disabled=false;if(b(B).is(":input")){b(B).prop("disabled",false)}};this.disable=function(){D.disabled=true;if(b(B).is(":input")){b(B).prop("disabled",true)}};this.formatDate=function(N,F,G){if(!F){return null}var O=b.extend({},this.settings,G),L=function(P){var Q=0;while(J+1<N.length&&N.charAt(J+1)==P){Q++;J++}return Q},I=function(Q,R,P){var S=""+R;if(L(Q)){while(S.length<P){S="0"+S}}return S},H=function(P,S,R,Q){return(L(P)?Q[S]:R[S])},E="",M=false;for(var J=0;J<N.length;J++){if(M){if(N.charAt(J)=="'"&&!L("'")){M=false}else{E+=N.charAt(J)}}else{switch(N.charAt(J)){case"d":E+=I("d",F.getDate(),2);break;case"D":E+=H("D",F.getDay(),O.dayNamesShort,O.dayNames);break;case"o":E+=I("o",(F.getTime()-new Date(F.getFullYear(),0,0).getTime())/86400000,3);break;case"m":E+=I("m",F.getMonth()+1,2);break;case"M":E+=H("M",F.getMonth(),O.monthNamesShort,O.monthNames);break;case"y":E+=(L("y")?F.getFullYear():(F.getYear()%100<10?"0":"")+F.getYear()%100);break;case"h":var K=F.getHours();E+=I("h",(K>12?(K-12):(K==0?12:K)),2);break;case"H":E+=I("H",F.getHours(),2);break;case"i":E+=I("i",F.getMinutes(),2);break;case"s":E+=I("s",F.getSeconds(),2);break;case"a":E+=F.getHours()>11?"pm":"am";break;case"A":E+=F.getHours()>11?"PM":"AM";break;case"'":if(L("'")){E+="'"}else{M=true}break;default:E+=N.charAt(J)}}}return E};this.parseDate=function(U,N,W){var I=new Date();if(!U||!N){return I}N=(typeof N=="object"?N.toString():N+"");var K=b.extend({},this.settings,W),F=I.getFullYear(),Y=I.getMonth(),S=I.getDate(),H=-1,V=I.getHours(),O=I.getMinutes(),G=I.getSeconds(),L=0,R=false,M=function(aa){var ab=(E+1<U.length&&U.charAt(E+1)==aa);if(ab){E++}return ab},Z=function(ab){M(ab);var ac=(ab=="@"?14:(ab=="!"?20:(ab=="y"?4:(ab=="o"?3:2))));var ad=new RegExp("^\\d{1,"+ac+"}");var aa=N.substr(T).match(ad);if(!aa){throw"Missing number at position "+T}T+=aa[0].length;return parseInt(aa[0],10)},J=function(ab,ad,aa){var ae=(M(ab)?aa:ad);for(var ac=0;ac<ae.length;ac++){if(N.substr(T,ae[ac].length).toLowerCase()==ae[ac].toLowerCase()){T+=ae[ac].length;return ac+1}}throw"Unknown name at position "+T},Q=function(){if(N.charAt(T)!=U.charAt(E)){throw"Unexpected literal at position "+T}T++},T=0;for(var E=0;E<U.length;E++){if(R){if(U.charAt(E)=="'"&&!M("'")){R=false}else{Q()}}else{switch(U.charAt(E)){case"d":S=Z("d");break;case"D":J("D",K.dayNamesShort,K.dayNames);break;case"o":H=Z("o");break;case"m":Y=Z("m");break;case"M":Y=J("M",K.monthNamesShort,K.monthNames);break;case"y":F=Z("y");break;case"H":V=Z("H");break;case"h":V=Z("h");break;case"i":O=Z("i");break;case"s":G=Z("s");break;case"a":L=J("a",["am","pm"],["am","pm"])-1;break;case"A":L=J("A",["am","pm"],["am","pm"])-1;break;case"'":if(M("'")){Q()}else{R=true}break;default:Q()}}}if(F<100){F+=new Date().getFullYear()-new Date().getFullYear()%100+(F<=K.shortYearCutoff?0:-100)}if(H>-1){Y=1;S=H;do{var P=32-new Date(F,Y-1,32).getDate();if(S<=P){break}Y++;S-=P}while(true)}if(L&&V<12){V+=12}var X=new Date(F,Y-1,S,V,O,G);if(X.getFullYear()!=F||X.getMonth()+1!=Y||X.getDate()!=S){throw"Invalid date"}return X};this.setValue=function(F){if(F==undefined){F=true}var E=this.formatResult();this.val=E;this.values=this.temp.slice(0);if(F&&b(B).is(":input")){b(B).val(E).change()}};this.getDate=function(){var F=this.values;if(D.preset=="date"){return new Date(F[A],F[C],F[m])}if(D.preset=="time"){var E=(D.ampm&&F[D.seconds?3:2]=="PM"&&(F[0]-0)<12)?(F[0]-0+12):F[0];return new Date(1970,0,1,E,F[1],D.seconds?F[2]:null)}if(D.preset=="datetime"){var E=(D.ampm&&F[D.seconds?6:5]=="PM"&&(F[3]-0)<12)?(F[3]-0+12):F[3];return new Date(F[A],F[C],F[m],E,F[4],D.seconds?F[5]:null)}};this.setDate=function(G,F){if(D.preset.match(/date/i)){this.temp[A]=G.getFullYear();this.temp[C]=G.getMonth();this.temp[m]=G.getDate()}if(D.preset=="time"){var E=G.getHours();this.temp[0]=(D.ampm)?(E>12?(E-12):(E==0?12:E)):E;this.temp[1]=G.getMinutes();if(D.seconds){this.temp[2]=G.getSeconds()}if(D.ampm){this.temp[D.seconds?3:2]=E>11?"PM":"AM"}}if(D.preset=="datetime"){var E=G.getHours();this.temp[3]=(D.ampm)?(E>12?(E-12):(E==0?12:E)):E;this.temp[4]=G.getMinutes();if(D.seconds){this.temp[5]=G.getSeconds()}if(D.ampm){this.temp[D.seconds?6:5]=E>11?"PM":"AM"}}this.setValue(F)};this.parseValue=function(I){if(this.preset){var E=[];if(D.preset=="date"){try{var H=this.parseDate(D.dateFormat,I,D)}catch(G){var H=new Date()}E[A]=H.getFullYear();E[C]=H.getMonth();E[m]=H.getDate()}else{if(D.preset=="time"){try{var H=this.parseDate(D.timeFormat,I,D)}catch(G){var H=new Date()}var F=H.getHours();E[0]=(D.ampm)?(F>12?(F-12):(F==0?12:F)):F;E[1]=H.getMinutes();if(D.seconds){E[2]=H.getSeconds()}if(D.ampm){E[D.seconds?3:2]=F>11?"PM":"AM"}}else{if(D.preset=="datetime"){try{var H=this.parseDate(D.dateFormat+" "+D.timeFormat,I,D)}catch(G){var H=new Date()}var F=H.getHours();E[A]=H.getFullYear();E[C]=H.getMonth();E[m]=H.getDate();E[3]=(D.ampm)?(F>12?(F-12):(F==0?12:F)):F;E[4]=H.getMinutes();if(D.seconds){E[5]=H.getSeconds()}if(D.ampm){E[D.seconds?6:5]=F>11?"PM":"AM"}}}}return E}return D.parseValue(I)};this.formatResult=function(){var F=this.temp;if(this.preset){if(D.preset=="date"){return this.formatDate(D.dateFormat,new Date(F[A],F[C],F[m]),D)}else{if(D.preset=="datetime"){var E=(D.ampm)?((F[D.seconds?6:5]=="PM"&&(F[3]-0)<12)?(F[3]-0+12):(F[D.seconds?6:5]=="AM"&&(F[3]==12)?0:F[3])):F[3];return this.formatDate(D.dateFormat+" "+D.timeFormat,new Date(F[A],F[C],F[m],E,F[4],D.seconds?F[5]:null),D)}else{if(D.preset=="time"){var E=(D.ampm)?((F[D.seconds?3:2]=="PM"&&(F[0]-0)<12)?(F[0]-0+12):(F[D.seconds?3:2]=="AM"&&(F[0]==12)?0:F[0])):F[0];return this.formatDate(D.timeFormat,new Date(1970,0,1,E,F[1],D.seconds?F[2]:null),D)}}}}return D.formatResult(F)};this.validate=function(F){if(this.preset&&D.preset.match(/date/i)&&((F==A)||(F==C)||(F==-1))){var G=32-new Date(this.temp[A],this.temp[C],32).getDate()-1;var E=b("ul:eq("+m+")",h);b("li",E).show();b("li:gt("+G+")",E).hide();if(this.temp[m]>G){E.addClass("dwa").css("top",(r*(n-G-1))+"px");this.temp[m]=b("li:eq("+G+")",E).data("val")}}else{D.validate(F)}};this.hide=function(){if(D.onClose(this.val,this)===false){return false}b(".dwtd").prop("disabled",false).removeClass("dwtd");b(B).blur();h.hide();i.hide();x=false;if(this.preset){D.wheels=null}b(window).unbind("resize.dw")};this.show=function(){if(D.disabled||x){return false}D.beforeShow(B,this);r=D.height;n=Math.round(D.rows/2);a=this;this.init();if(this.preset){D.wheels=new Array();if(D.preset.match(/date/i)){var E={};for(var G=0;G<3;G++){if(G==A){E[D.yearText]={};for(var J=D.startYear;J<=D.endYear;J++){E[D.yearText][J]=D.dateOrder.search(/yy/i)<0?J.toString().substr(2,2):J.toString()}}else{if(G==C){E[D.monthText]={};for(var J=0;J<12;J++){E[D.monthText][J]=(D.dateOrder.search(/MM/)<0?(D.dateOrder.search(/M/)<0?(D.dateOrder.search(/mm/)<0?(J+1):(J<9)?("0"+(J+1)):(J+1)):D.monthNamesShort[J]):D.monthNames[J])}}else{if(G==m){E[D.dayText]={};for(var J=1;J<32;J++){E[D.dayText][J]=D.dateOrder.search(/dd/i)<0?J:(J<10)?("0"+J):J}}}}}D.wheels.push(E)}if(D.preset.match(/time/i)){D.stepHour=(D.stepHour<1)?1:parseInt(D.stepHour);D.stepMinute=(D.stepMinute<1)?1:parseInt(D.stepMinute);D.stepSecond=(D.stepSecond<1)?1:parseInt(D.stepSecond);var E={};E[D.hourText]={};for(var J=0;J<(D.ampm?13:24);J+=D.stepHour){E[D.hourText][J]=(J<10)?("0"+J):J}E[D.minuteText]={};for(var J=0;J<60;J+=D.stepMinute){E[D.minuteText][J]=(J<10)?("0"+J):J}if(D.seconds){E[D.secText]={};for(var J=0;J<60;J+=D.stepSecond){E[D.secText][J]=(J<10)?("0"+J):J}}if(D.ampm){E[D.ampmText]={};E[D.ampmText]["AM"]="AM";E[D.ampmText]["PM"]="PM"}D.wheels.push(E)}}b(".dwc",h).remove();for(var J=0;J<D.wheels.length;J++){var F=b('<div class="dwc'+(D.mode=="clickpick"?" dwpm":"")+'"><div class="dwwc dwrc"><div class="clear" style="clear:both;"></div></div>').insertBefore(b(".dwbc",h));for(var I in D.wheels[J]){var K=b(".dwwc .clear",F);var E=b('<div class="dwwl dwrc">'+(D.mode=="clickpick"?'<div class="dwwb dwwbp">+</div><div class="dwwb dwwbm">&ndash;</div>':"")+'<div class="dwl">'+I+'</div><div class="dww dwrc"><ul></ul><div class="dwwo"></div></div><div class="dwwol"></div></div>').insertBefore(K);for(var H in D.wheels[J][I]){b('<li class="val_'+H+'">'+D.wheels[J][I][H]+"</li>").data("val",H).appendTo(b("ul",E))}}}b(".dww ul",h).each(function(M){var L=b("li",this).index(b("li.val_"+z.temp[M],this));while((L<0)&&(--z.temp[M]>=0)){L=b("li",this).index(b("li.val_"+z.temp[M],this))}var N=r*(n-(L<0?0:L)-1);b(this).css("top",N)});b(".dwv",h).html(this.formatResult());z.validate(-1);b("#dw_set",h).text(D.setText).unbind().bind("click",function(L){z.setValue();D.onSelect(z.val,a);z.hide();return false});b("#dw_cancel",h).text(D.cancelText).unbind().bind("click",function(L){D.onCancel(z.val,a);z.hide();return false});b(":input:not(:disabled)").addClass("dwtd");b(":input").prop("disabled",true);i.show();h.attr("class","dw "+D.theme).show();x=true;b(".dww, .dwwl",h).height(D.rows*r);b(".dww",h).each(function(){b(this).width(b(this).parent().width()<D.width?D.width:b(this).parent().width())});b(".dwbc a",h).attr("class",D.btnClass);b(".dww li, .dwwb",h).css({height:r,lineHeight:r+"px"});b(".dwwc",h).each(function(){var L=0;b(".dwwl",this).each(function(){L+=b(this).outerWidth(true)});b(this).width(L)});b(".dwc",h).each(function(){b(this).width(b(".dwwc",this).outerWidth(true))});this.pos();b(window).bind("resize.dw",function(){z.pos()})};this.pos=function(){var E=0,H=0,K=b(window).width(),G=b(window).height(),I=b(window).scrollTop(),F,J;b(".dwc",h).each(function(){F=b(this).outerWidth(true);E+=F;H=(F>H)?F:H});F=E>K?H:E;h.width(F);F=h.outerWidth();J=h.outerHeight();h.css({left:(K-F)/2,top:I+(G-J)/2});i.height(0);i.height(b(document).height())};this.init=function(){var E=D.dateOrder.search(/y/i),F=D.dateOrder.search(/m/i),G=D.dateOrder.search(/d/i);A=(E<F)?(E<G?0:1):(E<G?1:2);C=(F<E)?(F<G?0:1):(F<G?1:2);m=(G<E)?(G<F?0:1):(G<F?1:2);this.preset=(D.wheels===null);this.temp=this.parseValue(b(B).val()?b(B).val():"");this.setValue(false)};this.init();if(b(B).is(":input")&&D.showOnFocus){b(B).data("dwro",b(B).prop("readonly")).prop("readonly",true)}b(B).addClass("scroller").unbind("focus.dw").bind("focus.dw",function(E){if(D.showOnFocus){z.show()}})}var j,i,r,n,a,g={},v=new Date(),q=v.getTime(),l=false,w=null,c,o,d,e=("ontouchstart" in window),p=e?"touchstart":"mousedown",u=e?"touchmove":"mousemove",k=e?"touchend":"mouseup",f={width:90,height:40,rows:3,disabled:false,showOnFocus:true,wheels:null,theme:"",mode:"scroller",preset:"date",dateFormat:"mm/dd/yy",dateOrder:"mmddy",ampm:true,seconds:false,timeFormat:"hh:ii A",startYear:v.getFullYear()-10,endYear:v.getFullYear()+10,monthNames:["January","February","March","April","May","June","July","August","September","October","November","December"],monthNamesShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],dayNames:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],dayNamesShort:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],shortYearCutoff:"+10",monthText:"Month",dayText:"Day",yearText:"Year",hourText:"Hours",minuteText:"Minutes",secText:"Seconds",ampmText:"&nbsp;",setText:"Set",cancelText:"Cancel",btnClass:"dwb",stepHour:1,stepMinute:1,stepSecond:1,beforeShow:function(){},onClose:function(){},onSelect:function(){},onCancel:function(){},formatResult:function(x){var h="";for(var m=0;m<x.length;m++){h+=(m>0?" ":"")+x[m]}return h},parseValue:function(h){return h.split(" ")},validate:function(){return true}},t={init:function(y){if(y===undefined){y={}}var h={};if(y.theme=="ios"){h.dateOrder="MMdyy";h.rows=5;h.height=30;h.width=55}if(y.mode=="clickpick"){h.height=50;h.rows=3}var z=b.extend({},f,h,y),B=false,m=false;if(b(".dw").length){i=b(".dwo");j=b(".dw")}else{i=b('<div class="dwo"></div>').hide().appendTo("body");j=b('<div class="dw"><div class="dwv">&nbsp;</div><div class="dwbc" style="clear:both;"><span class="dwbw dwb-s"><a id="dw_set" href="#"></a></span><span class="dwbw dwb-c"><a id="dw_cancel" href="#"></a></span></div></div>');j.hide().appendTo("body");b(document).bind(u,function(D){if(l){o=e?D.originalEvent.changedTouches[0].pageY:D.pageY;w.removeClass("dwa").css("top",(d+o-c)+"px");D.preventDefault();D.stopPropagation();return false}});function x(F,G){var E=b("ul",j).index(F),D=b("li:visible",F).length;G=G>(n-1)?(n-1):G;G=G<(n-D)?(n-D):G;F.addClass("dwa").css("top",G*r);a.temp[E]=b("li:eq("+(n-1-G)+")",F).data("val");a.validate(E);b(".dwv",j).html(a.formatResult())}function C(D){if(B){var E=D.css("top").replace(/px/i,"")-0,F=(E-r)/r;F=F<(n-b("li:visible",D).length)?(n-1):F;x(D,F)}else{clearInterval(B)}}function A(D){if(m){var E=D.css("top").replace(/px/i,"")-0,F=(E+r)/r;F=F>(n-1)?(n-b("li:visible",D).length):F;x(D,F)}else{clearInterval(m)}}b(document).bind(k,function(D){if(l){var E=Math.round((d+o-c)/r);x(w,E);l=false;w=null;D.preventDefault();D.stopPropagation()}clearInterval(B);clearInterval(m);B=false;m=false;b(".dwb-a").removeClass("dwb-a")});j.delegate(".dwwl","DOMMouseScroll mousewheel",function(F){var H=F.wheelDelta?(F.wheelDelta/120):(F.detail?(-F.detail/3):0),D=b("ul",this),E=D.css("top").replace(/px/i,"")-0,G=Math.round((E+H*r)/r);x(D,G);F.preventDefault();F.stopPropagation()}).delegate(".dwb, .dwwb",p,function(D){b(this).addClass("dwb-a")}).delegate(".dwwbp",p,function(E){E.preventDefault();var D=b(this).closest(".dwwl").find("ul");clearInterval(B);B=setInterval(function(){C(D)},200);C(D)}).delegate(".dwwbm",p,function(E){E.preventDefault();var D=b(this).closest(".dwwl").find("ul");clearInterval(m);m=setInterval(function(){A(D)},200);A(D)}).delegate(".dwwl",p,function(F){if(!l&&a.settings.mode=="scroller"){var E=e?F.originalEvent.changedTouches[0].pageX:F.pageX,D=b(this).offset().left;l=true;w=b("ul",this);d=w.css("top").replace(/px/i,"")-0;c=e?F.originalEvent.changedTouches[0].pageY:F.pageY;o=c;F.preventDefault();F.stopPropagation()}})}return this.each(function(){if(!this.id){q+=1;this.id="scoller"+q}g[this.id]=new s(this,j,z)})},enable:function(){return this.each(function(){if(g[this.id]){g[this.id].enable()}})},disable:function(){return this.each(function(){if(g[this.id]){g[this.id].disable()}})},isDisabled:function(){if(g[this[0].id]){return g[this[0].id].settings.disabled}},option:function(h,m){return this.each(function(){if(g[this.id]){if(typeof h==="object"){b.extend(g[this.id].settings,h)}else{g[this.id].settings[h]=m}g[this.id].init()}})},setValue:function(m,h){if(h==undefined){h=false}return this.each(function(){if(g[this.id]){g[this.id].temp=m;g[this.id].setValue(m,h)}})},getValue:function(){if(g[this[0].id]){return g[this[0].id].values}},setDate:function(m,h){if(h==undefined){h=false}return this.each(function(){if(g[this.id]){g[this.id].setDate(m,h)}})},getDate:function(){if(g[this[0].id]){return g[this[0].id].getDate()}},show:function(){if(g[this[0].id]){return g[this[0].id].show()}},hide:function(){return this.each(function(){if(g[this.id]){g[this.id].hide()}})},destroy:function(){return this.each(function(){if(g[this.id]){b(this).unbind("focus.dw").removeClass("scroller");if(b(this).is(":input")){b(this).prop("readonly",b(this).data("dwro"))}delete g[this.id]}})}};b.fn.scroller=function(h){if(t[h]){return t[h].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof h==="object"||!h){return t.init.apply(this,arguments)}else{b.error("Unknown method")}}};b.scroller=new s(null,null,f)})(jQuery);

