/*
 jQuery timepicker addon
 By: Trent Richardson [http://trentrichardson.com]
 Version 0.9.3
 Last Modified: 02/05/2011

 Copyright 2010 Trent Richardson
 Dual licensed under the MIT and GPL licenses.
 http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
 http://trentrichardson.com/Impromptu/MIT-LICENSE.txt

 HERES THE CSS:
 .ui-timepicker-div .ui-widget-header{ margin-bottom: 8px; }
 .ui-timepicker-div dl{ text-align: left; }
 .ui-timepicker-div dl dt{ height: 25px; }
 .ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; }
 .ui-timepicker-div td { font-size: 90%; }
*/
(function(d){function l(){this.regional=[];this.regional[""]={currentText:"Now",closeText:"Done",ampm:!1,timeFormat:"hh:mm tt",timeOnlyTitle:"Choose Time",timeText:"Time",hourText:"Hour",minuteText:"Minute",secondText:"Second"};this._defaults={showButtonPanel:!0,timeOnly:!1,showHour:!0,showMinute:!0,showSecond:!1,showTime:!0,stepHour:0.05,stepMinute:0.05,stepSecond:0.05,hour:0,minute:0,second:0,hourMin:0,minuteMin:0,secondMin:0,hourMax:23,minuteMax:59,secondMax:59,minDateTime:null,maxDateTime:null,
hourGrid:0,minuteGrid:0,secondGrid:0,alwaysSetTime:!0,separator:" ",altFieldTimeOnly:!0,showTimepicker:!0};d.extend(this._defaults,this.regional[""])}d.extend(d.ui,{timepicker:{version:"0.9.3"}});d.extend(l.prototype,{$input:null,$altInput:null,$timeObj:null,inst:null,hour_slider:null,minute_slider:null,second_slider:null,hour:0,minute:0,second:0,hourMinOriginal:null,minuteMinOriginal:null,secondMinOriginal:null,hourMaxOriginal:null,minuteMaxOriginal:null,secondMaxOriginal:null,ampm:"",formattedDate:"",
formattedTime:"",formattedDateTime:"",setDefaults:function(a){var b=this._defaults,a=a||{};d.extend(b,a);for(var c in a)if(a[c]===null||a[c]===void 0)b[c]=a[c];return this},_newInst:function(a,b){var c=new l,e={};c.hour=c._defaults.hour;c.minute=c._defaults.minute;c.second=c._defaults.second;c.ampm="";c.$input=a;for(var f in this._defaults){var i=a.attr("time:"+f);if(i)try{e[f]=eval(i)}catch(g){e[f]=i}}c._defaults=d.extend({},this._defaults,e,b,{beforeShow:function(a,e){d.isFunction(b.beforeShow)&&
b.beforeShow(a,e,c)},onChangeMonthYear:function(a,e,f){c._updateDateTime(f);if(d.isFunction(b.onChangeMonthYear))b.onChangeMonthYear(a,e,f,c)},onClose:function(e,f){c.timeDefined===!0&&a.val()!=""&&c._updateDateTime(f);if(d.isFunction(b.onClose))b.onClose(e,f,c)},timepicker:c});if(b.altField)c.$altInput=d(b.altField).css({cursor:"pointer"}).focus(function(){a.trigger("focus")});if(c._defaults.minDate!==void 0&&c._defaults.minDate instanceof Date)c._defaults.minDateTime=new Date(c._defaults.minDate.getTime());
if(c._defaults.minDateTime!==void 0&&c._defaults.minDateTime instanceof Date)c._defaults.minDate=new Date(c._defaults.minDateTime.getTime());if(c._defaults.maxDate!==void 0&&c._defaults.maxDate instanceof Date)c._defaults.maxDateTime=new Date(c._defaults.maxDate.getTime());if(c._defaults.maxDateTime!==void 0&&c._defaults.maxDateTime instanceof Date)c._defaults.maxDate=new Date(c._defaults.maxDateTime.getTime());return c},_addTimePicker:function(a){this.timeDefined=this._parseTime(this.$altInput&&
this._defaults.altFieldTimeOnly?this.$input.val()+" "+this.$altInput.val():this.$input.val());this._limitMinMaxDateTime(a,!1);this._injectTimePicker()},_parseTime:function(a,b){var c=this._defaults.timeFormat.toString().replace(/h{1,2}/ig,"(\\d?\\d)").replace(/m{1,2}/ig,"(\\d?\\d)").replace(/s{1,2}/ig,"(\\d?\\d)").replace(/t{1,2}/ig,"(am|pm|a|p)?").replace(/\s/g,"\\s?")+"$",e=this._getFormatPositions();if(!this.inst)this.inst=d.datepicker._getInst(this.$input[0]);if(b||!this._defaults.timeOnly)c=
".{"+d.datepicker._get(this.inst,"dateFormat").length+",}"+this._defaults.separator+c;if(c=a.match(RegExp(c,"i"))){if(e.t!==-1)this.ampm=(c[e.t]===void 0||c[e.t].length===0?"":c[e.t].charAt(0).toUpperCase()=="A"?"AM":"PM").toUpperCase();if(e.h!==-1)this.hour=this.ampm=="AM"&&c[e.h]=="12"?0:this.ampm=="PM"&&c[e.h]!="12"?(parseFloat(c[e.h])+12).toFixed(0):Number(c[e.h]);if(e.m!==-1)this.minute=Number(c[e.m]);if(e.s!==-1)this.second=Number(c[e.s]);return!0}return!1},_getFormatPositions:function(){var a=
this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2})/g),b={h:-1,m:-1,s:-1,t:-1};if(a)for(var c=0;c<a.length;c++)b[a[c].toString().charAt(0)]==-1&&(b[a[c].toString().charAt(0)]=c+1);return b},_injectTimePicker:function(){var a=this.inst.dpDiv,b=this._defaults,c=this,e=(b.hourMax-b.hourMax%b.stepHour).toFixed(0),f=(b.minuteMax-b.minuteMax%b.stepMinute).toFixed(0),i=(b.secondMax-b.secondMax%b.stepSecond).toFixed(0),g=this.inst.id.toString().replace(/([^A-Za-z0-9_])/g,"");if(a.find("div#ui-timepicker-div-"+
g).length===0&&b.showTimepicker){var j='<div class="ui-timepicker-div" id="ui-timepicker-div-'+g+'"><dl><dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_'+g+'"'+(b.showTime?"":' style="display:none;"')+">"+b.timeText+'</dt><dd class="ui_tpicker_time" id="ui_tpicker_time_'+g+'"'+(b.showTime?"":' style="display:none;"')+'></dd><dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_'+g+'"'+(b.showHour?"":' style="display:none;"')+">"+b.hourText+"</dt>",n=0,o=0,l=0,k;if(b.showHour&&b.hourGrid>
0){j+='<dd class="ui_tpicker_hour"><div id="ui_tpicker_hour_'+g+'"'+(b.showHour?"":' style="display:none;"')+'></div><div style="padding-left: 1px"><table><tr>';for(var h=b.hourMin;h<e;h+=b.hourGrid){n++;var m=b.ampm&&h>12?h-12:h;m<10&&(m="0"+m);b.ampm&&(h==0?m="12a":m+=h<12?"a":"p");j+="<td>"+m+"</td>"}j+="</tr></table></div></dd>"}else j+='<dd class="ui_tpicker_hour" id="ui_tpicker_hour_'+g+'"'+(b.showHour?"":' style="display:none;"')+"></dd>";j+='<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_'+
g+'"'+(b.showMinute?"":' style="display:none;"')+">"+b.minuteText+"</dt>";if(b.showMinute&&b.minuteGrid>0){j+='<dd class="ui_tpicker_minute ui_tpicker_minute_'+b.minuteGrid+'"><div id="ui_tpicker_minute_'+g+'"'+(b.showMinute?"":' style="display:none;"')+'></div><div style="padding-left: 1px"><table><tr>';for(h=b.minuteMin;h<f;h+=b.minuteGrid)o++,j+="<td>"+(h<10?"0":"")+h+"</td>";j+="</tr></table></div></dd>"}else j+='<dd class="ui_tpicker_minute" id="ui_tpicker_minute_'+g+'"'+(b.showMinute?"":' style="display:none;"')+
"></dd>";j+='<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_'+g+'"'+(b.showSecond?"":' style="display:none;"')+">"+b.secondText+"</dt>";if(b.showSecond&&b.secondGrid>0){j+='<dd class="ui_tpicker_second ui_tpicker_second_'+b.secondGrid+'"><div id="ui_tpicker_second_'+g+'"'+(b.showSecond?"":' style="display:none;"')+'></div><div style="padding-left: 1px"><table><tr>';for(h=b.secondMin;h<i;h+=b.secondGrid)l++,j+="<td>"+(h<10?"0":"")+h+"</td>";j+="</tr></table></div></dd>"}else j+='<dd class="ui_tpicker_second" id="ui_tpicker_second_'+
g+'"'+(b.showSecond?"":' style="display:none;"')+"></dd>";j+="</dl></div>";$tp=d(j);b.timeOnly===!0&&($tp.prepend('<div class="ui-widget-header ui-helper-clearfix ui-corner-all"><div class="ui-datepicker-title">'+b.timeOnlyTitle+"</div></div>"),a.find(".ui-datepicker-header, .ui-datepicker-calendar").hide());this.hour_slider=$tp.find("#ui_tpicker_hour_"+g).slider({orientation:"horizontal",value:this.hour,min:b.hourMin,max:e,step:b.stepHour,slide:function(b,a){c.hour_slider.slider("option","value",
a.value);c._onTimeChange()}});c.hour_slider.get(0).slide=null;this.minute_slider=$tp.find("#ui_tpicker_minute_"+g).slider({orientation:"horizontal",value:this.minute,min:b.minuteMin,max:f,step:b.stepMinute,slide:function(b,a){c.minute_slider.slider("option","value",a.value);c._onTimeChange()}});c.minute_slider.get(0).slide=null;this.second_slider=$tp.find("#ui_tpicker_second_"+g).slider({orientation:"horizontal",value:this.second,min:b.secondMin,max:i,step:b.stepSecond,slide:function(b,a){c.second_slider.slider("option",
"value",a.value);c._onTimeChange()}});c.second_slider.get(0).slide=null;b.showHour&&b.hourGrid>0&&(k=100*n*b.hourGrid/(e-b.hourMin),$tp.find(".ui_tpicker_hour table").css({width:k+"%",marginLeft:k/(-2*n)+"%",borderCollapse:"collapse"}).find("td").each(function(){d(this).click(function(){var a=d(this).html();if(b.ampm)var e=a.substring(2).toLowerCase(),a=parseInt(a.substring(0,2)),a=e=="a"?a==12?0:a:a==12?12:a+12;c.hour_slider.slider("option","value",a);c._onTimeChange()}).css({cursor:"pointer",width:100/
n+"%",textAlign:"center",overflow:"hidden"})}));b.showMinute&&b.minuteGrid>0&&(k=100*o*b.minuteGrid/(f-b.minuteMin),$tp.find(".ui_tpicker_minute table").css({width:k+"%",marginLeft:k/(-2*o)+"%",borderCollapse:"collapse"}).find("td").each(function(){d(this).click(function(){c.minute_slider.slider("option","value",d(this).html());c._onTimeChange()}).css({cursor:"pointer",width:100/o+"%",textAlign:"center",overflow:"hidden"})}));b.showSecond&&b.secondGrid>0&&$tp.find(".ui_tpicker_second table").css({width:k+
"%",marginLeft:k/(-2*l)+"%",borderCollapse:"collapse"}).find("td").each(function(){d(this).click(function(){c.second_slider.slider("option","value",d(this).html());c._onTimeChange()}).css({cursor:"pointer",width:100/l+"%",textAlign:"center",overflow:"hidden"})});e=a.find(".ui-datepicker-buttonpane");e.length?e.before($tp):a.append($tp);this.$timeObj=d("#ui_tpicker_time_"+g);if(this.inst!==null)a=this.timeDefined,this._onTimeChange(),this.timeDefined=a;var p=c._defaults.onSelect;if(p){var q=c.$input?
c.$input[0]:null,a=function(){p.apply(q,[c.formattedDateTime,c])};this.hour_slider.bind("slidestop",a);this.minute_slider.bind("slidestop",a);this.second_slider.bind("slidestop",a)}}},_limitMinMaxDateTime:function(a,b){var c=this._defaults,e=new Date(a.selectedYear,a.selectedMonth,a.selectedDay);if(this._defaults.minDateTime!==null&&e){var d=this._defaults.minDateTime,i=new Date(d.getFullYear(),d.getMonth(),d.getDate(),0,0,0,0);if(this.hourMinOriginal===null||this.minuteMinOriginal===null||this.secondMinOriginal===
null)this.hourMinOriginal=c.hourMin,this.minuteMinOriginal=c.minuteMin,this.secondMinOriginal=c.secondMin;if(i.getTime()==e.getTime()){this._defaults.hourMin=d.getHours();this._defaults.minuteMin=d.getMinutes();this._defaults.secondMin=d.getSeconds();if(this.hour<this._defaults.hourMin)this.hour=this._defaults.hourMin;if(this.minute<this._defaults.minuteMin)this.minute=this._defaults.minuteMin;if(this.second<this._defaults.secondMin)this.second=this._defaults.secondMin}else this._defaults.hourMin=
this.hourMinOriginal,this._defaults.minuteMin=this.minuteMinOriginal,this._defaults.secondMin=this.secondMinOriginal}if(this._defaults.maxDateTime!==null&&e){d=this._defaults.maxDateTime;i=new Date(d.getFullYear(),d.getMonth(),d.getDate(),0,0,0,0);if(this.hourMaxOriginal===null||this.minuteMaxOriginal===null||this.secondMaxOriginal===null)this.hourMaxOriginal=c.hourMax,this.minuteMaxOriginal=c.minuteMax,this.secondMaxOriginal=c.secondMax;if(i.getTime()==e.getTime()){this._defaults.hourMax=d.getHours();
this._defaults.minuteMax=d.getMinutes();this._defaults.secondMax=d.getSeconds();if(this.hour>this._defaults.hourMax)this.hour=this._defaults.hourMax;if(this.minute>this._defaults.minuteMax)this.minute=this._defaults.minuteMax;if(this.second>this._defaults.secondMax)this.second=this._defaults.secondMax}else this._defaults.hourMax=this.hourMaxOriginal,this._defaults.minuteMax=this.minuteMaxOriginal,this._defaults.secondMax=this.secondMaxOriginal}b!==void 0&&b===!0&&(this.hour_slider.slider("option",
{min:this._defaults.hourMin,max:this._defaults.hourMax}).slider("value",this.hour),this.minute_slider.slider("option",{min:this._defaults.minuteMin,max:this._defaults.minuteMax}).slider("value",this.minute),this.second_slider.slider("option",{min:this._defaults.secondMin,max:this._defaults.secondMax}).slider("value",this.second))},_onTimeChange:function(){var a=this.hour_slider?this.hour_slider.slider("value"):!1,b=this.minute_slider?this.minute_slider.slider("value"):!1,c=this.second_slider?this.second_slider.slider("value"):
!1;a!==!1&&(a=parseInt(a,10));b!==!1&&(b=parseInt(b,10));c!==!1&&(c=parseInt(c,10));var d=a<12?"AM":"PM",f=a!=this.hour||b!=this.minute||c!=this.second||this.ampm.length>0&&this.ampm!=d;if(f){if(a!==!1)this.hour=a;if(b!==!1)this.minute=b;if(c!==!1)this.second=c}if(this._defaults.ampm)this.ampm=d;this._formatTime();this.$timeObj&&this.$timeObj.text(this.formattedTime);this.timeDefined=!0;f&&this._updateDateTime()},_formatTime:function(a,b,c){if(c==void 0)c=this._defaults.ampm;var a=a||{hour:this.hour,
minute:this.minute,second:this.second,ampm:this.ampm},e=b||this._defaults.timeFormat.toString();if(c)var f=a.ampm=="AM"?a.hour:a.hour%12,f=Number(f)===0?12:f,e=e.toString().replace(/hh/g,(f<10?"0":"")+f).replace(/h/g,f).replace(/mm/g,(a.minute<10?"0":"")+a.minute).replace(/m/g,a.minute).replace(/ss/g,(a.second<10?"0":"")+a.second).replace(/s/g,a.second).replace(/TT/g,a.ampm.toUpperCase()).replace(/tt/g,a.ampm.toLowerCase()).replace(/T/g,a.ampm.charAt(0).toUpperCase()).replace(/t/g,a.ampm.charAt(0).toLowerCase());
else e=e.toString().replace(/hh/g,(a.hour<10?"0":"")+a.hour).replace(/h/g,a.hour).replace(/mm/g,(a.minute<10?"0":"")+a.minute).replace(/m/g,a.minute).replace(/ss/g,(a.second<10?"0":"")+a.second).replace(/s/g,a.second),e=d.trim(e.replace(/t/gi,""));if(arguments.length)return e;else this.formattedTime=e},_updateDateTime:function(a){a=this.inst||a;dt=new Date(a.selectedYear,a.selectedMonth,a.selectedDay);dateFmt=d.datepicker._get(a,"dateFormat");formatCfg=d.datepicker._getFormatConfig(a);timeAvailable=
dt!==null&&this.timeDefined;var b=this.formattedDate=d.datepicker.formatDate(dateFmt,dt===null?new Date:dt,formatCfg);if(!(a.lastVal!==void 0&&a.lastVal.length>0&&this.$input.val().length===0)){if(this._defaults.timeOnly===!0)b=this.formattedTime;else if(this._defaults.timeOnly!==!0&&(this._defaults.alwaysSetTime||timeAvailable))b+=this._defaults.separator+this.formattedTime;this.formattedDateTime=b;this._defaults.showTimepicker?this.$altInput&&this._defaults.altFieldTimeOnly===!0?(this.$altInput.val(this.formattedTime),
this.$input.val(this.formattedDate)):(this.$altInput&&this.$altInput.val(b),this.$input.val(b)):this.$input.val(this.formattedDate);this.$input.trigger("change")}}});d.fn.extend({timepicker:function(a){var a=a||{},b=arguments;typeof a=="object"&&(b[0]=d.extend(a,{timeOnly:!0}));return d(this).each(function(){d.fn.datetimepicker.apply(d(this),b)})},datetimepicker:function(a){var a=a||{},b=arguments;return typeof a=="string"?a=="getDate"?d.fn.datepicker.apply(d(this[0]),b):this.each(function(){var a=
d(this);a.datepicker.apply(a,b)}):this.each(function(){var b=d(this);b.datepicker(d.timepicker._newInst(b,a)._defaults)})}});d.datepicker._base_selectDate=d.datepicker._selectDate;d.datepicker._selectDate=function(a,b){var c=this._getInst(d(a)[0]),e=this._get(c,"timepicker");e?(e._limitMinMaxDateTime(c,!0),c.inline=c.stay_open=!0,this._base_selectDate(a,b+e._defaults.separator+e.formattedTime),c.inline=c.stay_open=!1,this._notifyChange(c),this._updateDatepicker(c)):this._base_selectDate(a,b)};d.datepicker._base_updateDatepicker=
d.datepicker._updateDatepicker;d.datepicker._updateDatepicker=function(a){if(typeof a.stay_open!=="boolean"||a.stay_open===!1){this._base_updateDatepicker(a);var b=this._get(a,"timepicker");b&&b._addTimePicker(a)}};d.datepicker._base_doKeyPress=d.datepicker._doKeyPress;d.datepicker._doKeyPress=function(a){var b=d.datepicker._getInst(a.target),c=d.datepicker._get(b,"timepicker");if(c&&d.datepicker._get(b,"constrainInput")){var e=c._defaults.ampm,b=c._defaults.timeFormat.toString().replace(/[hms]/g,
"").replace(/TT/g,e?"APM":"").replace(/T/g,e?"AP":"").replace(/tt/g,e?"apm":"").replace(/t/g,e?"ap":"")+" "+c._defaults.separator+d.datepicker._possibleChars(d.datepicker._get(b,"dateFormat")),c=String.fromCharCode(a.charCode===void 0?a.keyCode:a.charCode);return a.ctrlKey||c<" "||!b||b.indexOf(c)>-1}return d.datepicker._base_doKeyPress(a)};d.datepicker._base_doKeyUp=d.datepicker._doKeyUp;d.datepicker._doKeyUp=function(a){var b=d.datepicker._getInst(a.target),c=d.datepicker._get(b,"timepicker");if(c&&
c._defaults.timeOnly&&b.input.val()!=b.lastVal)try{d.datepicker._updateDatepicker(b)}catch(e){d.datepicker.log(e)}return d.datepicker._base_doKeyUp(a)};d.datepicker._base_gotoToday=d.datepicker._gotoToday;d.datepicker._gotoToday=function(a){this._base_gotoToday(a);this._setTime(this._getInst(d(a)[0]),new Date)};d.datepicker._disableTimepickerDatepicker=function(a){var a=this._getInst(a),b=this._get(a,"timepicker");if(b)b._defaults.showTimepicker=!1,b._onTimeChange(),b._updateDateTime(a)};d.datepicker._enableTimepickerDatepicker=
function(a){var a=this._getInst(a),b=this._get(a,"timepicker");if(b)b._defaults.showTimepicker=!0,b._onTimeChange(),b._updateDateTime(a)};d.datepicker._setTime=function(a,b){var c=this._get(a,"timepicker");if(c){var d=c._defaults,f=b?b.getHours():d.hour,i=b?b.getMinutes():d.minute,g=b?b.getSeconds():d.second;if(f<d.hourMin||f>d.hourMax||i<d.minuteMin||i>d.minuteMax||g<d.secondMin||g>d.secondMax)f=d.hourMin,i=d.minuteMin,g=d.secondMin;c.hour_slider?c.hour_slider.slider("value",f):c.hour=f;c.minute_slider?
c.minute_slider.slider("value",i):c.minute=i;c.second_slider?c.second_slider.slider("value",g):c.second=g;c._onTimeChange();c._updateDateTime(a)}};d.datepicker._setTimeDatepicker=function(a,b,c){var a=this._getInst(a),d=this._get(a,"timepicker");if(d){this._setDateFromField(a);var f;b&&(typeof b=="string"?(d._parseTime(b,c),f=new Date,f.setHours(d.hour,d.minute,d.second)):f=new Date(b.getTime()),f.toString()=="Invalid Date"&&(f=void 0));this._setTime(a,f)}};d.datepicker._base_setDateDatepicker=d.datepicker._setDateDatepicker;
d.datepicker._setDateDatepicker=function(a,b){var c=this._getInst(a),d=b instanceof Date?new Date(b.getTime()):b;this._updateDatepicker(c);this._base_setDateDatepicker.apply(this,arguments);this._setTimeDatepicker(a,d,!0)};d.datepicker._base_getDateDatepicker=d.datepicker._getDateDatepicker;d.datepicker._getDateDatepicker=function(a,b){var c=this._getInst(a),e=this._get(c,"timepicker");if(e)return this._setDateFromField(c,b),(c=this._getDate(c))&&e._parseTime(d(a).val(),!0)&&c.setHours(e.hour,e.minute,
e.second),c;return this._base_getDateDatepicker(a,b)};d.timepicker=new l;d.timepicker.version="0.9.3"})(jQuery);
