rt 4.0.7
[freeside.git] / rt / share / html / NoAuth / js / jquery-ui-timepicker-addon.js
1 /*
2 * jQuery timepicker addon
3 * By: Trent Richardson [http://trentrichardson.com]
4 * Version 1.0.0
5 * Last Modified: 02/05/2012
6 *
7 * Copyright 2012 Trent Richardson
8 * Dual licensed under the MIT and GPL licenses.
9 * http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
10 * http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
11 *
12 * HERES THE CSS:
13 * .ui-timepicker-div .ui-widget-header { margin-bottom: 8px; }
14 * .ui-timepicker-div dl { text-align: left; }
15 * .ui-timepicker-div dl dt { height: 25px; margin-bottom: -25px; }
16 * .ui-timepicker-div dl dd { margin: 0 10px 10px 65px; }
17 * .ui-timepicker-div td { font-size: 90%; }
18 * .ui-tpicker-grid-label { background: none; border: none; margin: 0; padding: 0; }
19 */
20
21 (function($) {
22
23 // Prevent "Uncaught RangeError: Maximum call stack size exceeded"
24 $.ui.timepicker = $.ui.timepicker || {};
25 if ($.ui.timepicker.version) {
26         return;
27 }
28
29 $.extend($.ui, { timepicker: { version: "1.0.0" } });
30
31 /* Time picker manager.
32    Use the singleton instance of this class, $.timepicker, to interact with the time picker.
33    Settings for (groups of) time pickers are maintained in an instance object,
34    allowing multiple different settings on the same page. */
35
36 function Timepicker() {
37         this.regional = []; // Available regional settings, indexed by language code
38         this.regional[''] = { // Default regional settings
39                 currentText: 'Now',
40                 closeText: 'Done',
41                 ampm: false,
42                 amNames: ['AM', 'A'],
43                 pmNames: ['PM', 'P'],
44                 timeFormat: 'hh:mm tt',
45                 timeSuffix: '',
46                 timeOnlyTitle: 'Choose Time',
47                 timeText: 'Time',
48                 hourText: 'Hour',
49                 minuteText: 'Minute',
50                 secondText: 'Second',
51                 millisecText: 'Millisecond',
52                 timezoneText: 'Time Zone'
53         };
54         this._defaults = { // Global defaults for all the datetime picker instances
55                 showButtonPanel: true,
56                 timeOnly: false,
57                 showHour: true,
58                 showMinute: true,
59                 showSecond: false,
60                 showMillisec: false,
61                 showTimezone: false,
62                 showTime: true,
63                 stepHour: 1,
64                 stepMinute: 1,
65                 stepSecond: 1,
66                 stepMillisec: 1,
67                 hour: 0,
68                 minute: 0,
69                 second: 0,
70                 millisec: 0,
71                 timezone: '+0000',
72                 hourMin: 0,
73                 minuteMin: 0,
74                 secondMin: 0,
75                 millisecMin: 0,
76                 hourMax: 23,
77                 minuteMax: 59,
78                 secondMax: 59,
79                 millisecMax: 999,
80                 minDateTime: null,
81                 maxDateTime: null,
82                 onSelect: null,
83                 hourGrid: 0,
84                 minuteGrid: 0,
85                 secondGrid: 0,
86                 millisecGrid: 0,
87                 alwaysSetTime: true,
88                 separator: ' ',
89                 altFieldTimeOnly: true,
90                 showTimepicker: true,
91                 timezoneIso8609: false,
92                 timezoneList: null,
93                 addSliderAccess: false,
94                 sliderAccessArgs: null
95         };
96         $.extend(this._defaults, this.regional['']);
97 };
98
99 $.extend(Timepicker.prototype, {
100         $input: null,
101         $altInput: null,
102         $timeObj: null,
103         inst: null,
104         hour_slider: null,
105         minute_slider: null,
106         second_slider: null,
107         millisec_slider: null,
108         timezone_select: null,
109         hour: 0,
110         minute: 0,
111         second: 0,
112         millisec: 0,
113         timezone: '+0000',
114         hourMinOriginal: null,
115         minuteMinOriginal: null,
116         secondMinOriginal: null,
117         millisecMinOriginal: null,
118         hourMaxOriginal: null,
119         minuteMaxOriginal: null,
120         secondMaxOriginal: null,
121         millisecMaxOriginal: null,
122         ampm: '',
123         formattedDate: '',
124         formattedTime: '',
125         formattedDateTime: '',
126         timezoneList: null,
127
128         /* Override the default settings for all instances of the time picker.
129            @param  settings  object - the new settings to use as defaults (anonymous object)
130            @return the manager object */
131         setDefaults: function(settings) {
132                 extendRemove(this._defaults, settings || {});
133                 return this;
134         },
135
136         //########################################################################
137         // Create a new Timepicker instance
138         //########################################################################
139         _newInst: function($input, o) {
140                 var tp_inst = new Timepicker(),
141                         inlineSettings = {};
142
143                 for (var attrName in this._defaults) {
144                         var attrValue = $input.attr('time:' + attrName);
145                         if (attrValue) {
146                                 try {
147                                         inlineSettings[attrName] = eval(attrValue);
148                                 } catch (err) {
149                                         inlineSettings[attrName] = attrValue;
150                                 }
151                         }
152                 }
153                 tp_inst._defaults = $.extend({}, this._defaults, inlineSettings, o, {
154                         beforeShow: function(input, dp_inst) {
155                                 if ($.isFunction(o.beforeShow))
156                                         return o.beforeShow(input, dp_inst, tp_inst);
157                         },
158                         onChangeMonthYear: function(year, month, dp_inst) {
159                                 // Update the time as well : this prevents the time from disappearing from the $input field.
160                                 tp_inst._updateDateTime(dp_inst);
161                                 if ($.isFunction(o.onChangeMonthYear))
162                                         o.onChangeMonthYear.call($input[0], year, month, dp_inst, tp_inst);
163                         },
164                         onClose: function(dateText, dp_inst) {
165                                 if (tp_inst.timeDefined === true && $input.val() != '')
166                                         tp_inst._updateDateTime(dp_inst);
167                                 if ($.isFunction(o.onClose))
168                                         o.onClose.call($input[0], dateText, dp_inst, tp_inst);
169                         },
170                         timepicker: tp_inst // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
171                 });
172                 tp_inst.amNames = $.map(tp_inst._defaults.amNames, function(val) { return val.toUpperCase(); });
173                 tp_inst.pmNames = $.map(tp_inst._defaults.pmNames, function(val) { return val.toUpperCase(); });
174
175                 if (tp_inst._defaults.timezoneList === null) {
176                         var timezoneList = [];
177                         for (var i = -11; i <= 12; i++)
178                                 timezoneList.push((i >= 0 ? '+' : '-') + ('0' + Math.abs(i).toString()).slice(-2) + '00');
179                         if (tp_inst._defaults.timezoneIso8609)
180                                 timezoneList = $.map(timezoneList, function(val) {
181                                         return val == '+0000' ? 'Z' : (val.substring(0, 3) + ':' + val.substring(3));
182                                 });
183                         tp_inst._defaults.timezoneList = timezoneList;
184                 }
185
186                 tp_inst.hour = tp_inst._defaults.hour;
187                 tp_inst.minute = tp_inst._defaults.minute;
188                 tp_inst.second = tp_inst._defaults.second;
189                 tp_inst.millisec = tp_inst._defaults.millisec;
190                 tp_inst.ampm = '';
191                 tp_inst.$input = $input;
192
193                 if (o.altField)
194                         tp_inst.$altInput = $(o.altField)
195                                 .css({ cursor: 'pointer' })
196                                 .focus(function(){ $input.trigger("focus"); });
197
198                 if(tp_inst._defaults.minDate==0 || tp_inst._defaults.minDateTime==0)
199                 {
200                         tp_inst._defaults.minDate=new Date();
201                 }
202                 if(tp_inst._defaults.maxDate==0 || tp_inst._defaults.maxDateTime==0)
203                 {
204                         tp_inst._defaults.maxDate=new Date();
205                 }
206
207                 // datepicker needs minDate/maxDate, timepicker needs minDateTime/maxDateTime..
208                 if(tp_inst._defaults.minDate !== undefined && tp_inst._defaults.minDate instanceof Date)
209                         tp_inst._defaults.minDateTime = new Date(tp_inst._defaults.minDate.getTime());
210                 if(tp_inst._defaults.minDateTime !== undefined && tp_inst._defaults.minDateTime instanceof Date)
211                         tp_inst._defaults.minDate = new Date(tp_inst._defaults.minDateTime.getTime());
212                 if(tp_inst._defaults.maxDate !== undefined && tp_inst._defaults.maxDate instanceof Date)
213                         tp_inst._defaults.maxDateTime = new Date(tp_inst._defaults.maxDate.getTime());
214                 if(tp_inst._defaults.maxDateTime !== undefined && tp_inst._defaults.maxDateTime instanceof Date)
215                         tp_inst._defaults.maxDate = new Date(tp_inst._defaults.maxDateTime.getTime());
216                 return tp_inst;
217         },
218
219         //########################################################################
220         // add our sliders to the calendar
221         //########################################################################
222         _addTimePicker: function(dp_inst) {
223                 var currDT = (this.$altInput && this._defaults.altFieldTimeOnly) ?
224                                 this.$input.val() + ' ' + this.$altInput.val() :
225                                 this.$input.val();
226
227                 this.timeDefined = this._parseTime(currDT);
228                 this._limitMinMaxDateTime(dp_inst, false);
229                 this._injectTimePicker();
230         },
231
232         //########################################################################
233         // parse the time string from input value or _setTime
234         //########################################################################
235         _parseTime: function(timeString, withDate) {
236                 var regstr = this._defaults.timeFormat.toString()
237                                 .replace(/h{1,2}/ig, '(\\d?\\d)')
238                                 .replace(/m{1,2}/ig, '(\\d?\\d)')
239                                 .replace(/s{1,2}/ig, '(\\d?\\d)')
240                                 .replace(/l{1}/ig, '(\\d?\\d?\\d)')
241                                 .replace(/t{1,2}/ig, this._getPatternAmpm())
242                                 .replace(/z{1}/ig, '(z|[-+]\\d\\d:?\\d\\d)?')
243                                 .replace(/\s/g, '\\s?') + this._defaults.timeSuffix + '$',
244                         order = this._getFormatPositions(),
245                         ampm = '',
246                         treg;
247
248                 if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
249
250                 if (withDate || !this._defaults.timeOnly) {
251                         // the time should come after x number of characters and a space.
252                         // x = at least the length of text specified by the date format
253                         var dp_dateFormat = $.datepicker._get(this.inst, 'dateFormat');
254                         // escape special regex characters in the seperator
255                         var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g");
256                         regstr = '^.{' + dp_dateFormat.length + ',}?' + this._defaults.separator.replace(specials, "\\$&") + regstr;
257                 }
258
259                 treg = timeString.match(new RegExp(regstr, 'i'));
260
261                 if (treg) {
262                         if (order.t !== -1) {
263                                 if (treg[order.t] === undefined || treg[order.t].length === 0) {
264                                         ampm = '';
265                                         this.ampm = '';
266                                 } else {
267                                         ampm = $.inArray(treg[order.t].toUpperCase(), this.amNames) !== -1 ? 'AM' : 'PM';
268                                         this.ampm = this._defaults[ampm == 'AM' ? 'amNames' : 'pmNames'][0];
269                                 }
270                         }
271
272                         if (order.h !== -1) {
273                                 if (ampm == 'AM' && treg[order.h] == '12')
274                                         this.hour = 0; // 12am = 0 hour
275                                 else if (ampm == 'PM' && treg[order.h] != '12')
276                                         this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); // 12pm = 12 hour, any other pm = hour + 12
277                                 else this.hour = Number(treg[order.h]);
278                         }
279
280                         if (order.m !== -1) this.minute = Number(treg[order.m]);
281                         if (order.s !== -1) this.second = Number(treg[order.s]);
282                         if (order.l !== -1) this.millisec = Number(treg[order.l]);
283                         if (order.z !== -1 && treg[order.z] !== undefined) {
284                                 var tz = treg[order.z].toUpperCase();
285                                 switch (tz.length) {
286                                 case 1: // Z
287                                         tz = this._defaults.timezoneIso8609 ? 'Z' : '+0000';
288                                         break;
289                                 case 5: // +hhmm
290                                         if (this._defaults.timezoneIso8609)
291                                                 tz = tz.substring(1) == '0000'
292                                                    ? 'Z'
293                                                    : tz.substring(0, 3) + ':' + tz.substring(3);
294                                         break;
295                                 case 6: // +hh:mm
296                                         if (!this._defaults.timezoneIso8609)
297                                                 tz = tz == 'Z' || tz.substring(1) == '00:00'
298                                                    ? '+0000'
299                                                    : tz.replace(/:/, '');
300                                         else if (tz.substring(1) == '00:00')
301                                                 tz = 'Z';
302                                         break;
303                                 }
304                                 this.timezone = tz;
305                         }
306
307                         return true;
308
309                 }
310                 return false;
311         },
312
313         //########################################################################
314         // pattern for standard and localized AM/PM markers
315         //########################################################################
316         _getPatternAmpm: function() {
317                 var markers = [],
318                         o = this._defaults;
319                 if (o.amNames)
320                         $.merge(markers, o.amNames);
321                 if (o.pmNames)
322                         $.merge(markers, o.pmNames);
323                 markers = $.map(markers, function(val) { return val.replace(/[.*+?|()\[\]{}\\]/g, '\\$&'); });
324                 return '(' + markers.join('|') + ')?';
325         },
326
327         //########################################################################
328         // figure out position of time elements.. cause js cant do named captures
329         //########################################################################
330         _getFormatPositions: function() {
331                 var finds = this._defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|l{1}|t{1,2}|z)/g),
332                         orders = { h: -1, m: -1, s: -1, l: -1, t: -1, z: -1 };
333
334                 if (finds)
335                         for (var i = 0; i < finds.length; i++)
336                                 if (orders[finds[i].toString().charAt(0)] == -1)
337                                         orders[finds[i].toString().charAt(0)] = i + 1;
338
339                 return orders;
340         },
341
342         //########################################################################
343         // generate and inject html for timepicker into ui datepicker
344         //########################################################################
345         _injectTimePicker: function() {
346                 var $dp = this.inst.dpDiv,
347                         o = this._defaults,
348                         tp_inst = this,
349                         // Added by Peter Medeiros:
350                         // - Figure out what the hour/minute/second max should be based on the step values.
351                         // - Example: if stepMinute is 15, then minMax is 45.
352                         hourMax = parseInt((o.hourMax - ((o.hourMax - o.hourMin) % o.stepHour)) ,10),
353                         minMax  = parseInt((o.minuteMax - ((o.minuteMax - o.minuteMin) % o.stepMinute)) ,10),
354                         secMax  = parseInt((o.secondMax - ((o.secondMax - o.secondMin) % o.stepSecond)) ,10),
355                         millisecMax  = parseInt((o.millisecMax - ((o.millisecMax - o.millisecMin) % o.stepMillisec)) ,10),
356                         dp_id = this.inst.id.toString().replace(/([^A-Za-z0-9_])/g, '');
357
358                 // Prevent displaying twice
359                 //if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0) {
360                 if ($dp.find("div#ui-timepicker-div-"+ dp_id).length === 0 && o.showTimepicker) {
361                         var noDisplay = ' style="display:none;"',
362                                 html =  '<div class="ui-timepicker-div" id="ui-timepicker-div-' + dp_id + '"><dl>' +
363                                                 '<dt class="ui_tpicker_time_label" id="ui_tpicker_time_label_' + dp_id + '"' +
364                                                 ((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
365                                                 '<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
366                                                 ((o.showTime) ? '' : noDisplay) + '></dd>' +
367                                                 '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
368                                                 ((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
369                                 hourGridSize = 0,
370                                 minuteGridSize = 0,
371                                 secondGridSize = 0,
372                                 millisecGridSize = 0,
373                                 size = null;
374
375                         // Hours
376                         html += '<dd class="ui_tpicker_hour"><div id="ui_tpicker_hour_' + dp_id + '"' +
377                                                 ((o.showHour) ? '' : noDisplay) + '></div>';
378                         if (o.showHour && o.hourGrid > 0) {
379                                 html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
380
381                                 for (var h = o.hourMin; h <= hourMax; h += parseInt(o.hourGrid,10)) {
382                                         hourGridSize++;
383                                         var tmph = (o.ampm && h > 12) ? h-12 : h;
384                                         if (tmph < 10) tmph = '0' + tmph;
385                                         if (o.ampm) {
386                                                 if (h == 0) tmph = 12 +'a';
387                                                 else if (h < 12) tmph += 'a';
388                                                 else tmph += 'p';
389                                         }
390                                         html += '<td>' + tmph + '</td>';
391                                 }
392
393                                 html += '</tr></table></div>';
394                         }
395                         html += '</dd>';
396
397                         // Minutes
398                         html += '<dt class="ui_tpicker_minute_label" id="ui_tpicker_minute_label_' + dp_id + '"' +
399                                         ((o.showMinute) ? '' : noDisplay) + '>' + o.minuteText + '</dt>'+
400                                         '<dd class="ui_tpicker_minute"><div id="ui_tpicker_minute_' + dp_id + '"' +
401                                                         ((o.showMinute) ? '' : noDisplay) + '></div>';
402
403                         if (o.showMinute && o.minuteGrid > 0) {
404                                 html += '<div style="padding-left: 1px"><table class="ui-tpicker-grid-label"><tr>';
405
406                                 for (var m = o.minuteMin; m <= minMax; m += parseInt(o.minuteGrid,10)) {
407                                         minuteGridSize++;
408                                         html += '<td>' + ((m < 10) ? '0' : '') + m + '</td>';
409                                 }
410
411                                 html += '</tr></table></div>';
412                         }
413                         html += '</dd>';
414
415                         // Seconds
416                         html += '<dt class="ui_tpicker_second_label" id="ui_tpicker_second_label_' + dp_id + '"' +
417                                         ((o.showSecond) ? '' : noDisplay) + '>' + o.secondText + '</dt>'+
418                                         '<dd class="ui_tpicker_second"><div id="ui_tpicker_second_' + dp_id + '"'+
419                                                         ((o.showSecond) ? '' : noDisplay) + '></div>';
420
421                         if (o.showSecond && o.secondGrid > 0) {
422                                 html += '<div style="padding-left: 1px"><table><tr>';
423
424                                 for (var s = o.secondMin; s <= secMax; s += parseInt(o.secondGrid,10)) {
425                                         secondGridSize++;
426                                         html += '<td>' + ((s < 10) ? '0' : '') + s + '</td>';
427                                 }
428
429                                 html += '</tr></table></div>';
430                         }
431                         html += '</dd>';
432
433                         // Milliseconds
434                         html += '<dt class="ui_tpicker_millisec_label" id="ui_tpicker_millisec_label_' + dp_id + '"' +
435                                         ((o.showMillisec) ? '' : noDisplay) + '>' + o.millisecText + '</dt>'+
436                                         '<dd class="ui_tpicker_millisec"><div id="ui_tpicker_millisec_' + dp_id + '"'+
437                                                         ((o.showMillisec) ? '' : noDisplay) + '></div>';
438
439                         if (o.showMillisec && o.millisecGrid > 0) {
440                                 html += '<div style="padding-left: 1px"><table><tr>';
441
442                                 for (var l = o.millisecMin; l <= millisecMax; l += parseInt(o.millisecGrid,10)) {
443                                         millisecGridSize++;
444                                         html += '<td>' + ((l < 10) ? '0' : '') + l + '</td>';
445                                 }
446
447                                 html += '</tr></table></div>';
448                         }
449                         html += '</dd>';
450
451                         // Timezone
452                         html += '<dt class="ui_tpicker_timezone_label" id="ui_tpicker_timezone_label_' + dp_id + '"' +
453                                         ((o.showTimezone) ? '' : noDisplay) + '>' + o.timezoneText + '</dt>';
454                         html += '<dd class="ui_tpicker_timezone" id="ui_tpicker_timezone_' + dp_id + '"'        +
455                                                         ((o.showTimezone) ? '' : noDisplay) + '></dd>';
456
457                         html += '</dl></div>';
458                         $tp = $(html);
459
460                                 // if we only want time picker...
461                         if (o.timeOnly === true) {
462                                 $tp.prepend(
463                                         '<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
464                                                 '<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' +
465                                         '</div>');
466                                 $dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
467                         }
468
469                         this.hour_slider = $tp.find('#ui_tpicker_hour_'+ dp_id).slider({
470                                 orientation: "horizontal",
471                                 value: this.hour,
472                                 min: o.hourMin,
473                                 max: hourMax,
474                                 step: o.stepHour,
475                                 slide: function(event, ui) {
476                                         tp_inst.hour_slider.slider( "option", "value", ui.value);
477                                         tp_inst._onTimeChange();
478                                 }
479                         });
480
481
482                         // Updated by Peter Medeiros:
483                         // - Pass in Event and UI instance into slide function
484                         this.minute_slider = $tp.find('#ui_tpicker_minute_'+ dp_id).slider({
485                                 orientation: "horizontal",
486                                 value: this.minute,
487                                 min: o.minuteMin,
488                                 max: minMax,
489                                 step: o.stepMinute,
490                                 slide: function(event, ui) {
491                                         tp_inst.minute_slider.slider( "option", "value", ui.value);
492                                         tp_inst._onTimeChange();
493                                 }
494                         });
495
496                         this.second_slider = $tp.find('#ui_tpicker_second_'+ dp_id).slider({
497                                 orientation: "horizontal",
498                                 value: this.second,
499                                 min: o.secondMin,
500                                 max: secMax,
501                                 step: o.stepSecond,
502                                 slide: function(event, ui) {
503                                         tp_inst.second_slider.slider( "option", "value", ui.value);
504                                         tp_inst._onTimeChange();
505                                 }
506                         });
507
508                         this.millisec_slider = $tp.find('#ui_tpicker_millisec_'+ dp_id).slider({
509                                 orientation: "horizontal",
510                                 value: this.millisec,
511                                 min: o.millisecMin,
512                                 max: millisecMax,
513                                 step: o.stepMillisec,
514                                 slide: function(event, ui) {
515                                         tp_inst.millisec_slider.slider( "option", "value", ui.value);
516                                         tp_inst._onTimeChange();
517                                 }
518                         });
519
520                         this.timezone_select = $tp.find('#ui_tpicker_timezone_'+ dp_id).append('<select></select>').find("select");
521                         $.fn.append.apply(this.timezone_select,
522                                 $.map(o.timezoneList, function(val, idx) {
523                                         return $("<option />")
524                                                 .val(typeof val == "object" ? val.value : val)
525                                                 .text(typeof val == "object" ? val.label : val);
526                                 })
527                         );
528                         this.timezone_select.val((typeof this.timezone != "undefined" && this.timezone != null && this.timezone != "") ? this.timezone : o.timezone);
529                         this.timezone_select.change(function() {
530                                 tp_inst._onTimeChange();
531                         });
532
533                         // Add grid functionality
534                         if (o.showHour && o.hourGrid > 0) {
535                                 size = 100 * hourGridSize * o.hourGrid / (hourMax - o.hourMin);
536
537                                 $tp.find(".ui_tpicker_hour table").css({
538                                         width: size + "%",
539                                         marginLeft: (size / (-2 * hourGridSize)) + "%",
540                                         borderCollapse: 'collapse'
541                                 }).find("td").each( function(index) {
542                                         $(this).click(function() {
543                                                 var h = $(this).html();
544                                                 if(o.ampm)      {
545                                                         var ap = h.substring(2).toLowerCase(),
546                                                                 aph = parseInt(h.substring(0,2), 10);
547                                                         if (ap == 'a') {
548                                                                 if (aph == 12) h = 0;
549                                                                 else h = aph;
550                                                         } else if (aph == 12) h = 12;
551                                                         else h = aph + 12;
552                                                 }
553                                                 tp_inst.hour_slider.slider("option", "value", h);
554                                                 tp_inst._onTimeChange();
555                                                 tp_inst._onSelectHandler();
556                                         }).css({
557                                                 cursor: 'pointer',
558                                                 width: (100 / hourGridSize) + '%',
559                                                 textAlign: 'center',
560                                                 overflow: 'hidden'
561                                         });
562                                 });
563                         }
564
565                         if (o.showMinute && o.minuteGrid > 0) {
566                                 size = 100 * minuteGridSize * o.minuteGrid / (minMax - o.minuteMin);
567                                 $tp.find(".ui_tpicker_minute table").css({
568                                         width: size + "%",
569                                         marginLeft: (size / (-2 * minuteGridSize)) + "%",
570                                         borderCollapse: 'collapse'
571                                 }).find("td").each(function(index) {
572                                         $(this).click(function() {
573                                                 tp_inst.minute_slider.slider("option", "value", $(this).html());
574                                                 tp_inst._onTimeChange();
575                                                 tp_inst._onSelectHandler();
576                                         }).css({
577                                                 cursor: 'pointer',
578                                                 width: (100 / minuteGridSize) + '%',
579                                                 textAlign: 'center',
580                                                 overflow: 'hidden'
581                                         });
582                                 });
583                         }
584
585                         if (o.showSecond && o.secondGrid > 0) {
586                                 $tp.find(".ui_tpicker_second table").css({
587                                         width: size + "%",
588                                         marginLeft: (size / (-2 * secondGridSize)) + "%",
589                                         borderCollapse: 'collapse'
590                                 }).find("td").each(function(index) {
591                                         $(this).click(function() {
592                                                 tp_inst.second_slider.slider("option", "value", $(this).html());
593                                                 tp_inst._onTimeChange();
594                                                 tp_inst._onSelectHandler();
595                                         }).css({
596                                                 cursor: 'pointer',
597                                                 width: (100 / secondGridSize) + '%',
598                                                 textAlign: 'center',
599                                                 overflow: 'hidden'
600                                         });
601                                 });
602                         }
603
604                         if (o.showMillisec && o.millisecGrid > 0) {
605                                 $tp.find(".ui_tpicker_millisec table").css({
606                                         width: size + "%",
607                                         marginLeft: (size / (-2 * millisecGridSize)) + "%",
608                                         borderCollapse: 'collapse'
609                                 }).find("td").each(function(index) {
610                                         $(this).click(function() {
611                                                 tp_inst.millisec_slider.slider("option", "value", $(this).html());
612                                                 tp_inst._onTimeChange();
613                                                 tp_inst._onSelectHandler();
614                                         }).css({
615                                                 cursor: 'pointer',
616                                                 width: (100 / millisecGridSize) + '%',
617                                                 textAlign: 'center',
618                                                 overflow: 'hidden'
619                                         });
620                                 });
621                         }
622
623                         var $buttonPanel = $dp.find('.ui-datepicker-buttonpane');
624                         if ($buttonPanel.length) $buttonPanel.before($tp);
625                         else $dp.append($tp);
626
627                         this.$timeObj = $tp.find('#ui_tpicker_time_'+ dp_id);
628
629                         if (this.inst !== null) {
630                                 var timeDefined = this.timeDefined;
631                                 this._onTimeChange();
632                                 this.timeDefined = timeDefined;
633                         }
634
635                         //Emulate datepicker onSelect behavior. Call on slidestop.
636                         var onSelectDelegate = function() {
637                                 tp_inst._onSelectHandler();
638                         };
639                         this.hour_slider.bind('slidestop',onSelectDelegate);
640                         this.minute_slider.bind('slidestop',onSelectDelegate);
641                         this.second_slider.bind('slidestop',onSelectDelegate);
642                         this.millisec_slider.bind('slidestop',onSelectDelegate);
643
644                         // slideAccess integration: http://trentrichardson.com/2011/11/11/jquery-ui-sliders-and-touch-accessibility/
645                         if (this._defaults.addSliderAccess){
646                                 var sliderAccessArgs = this._defaults.sliderAccessArgs;
647                                 setTimeout(function(){ // fix for inline mode
648                                         if($tp.find('.ui-slider-access').length == 0){
649                                                 $tp.find('.ui-slider:visible').sliderAccess(sliderAccessArgs);
650
651                                                 // fix any grids since sliders are shorter
652                                                 var sliderAccessWidth = $tp.find('.ui-slider-access:eq(0)').outerWidth(true);
653                                                 if(sliderAccessWidth){
654                                                         $tp.find('table:visible').each(function(){
655                                                                 var $g = $(this),
656                                                                         oldWidth = $g.outerWidth(),
657                                                                         oldMarginLeft = $g.css('marginLeft').toString().replace('%',''),
658                                                                         newWidth = oldWidth - sliderAccessWidth,
659                                                                         newMarginLeft = ((oldMarginLeft * newWidth)/oldWidth) + '%';
660
661                                                                 $g.css({ width: newWidth, marginLeft: newMarginLeft });
662                                                         });
663                                                 }
664                                         }
665                                 },0);
666                         }
667                         // end slideAccess integration
668
669                 }
670         },
671
672         //########################################################################
673         // This function tries to limit the ability to go outside the
674         // min/max date range
675         //########################################################################
676         _limitMinMaxDateTime: function(dp_inst, adjustSliders){
677                 var o = this._defaults,
678                         dp_date = new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay);
679
680                 if(!this._defaults.showTimepicker) return; // No time so nothing to check here
681
682                 if($.datepicker._get(dp_inst, 'minDateTime') !== null && $.datepicker._get(dp_inst, 'minDateTime') !== undefined && dp_date){
683                         var minDateTime = $.datepicker._get(dp_inst, 'minDateTime'),
684                                 minDateTimeDate = new Date(minDateTime.getFullYear(), minDateTime.getMonth(), minDateTime.getDate(), 0, 0, 0, 0);
685
686                         if(this.hourMinOriginal === null || this.minuteMinOriginal === null || this.secondMinOriginal === null || this.millisecMinOriginal === null){
687                                 this.hourMinOriginal = o.hourMin;
688                                 this.minuteMinOriginal = o.minuteMin;
689                                 this.secondMinOriginal = o.secondMin;
690                                 this.millisecMinOriginal = o.millisecMin;
691                         }
692
693                         if(dp_inst.settings.timeOnly || minDateTimeDate.getTime() == dp_date.getTime()) {
694                                 this._defaults.hourMin = minDateTime.getHours();
695                                 if (this.hour <= this._defaults.hourMin) {
696                                         this.hour = this._defaults.hourMin;
697                                         this._defaults.minuteMin = minDateTime.getMinutes();
698                                         if (this.minute <= this._defaults.minuteMin) {
699                                                 this.minute = this._defaults.minuteMin;
700                                                 this._defaults.secondMin = minDateTime.getSeconds();
701                                         } else if (this.second <= this._defaults.secondMin){
702                                                 this.second = this._defaults.secondMin;
703                                                 this._defaults.millisecMin = minDateTime.getMilliseconds();
704                                         } else {
705                                                 if(this.millisec < this._defaults.millisecMin)
706                                                         this.millisec = this._defaults.millisecMin;
707                                                 this._defaults.millisecMin = this.millisecMinOriginal;
708                                         }
709                                 } else {
710                                         this._defaults.minuteMin = this.minuteMinOriginal;
711                                         this._defaults.secondMin = this.secondMinOriginal;
712                                         this._defaults.millisecMin = this.millisecMinOriginal;
713                                 }
714                         }else{
715                                 this._defaults.hourMin = this.hourMinOriginal;
716                                 this._defaults.minuteMin = this.minuteMinOriginal;
717                                 this._defaults.secondMin = this.secondMinOriginal;
718                                 this._defaults.millisecMin = this.millisecMinOriginal;
719                         }
720                 }
721
722                 if($.datepicker._get(dp_inst, 'maxDateTime') !== null && $.datepicker._get(dp_inst, 'maxDateTime') !== undefined && dp_date){
723                         var maxDateTime = $.datepicker._get(dp_inst, 'maxDateTime'),
724                                 maxDateTimeDate = new Date(maxDateTime.getFullYear(), maxDateTime.getMonth(), maxDateTime.getDate(), 0, 0, 0, 0);
725
726                         if(this.hourMaxOriginal === null || this.minuteMaxOriginal === null || this.secondMaxOriginal === null){
727                                 this.hourMaxOriginal = o.hourMax;
728                                 this.minuteMaxOriginal = o.minuteMax;
729                                 this.secondMaxOriginal = o.secondMax;
730                                 this.millisecMaxOriginal = o.millisecMax;
731                         }
732
733                         if(dp_inst.settings.timeOnly || maxDateTimeDate.getTime() == dp_date.getTime()){
734                                 this._defaults.hourMax = maxDateTime.getHours();
735                                 if (this.hour >= this._defaults.hourMax) {
736                                         this.hour = this._defaults.hourMax;
737                                         this._defaults.minuteMax = maxDateTime.getMinutes();
738                                         if (this.minute >= this._defaults.minuteMax) {
739                                                 this.minute = this._defaults.minuteMax;
740                                                 this._defaults.secondMax = maxDateTime.getSeconds();
741                                         } else if (this.second >= this._defaults.secondMax) {
742                                                 this.second = this._defaults.secondMax;
743                                                 this._defaults.millisecMax = maxDateTime.getMilliseconds();
744                                         } else {
745                                                 if(this.millisec > this._defaults.millisecMax) this.millisec = this._defaults.millisecMax;
746                                                 this._defaults.millisecMax = this.millisecMaxOriginal;
747                                         }
748                                 } else {
749                                         this._defaults.minuteMax = this.minuteMaxOriginal;
750                                         this._defaults.secondMax = this.secondMaxOriginal;
751                                         this._defaults.millisecMax = this.millisecMaxOriginal;
752                                 }
753                         }else{
754                                 this._defaults.hourMax = this.hourMaxOriginal;
755                                 this._defaults.minuteMax = this.minuteMaxOriginal;
756                                 this._defaults.secondMax = this.secondMaxOriginal;
757                                 this._defaults.millisecMax = this.millisecMaxOriginal;
758                         }
759                 }
760
761                 if(adjustSliders !== undefined && adjustSliders === true){
762                         var hourMax = parseInt((this._defaults.hourMax - ((this._defaults.hourMax - this._defaults.hourMin) % this._defaults.stepHour)) ,10),
763                 minMax  = parseInt((this._defaults.minuteMax - ((this._defaults.minuteMax - this._defaults.minuteMin) % this._defaults.stepMinute)) ,10),
764                 secMax  = parseInt((this._defaults.secondMax - ((this._defaults.secondMax - this._defaults.secondMin) % this._defaults.stepSecond)) ,10),
765                                 millisecMax  = parseInt((this._defaults.millisecMax - ((this._defaults.millisecMax - this._defaults.millisecMin) % this._defaults.stepMillisec)) ,10);
766
767                         if(this.hour_slider)
768                                 this.hour_slider.slider("option", { min: this._defaults.hourMin, max: hourMax }).slider('value', this.hour);
769                         if(this.minute_slider)
770                                 this.minute_slider.slider("option", { min: this._defaults.minuteMin, max: minMax }).slider('value', this.minute);
771                         if(this.second_slider)
772                                 this.second_slider.slider("option", { min: this._defaults.secondMin, max: secMax }).slider('value', this.second);
773                         if(this.millisec_slider)
774                                 this.millisec_slider.slider("option", { min: this._defaults.millisecMin, max: millisecMax }).slider('value', this.millisec);
775                 }
776
777         },
778
779
780         //########################################################################
781         // when a slider moves, set the internal time...
782         // on time change is also called when the time is updated in the text field
783         //########################################################################
784         _onTimeChange: function() {
785                 var hour   = (this.hour_slider) ? this.hour_slider.slider('value') : false,
786                         minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
787                         second = (this.second_slider) ? this.second_slider.slider('value') : false,
788                         millisec = (this.millisec_slider) ? this.millisec_slider.slider('value') : false,
789                         timezone = (this.timezone_select) ? this.timezone_select.val() : false,
790                         o = this._defaults;
791
792                 if (typeof(hour) == 'object') hour = false;
793                 if (typeof(minute) == 'object') minute = false;
794                 if (typeof(second) == 'object') second = false;
795                 if (typeof(millisec) == 'object') millisec = false;
796                 if (typeof(timezone) == 'object') timezone = false;
797
798                 if (hour !== false) hour = parseInt(hour,10);
799                 if (minute !== false) minute = parseInt(minute,10);
800                 if (second !== false) second = parseInt(second,10);
801                 if (millisec !== false) millisec = parseInt(millisec,10);
802
803                 var ampm = o[hour < 12 ? 'amNames' : 'pmNames'][0];
804
805                 // If the update was done in the input field, the input field should not be updated.
806                 // If the update was done using the sliders, update the input field.
807                 var hasChanged = (hour != this.hour || minute != this.minute
808                                 || second != this.second || millisec != this.millisec
809                                 || (this.ampm.length > 0
810                                     && (hour < 12) != ($.inArray(this.ampm.toUpperCase(), this.amNames) !== -1))
811                                 || timezone != this.timezone);
812
813                 if (hasChanged) {
814
815                         if (hour !== false)this.hour = hour;
816                         if (minute !== false) this.minute = minute;
817                         if (second !== false) this.second = second;
818                         if (millisec !== false) this.millisec = millisec;
819                         if (timezone !== false) this.timezone = timezone;
820
821                         if (!this.inst) this.inst = $.datepicker._getInst(this.$input[0]);
822
823                         this._limitMinMaxDateTime(this.inst, true);
824                 }
825                 if (o.ampm) this.ampm = ampm;
826
827                 //this._formatTime();
828                 this.formattedTime = $.datepicker.formatTime(this._defaults.timeFormat, this, this._defaults);
829                 if (this.$timeObj) this.$timeObj.text(this.formattedTime + o.timeSuffix);
830                 this.timeDefined = true;
831                 if (hasChanged) this._updateDateTime();
832         },
833
834         //########################################################################
835         // call custom onSelect.
836         // bind to sliders slidestop, and grid click.
837         //########################################################################
838         _onSelectHandler: function() {
839                 var onSelect = this._defaults.onSelect;
840                 var inputEl = this.$input ? this.$input[0] : null;
841                 if (onSelect && inputEl) {
842                         onSelect.apply(inputEl, [this.formattedDateTime, this]);
843                 }
844         },
845
846         //########################################################################
847         // left for any backwards compatibility
848         //########################################################################
849         _formatTime: function(time, format) {
850                 time = time || { hour: this.hour, minute: this.minute, second: this.second, millisec: this.millisec, ampm: this.ampm, timezone: this.timezone };
851                 var tmptime = (format || this._defaults.timeFormat).toString();
852
853                 tmptime = $.datepicker.formatTime(tmptime, time, this._defaults);
854
855                 if (arguments.length) return tmptime;
856                 else this.formattedTime = tmptime;
857         },
858
859         //########################################################################
860         // update our input with the new date time..
861         //########################################################################
862         _updateDateTime: function(dp_inst) {
863                 dp_inst = this.inst || dp_inst;
864                 var dt = $.datepicker._daylightSavingAdjust(new Date(dp_inst.selectedYear, dp_inst.selectedMonth, dp_inst.selectedDay)),
865                         dateFmt = $.datepicker._get(dp_inst, 'dateFormat'),
866                         formatCfg = $.datepicker._getFormatConfig(dp_inst),
867                         timeAvailable = dt !== null && this.timeDefined;
868                 this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
869                 var formattedDateTime = this.formattedDate;
870                 if (dp_inst.lastVal !== undefined && (dp_inst.lastVal.length > 0 && this.$input.val().length === 0))
871                         return;
872
873                 if (this._defaults.timeOnly === true) {
874                         formattedDateTime = this.formattedTime;
875                 } else if (this._defaults.timeOnly !== true && (this._defaults.alwaysSetTime || timeAvailable)) {
876                         formattedDateTime += this._defaults.separator + this.formattedTime + this._defaults.timeSuffix;
877                 }
878
879                 this.formattedDateTime = formattedDateTime;
880
881                 if(!this._defaults.showTimepicker) {
882                         this.$input.val(this.formattedDate);
883                 } else if (this.$altInput && this._defaults.altFieldTimeOnly === true) {
884                         this.$altInput.val(this.formattedTime);
885                         this.$input.val(this.formattedDate);
886                 } else if(this.$altInput) {
887                         this.$altInput.val(formattedDateTime);
888                         this.$input.val(formattedDateTime);
889                 } else {
890                         this.$input.val(formattedDateTime);
891                 }
892
893                 this.$input.trigger("change");
894         }
895
896 });
897
898 $.fn.extend({
899         //########################################################################
900         // shorthand just to use timepicker..
901         //########################################################################
902         timepicker: function(o) {
903                 o = o || {};
904                 var tmp_args = arguments;
905
906                 if (typeof o == 'object') tmp_args[0] = $.extend(o, { timeOnly: true });
907
908                 return $(this).each(function() {
909                         $.fn.datetimepicker.apply($(this), tmp_args);
910                 });
911         },
912
913         //########################################################################
914         // extend timepicker to datepicker
915         //########################################################################
916         datetimepicker: function(o) {
917                 o = o || {};
918                 tmp_args = arguments;
919
920                 if (typeof(o) == 'string'){
921                         if(o == 'getDate')
922                                 return $.fn.datepicker.apply($(this[0]), tmp_args);
923                         else
924                                 return this.each(function() {
925                                         var $t = $(this);
926                                         $t.datepicker.apply($t, tmp_args);
927                                 });
928                 }
929                 else
930                         return this.each(function() {
931                                 var $t = $(this);
932                                 $t.datepicker($.timepicker._newInst($t, o)._defaults);
933                         });
934         }
935 });
936
937 //########################################################################
938 // format the time all pretty...
939 // format = string format of the time
940 // time = a {}, not a Date() for timezones
941 // options = essentially the regional[].. amNames, pmNames, ampm
942 //########################################################################
943 $.datepicker.formatTime = function(format, time, options) {
944         options = options || {};
945         options = $.extend($.timepicker._defaults, options);
946         time = $.extend({hour:0, minute:0, second:0, millisec:0, timezone:'+0000'}, time);
947
948         var tmptime = format;
949         var ampmName = options['amNames'][0];
950
951         var hour = parseInt(time.hour, 10);
952         if (options.ampm) {
953                 if (hour > 11){
954                         ampmName = options['pmNames'][0];
955                         if(hour > 12)
956                                 hour = hour % 12;
957                 }
958                 if (hour === 0)
959                         hour = 12;
960         }
961         tmptime = tmptime.replace(/(?:hh?|mm?|ss?|[tT]{1,2}|[lz])/g, function(match) {
962                 switch (match.toLowerCase()) {
963                         case 'hh': return ('0' + hour).slice(-2);
964                         case 'h':  return hour;
965                         case 'mm': return ('0' + time.minute).slice(-2);
966                         case 'm':  return time.minute;
967                         case 'ss': return ('0' + time.second).slice(-2);
968                         case 's':  return time.second;
969                         case 'l':  return ('00' + time.millisec).slice(-3);
970                         case 'z':  return time.timezone;
971                         case 't': case 'tt':
972                                 if (options.ampm) {
973                                         if (match.length == 1)
974                                                 ampmName = ampmName.charAt(0);
975                                         return match.charAt(0) == 'T' ? ampmName.toUpperCase() : ampmName.toLowerCase();
976                                 }
977                                 return '';
978                 }
979         });
980
981         tmptime = $.trim(tmptime);
982         return tmptime;
983 };
984
985 //########################################################################
986 // the bad hack :/ override datepicker so it doesnt close on select
987 // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
988 //########################################################################
989 $.datepicker._base_selectDate = $.datepicker._selectDate;
990 $.datepicker._selectDate = function (id, dateStr) {
991         var inst = this._getInst($(id)[0]),
992                 tp_inst = this._get(inst, 'timepicker');
993
994         if (tp_inst) {
995                 tp_inst._limitMinMaxDateTime(inst, true);
996                 inst.inline = inst.stay_open = true;
997                 //This way the onSelect handler called from calendarpicker get the full dateTime
998                 this._base_selectDate(id, dateStr);
999                 inst.inline = inst.stay_open = false;
1000                 this._notifyChange(inst);
1001                 this._updateDatepicker(inst);
1002         }
1003         else this._base_selectDate(id, dateStr);
1004 };
1005
1006 //#############################################################################################
1007 // second bad hack :/ override datepicker so it triggers an event when changing the input field
1008 // and does not redraw the datepicker on every selectDate event
1009 //#############################################################################################
1010 $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
1011 $.datepicker._updateDatepicker = function(inst) {
1012
1013         // don't popup the datepicker if there is another instance already opened
1014         var input = inst.input[0];
1015         if($.datepicker._curInst &&
1016            $.datepicker._curInst != inst &&
1017            $.datepicker._datepickerShowing &&
1018            $.datepicker._lastInput != input) {
1019                 return;
1020         }
1021
1022         if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
1023
1024                 this._base_updateDatepicker(inst);
1025
1026                 // Reload the time control when changing something in the input text field.
1027                 var tp_inst = this._get(inst, 'timepicker');
1028                 if(tp_inst) tp_inst._addTimePicker(inst);
1029         }
1030 };
1031
1032 //#######################################################################################
1033 // third bad hack :/ override datepicker so it allows spaces and colon in the input field
1034 //#######################################################################################
1035 $.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
1036 $.datepicker._doKeyPress = function(event) {
1037         var inst = $.datepicker._getInst(event.target),
1038                 tp_inst = $.datepicker._get(inst, 'timepicker');
1039
1040         if (tp_inst) {
1041                 if ($.datepicker._get(inst, 'constrainInput')) {
1042                         var ampm = tp_inst._defaults.ampm,
1043                                 dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')),
1044                                 datetimeChars = tp_inst._defaults.timeFormat.toString()
1045                                                                 .replace(/[hms]/g, '')
1046                                                                 .replace(/TT/g, ampm ? 'APM' : '')
1047                                                                 .replace(/Tt/g, ampm ? 'AaPpMm' : '')
1048                                                                 .replace(/tT/g, ampm ? 'AaPpMm' : '')
1049                                                                 .replace(/T/g, ampm ? 'AP' : '')
1050                                                                 .replace(/tt/g, ampm ? 'apm' : '')
1051                                                                 .replace(/t/g, ampm ? 'ap' : '') +
1052                                                                 " " +
1053                                                                 tp_inst._defaults.separator +
1054                                                                 tp_inst._defaults.timeSuffix +
1055                                                                 (tp_inst._defaults.showTimezone ? tp_inst._defaults.timezoneList.join('') : '') +
1056                                                                 (tp_inst._defaults.amNames.join('')) +
1057                                                                 (tp_inst._defaults.pmNames.join('')) +
1058                                                                 dateChars,
1059                                 chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
1060                         return event.ctrlKey || (chr < ' ' || !dateChars || datetimeChars.indexOf(chr) > -1);
1061                 }
1062         }
1063
1064         return $.datepicker._base_doKeyPress(event);
1065 };
1066
1067 //#######################################################################################
1068 // Override key up event to sync manual input changes.
1069 //#######################################################################################
1070 $.datepicker._base_doKeyUp = $.datepicker._doKeyUp;
1071 $.datepicker._doKeyUp = function (event) {
1072         var inst = $.datepicker._getInst(event.target),
1073                 tp_inst = $.datepicker._get(inst, 'timepicker');
1074
1075         if (tp_inst) {
1076                 if (tp_inst._defaults.timeOnly && (inst.input.val() != inst.lastVal)) {
1077                         try {
1078                                 $.datepicker._updateDatepicker(inst);
1079                         }
1080                         catch (err) {
1081                                 $.datepicker.log(err);
1082                         }
1083                 }
1084         }
1085
1086         return $.datepicker._base_doKeyUp(event);
1087 };
1088
1089 //#######################################################################################
1090 // override "Today" button to also grab the time.
1091 //#######################################################################################
1092 $.datepicker._base_gotoToday = $.datepicker._gotoToday;
1093 $.datepicker._gotoToday = function(id) {
1094         var inst = this._getInst($(id)[0]),
1095                 $dp = inst.dpDiv;
1096         this._base_gotoToday(id);
1097         var now = new Date();
1098         var tp_inst = this._get(inst, 'timepicker');
1099         if (tp_inst && tp_inst._defaults.showTimezone && tp_inst.timezone_select) {
1100                 var tzoffset = now.getTimezoneOffset(); // If +0100, returns -60
1101                 var tzsign = tzoffset > 0 ? '-' : '+';
1102                 tzoffset = Math.abs(tzoffset);
1103                 var tzmin = tzoffset % 60;
1104                 tzoffset = tzsign + ('0' + (tzoffset - tzmin) / 60).slice(-2) + ('0' + tzmin).slice(-2);
1105                 if (tp_inst._defaults.timezoneIso8609)
1106                         tzoffset = tzoffset.substring(0, 3) + ':' + tzoffset.substring(3);
1107                 tp_inst.timezone_select.val(tzoffset);
1108         }
1109         this._setTime(inst, now);
1110         $( '.ui-datepicker-today', $dp).click();
1111 };
1112
1113 //#######################################################################################
1114 // Disable & enable the Time in the datetimepicker
1115 //#######################################################################################
1116 $.datepicker._disableTimepickerDatepicker = function(target, date, withDate) {
1117         var inst = this._getInst(target),
1118         tp_inst = this._get(inst, 'timepicker');
1119         $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
1120         if (tp_inst) {
1121                 tp_inst._defaults.showTimepicker = false;
1122                 tp_inst._updateDateTime(inst);
1123         }
1124 };
1125
1126 $.datepicker._enableTimepickerDatepicker = function(target, date, withDate) {
1127         var inst = this._getInst(target),
1128         tp_inst = this._get(inst, 'timepicker');
1129         $(target).datepicker('getDate'); // Init selected[Year|Month|Day]
1130         if (tp_inst) {
1131                 tp_inst._defaults.showTimepicker = true;
1132                 tp_inst._addTimePicker(inst); // Could be disabled on page load
1133                 tp_inst._updateDateTime(inst);
1134         }
1135 };
1136
1137 //#######################################################################################
1138 // Create our own set time function
1139 //#######################################################################################
1140 $.datepicker._setTime = function(inst, date) {
1141         var tp_inst = this._get(inst, 'timepicker');
1142         if (tp_inst) {
1143                 var defaults = tp_inst._defaults,
1144                         // calling _setTime with no date sets time to defaults
1145                         hour = date ? date.getHours() : defaults.hour,
1146                         minute = date ? date.getMinutes() : defaults.minute,
1147                         second = date ? date.getSeconds() : defaults.second,
1148                         millisec = date ? date.getMilliseconds() : defaults.millisec;
1149
1150                 //check if within min/max times..
1151                 if ((hour < defaults.hourMin || hour > defaults.hourMax) || (minute < defaults.minuteMin || minute > defaults.minuteMax) || (second < defaults.secondMin || second > defaults.secondMax) || (millisec < defaults.millisecMin || millisec > defaults.millisecMax)) {
1152                         hour = defaults.hourMin;
1153                         minute = defaults.minuteMin;
1154                         second = defaults.secondMin;
1155                         millisec = defaults.millisecMin;
1156                 }
1157
1158                 tp_inst.hour = hour;
1159                 tp_inst.minute = minute;
1160                 tp_inst.second = second;
1161                 tp_inst.millisec = millisec;
1162
1163                 if (tp_inst.hour_slider) tp_inst.hour_slider.slider('value', hour);
1164                 if (tp_inst.minute_slider) tp_inst.minute_slider.slider('value', minute);
1165                 if (tp_inst.second_slider) tp_inst.second_slider.slider('value', second);
1166                 if (tp_inst.millisec_slider) tp_inst.millisec_slider.slider('value', millisec);
1167
1168                 tp_inst._onTimeChange();
1169                 tp_inst._updateDateTime(inst);
1170         }
1171 };
1172
1173 //#######################################################################################
1174 // Create new public method to set only time, callable as $().datepicker('setTime', date)
1175 //#######################################################################################
1176 $.datepicker._setTimeDatepicker = function(target, date, withDate) {
1177         var inst = this._getInst(target),
1178                 tp_inst = this._get(inst, 'timepicker');
1179
1180         if (tp_inst) {
1181                 this._setDateFromField(inst);
1182                 var tp_date;
1183                 if (date) {
1184                         if (typeof date == "string") {
1185                                 tp_inst._parseTime(date, withDate);
1186                                 tp_date = new Date();
1187                                 tp_date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
1188                         }
1189                         else tp_date = new Date(date.getTime());
1190                         if (tp_date.toString() == 'Invalid Date') tp_date = undefined;
1191                         this._setTime(inst, tp_date);
1192                 }
1193         }
1194
1195 };
1196
1197 //#######################################################################################
1198 // override setDate() to allow setting time too within Date object
1199 //#######################################################################################
1200 $.datepicker._base_setDateDatepicker = $.datepicker._setDateDatepicker;
1201 $.datepicker._setDateDatepicker = function(target, date) {
1202         var inst = this._getInst(target),
1203         tp_date = (date instanceof Date) ? new Date(date.getTime()) : date;
1204
1205         this._updateDatepicker(inst);
1206         this._base_setDateDatepicker.apply(this, arguments);
1207         this._setTimeDatepicker(target, tp_date, true);
1208 };
1209
1210 //#######################################################################################
1211 // override getDate() to allow getting time too within Date object
1212 //#######################################################################################
1213 $.datepicker._base_getDateDatepicker = $.datepicker._getDateDatepicker;
1214 $.datepicker._getDateDatepicker = function(target, noDefault) {
1215         var inst = this._getInst(target),
1216                 tp_inst = this._get(inst, 'timepicker');
1217
1218         if (tp_inst) {
1219                 this._setDateFromField(inst, noDefault);
1220                 var date = this._getDate(inst);
1221                 if (date && tp_inst._parseTime($(target).val(), tp_inst.timeOnly)) date.setHours(tp_inst.hour, tp_inst.minute, tp_inst.second, tp_inst.millisec);
1222                 return date;
1223         }
1224         return this._base_getDateDatepicker(target, noDefault);
1225 };
1226
1227 //#######################################################################################
1228 // override parseDate() because UI 1.8.14 throws an error about "Extra characters"
1229 // An option in datapicker to ignore extra format characters would be nicer.
1230 //#######################################################################################
1231 $.datepicker._base_parseDate = $.datepicker.parseDate;
1232 $.datepicker.parseDate = function(format, value, settings) {
1233         var date;
1234         try {
1235                 date = this._base_parseDate(format, value, settings);
1236         } catch (err) {
1237                 if (err.indexOf(":") >= 0) {
1238                         // Hack!  The error message ends with a colon, a space, and
1239                         // the "extra" characters.  We rely on that instead of
1240                         // attempting to perfectly reproduce the parsing algorithm.
1241                         date = this._base_parseDate(format, value.substring(0,value.length-(err.length-err.indexOf(':')-2)), settings);
1242                 } else {
1243                         // The underlying error was not related to the time
1244                         throw err;
1245                 }
1246         }
1247         return date;
1248 };
1249
1250 //#######################################################################################
1251 // override formatDate to set date with time to the input
1252 //#######################################################################################
1253 $.datepicker._base_formatDate = $.datepicker._formatDate;
1254 $.datepicker._formatDate = function(inst, day, month, year){
1255         var tp_inst = this._get(inst, 'timepicker');
1256         if(tp_inst) {
1257                 tp_inst._updateDateTime(inst);
1258                 return tp_inst.$input.val();
1259         }
1260         return this._base_formatDate(inst);
1261 };
1262
1263 //#######################################################################################
1264 // override options setter to add time to maxDate(Time) and minDate(Time). MaxDate
1265 //#######################################################################################
1266 $.datepicker._base_optionDatepicker = $.datepicker._optionDatepicker;
1267 $.datepicker._optionDatepicker = function(target, name, value) {
1268         var inst = this._getInst(target),
1269                 tp_inst = this._get(inst, 'timepicker');
1270         if (tp_inst) {
1271                 var min = null, max = null, onselect = null;
1272                 if (typeof name == 'string') { // if min/max was set with the string
1273                         if (name === 'minDate' || name === 'minDateTime' )
1274                                 min = value;
1275                         else if (name === 'maxDate' || name === 'maxDateTime')
1276                                 max = value;
1277                         else if (name === 'onSelect')
1278                                 onselect = value;
1279                 } else if (typeof name == 'object') { //if min/max was set with the JSON
1280                         if (name.minDate)
1281                                 min = name.minDate;
1282                         else if (name.minDateTime)
1283                                 min = name.minDateTime;
1284                         else if (name.maxDate)
1285                                 max = name.maxDate;
1286                         else if (name.maxDateTime)
1287                                 max = name.maxDateTime;
1288                 }
1289                 if(min) { //if min was set
1290                         if (min == 0)
1291                                 min = new Date();
1292                         else
1293                                 min = new Date(min);
1294
1295                         tp_inst._defaults.minDate = min;
1296                         tp_inst._defaults.minDateTime = min;
1297                 } else if (max) { //if max was set
1298                         if(max==0)
1299                                 max=new Date();
1300                         else
1301                                 max= new Date(max);
1302                         tp_inst._defaults.maxDate = max;
1303                         tp_inst._defaults.maxDateTime = max;
1304                 } else if (onselect)
1305                         tp_inst._defaults.onSelect = onselect;
1306         }
1307         if (value === undefined)
1308                 return this._base_optionDatepicker(target, name);
1309         return this._base_optionDatepicker(target, name, value);
1310 };
1311
1312 //#######################################################################################
1313 // jQuery extend now ignores nulls!
1314 //#######################################################################################
1315 function extendRemove(target, props) {
1316         $.extend(target, props);
1317         for (var name in props)
1318                 if (props[name] === null || props[name] === undefined)
1319                         target[name] = props[name];
1320         return target;
1321 };
1322
1323 $.timepicker = new Timepicker(); // singleton instance
1324 $.timepicker.version = "1.0.0";
1325
1326 })(jQuery);