(function(a,b){a.widget("mobile.datebox",a.mobile.widget,{options:{theme:"c",pickPageTheme:"b",pickPageInputTheme:"e",pickPageButtonTheme:"a",pickPageHighButtonTheme:"e",pickPageOHighButtonTheme:"e",pickPageODHighButtonTheme:"e",pickPageTodayButtonTheme:"e",pickPageSlideButtonTheme:"d",centerWindow:false,calHighToday:true,calHighPicked:true,noAnimation:false,disabled:false,wheelExists:false,swipeEnabled:true,zindex:"500",experimentalReg:true,debug:false,setDateButtonLabel:"Set Date",setTimeButtonLabel:"Set Time",setDurationButtonLabel:"Set Duration",titleDateDialogLabel:"Set Date",titleTimeDialogLabel:"Set Time",titleDialogLabel:false,meridiemLetters:["AM","PM"],daysOfWeek:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],daysOfWeekShort:["Su","Mo","Tu","We","Th","Fr","Sa"],monthsOfYear:["January","February","March","April","May","June","July","August","September","October","November","December"],monthsOfYearShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],durationLabel:["Days","Hours","Minutes","Seconds"],durationDays:["Day","Days"],timeFormat:24,mode:"datebox",calShowDays:true,calShowOnlyMonth:false,useDialogForceTrue:false,useDialogForceFalse:false,useDialog:false,useModal:false,useInline:false,noButtonFocusMode:false,noButton:false,noSetButton:false,closeCallback:false,open:false,fieldsOrder:false,dateFieldOrder:["m","d","y"],timeFieldOrder:["h","i","s"],durationOrder:["d","h","i","s"],headerFormat:"ddd, mmm dd, YYYY",dateFormat:"YYYY-MM-DD",minuteStep:1,calWeekMode:false,calWeekModeFirstDay:1,calWeekModeHighlight:true,calStartDay:0,defaultDate:false,minYear:false,maxYear:false,afterToday:false,beforeToday:false,maxDays:false,minDays:false,highDays:false,highDates:false,blackDays:false,blackDates:false,durationNoDays:false,durationShort:true,durationSteppers:{d:1,h:1,i:1,s:1},disabledDayColor:"#888"},_dateboxHandler:function(c,d){if(!c.isPropagationStopped()){switch(d.method){case"close":a(this).data("datebox").close();break;case"open":a(this).data("datebox").open();break;case"set":a(this).val(d.value);a(this).trigger("change");break;case"doset":if(a(this).data("datebox").options.mode==="timebox"||a(this).data("datebox").options.mode==="durationbox"){a(this).trigger("datebox",{method:"set",value:a(this).data("datebox")._formatTime(a(this).data("datebox").theDate)})}else{a(this).trigger("datebox",{method:"set",value:a(this).data("datebox")._formatDate(a(this).data("datebox").theDate)})}case"dooffset":a(this).data("datebox")._offset(d.type,d.amount,true);break;case"dorefresh":a(this).data("datebox")._update();break}}},_zeroPad:function(c){return((c<10)?"0":"")+String(c)},_makeOrd:function(d){var c=d%10;if(d>9&&d<21){return"th"}if(c>3){return"th"}return["th","st","nd","rd"][c]},_isInt:function(c){return(c.toString().search(/^[0-9]+$/)===0)},_dstAdjust:function(c){if(!c){return null}c.setHours(c.getHours()>12?c.getHours()+2:0);return c},_getFirstDay:function(c){return new Date(c.getFullYear(),c.getMonth(),1).getDay()},_getLastDate:function(c){return 32-this._dstAdjust(new Date(c.getFullYear(),c.getMonth(),32)).getDate()},_getLastDateBefore:function(c){return 32-this._dstAdjust(new Date(c.getFullYear(),c.getMonth()-1,32)).getDate()},_formatter:function(d,c){d=d.replace("SS",this._makeOrd(c.getDate()));d=d.replace("YYYY",c.getFullYear());d=d.replace("mmm",this.options.monthsOfYear[c.getMonth()]);d=d.replace("MM",this._zeroPad(c.getMonth()+1));d=d.replace("mm",c.getMonth()+1);d=d.replace("ddd",this.options.daysOfWeek[c.getDay()]);d=d.replace("DD",this._zeroPad(c.getDate()));d=d.replace("dd",c.getDate());return d},_formatHeader:function(c){return this._formatter(this.options.headerFormat,c)},_formatDate:function(c){return this._formatter(this.options.dateFormat,c)},_isoDate:function(f,c,e){return String(f)+"-"+((c<10)?"0":"")+String(c)+"-"+((e<10)?"0":"")+String(e)},_formatTime:function(e){var d=this,c="0",g,f,l,k="",j=0;if(this.options.mode==="durationbox"){f=((d.theDate.getTime()-d.theDate.getMilliseconds())/1000)-((d.initDate.getTime()-d.initDate.getMilliseconds())/1000);l=parseInt(f/(60*60*24),10);if(!d.options.durationNoDays){k=(l===0)?"":String(l)+" "+((l>1)?this.options.durationDays[1]:this.options.durationDays[0])+", ";f=f-(l*60*60*24)}g=parseInt(f/(60*60),10);f=f-(g*60*60);l=parseInt(f/(60),10);f=f-(l*60);if(d.options.durationShort){return k+((g>0||k!=="")?d._zeroPad(g)+":":"")+((l>0||g>0||k!=="")?d._zeroPad(l)+":":"")+((l>0||g>0||k!=="")?d._zeroPad(parseInt(f,10)):String(f))}else{return k+d._zeroPad(g)+":"+d._zeroPad(l)+":"+d._zeroPad(parseInt(f,10))}}if(this.options.timeFormat===12){if(e.getHours()>11){j=1;c=d._zeroPad(e.getHours()-12);if(c==="00"){c="12"}}else{j=0;c=d._zeroPad(e.getHours());if(c==="00"){c="12"}}return c+":"+d._zeroPad(e.getMinutes())+" "+this.options.meridiemLetters[j]}else{return d._zeroPad(e.getHours())+":"+d._zeroPad(e.getMinutes())}},_makeDate:function(p){p=a.trim(p);var m=this.options,h=this,q=m.dateFormat.replace(/[mydo ]/gi,"").substr(0,1),c=m.dateFormat,t=null,k=null,f=null,n=m.dateFormat.split(q),w=p.split(q),u=new Date(),l=1,j=0,e=2000,g=0,v={"12":/^([012]?[0-9]):([0-5][0-9])\s*(am?|pm?)?$/i,"24":/^([012]?[0-9]):([0-5][0-9])$/i},s=/^(?:([0-9]+) .+, )?(?:([0-9]+):)?(?:([0-9]+):)?([0-9]+)$/i,d=null,r;if(m.mode==="durationbox"){d=s.exec(p);if(d===null){return new Date(h.initDate.getTime())}else{g=((h.initDate.getTime()-h.initDate.getMilliseconds())/1000)+parseInt(d[4],10);if(typeof d[3]!=="undefined"){g=g+(parseInt(d[3],10)*60)}if(typeof d[2]!=="undefined"){if(typeof d[3]==="undefined"){g=g+(parseInt(d[2],10)*60)}else{g=g+(parseInt(d[2],10)*60*60)}}if(typeof d[1]!=="undefined"){g=g+(parseInt(d[1],10)*60*60*24)}g=g*1000;return new Date(g)}}if(m.mode==="timebox"){if(m.timeFormat===12){d=v[m.timeFormat].exec(p);if(d===null||d.length<3){return new Date()}if(typeof d[3]==="undefined"){d[3]="AM"}if(d[1]<12&&d[3].toLowerCase().charAt(0)==="p"){d[1]=parseInt(d[1],10)+12}else{if(d[1]===12){if(d[3].toLowerCase().charAt(0)==="a"){d[1]=0}else{d[1]=12}}else{d[1]=parseInt(d[1],10)}}}else{d=v[m.timeFormat].exec(p);if(d===null||d.length<2||d[1]>24){return new Date()}}u.setMinutes(d[2]);u.setHours(d[1]);return u}else{if(m.experimentalReg){c=c.replace(/ddd|SS/g,".+?");c=c.replace(/mmm/g,"(.+?)");c=c.replace(/yyyy|dd|mm/ig,"([0-9ydm]+)");c=RegExp("^"+c+"$","i");t=c.exec(p);k=c.exec(m.dateFormat);if(m.debug){console.log({info:"EXPERIMENTAL REGEX MODE ENABLED",regex:c,input:t,format:k})}if(t===null||t.length!==k.length){if(m.defaultDate!==false){if(a.isArray(m.defaultDate)&&m.defaultDate.length===3){return new Date(m.defaultDate[0],m.defaultDate[1],m.defaultDate[2],0,0,0,0)}else{f=m.defaultDate.split("-");if(f.length===3){u=new Date(parseInt(f[0],10),parseInt(f[1],10)-1,parseInt(f[2],10),0,0,0,0);if(isNaN(u.getDate())){u=new Date()}}}}}else{for(r=0;r<t.length;r++){if(k[r].match(/^dd$/i)){u.setDate(parseInt(t[r],10))}if(k[r].match(/^mm$/i)){u.setMonth(parseInt(t[r],10)-1)}if(k[r].match(/^yyyy$/i)){u.setYear(parseInt(t[r],10))}if(k[r].match(/^mmm$/i)){f=a.inArray(t[r],m.monthsOfYear);if(f>-1){u.setMonth(f)}}}}return new Date(u.getFullYear(),u.getMonth(),u.getDate(),0,0,0,0)}if(n.length!==w.length){if(m.defaultDate!==false){u=new Date(m.defaultDate);if(!u.getDate()){return new Date()}else{return u}}else{return new Date()}}else{for(r=0;r<n.length;r++){if(n[r].match(/d/i)){l=w[r]}if(n[r].match(/m/i)){j=w[r]}if(n[r].match(/y/i)){e=w[r]}}u=new Date(e,j-1,l,0,0,0,0);if(!u.getDate()){return new Date()}else{return u}}}},_checker:function(c){return parseInt(String(c.getFullYear())+this._zeroPad(c.getMonth()+1)+this._zeroPad(c.getDate()),10)},_hoover:function(c){a(c).toggleClass("ui-btn-up-"+a(c).attr("data-theme")+" ui-btn-down-"+a(c).attr("data-theme"))},_offset:function(f,d,g){var c=this,e=this.options;if(typeof(g)==="undefined"){g=true}c.input.trigger("datebox",{method:"offset",type:f,amount:d});switch(f){case"y":c.theDate.setYear(c.theDate.getFullYear()+d);break;case"m":c.theDate.setMonth(c.theDate.getMonth()+d);break;case"d":c.theDate.setDate(c.theDate.getDate()+d);break;case"h":c.theDate.setHours(c.theDate.getHours()+d);break;case"i":c.theDate.setMinutes(c.theDate.getMinutes()+d);break;case"s":c.theDate.setSeconds(c.theDate.getSeconds()+d);break;case"a":if(c.pickerMeri.val()===e.meridiemLetters[0]){c._offset("h",12,false)}else{c._offset("h",-12,false)}break}if(g===true){c._update()}},_updateduration:function(){var c=this,d=(c.initDate.getTime()-c.initDate.getMilliseconds())/1000;if(!c._isInt(c.pickerDay.val())){c.pickerDay.val(0)}if(!c._isInt(c.pickerHour.val())){c.pickerHour.val(0)}if(!c._isInt(c.pickerMins.val())){c.pickerMins.val(0)}if(!c._isInt(c.pickerSecs.val())){c.pickerSecs.val(0)}d=d+(parseInt(c.pickerDay.val(),10)*60*60*24);d=d+(parseInt(c.pickerHour.val(),10)*60*60);d=d+(parseInt(c.pickerMins.val(),10)*60);d=d+(parseInt(c.pickerSecs.val(),10));c.theDate.setTime(d*1000);c._update()},_update:function(){var s=this,e=s.options,p=null,g,h,j,q,n,m,k,f,r,c,d={d:60*60*24,h:60*60,i:60,s:1},l={};s.input.trigger("datebox",{method:"refresh"});if(e.mode==="durationbox"){g=((s.theDate.getTime()-s.theDate.getMilliseconds())/1000)-((s.initDate.getTime()-s.initDate.getMilliseconds())/1000);if(g<0){g=0;s.theDate.setTime(s.initDate.getTime())}m=parseInt(g/d.d,10);g=g-(m*d.d);s.pickerDay.val(m);m=parseInt(g/d.h,10);g=g-(m*d.h);s.pickerHour.val(m);m=parseInt(g/d.i,10);g=g-(m*d.i);s.pickerMins.val(m);s.pickerSecs.val(parseInt(g,10))}if(e.mode==="timebox"){if(e.minuteStep!==1){g=s.theDate.getMinutes()%e.minuteStep;if(g!==0){s.theDate.setMinutes(s.theDate.getMinutes()-g)}}s.pickerMins.val(s._zeroPad(s.theDate.getMinutes()));if(e.timeFormat===12){if(s.theDate.getHours()>11){s.pickerMeri.val(e.meridiemLetters[1]);if(s.theDate.getHours()===12){s.pickerHour.val(12)}else{s.pickerHour.val(s.theDate.getHours()-12)}}else{s.pickerMeri.val(e.meridiemLetters[0]);if(s.theDate.getHours()===0){s.pickerHour.val(12)}else{s.pickerHour.val(s.theDate.getHours())}}}else{s.pickerHour.val(s.theDate.getHours())}}if(e.mode==="slidebox"){if(e.afterToday!==false){p=new Date();if(s.theDate<p){s.theDate=p}}if(e.maxDays!==false){p=new Date();p.setDate(p.getDate()+e.maxDays);if(s.theDate>p){s.theDate=p}}if(e.minDays!==false){p=new Date();p.setDate(p.getDate()-e.minDays);if(s.theDate<p){s.theDate=p}}if(e.maxYear!==false){p=new Date(e.maxYear,0,1);p.setDate(p.getDate()-1);if(s.theDate>p){s.theDate=p}}if(e.minYear!==false){p=new Date(e.minYear,0,1);if(s.theDate<p){s.theDate=p}}f=s._makeDate(s.input.val());s.controlsHeader.html(s._formatHeader(s.theDate));s.controlsInput.html("");for(m=0;m<3;m++){r=a("<div>",{"data-rowtype":m});if(e.wheelExists){r.bind("mousewheel",function(i,o){i.preventDefault();s._offset(["y","m","d"][a(this).attr("data-rowtype")],((o>0)?1:-1))})}n=a("<div>",{"class":"ui-datebox-sliderow-int","data-rowtype":m}).appendTo(r);if(e.swipeEnabled){n.bind(s.START_DRAG,function(i){if(!s.dragMove){s.dragMove=true;s.dragTarget=a(this);s.dragPos=parseInt(s.dragTarget.css("marginLeft").replace(/px/i,""),10);s.dragStart=s.touch?i.originalEvent.changedTouches[0].pageX:i.pageX;s.dragEnd=false;i.stopPropagation();i.preventDefault()}})}switch(m){case 0:r.addClass("ui-datebox-sliderow-ym");n.css("marginLeft","-333px");for(g=-5;g<6;g++){k=((f.getFullYear()===(s.theDate.getFullYear()+g))?e.pickPageHighButtonTheme:e.pickPageSlideButtonTheme);if(g===0){k=e.pickPageButtonTheme}a("<div>",{"class":"ui-datebox-slideyear ui-corner-all ui-btn-up-"+k}).text(s.theDate.getFullYear()+g).attr("data-offset",g).attr("data-theme",k).bind("vmouseover vmouseout",function(){s._hoover(this)}).bind("vclick",function(i){i.preventDefault();s._offset("y",parseInt(a(this).attr("data-offset"),10))}).appendTo(n)}break;case 1:r.addClass("ui-datebox-sliderow-ym");n.css("marginLeft","-204px");for(g=-6;g<7;g++){p=new Date(s.theDate.getFullYear(),s.theDate.getMonth(),s.theDate.getDate());p.setMonth(p.getMonth()+g);k=(f.getMonth()===p.getMonth()&&f.getYear()===p.getYear())?e.pickPageHighButtonTheme:e.pickPageSlideButtonTheme;if(g===0){k=e.pickPageButtonTheme}a("<div>",{"class":"ui-datebox-slidemonth ui-corner-all ui-btn-up-"+k}).attr("data-offset",g).attr("data-theme",k).text(e.monthsOfYearShort[p.getMonth()]).bind("vmouseover vmouseout",function(){s._hoover(this)}).bind("vclick",function(i){i.preventDefault();s._offset("m",parseInt(a(this).attr("data-offset"),10))}).appendTo(n)}break;case 2:r.addClass("ui-datebox-sliderow-d");n.css("marginLeft","-386px");for(g=-15;g<16;g++){p=new Date(s.theDate.getFullYear(),s.theDate.getMonth(),s.theDate.getDate());p.setDate(p.getDate()+g);k=(f.getDate()===p.getDate()&&f.getMonth()===p.getMonth()&&f.getYear()===p.getYear())?e.pickPageHighButtonTheme:e.pickPageSlideButtonTheme;if(g===0){k=e.pickPageButtonTheme}a("<div>",{"class":"ui-datebox-slideday ui-corner-all ui-btn-up-"+k}).attr("data-offset",g).attr("data-theme",k).html(p.getDate()+'<br /><span class="ui-datebox-slidewday">'+e.daysOfWeekShort[p.getDay()]+"</span>").bind("vmouseover vmouseout",function(){s._hoover(this)}).bind("vclick",function(i){i.preventDefault();s._offset("d",parseInt(a(this).attr("data-offset"),10))}).appendTo(n)}break}r.appendTo(s.controlsInput)}}if(e.mode==="datebox"){if(e.afterToday!==false){p=new Date();if(s.theDate<p){s.theDate=p}}if(e.beforeToday!==false){p=new Date();if(s.theDate>p){s.theDate=p}}if(e.maxDays!==false){p=new Date();p.setDate(p.getDate()+e.maxDays);if(s.theDate>p){s.theDate=p}}if(e.minDays!==false){p=new Date();p.setDate(p.getDate()-e.minDays);if(s.theDate<p){s.theDate=p}}if(e.maxYear!==false){p=new Date(e.maxYear,0,1);p.setDate(p.getDate()-1);if(s.theDate>p){s.theDate=p}}if(e.minYear!==false){p=new Date(e.minYear,0,1);if(s.theDate<p){s.theDate=p}}s.controlsHeader.html(s._formatHeader(s.theDate));s.pickerMon.val(s.theDate.getMonth()+1);s.pickerDay.val(s.theDate.getDate());s.pickerYar.val(s.theDate.getFullYear())}if(e.mode==="calbox"){s.controlsInput.text(e.monthsOfYear[s.theDate.getMonth()]+" "+s.theDate.getFullYear());s.controlsSet.html("");l={today:-1,highlightDay:-1,presetDay:-1,nexttoday:1,thisDate:new Date(),maxDate:new Date(),minDate:new Date(),currentMonth:false,weekMode:0,weekDays:null,thisTheme:e.pickPageButtoTheme};l.start=s._getFirstDay(s.theDate);l.end=s._getLastDate(s.theDate);l.lastend=s._getLastDateBefore(s.theDate);l.presetDate=s._makeDate(s.input.val());l.prevtoday=l.lastend-(l.start-1);l.checkDates=(e.afterToday!==false||e.beforeToday!==false||e.notToday!==false||e.maxDays!==false||e.minDays!==false||e.blackDates!==false||e.blackDays!==false);if(e.calStartDay>0){l.start=l.start-e.calStartDay;if(l.start<0){l.start=l.start+7}}if(l.thisDate.getMonth()===s.theDate.getMonth()&&l.thisDate.getFullYear()===s.theDate.getFullYear()){l.currentMonth=true;l.highlightDay=l.thisDate.getDate()}if(s._checker(l.presetDate)===s._checker(s.theDate)){l.presetDay=l.presetDate.getDate()}s.calNoPrev=false;s.calNoNext=false;if(e.afterToday===true&&(l.currentMonth===true||(l.thisDate.getMonth()>=s.theDate.getMonth()&&s.theDate.getFullYear()===l.thisDate.getFullYear()))){s.calNoPrev=true}if(e.beforeToday===true&&(l.currentMonth===true||(l.thisDate.getMonth()<=s.theDate.getMonth()&&s.theDate.getFullYear()===l.thisDate.getFullYear()))){s.calNoNext=true}if(e.minDays!==false){l.minDate.setDate(l.minDate.getDate()-e.minDays);if(s.theDate.getFullYear()===l.minDate.getFullYear()&&s.theDate.getMonth()<=l.minDate.getMonth()){s.calNoPrev=true}}if(e.maxDays!==false){l.maxDate.setDate(l.maxDate.getDate()+e.maxDays);if(s.theDate.getFullYear()===l.maxDate.getFullYear()&&s.theDate.getMonth()>=l.maxDate.getMonth()){s.calNoNext=true}}if(e.calShowDays){if(e.daysOfWeekShort.length<8){e.daysOfWeekShort=e.daysOfWeekShort.concat(e.daysOfWeekShort)}l.weekDays=a("<div>",{"class":"ui-datebox-gridrow"}).appendTo(s.controlsSet);for(g=0;g<=6;g++){a("<div>"+e.daysOfWeekShort[g+e.calStartDay]+"</div>").addClass("ui-datebox-griddate ui-datebox-griddate-empty ui-datebox-griddate-label").appendTo(l.weekDays)}}for(h=0;h<=5;h++){if(h===0||(h>0&&(l.today>0&&l.today<=l.end))){n=a("<div>",{"class":"ui-datebox-gridrow"}).appendTo(s.controlsSet);for(j=0;j<=6;j++){if(j===0){l.weekMode=(l.today<1)?(l.prevtoday-l.lastend+e.calWeekModeFirstDay):(l.today+e.calWeekModeFirstDay)}if(j===l.start&&h===0){l.today=1}if(l.today>l.end){l.today=-1}if(l.today<1){if(e.calShowOnlyMonth){a("<div>",{"class":"ui-datebox-griddate ui-datebox-griddate-empty"}).appendTo(n)}else{if((e.blackDays!==false&&a.inArray(j,e.blackDays)>-1)||(e.blackDates!==false&&a.inArray(s._isoDate(s.theDate.getFullYear(),(s.theDate.getMonth()),l.prevtoday),e.blackDates)>-1)||(e.blackDates!==false&&a.inArray(s._isoDate(s.theDate.getFullYear(),(s.theDate.getMonth()+2),l.nexttoday),e.blackDates)>-1)){q=true}else{q=false}if(h===0){a("<div>"+String(l.prevtoday)+"</div>").addClass("ui-datebox-griddate ui-datebox-griddate-empty").appendTo(n).attr("data-date",((e.calWeekMode)?(l.weekMode+l.lastend):l.prevtoday)).bind((!q)?"vclick":"error",function(i){i.preventDefault();if(!s.calNoPrev){s.theDate.setMonth(s.theDate.getMonth()-1);s.theDate.setDate(a(this).attr("data-date"));s.input.trigger("datebox",{method:"set",value:s._formatDate(s.theDate)});s.input.trigger("datebox",{method:"close"})}});l.prevtoday++}else{a("<div>"+String(l.nexttoday)+"</div>").addClass("ui-datebox-griddate ui-datebox-griddate-empty").appendTo(n).attr("data-date",((e.calWeekMode)?l.weekMode:l.nexttoday)).bind((!q)?"vclick":"error",function(i){i.preventDefault();if(!s.calNoNext){s.theDate.setDate(a(this).attr("data-date"));if(!e.calWeekMode){s.theDate.setMonth(s.theDate.getMonth()+1)}s.input.trigger("datebox",{method:"set",value:s._formatDate(s.theDate)});s.input.trigger("datebox",{method:"close"})}});l.nexttoday++}}}else{q=false;if(l.checkDates){if(e.afterToday&&s._checker(l.thisDate)>(s._checker(s.theDate)+l.today-s.theDate.getDate())){q=true}if(!q&&e.beforeToday&&s._checker(l.thisDate)<(s._checker(s.theDate)+l.today-s.theDate.getDate())){q=true}if(!q&&e.notToday&&l.today===l.highlightDay){q=true}if(!q&&e.maxDays!==false&&s._checker(l.maxDate)<(s._checker(s.theDate)+l.today-s.theDate.getDate())){q=true}if(!q&&e.minDays!==false&&s._checker(l.minDate)>(s._checker(s.theDate)+l.today-s.theDate.getDate())){q=true}if(!q&&(e.blackDays!==false||e.blackDates!==false)){if((a.inArray(j,e.blackDays)>-1)||(a.inArray(s._isoDate(s.theDate.getFullYear(),s.theDate.getMonth()+1,l.today),e.blackDates)>-1)){q=true}}}if(e.calHighPicked&&l.today===l.presetDay){l.thisTheme=e.pickPageHighButtonTheme}else{if(e.calHighToday&&l.today===l.highlightDay){l.thisTheme=e.pickPageTodayButtonTheme}else{if(a.isArray(e.highDates)&&(a.inArray(s._isoDate(s.theDate.getFullYear(),s.theDate.getMonth()+1,l.today),e.highDates)>-1)){l.thisTheme=e.pickPageOHighButtonTheme}else{if(a.isArray(e.highDays)&&a.inArray(j,e.highDays)>-1){l.thisTheme=e.pickPageODHighButtonTheme}else{l.thisTheme=e.pickPageButtonTheme}}}}a("<div>"+String(l.today)+"</div>").addClass("ui-datebox-griddate ui-corner-all").attr("data-date",((e.calWeekMode)?l.weekMode:l.today)).attr("data-theme",l.thisTheme).appendTo(n).addClass("ui-btn-up-"+l.thisTheme).bind("vmouseover vmouseout",function(){if(e.calWeekMode!==false&&e.calWeekModeHighlight===true){a(this).parent().find("div").each(function(){s._hoover(this)})}else{s._hoover(this)}}).bind((!q)?"vclick":"error",function(i){i.preventDefault();s.theDate.setDate(a(this).attr("data-date"));s.input.trigger("datebox",{method:"set",value:s._formatDate(s.theDate)});s.input.trigger("datebox",{method:"close"})}).css((q)?"color":"nocolor",e.disabledDayColor);l.today++}}}}}},_create:function(){var h=this,i=a.extend(this.options,this.element.data("options")),f=this.element,x=f.wrap('<div class="ui-input-datebox ui-shadow-inset ui-corner-all ui-body-'+i.theme+'"></div>').parent(),l=new Date(),t=new Date(l.getTime()),g=((i.titleDialogLabel===false)?((i.mode==="timebox")?i.titleTimeDialogLabel:i.titleDateDialogLabel):i.titleDialogLabel),k=a('<a href="#" class="ui-input-clear" title="date picker">date picker</a>').bind("vclick",function(o){o.preventDefault();if(!i.disabled){h.input.trigger("datebox",{method:"open"})}setTimeout(function(){a(o.target).closest("a").removeClass(a.mobile.activeBtnClass)},300)}).appendTo(x).buttonMarkup({icon:"grid",iconpos:"notext",corners:true,shadow:true}).css({"vertical-align":"middle","float":"right"}),c=f.closest(".ui-page"),p=a("<div data-role='dialog' class='ui-dialog-datebox' data-theme='"+i.pickPageTheme+"' ><div data-role='header' data-backbtn='false' data-theme='a'><div class='ui-title'>"+g+"</div></div><div data-role='content'></div></div>").appendTo(a.mobile.pageContainer).page().css("minHeight","0px").css("zIndex",i.zindex).addClass("pop"),d=p.find(".ui-content"),e=("ontouchstart" in window),n=e?"touchstart":"mousedown",r=e?"touchmove":"mousemove",j=e?"touchend":"mouseup",u=false,s=false,m=false,q=false,v=false,w=0;a("label[for="+f.attr("id")+"]").addClass("ui-input-text").css("verticalAlign","middle");if(i.mode==="datebox"&&i.fieldsOrder===false){i.fieldsOrder=i.dateFieldOrder}if(i.mode==="timebox"&&i.fieldsOrder===false){i.fieldsOrder=i.timeFieldOrder}if(i.noButtonFocusMode||i.useInline||i.noButton){k.hide()}x.bind("vclick",function(){if(!i.disabled&&i.noButtonFocusMode){f.trigger("datebox",{method:"open"})}});f.removeClass("ui-corner-all ui-shadow-inset").focus(function(){if(!i.disabled){x.addClass("ui-focus");if(i.noButtonFocusMode){x.addClass("ui-focus");f.trigger("datebox",{method:"open"})}}f.removeClass("ui-focus")}).blur(function(){x.removeClass("ui-focus");f.removeClass("ui-focus")}).change(function(){h.theDate=h._makeDate(h.input.val());h._update()});f.bind("datebox",h._dateboxHandler);p.find(".ui-header a").bind("vclick",function(o){o.preventDefault();o.stopImmediatePropagation();h.input.trigger("datebox",{method:"close"})});a.extend(h,{pickPage:p,thisPage:c,pickPageContent:d,input:f,theDate:l,initDate:t,focusedEl:x,touch:e,START_DRAG:n,MOVE_DRAG:r,END_DRAG:j,dragMove:u,dragStart:s,dragEnd:m,dragPos:q});if(typeof a.event.special.mousewheel!=="undefined"){i.wheelExists=true}h._buildPage();if(i.swipeEnabled){a(document).bind(h.MOVE_DRAG,function(o){if(h.dragMove){if(i.mode==="slidebox"){h.dragEnd=h.touch?o.originalEvent.changedTouches[0].pageX:o.pageX;h.dragTarget.css("marginLeft",(h.dragPos+h.dragEnd-h.dragStart)+"px");o.preventDefault();o.stopPropagation();return false}else{if(i.mode==="durationbox"||i.mode==="timebox"||i.mode==="datebox"){h.dragEnd=h.touch?o.originalEvent.changedTouches[0].pageY:o.pageY;if((h.dragEnd-h.dragStart)%2===0){w=(h.dragEnd-h.dragStart)/-2;if(w<h.dragPos){h._offset(h.dragTarget,-1*(h.dragTarget==="i"?i.minuteStep:1))}else{if(w>h.dragPos){h._offset(h.dragTarget,1*(h.dragTarget==="i"?i.minuteStep:1))}}h.dragPos=w}o.preventDefault();o.stopPropagation();return false}}}});a(document).bind(h.END_DRAG,function(o){if(h.dragMove){h.dragMove=false;if(i.mode==="slidebox"){if(h.dragEnd!==false&&Math.abs(h.dragStart-h.dragEnd)>25){o.preventDefault();o.stopPropagation();switch(h.dragTarget.parent().data("rowtype")){case 0:h._offset("y",parseInt((h.dragStart-h.dragEnd)/84,10));break;case 1:h._offset("m",parseInt((h.dragStart-h.dragEnd)/51,10));break;case 2:h._offset("d",parseInt((h.dragStart-h.dragEnd)/32,10));break}}}h.dragStart=false;h.dragEnd=false}})}if(f.is(":disabled")){h.disable()}},_buildPage:function(){var m=this,p=m.options,j,s,t=a("<div><a href='#'></a></div>"),e=a("<div>",{"class":"ui-datebox-container ui-overlay-shadow ui-corner-all ui-datebox-hidden pop ui-body-"+p.pickPageTheme}).css("zIndex",p.zindex),u=a("<input type='text' />").addClass("ui-input-text ui-corner-all ui-shadow-inset ui-datebox-input ui-body-"+p.pickPageInputTheme),i=a("<div>",{"class":"ui-datebox-controls"}),h,z,l,q,r,d,f,k,y,A,n,c,w=false,v=false,g=a("<div>",{"class":"ui-datebox-screen ui-datebox-hidden"+((p.useModal)?" ui-datebox-screen-modal":"")}).css({"z-index":p.zindex-1}).appendTo(m.thisPage).bind("vclick",function(o){o.preventDefault();m.input.trigger("datebox",{method:"close"})});if(p.noAnimation){e.removeClass("pop")}if(p.mode==="durationbox"){h=i.clone().removeClass("ui-datebox-controls").addClass("ui-datebox-scontrols").appendTo(e);z=h.clone().appendTo(e);l=h.clone().appendTo(e);q=i.clone().appendTo(e);A=u.removeClass("ui-datebox-input").clone().keyup(function(){if(a(this).val()!==""){m._updateduration()}});d=A.clone().keyup(function(){if(a(this).val()!==""){m._updateduration()}});f=A.clone().keyup(function(){if(a(this).val()!==""){m._updateduration()}});c=A.clone().keyup(function(){if(a(this).val()!==""){m._updateduration()}});if(p.wheelExists){A.bind("mousewheel",function(o,x){o.preventDefault();m._offset("d",((x<0)?-1:1)*p.durationSteppers.d)});d.bind("mousewheel",function(o,x){o.preventDefault();m._offset("h",((x<0)?-1:1)*p.durationSteppers.h)});f.bind("mousewheel",function(o,x){o.preventDefault();m._offset("i",((x<0)?-1:1)*p.durationSteppers.i)});c.bind("mousewheel",function(o,x){o.preventDefault();m._offset("s",((x<0)?-1:1)*p.durationSteppers.s)})}for(j=0;j<p.durationOrder.length;j++){switch(p.durationOrder[j]){case"d":a("<div>",{"class":"ui-datebox-sinput","data-field":"d"}).append(A).appendTo(z).prepend("<label>"+p.durationLabel[0]+"</label>");break;case"h":a("<div>",{"class":"ui-datebox-sinput","data-field":"h"}).append(d).appendTo(z).prepend("<label>"+p.durationLabel[1]+"</label>");break;case"i":a("<div>",{"class":"ui-datebox-sinput","data-field":"i"}).append(f).appendTo(z).prepend("<label>"+p.durationLabel[2]+"</label>");break;case"s":a("<div>",{"class":"ui-datebox-sinput","data-field":"s"}).append(c).appendTo(z).prepend("<label>"+p.durationLabel[3]+"</label>");break}}if(p.swipeEnabled){z.find("input").bind(m.START_DRAG,function(o){if(!m.dragMove){m.dragMove=true;m.dragTarget=a(this).parent().data("field");m.dragPos=0;m.dragStart=m.touch?o.originalEvent.changedTouches[0].pageY:o.pageY;m.dragEnd=false;o.stopPropagation()}})}if(p.noSetButton===false){a("<a href='#'>"+p.setDurationButtonLabel+"</a>").appendTo(q).buttonMarkup({theme:p.pickPageTheme,icon:"check",iconpos:"left",corners:true,shadow:true}).bind("vclick",function(o){o.preventDefault();m.input.trigger("datebox",{method:"set",value:m._formatTime(m.theDate)});m.input.trigger("datebox",{method:"close"})})}for(j=0;j<p.durationOrder.length;j++){t.clone().appendTo(h).buttonMarkup({theme:p.pickPageButtonTheme,icon:"plus",iconpos:"bottom",corners:true,shadow:true}).attr("data-field",p.durationOrder[j]).bind("vclick",function(o){o.preventDefault();m._offset(a(this).attr("data-field"),p.durationSteppers[a(this).attr("data-field")])});t.clone().appendTo(l).buttonMarkup({theme:p.pickPageButtonTheme,icon:"minus",iconpos:"top",corners:true,shadow:true}).attr("data-field",p.durationOrder[j]).bind("vclick",function(o){o.preventDefault();m._offset(a(this).attr("data-field"),-1*p.durationSteppers[a(this).attr("data-field")])})}a.extend(m,{pickerHour:d,pickerMins:f,pickerDay:A,pickerSecs:c});e.appendTo(m.thisPage)}if(p.mode==="datebox"||p.mode==="timebox"){r=a("<div class='ui-datebox-header'><h4>Unitialized</h4></div>").appendTo(e).find("h4");h=i.clone().appendTo(e);z=i.clone().appendTo(e);l=i.clone().appendTo(e);q=i.clone().appendTo(e);if(p.mode==="timebox"){r.parent().html("")}y=u.clone().attr("data-field","m").keyup(function(){if(a(this).val()!==""&&m._isInt(a(this).val())){m.theDate.setMonth(parseInt(a(this).val(),10)-1);m._update()}});A=y.clone().attr("data-field","d").keyup(function(){if(a(this).val()!==""&&m._isInt(a(this).val())){m.theDate.setDate(parseInt(a(this).val(),10));m._update()}});n=y.clone().attr("data-field","y").keyup(function(){if(a(this).val()!==""&&m._isInt(a(this).val())){m.theDate.setFullYear(parseInt(a(this).val(),10));m._update()}});d=u.clone().attr("data-field","h").keyup(function(){if(a(this).val()!==""&&m._isInt(a(this).val())){s=parseInt(a(this).val(),10);if(s===12){if(p.timeFormat===12&&k.val()===p.meridiemLetters[0]){s=0}}m.theDate.setHours(s);m._update()}});f=u.clone().attr("data-field","i").keyup(function(){if(a(this).val()!==""&&m._isInt(a(this).val())){m.theDate.setMinutes(parseInt(a(this).val(),10));m._update()}});k=u.clone().attr("data-field","a").keyup(function(){if(a(this).val()!==""){m._update()}});if(p.wheelExists){n.bind("mousewheel",function(o,x){o.preventDefault();m._offset("y",(x<0)?-1:1)});y.bind("mousewheel",function(o,x){o.preventDefault();m._offset("m",(x<0)?-1:1)});A.bind("mousewheel",function(o,x){o.preventDefault();m._offset("d",(x<0)?-1:1)});d.bind("mousewheel",function(o,x){o.preventDefault();m._offset("h",(x<0)?-1:1)});f.bind("mousewheel",function(o,x){o.preventDefault();m._offset("i",((x<0)?-1:1)*p.minuteStep)});k.bind("mousewheel",function(o,x){o.preventDefault();m._offset("a",x)})}for(j=0;j<=p.fieldsOrder.length;j++){if(p.fieldsOrder[j]==="y"){n.appendTo(z)}if(p.fieldsOrder[j]==="m"){y.appendTo(z)}if(p.fieldsOrder[j]==="d"){A.appendTo(z)}if(p.fieldsOrder[j]==="h"){d.appendTo(z)}if(p.fieldsOrder[j]==="i"){f.appendTo(z)}if(p.fieldsOrder[j]==="s"&&p.timeFormat===12){k.appendTo(z)}}if(p.swipeEnabled){z.find("input").bind(m.START_DRAG,function(o){if(!m.dragMove){m.dragMove=true;m.dragTarget=a(this).data("field");m.dragPos=0;m.dragStart=m.touch?o.originalEvent.changedTouches[0].pageY:o.pageY;m.dragEnd=false;o.stopPropagation()}})}if(p.noSetButton===false){a("<a href='#'>"+p.setDateButtonLabel+"</a>").appendTo(q).buttonMarkup({theme:p.pickPageTheme,icon:"check",iconpos:"left",corners:true,shadow:true}).bind("vclick",function(o){o.preventDefault();if(p.mode==="timebox"){m.input.trigger("datebox",{method:"set",value:m._formatTime(m.theDate)})}else{m.input.trigger("datebox",{method:"set",value:m._formatDate(m.theDate)})}m.input.trigger("datebox",{method:"close"})})}for(j=0;j<m.options.fieldsOrder.length;j++){t.clone().appendTo(h).buttonMarkup({theme:p.pickPageButtonTheme,icon:"plus",iconpos:"bottom",corners:true,shadow:true}).attr("data-field",p.fieldsOrder[j]).bind("vclick",function(o){o.preventDefault();m._offset(a(this).attr("data-field"),1*(a(this).attr("data-field")==="i"?p.minuteStep:1))});t.clone().appendTo(l).buttonMarkup({theme:p.pickPageButtonTheme,icon:"minus",iconpos:"top",corners:true,shadow:true}).attr("data-field",p.fieldsOrder[j]).bind("vclick",function(o){o.preventDefault();m._offset(a(this).attr("data-field"),-1*(a(this).attr("data-field")==="i"?p.minuteStep:1))})}a.extend(m,{controlsHeader:r,pickerDay:A,pickerMon:y,pickerYar:n,pickerHour:d,pickerMins:f,pickerMeri:k});e.appendTo(m.thisPage)}if(p.mode==="calbox"){r=a("<div>",{"class":"ui-datebox-gridheader"}).appendTo(e);q=a("<div>",{"class":"ui-datebox-grid"}).appendTo(e);z=a("<div class='ui-datebox-gridlabel'><h4>Uninitialized</h4></div>").appendTo(r).find("h4");if(p.swipeEnabled){e.bind("swipeleft",function(){if(!m.calNoNext){m._offset("m",1)}}).bind("swiperight",function(){if(!m.calNoPrev){m._offset("m",-1)}})}if(p.wheelExists){e.bind("mousewheel",function(o,x){o.preventDefault();if(x>0&&!m.calNoNext){if(m.theDate.getDate()>28){m.theDate.setDate(1)}m.theDate.setMonth(m.theDate.getMonth()+1);m._update()}if(x<0&&!m.calNoPrev){if(m.theDate.getDate()>28){m.theDate.setDate(1)}m.theDate.setMonth(m.theDate.getMonth()-1);m._update()}})}a("<div class='ui-datebox-gridplus'><a href='#'>Next Month</a></div>").prependTo(r).buttonMarkup({theme:p.pickPageButtonTheme,icon:"plus",inline:true,iconpos:"notext",corners:true,shadow:true}).bind("vclick",function(o){o.preventDefault();if(!m.calNoNext){if(m.theDate.getDate()>28){m.theDate.setDate(1)}m._offset("m",1)}});a("<div class='ui-datebox-gridminus'><a href='#'>Prev Month</a></div>").prependTo(r).buttonMarkup({theme:p.pickPageButtonTheme,icon:"minus",inline:true,iconpos:"notext",corners:true,shadow:true}).bind("vclick",function(o){o.preventDefault();if(!m.calNoPrev){if(m.theDate.getDate()>28){m.theDate.setDate(1)}m._offset("m",-1)}});a.extend(m,{controlsInput:z,controlsSet:q,calNoNext:w,calNoPrev:v});e.appendTo(m.thisPage)}if(p.mode==="slidebox"){r=a("<div class='ui-datebox-header'><h4>Unitialized</h4></div>").appendTo(e).find("h4");z=a("<div>").addClass("ui-datebox-slide").appendTo(e);q=a("<div>",{"class":"ui-datebox-controls"}).appendTo(e);if(p.noSetButton===false){a("<a href='#'>"+p.setDateButtonLabel+"</a>").appendTo(q).buttonMarkup({theme:p.pickPageTheme,icon:"check",iconpos:"left",corners:true,shadow:true}).bind("vclick",function(o){o.preventDefault();m.input.trigger("datebox",{method:"set",value:m._formatDate(m.theDate)});m.input.trigger("datebox",{method:"close"})})}a.extend(m,{controlsHeader:r,controlsInput:z});e.appendTo(m.thisPage)}a.extend(m,{pickerContent:e,screen:g});if(p.useInline){m.input.parent().parent().append(m.pickerContent);if(p.useInlineHideInput){m.input.parent().hide()}m.input.trigger("change");m.pickerContent.removeClass("ui-datebox-hidden")}},refresh:function(){if(this.options.useInline===true){this.input.trigger("change")}this._update()},open:function(){if(this.options.useInline){return false}if(this.options.open===true){return false}else{this.options.open=true}this.input.trigger("change").blur();var c=this,i=this.options,e=c.focusedEl.offset(),g=c.pickerContent.outerHeight(),f=c.pickerContent.innerWidth(),h=e.top+(c.focusedEl.outerHeight()/2)-(g/2),d=e.left+(c.focusedEl.outerWidth()/2)-(f/2);if((d+f)>a(document).width()){d=a(document).width()-f-1}if(d<0){d=0}if(i.centerWindow){d=(a(document).width()/2)-(f/2)}if((g+h)>a(document).height()){h=a(document).height()-(g+2)}if(h<45){h=45}if((a(document).width()>400&&!i.useDialogForceTrue)||i.useDialogForceFalse){i.useDialog=false;if(i.useModal){c.screen.fadeIn("slow")}else{c.screen.removeClass("ui-datebox-hidden")}c.pickerContent.addClass("ui-overlay-shadow in").css({position:"absolute",top:h,left:d}).removeClass("ui-datebox-hidden")}else{i.useDialog=true;c.pickPageContent.append(c.pickerContent);c.pickerContent.css({top:"auto",left:"auto",marginLeft:"auto",marginRight:"auto"}).removeClass("ui-overlay-shadow ui-datebox-hidden");a.mobile.changePage(c.pickPage,{transition:"pop"})}},close:function(){var c=this,d;if(c.options.useInline){return true}c.options.open=false;if(c.options.useDialog){a(c.pickPage).dialog("close");c.pickerContent.addClass("ui-datebox-hidden").removeAttr("style").css("zIndex",c.options.zindex);c.thisPage.append(c.pickerContent)}else{if(c.options.useModal){c.screen.fadeOut("slow")}else{c.screen.addClass("ui-datebox-hidden")}c.pickerContent.addClass("ui-datebox-hidden").removeAttr("style").css("zIndex",c.options.zindex).removeClass("in")}c.focusedEl.removeClass("ui-focus");if(c.options.closeCallback!==false){d=new Function(c.options.closeCallback);d()}},disable:function(){this.element.attr("disabled",true);this.element.parent().addClass("ui-disabled");this.options.disabled=true;this.element.blur();this.input.trigger("datebox",{method:"disable"})},enable:function(){this.element.attr("disabled",false);this.element.parent().removeClass("ui-disabled");this.options.disabled=false;this.input.trigger("datebox",{method:"enable"})}});a(".ui-page").live("pagecreate",function(){a('input[data-role="datebox"]',this).each(function(){a(this).datebox()})})})(jQuery);
