hjg
2023-11-18 bb48edb3d9faaaeab0088151c86fc24137acdb08
提交 | 用户 | 时间
58d006 1 /*!
A 2  * Datepicker for Bootstrap v1.5.0 (https://github.com/eternicode/bootstrap-datepicker)
3  *
4  * Copyright 2012 Stefan Petre
5  * Improvements by Andrew Rowls
6  * Licensed under the Apache License v2.0 (http://www.apache.org/licenses/LICENSE-2.0)
7  */(function(factory){
8     if (typeof define === "function" && define.amd) {
9         define(["jquery"], factory);
10     } else if (typeof exports === 'object') {
11         factory(require('jquery'));
12     } else {
13         factory(jQuery);
14     }
15 }(function($, undefined){
16
17     function UTCDate(){
18         return new Date(Date.UTC.apply(Date, arguments));
19     }
20     function UTCToday(){
21         var today = new Date();
22         return UTCDate(today.getFullYear(), today.getMonth(), today.getDate());
23     }
24     function isUTCEquals(date1, date2) {
25         return (
26             date1.getUTCFullYear() === date2.getUTCFullYear() &&
27             date1.getUTCMonth() === date2.getUTCMonth() &&
28             date1.getUTCDate() === date2.getUTCDate()
29         );
30     }
31     function alias(method){
32         return function(){
33             return this[method].apply(this, arguments);
34         };
35     }
36     function isValidDate(d) {
37         return d && !isNaN(d.getTime());
38     }
39
40     var DateArray = (function(){
41         var extras = {
42             get: function(i){
43                 return this.slice(i)[0];
44             },
45             contains: function(d){
46                 // Array.indexOf is not cross-browser;
47                 // $.inArray doesn't work with Dates
48                 var val = d && d.valueOf();
49                 for (var i=0, l=this.length; i < l; i++)
50                     if (this[i].valueOf() === val)
51                         return i;
52                 return -1;
53             },
54             remove: function(i){
55                 this.splice(i,1);
56             },
57             replace: function(new_array){
58                 if (!new_array)
59                     return;
60                 if (!$.isArray(new_array))
61                     new_array = [new_array];
62                 this.clear();
63                 this.push.apply(this, new_array);
64             },
65             clear: function(){
66                 this.length = 0;
67             },
68             copy: function(){
69                 var a = new DateArray();
70                 a.replace(this);
71                 return a;
72             }
73         };
74
75         return function(){
76             var a = [];
77             a.push.apply(a, arguments);
78             $.extend(a, extras);
79             return a;
80         };
81     })();
82
83
84     // Picker object
85
86     var Datepicker = function(element, options){
87         this._process_options(options);
88
89         this.dates = new DateArray();
90         this.viewDate = this.o.defaultViewDate;
91         this.focusDate = null;
92
93         this.element = $(element);
94         this.isInline = false;
95         this.isInput = this.element.is('input');
96         this.component = this.element.hasClass('date') ? this.element.find('.add-on, .input-group-addon, .btn') : false;
97         this.hasInput = this.component && this.element.find('input').length;
98         if (this.component && this.component.length === 0)
99             this.component = false;
100
101         this.picker = $(DPGlobal.template);
102         this._buildEvents();
103         this._attachEvents();
104
105         if (this.isInline){
106             this.picker.addClass('datepicker-inline').appendTo(this.element);
107         }
108         else {
109             this.picker.addClass('datepicker-dropdown dropdown-menu');
110         }
111
112         if (this.o.rtl){
113             this.picker.addClass('datepicker-rtl');
114         }
115
116         this.viewMode = this.o.startView;
117
118         if (this.o.calendarWeeks)
119             this.picker.find('tfoot .today, tfoot .clear')
120                         .attr('colspan', function(i, val){
121                             return parseInt(val) + 1;
122                         });
123
124         this._allow_update = false;
125
126         this.setStartDate(this._o.startDate);
127         this.setEndDate(this._o.endDate);
128         this.setDaysOfWeekDisabled(this.o.daysOfWeekDisabled);
129         this.setDaysOfWeekHighlighted(this.o.daysOfWeekHighlighted);
130         this.setDatesDisabled(this.o.datesDisabled);
131
132         this.fillDow();
133         this.fillMonths();
134
135         this._allow_update = true;
136
137         this.update();
138         this.showMode();
139
140         if (this.isInline){
141             this.show();
142         }
143     };
144
145     Datepicker.prototype = {
146         constructor: Datepicker,
147
148         _process_options: function(opts){
149             // Store raw options for reference
150             this._o = $.extend({}, this._o, opts);
151             // Processed options
152             var o = this.o = $.extend({}, this._o);
153
154             // Check if "de-DE" style date is available, if not language should
155             // fallback to 2 letter code eg "de"
156             var lang = o.language;
157             if (!dates[lang]){
158                 lang = lang.split('-')[0];
159                 if (!dates[lang])
160                     lang = defaults.language;
161             }
162             o.language = lang;
163
164             switch (o.startView){
165                 case 2:
166                 case 'decade':
167                     o.startView = 2;
168                     break;
169                 case 1:
170                 case 'year':
171                     o.startView = 1;
172                     break;
173                 default:
174                     o.startView = 0;
175             }
176
177             switch (o.minViewMode){
178                 case 1:
179                 case 'months':
180                     o.minViewMode = 1;
181                     break;
182                 case 2:
183                 case 'years':
184                     o.minViewMode = 2;
185                     break;
186                 default:
187                     o.minViewMode = 0;
188             }
189
190             switch (o.maxViewMode) {
191                 case 0:
192                 case 'days':
193                     o.maxViewMode = 0;
194                     break;
195                 case 1:
196                 case 'months':
197                     o.maxViewMode = 1;
198                     break;
199                 default:
200                     o.maxViewMode = 2;
201             }
202
203             o.startView = Math.min(o.startView, o.maxViewMode);
204             o.startView = Math.max(o.startView, o.minViewMode);
205
206             // true, false, or Number > 0
207             if (o.multidate !== true){
208                 o.multidate = Number(o.multidate) || false;
209                 if (o.multidate !== false)
210                     o.multidate = Math.max(0, o.multidate);
211             }
212             o.multidateSeparator = String(o.multidateSeparator);
213
214             o.weekStart %= 7;
215             o.weekEnd = ((o.weekStart + 6) % 7);
216
217             var format = DPGlobal.parseFormat(o.format);
218             if (o.startDate !== -Infinity){
219                 if (!!o.startDate){
220                     if (o.startDate instanceof Date)
221                         o.startDate = this._local_to_utc(this._zero_time(o.startDate));
222                     else
223                         o.startDate = DPGlobal.parseDate(o.startDate, format, o.language);
224                 }
225                 else {
226                     o.startDate = -Infinity;
227                 }
228             }
229             if (o.endDate !== Infinity){
230                 if (!!o.endDate){
231                     if (o.endDate instanceof Date)
232                         o.endDate = this._local_to_utc(this._zero_time(o.endDate));
233                     else
234                         o.endDate = DPGlobal.parseDate(o.endDate, format, o.language);
235                 }
236                 else {
237                     o.endDate = Infinity;
238                 }
239             }
240
241             o.daysOfWeekDisabled = o.daysOfWeekDisabled||[];
242             if (!$.isArray(o.daysOfWeekDisabled))
243                 o.daysOfWeekDisabled = o.daysOfWeekDisabled.split(/[,\s]*/);
244             o.daysOfWeekDisabled = $.map(o.daysOfWeekDisabled, function(d){
245                 return parseInt(d, 10);
246             });
247
248             o.daysOfWeekHighlighted = o.daysOfWeekHighlighted||[];
249             if (!$.isArray(o.daysOfWeekHighlighted))
250                 o.daysOfWeekHighlighted = o.daysOfWeekHighlighted.split(/[,\s]*/);
251             o.daysOfWeekHighlighted = $.map(o.daysOfWeekHighlighted, function(d){
252                 return parseInt(d, 10);
253             });
254
255             o.datesDisabled = o.datesDisabled||[];
256             if (!$.isArray(o.datesDisabled)) {
257                 var datesDisabled = [];
258                 datesDisabled.push(DPGlobal.parseDate(o.datesDisabled, format, o.language));
259                 o.datesDisabled = datesDisabled;
260             }
261             o.datesDisabled = $.map(o.datesDisabled,function(d){
262                 return DPGlobal.parseDate(d, format, o.language);
263             });
264
265             var plc = String(o.orientation).toLowerCase().split(/\s+/g),
266                 _plc = o.orientation.toLowerCase();
267             plc = $.grep(plc, function(word){
268                 return /^auto|left|right|top|bottom$/.test(word);
269             });
270             o.orientation = {x: 'auto', y: 'auto'};
271             if (!_plc || _plc === 'auto')
272                 ; // no action
273             else if (plc.length === 1){
274                 switch (plc[0]){
275                     case 'top':
276                     case 'bottom':
277                         o.orientation.y = plc[0];
278                         break;
279                     case 'left':
280                     case 'right':
281                         o.orientation.x = plc[0];
282                         break;
283                 }
284             }
285             else {
286                 _plc = $.grep(plc, function(word){
287                     return /^left|right$/.test(word);
288                 });
289                 o.orientation.x = _plc[0] || 'auto';
290
291                 _plc = $.grep(plc, function(word){
292                     return /^top|bottom$/.test(word);
293                 });
294                 o.orientation.y = _plc[0] || 'auto';
295             }
296             if (o.defaultViewDate) {
297                 var year = o.defaultViewDate.year || new Date().getFullYear();
298                 var month = o.defaultViewDate.month || 0;
299                 var day = o.defaultViewDate.day || 1;
300                 o.defaultViewDate = UTCDate(year, month, day);
301             } else {
302                 o.defaultViewDate = UTCToday();
303             }
304             o.showOnFocus = o.showOnFocus !== undefined ? o.showOnFocus : true;
305             o.zIndexOffset = o.zIndexOffset !== undefined ? o.zIndexOffset : 10;
306         },
307         _events: [],
308         _secondaryEvents: [],
309         _applyEvents: function(evs){
310             for (var i=0, el, ch, ev; i < evs.length; i++){
311                 el = evs[i][0];
312                 if (evs[i].length === 2){
313                     ch = undefined;
314                     ev = evs[i][1];
315                 }
316                 else if (evs[i].length === 3){
317                     ch = evs[i][1];
318                     ev = evs[i][2];
319                 }
320                 el.on(ev, ch);
321             }
322         },
323         _unapplyEvents: function(evs){
324             for (var i=0, el, ev, ch; i < evs.length; i++){
325                 el = evs[i][0];
326                 if (evs[i].length === 2){
327                     ch = undefined;
328                     ev = evs[i][1];
329                 }
330                 else if (evs[i].length === 3){
331                     ch = evs[i][1];
332                     ev = evs[i][2];
333                 }
334                 el.off(ev, ch);
335             }
336         },
337         _buildEvents: function(){
338             var events = {
339                 keyup: $.proxy(function(e){
340                     if ($.inArray(e.keyCode, [27, 37, 39, 38, 40, 32, 13, 9]) === -1)
341                         this.update();
342                 }, this),
343                 keydown: $.proxy(this.keydown, this),
344                 paste: $.proxy(this.paste, this)
345             };
346
347             if (this.o.showOnFocus === true) {
348                 events.focus = $.proxy(this.show, this);
349             }
350
351             if (this.isInput) { // single input
352                 this._events = [
353                     [this.element, events]
354                 ];
355             }
356             else if (this.component && this.hasInput) { // component: input + button
357                 this._events = [
358                     // For components that are not readonly, allow keyboard nav
359                     [this.element.find('input'), events],
360                     [this.component, {
361                         click: $.proxy(this.show, this)
362                     }]
363                 ];
364             }
365             else if (this.element.is('div')){  // inline datepicker
366                 this.isInline = true;
367             }
368             else {
369                 this._events = [
370                     [this.element, {
371                         click: $.proxy(this.show, this)
372                     }]
373                 ];
374             }
375             this._events.push(
376                 // Component: listen for blur on element descendants
377                 [this.element, '*', {
378                     blur: $.proxy(function(e){
379                         this._focused_from = e.target;
380                     }, this)
381                 }],
382                 // Input: listen for blur on element
383                 [this.element, {
384                     blur: $.proxy(function(e){
385                         this._focused_from = e.target;
386                     }, this)
387                 }]
388             );
389
390             if (this.o.immediateUpdates) {
391                 // Trigger input updates immediately on changed year/month
392                 this._events.push([this.element, {
393                     'changeYear changeMonth': $.proxy(function(e){
394                         this.update(e.date);
395                     }, this)
396                 }]);
397             }
398
399             this._secondaryEvents = [
400                 [this.picker, {
401                     click: $.proxy(this.click, this)
402                 }],
403                 [$(window), {
404                     resize: $.proxy(this.place, this)
405                 }],
406                 [$(document), {
407                     mousedown: $.proxy(function(e){
408                         // Clicked outside the datepicker, hide it
409                         if (!(
410                             this.element.is(e.target) ||
411                             this.element.find(e.target).length ||
412                             this.picker.is(e.target) ||
413                             this.picker.find(e.target).length ||
414                             this.picker.hasClass('datepicker-inline')
415                         )){
416                             this.hide();
417                         }
418                     }, this)
419                 }]
420             ];
421         },
422         _attachEvents: function(){
423             this._detachEvents();
424             this._applyEvents(this._events);
425         },
426         _detachEvents: function(){
427             this._unapplyEvents(this._events);
428         },
429         _attachSecondaryEvents: function(){
430             this._detachSecondaryEvents();
431             this._applyEvents(this._secondaryEvents);
432         },
433         _detachSecondaryEvents: function(){
434             this._unapplyEvents(this._secondaryEvents);
435         },
436         _trigger: function(event, altdate){
437             var date = altdate || this.dates.get(-1),
438                 local_date = this._utc_to_local(date);
439
440             this.element.trigger({
441                 type: event,
442                 date: local_date,
443                 dates: $.map(this.dates, this._utc_to_local),
444                 format: $.proxy(function(ix, format){
445                     if (arguments.length === 0){
446                         ix = this.dates.length - 1;
447                         format = this.o.format;
448                     }
449                     else if (typeof ix === 'string'){
450                         format = ix;
451                         ix = this.dates.length - 1;
452                     }
453                     format = format || this.o.format;
454                     var date = this.dates.get(ix);
455                     return DPGlobal.formatDate(date, format, this.o.language);
456                 }, this)
457             });
458         },
459
460         show: function(){
461             if (this.element.attr('readonly') && this.o.enableOnReadonly === false)
462                 return;
463             if (!this.isInline)
464                 this.picker.appendTo(this.o.container);
465             this.place();
466             this.picker.show();
467             this._attachSecondaryEvents();
468             this._trigger('show');
469             if ((window.navigator.msMaxTouchPoints || 'ontouchstart' in document) && this.o.disableTouchKeyboard) {
470                 $(this.element).blur();
471             }
472             return this;
473         },
474
475         hide: function(){
476             if (this.isInline)
477                 return this;
478             if (!this.picker.is(':visible'))
479                 return this;
480             this.focusDate = null;
481             this.picker.hide().detach();
482             this._detachSecondaryEvents();
483             this.viewMode = this.o.startView;
484             this.showMode();
485
486             if (
487                 this.o.forceParse &&
488                 (
489                     this.isInput && this.element.val() ||
490                     this.hasInput && this.element.find('input').val()
491                 )
492             )
493                 this.setValue();
494             this._trigger('hide');
495             return this;
496         },
497
498         remove: function(){
499             this.hide();
500             this._detachEvents();
501             this._detachSecondaryEvents();
502             this.picker.remove();
503             delete this.element.data().datepicker;
504             if (!this.isInput){
505                 delete this.element.data().date;
506             }
507             return this;
508         },
509
510         paste: function(evt){
511             var dateString;
512             if (evt.originalEvent.clipboardData && evt.originalEvent.clipboardData.types
513                 && $.inArray('text/plain', evt.originalEvent.clipboardData.types) !== -1) {
514                 dateString = evt.originalEvent.clipboardData.getData('text/plain');
515             }
516             else if (window.clipboardData) {
517                 dateString = window.clipboardData.getData('Text');
518             }
519             else {
520                 return;
521             }
522             this.setDate(dateString);
523             this.update();
524             evt.preventDefault();
525         },
526
527         _utc_to_local: function(utc){
528             return utc && new Date(utc.getTime() + (utc.getTimezoneOffset()*60000));
529         },
530         _local_to_utc: function(local){
531             return local && new Date(local.getTime() - (local.getTimezoneOffset()*60000));
532         },
533         _zero_time: function(local){
534             return local && new Date(local.getFullYear(), local.getMonth(), local.getDate());
535         },
536         _zero_utc_time: function(utc){
537             return utc && new Date(Date.UTC(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate()));
538         },
539
540         getDates: function(){
541             return $.map(this.dates, this._utc_to_local);
542         },
543
544         getUTCDates: function(){
545             return $.map(this.dates, function(d){
546                 return new Date(d);
547             });
548         },
549
550         getDate: function(){
551             return this._utc_to_local(this.getUTCDate());
552         },
553
554         getUTCDate: function(){
555             var selected_date = this.dates.get(-1);
556             if (typeof selected_date !== 'undefined') {
557                 return new Date(selected_date);
558             } else {
559                 return null;
560             }
561         },
562
563         clearDates: function(){
564             var element;
565             if (this.isInput) {
566                 element = this.element;
567             } else if (this.component) {
568                 element = this.element.find('input');
569             }
570
571             if (element) {
572                 element.val('');
573             }
574
575             this.update();
576             this._trigger('changeDate');
577
578             if (this.o.autoclose) {
579                 this.hide();
580             }
581         },
582         setDates: function(){
583             var args = $.isArray(arguments[0]) ? arguments[0] : arguments;
584             this.update.apply(this, args);
585             this._trigger('changeDate');
586             this.setValue();
587             return this;
588         },
589
590         setUTCDates: function(){
591             var args = $.isArray(arguments[0]) ? arguments[0] : arguments;
592             this.update.apply(this, $.map(args, this._utc_to_local));
593             this._trigger('changeDate');
594             this.setValue();
595             return this;
596         },
597
598         setDate: alias('setDates'),
599         setUTCDate: alias('setUTCDates'),
600
601         setValue: function(){
602             var formatted = this.getFormattedDate();
603             if (!this.isInput){
604                 if (this.component){
605                     this.element.find('input').val(formatted);
606                 }
607             }
608             else {
609                 this.element.val(formatted);
610             }
611             return this;
612         },
613
614         getFormattedDate: function(format){
615             if (format === undefined)
616                 format = this.o.format;
617
618             var lang = this.o.language;
619             return $.map(this.dates, function(d){
620                 return DPGlobal.formatDate(d, format, lang);
621             }).join(this.o.multidateSeparator);
622         },
623
624         setStartDate: function(startDate){
625             this._process_options({startDate: startDate});
626             this.update();
627             this.updateNavArrows();
628             return this;
629         },
630
631         setEndDate: function(endDate){
632             this._process_options({endDate: endDate});
633             this.update();
634             this.updateNavArrows();
635             return this;
636         },
637
638         setDaysOfWeekDisabled: function(daysOfWeekDisabled){
639             this._process_options({daysOfWeekDisabled: daysOfWeekDisabled});
640             this.update();
641             this.updateNavArrows();
642             return this;
643         },
644
645         setDaysOfWeekHighlighted: function(daysOfWeekHighlighted){
646             this._process_options({daysOfWeekHighlighted: daysOfWeekHighlighted});
647             this.update();
648             return this;
649         },
650
651         setDatesDisabled: function(datesDisabled){
652             this._process_options({datesDisabled: datesDisabled});
653             this.update();
654             this.updateNavArrows();
655         },
656
657         place: function(){
658             if (this.isInline)
659                 return this;
660             var calendarWidth = this.picker.outerWidth(),
661                 calendarHeight = this.picker.outerHeight(),
662                 visualPadding = 10,
663                 container = $(this.o.container),
664                 windowWidth = container.width(),
665                 scrollTop = container.scrollTop(),
666                 appendOffset = container.offset();
667
668             var parentsZindex = [];
669             this.element.parents().each(function(){
670                 var itemZIndex = $(this).css('z-index');
671                 if (itemZIndex !== 'auto' && itemZIndex !== 0) parentsZindex.push(parseInt(itemZIndex));
672             });
673             var zIndex = Math.max.apply(Math, parentsZindex) + this.o.zIndexOffset;
674             var offset = this.component ? this.component.parent().offset() : this.element.offset();
675             var height = this.component ? this.component.outerHeight(true) : this.element.outerHeight(false);
676             var width = this.component ? this.component.outerWidth(true) : this.element.outerWidth(false);
677             var left = offset.left - appendOffset.left,
678                 top = offset.top - appendOffset.top;
679
680             this.picker.removeClass(
681                 'datepicker-orient-top datepicker-orient-bottom '+
682                 'datepicker-orient-right datepicker-orient-left'
683             );
684
685             if (this.o.orientation.x !== 'auto'){
686                 this.picker.addClass('datepicker-orient-' + this.o.orientation.x);
687                 if (this.o.orientation.x === 'right')
688                     left -= calendarWidth - width;
689             }
690             // auto x orientation is best-placement: if it crosses a window
691             // edge, fudge it sideways
692             else {
693                 if (offset.left < 0) {
694                     // component is outside the window on the left side. Move it into visible range
695                     this.picker.addClass('datepicker-orient-left');
696                     left -= offset.left - visualPadding;
697                 } else if (left + calendarWidth > windowWidth) {
698                     // the calendar passes the widow right edge. Align it to component right side
699                     this.picker.addClass('datepicker-orient-right');
700                     left = offset.left + width - calendarWidth;
701                 } else {
702                     // Default to left
703                     this.picker.addClass('datepicker-orient-left');
704                 }
705             }
706
707             // auto y orientation is best-situation: top or bottom, no fudging,
708             // decision based on which shows more of the calendar
709             var yorient = this.o.orientation.y,
710                 top_overflow;
711             if (yorient === 'auto'){
712                 top_overflow = -scrollTop + top - calendarHeight;
713                 yorient = top_overflow < 0 ? 'bottom' : 'top';
714             }
715
716             this.picker.addClass('datepicker-orient-' + yorient);
717             if (yorient === 'top')
718                 top -= calendarHeight + parseInt(this.picker.css('padding-top'));
719             else
720                 top += height;
721
722             if (this.o.rtl) {
723                 var right = windowWidth - (left + width);
724                 this.picker.css({
725                     top: top,
726                     right: right,
727                     zIndex: zIndex
728                 });
729             } else {
730                 this.picker.css({
731                     top: top,
732                     left: left,
733                     zIndex: zIndex
734                 });
735             }
736             return this;
737         },
738
739         _allow_update: true,
740         update: function(){
741             if (!this._allow_update)
742                 return this;
743
744             var oldDates = this.dates.copy(),
745                 dates = [],
746                 fromArgs = false;
747             if (arguments.length){
748                 $.each(arguments, $.proxy(function(i, date){
749                     if (date instanceof Date)
750                         date = this._local_to_utc(date);
751                     dates.push(date);
752                 }, this));
753                 fromArgs = true;
754             }
755             else {
756                 dates = this.isInput
757                         ? this.element.val()
758                         : this.element.data('date') || this.element.find('input').val();
759                 if (dates && this.o.multidate)
760                     dates = dates.split(this.o.multidateSeparator);
761                 else
762                     dates = [dates];
763                 delete this.element.data().date;
764             }
765
766             dates = $.map(dates, $.proxy(function(date){
767                 return DPGlobal.parseDate(date, this.o.format, this.o.language);
768             }, this));
769             dates = $.grep(dates, $.proxy(function(date){
770                 return (
771                     date < this.o.startDate ||
772                     date > this.o.endDate ||
773                     !date
774                 );
775             }, this), true);
776             this.dates.replace(dates);
777
778             if (this.dates.length)
779                 this.viewDate = new Date(this.dates.get(-1));
780             else if (this.viewDate < this.o.startDate)
781                 this.viewDate = new Date(this.o.startDate);
782             else if (this.viewDate > this.o.endDate)
783                 this.viewDate = new Date(this.o.endDate);
784             else
785                 this.viewDate = this.o.defaultViewDate;
786
787             if (fromArgs){
788                 // setting date by clicking
789                 this.setValue();
790             }
791             else if (dates.length){
792                 // setting date by typing
793                 if (String(oldDates) !== String(this.dates))
794                     this._trigger('changeDate');
795             }
796             if (!this.dates.length && oldDates.length)
797                 this._trigger('clearDate');
798
799             this.fill();
800             this.element.change();
801             return this;
802         },
803
804         fillDow: function(){
805             var dowCnt = this.o.weekStart,
806                 html = '<tr>';
807             if (this.o.calendarWeeks){
808                 this.picker.find('.datepicker-days .datepicker-switch')
809                     .attr('colspan', function(i, val){
810                         return parseInt(val) + 1;
811                     });
812                 html += '<th class="cw">&#160;</th>';
813             }
814             while (dowCnt < this.o.weekStart + 7){
815                 html += '<th class="dow">'+dates[this.o.language].daysMin[(dowCnt++)%7]+'</th>';
816             }
817             html += '</tr>';
818             this.picker.find('.datepicker-days thead').append(html);
819         },
820
821         fillMonths: function(){
822             var html = '',
823             i = 0;
824             while (i < 12){
825                 html += '<span class="month">'+dates[this.o.language].monthsShort[i++]+'</span>';
826             }
827             this.picker.find('.datepicker-months td').html(html);
828         },
829
830         setRange: function(range){
831             if (!range || !range.length)
832                 delete this.range;
833             else
834                 this.range = $.map(range, function(d){
835                     return d.valueOf();
836                 });
837             this.fill();
838         },
839
840         getClassNames: function(date){
841             var cls = [],
842                 year = this.viewDate.getUTCFullYear(),
843                 month = this.viewDate.getUTCMonth(),
844                 today = new Date();
845             if (date.getUTCFullYear() < year || (date.getUTCFullYear() === year && date.getUTCMonth() < month)){
846                 cls.push('old');
847             }
848             else if (date.getUTCFullYear() > year || (date.getUTCFullYear() === year && date.getUTCMonth() > month)){
849                 cls.push('new');
850             }
851             if (this.focusDate && date.valueOf() === this.focusDate.valueOf())
852                 cls.push('focused');
853             // Compare internal UTC date with local today, not UTC today
854             if (this.o.todayHighlight &&
855                 date.getUTCFullYear() === today.getFullYear() &&
856                 date.getUTCMonth() === today.getMonth() &&
857                 date.getUTCDate() === today.getDate()){
858                 cls.push('today');
859             }
860             if (this.dates.contains(date) !== -1)
861                 cls.push('active');
862             if (date.valueOf() < this.o.startDate || date.valueOf() > this.o.endDate ||
863                 $.inArray(date.getUTCDay(), this.o.daysOfWeekDisabled) !== -1){
864                 cls.push('disabled');
865             }
866             if ($.inArray(date.getUTCDay(), this.o.daysOfWeekHighlighted) !== -1){
867                 cls.push('highlighted');
868             }
869             if (this.o.datesDisabled.length > 0 &&
870                 $.grep(this.o.datesDisabled, function(d){
871                     return isUTCEquals(date, d); }).length > 0) {
872                 cls.push('disabled', 'disabled-date');
873             }
874
875             if (this.range){
876                 if (date > this.range[0] && date < this.range[this.range.length-1]){
877                     cls.push('range');
878                 }
879                 if ($.inArray(date.valueOf(), this.range) !== -1){
880                     cls.push('selected');
881                 }
882                 if (date.valueOf() === this.range[0]){
883           cls.push('range-start');
884         }
885         if (date.valueOf() === this.range[this.range.length-1]){
886           cls.push('range-end');
887         }
888             }
889             return cls;
890         },
891
892         fill: function(){
893             var d = new Date(this.viewDate),
894                 year = d.getUTCFullYear(),
895                 month = d.getUTCMonth(),
896                 startYear = this.o.startDate !== -Infinity ? this.o.startDate.getUTCFullYear() : -Infinity,
897                 startMonth = this.o.startDate !== -Infinity ? this.o.startDate.getUTCMonth() : -Infinity,
898                 endYear = this.o.endDate !== Infinity ? this.o.endDate.getUTCFullYear() : Infinity,
899                 endMonth = this.o.endDate !== Infinity ? this.o.endDate.getUTCMonth() : Infinity,
900                 todaytxt = dates[this.o.language].today || dates['en'].today || '',
901                 cleartxt = dates[this.o.language].clear || dates['en'].clear || '',
902                 titleFormat = dates[this.o.language].titleFormat || dates['en'].titleFormat,
903                 tooltip;
904             if (isNaN(year) || isNaN(month))
905                 return;
906             this.picker.find('.datepicker-days thead .datepicker-switch')
907                         .text(DPGlobal.formatDate(new UTCDate(year, month), titleFormat, this.o.language));
908             this.picker.find('tfoot .today')
909                         .text(todaytxt)
910                         .toggle(this.o.todayBtn !== false);
911             this.picker.find('tfoot .clear')
912                         .text(cleartxt)
913                         .toggle(this.o.clearBtn !== false);
914             this.picker.find('thead .datepicker-title')
915                         .text(this.o.title)
916                         .toggle(this.o.title !== '');
917             this.updateNavArrows();
918             this.fillMonths();
919             var prevMonth = UTCDate(year, month-1, 28),
920                 day = DPGlobal.getDaysInMonth(prevMonth.getUTCFullYear(), prevMonth.getUTCMonth());
921             prevMonth.setUTCDate(day);
922             prevMonth.setUTCDate(day - (prevMonth.getUTCDay() - this.o.weekStart + 7)%7);
923             var nextMonth = new Date(prevMonth);
924             if (prevMonth.getUTCFullYear() < 100){
925         nextMonth.setUTCFullYear(prevMonth.getUTCFullYear());
926       }
927             nextMonth.setUTCDate(nextMonth.getUTCDate() + 42);
928             nextMonth = nextMonth.valueOf();
929             var html = [];
930             var clsName;
931             while (prevMonth.valueOf() < nextMonth){
932                 if (prevMonth.getUTCDay() === this.o.weekStart){
933                     html.push('<tr>');
934                     if (this.o.calendarWeeks){
935                         // ISO 8601: First week contains first thursday.
936                         // ISO also states week starts on Monday, but we can be more abstract here.
937                         var
938                             // Start of current week: based on weekstart/current date
939                             ws = new Date(+prevMonth + (this.o.weekStart - prevMonth.getUTCDay() - 7) % 7 * 864e5),
940                             // Thursday of this week
941                             th = new Date(Number(ws) + (7 + 4 - ws.getUTCDay()) % 7 * 864e5),
942                             // First Thursday of year, year from thursday
943                             yth = new Date(Number(yth = UTCDate(th.getUTCFullYear(), 0, 1)) + (7 + 4 - yth.getUTCDay())%7*864e5),
944                             // Calendar week: ms between thursdays, div ms per day, div 7 days
945                             calWeek =  (th - yth) / 864e5 / 7 + 1;
946                         html.push('<td class="cw">'+ calWeek +'</td>');
947
948                     }
949                 }
950                 clsName = this.getClassNames(prevMonth);
951                 clsName.push('day');
952
953                 if (this.o.beforeShowDay !== $.noop){
954                     var before = this.o.beforeShowDay(this._utc_to_local(prevMonth));
955                     if (before === undefined)
956                         before = {};
957                     else if (typeof(before) === 'boolean')
958                         before = {enabled: before};
959                     else if (typeof(before) === 'string')
960                         before = {classes: before};
961                     if (before.enabled === false)
962                         clsName.push('disabled');
963                     if (before.classes)
964                         clsName = clsName.concat(before.classes.split(/\s+/));
965                     if (before.tooltip)
966                         tooltip = before.tooltip;
967                 }
968
969                 clsName = $.unique(clsName);
970                 html.push('<td class="'+clsName.join(' ')+'"' + (tooltip ? ' title="'+tooltip+'"' : '') + '>'+prevMonth.getUTCDate() + '</td>');
971                 tooltip = null;
972                 if (prevMonth.getUTCDay() === this.o.weekEnd){
973                     html.push('</tr>');
974                 }
975                 prevMonth.setUTCDate(prevMonth.getUTCDate()+1);
976             }
977             this.picker.find('.datepicker-days tbody').empty().append(html.join(''));
978
979             var months = this.picker.find('.datepicker-months')
980                         .find('.datepicker-switch')
981                             .text(this.o.maxViewMode < 2 ? 'Months' : year)
982                             .end()
983                         .find('span').removeClass('active');
984
985             $.each(this.dates, function(i, d){
986                 if (d.getUTCFullYear() === year)
987                     months.eq(d.getUTCMonth()).addClass('active');
988             });
989
990             if (year < startYear || year > endYear){
991                 months.addClass('disabled');
992             }
993             if (year === startYear){
994                 months.slice(0, startMonth).addClass('disabled');
995             }
996             if (year === endYear){
997                 months.slice(endMonth+1).addClass('disabled');
998             }
999
1000             if (this.o.beforeShowMonth !== $.noop){
1001                 var that = this;
1002                 $.each(months, function(i, month){
1003                     if (!$(month).hasClass('disabled')) {
1004                         var moDate = new Date(year, i, 1);
1005                         var before = that.o.beforeShowMonth(moDate);
1006                         if (before === false)
1007                             $(month).addClass('disabled');
1008                     }
1009                 });
1010             }
1011
1012             html = '';
1013             year = parseInt(year/10, 10) * 10;
1014             var yearCont = this.picker.find('.datepicker-years')
1015                                 .find('.datepicker-switch')
1016                                     .text(year + '-' + (year + 9))
1017                                     .end()
1018                                 .find('td');
1019             year -= 1;
1020             var years = $.map(this.dates, function(d){
1021                     return d.getUTCFullYear();
1022                 }),
1023                 classes;
1024             for (var i = -1; i < 11; i++){
1025                 classes = ['year'];
1026                 tooltip = null;
1027
1028                 if (i === -1)
1029                     classes.push('old');
1030                 else if (i === 10)
1031                     classes.push('new');
1032                 if ($.inArray(year, years) !== -1)
1033                     classes.push('active');
1034                 if (year < startYear || year > endYear)
1035                     classes.push('disabled');
1036
1037                 if (this.o.beforeShowYear !== $.noop) {
1038                     var yrBefore = this.o.beforeShowYear(new Date(year, 0, 1));
1039                     if (yrBefore === undefined)
1040                         yrBefore = {};
1041                     else if (typeof(yrBefore) === 'boolean')
1042                         yrBefore = {enabled: yrBefore};
1043                     else if (typeof(yrBefore) === 'string')
1044                         yrBefore = {classes: yrBefore};
1045                     if (yrBefore.enabled === false)
1046                         classes.push('disabled');
1047                     if (yrBefore.classes)
1048                         classes = classes.concat(yrBefore.classes.split(/\s+/));
1049                     if (yrBefore.tooltip)
1050                         tooltip = yrBefore.tooltip;
1051                 }
1052
1053                 html += '<span class="' + classes.join(' ') + '"' + (tooltip ? ' title="'+tooltip+'"' : '') + '>' + year + '</span>';
1054                 year += 1;
1055             }
1056             yearCont.html(html);
1057         },
1058
1059         updateNavArrows: function(){
1060             if (!this._allow_update)
1061                 return;
1062
1063             var d = new Date(this.viewDate),
1064                 year = d.getUTCFullYear(),
1065                 month = d.getUTCMonth();
1066             switch (this.viewMode){
1067                 case 0:
1068                     if (this.o.startDate !== -Infinity && year <= this.o.startDate.getUTCFullYear() && month <= this.o.startDate.getUTCMonth()){
1069                         this.picker.find('.prev').css({visibility: 'hidden'});
1070                     }
1071                     else {
1072                         this.picker.find('.prev').css({visibility: 'visible'});
1073                     }
1074                     if (this.o.endDate !== Infinity && year >= this.o.endDate.getUTCFullYear() && month >= this.o.endDate.getUTCMonth()){
1075                         this.picker.find('.next').css({visibility: 'hidden'});
1076                     }
1077                     else {
1078                         this.picker.find('.next').css({visibility: 'visible'});
1079                     }
1080                     break;
1081                 case 1:
1082                 case 2:
1083                     if (this.o.startDate !== -Infinity && year <= this.o.startDate.getUTCFullYear() || this.o.maxViewMode < 2){
1084                         this.picker.find('.prev').css({visibility: 'hidden'});
1085                     }
1086                     else {
1087                         this.picker.find('.prev').css({visibility: 'visible'});
1088                     }
1089                     if (this.o.endDate !== Infinity && year >= this.o.endDate.getUTCFullYear() || this.o.maxViewMode < 2){
1090                         this.picker.find('.next').css({visibility: 'hidden'});
1091                     }
1092                     else {
1093                         this.picker.find('.next').css({visibility: 'visible'});
1094                     }
1095                     break;
1096             }
1097         },
1098
1099         click: function(e){
1100             e.preventDefault();
1101             e.stopPropagation();
1102             var target = $(e.target).closest('span, td, th'),
1103                 year, month, day;
1104             if (target.length === 1){
1105                 switch (target[0].nodeName.toLowerCase()){
1106                     case 'th':
1107                         switch (target[0].className){
1108                             case 'datepicker-switch':
1109                                 this.showMode(1);
1110                                 break;
1111                             case 'prev':
1112                             case 'next':
1113                                 var dir = DPGlobal.modes[this.viewMode].navStep * (target[0].className === 'prev' ? -1 : 1);
1114                                 switch (this.viewMode){
1115                                     case 0:
1116                                         this.viewDate = this.moveMonth(this.viewDate, dir);
1117                                         this._trigger('changeMonth', this.viewDate);
1118                                         break;
1119                                     case 1:
1120                                     case 2:
1121                                         this.viewDate = this.moveYear(this.viewDate, dir);
1122                                         if (this.viewMode === 1)
1123                                             this._trigger('changeYear', this.viewDate);
1124                                         break;
1125                                 }
1126                                 this.fill();
1127                                 break;
1128                             case 'today':
1129                                 var date = new Date();
1130                                 date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
1131
1132                                 this.showMode(-2);
1133                                 var which = this.o.todayBtn === 'linked' ? null : 'view';
1134                                 this._setDate(date, which);
1135                                 break;
1136                             case 'clear':
1137                                 this.clearDates();
1138                                 break;
1139                         }
1140                         break;
1141                     case 'span':
1142                         if (!target.hasClass('disabled')){
1143                             this.viewDate.setUTCDate(1);
1144                             if (target.hasClass('month')){
1145                                 day = 1;
1146                                 month = target.parent().find('span').index(target);
1147                                 year = this.viewDate.getUTCFullYear();
1148                                 this.viewDate.setUTCMonth(month);
1149                                 this._trigger('changeMonth', this.viewDate);
1150                                 if (this.o.minViewMode === 1){
1151                                     this._setDate(UTCDate(year, month, day));
1152                                     this.showMode();
1153                                 } else {
1154                                     this.showMode(-1);
1155                                 }
1156                             }
1157                             else {
1158                                 day = 1;
1159                                 month = 0;
1160                                 year = parseInt(target.text(), 10)||0;
1161                                 this.viewDate.setUTCFullYear(year);
1162                                 this._trigger('changeYear', this.viewDate);
1163                                 if (this.o.minViewMode === 2){
1164                                     this._setDate(UTCDate(year, month, day));
1165                                 }
1166                                 this.showMode(-1);
1167                             }
1168                             this.fill();
1169                         }
1170                         break;
1171                     case 'td':
1172                         if (target.hasClass('day') && !target.hasClass('disabled')){
1173                             day = parseInt(target.text(), 10)||1;
1174                             year = this.viewDate.getUTCFullYear();
1175                             month = this.viewDate.getUTCMonth();
1176                             if (target.hasClass('old')){
1177                                 if (month === 0){
1178                                     month = 11;
1179                                     year -= 1;
1180                                 }
1181                                 else {
1182                                     month -= 1;
1183                                 }
1184                             }
1185                             else if (target.hasClass('new')){
1186                                 if (month === 11){
1187                                     month = 0;
1188                                     year += 1;
1189                                 }
1190                                 else {
1191                                     month += 1;
1192                                 }
1193                             }
1194                             this._setDate(UTCDate(year, month, day));
1195                         }
1196                         break;
1197                 }
1198             }
1199             if (this.picker.is(':visible') && this._focused_from){
1200                 $(this._focused_from).focus();
1201             }
1202             delete this._focused_from;
1203         },
1204
1205         _toggle_multidate: function(date){
1206             var ix = this.dates.contains(date);
1207             if (!date){
1208                 this.dates.clear();
1209             }
1210
1211             if (ix !== -1){
1212                 if (this.o.multidate === true || this.o.multidate > 1 || this.o.toggleActive){
1213                     this.dates.remove(ix);
1214                 }
1215             } else if (this.o.multidate === false) {
1216                 this.dates.clear();
1217                 this.dates.push(date);
1218             }
1219             else {
1220                 this.dates.push(date);
1221             }
1222
1223             if (typeof this.o.multidate === 'number')
1224                 while (this.dates.length > this.o.multidate)
1225                     this.dates.remove(0);
1226         },
1227
1228         _setDate: function(date, which){
1229             if (!which || which === 'date')
1230                 this._toggle_multidate(date && new Date(date));
1231             if (!which || which  === 'view')
1232                 this.viewDate = date && new Date(date);
1233
1234             this.fill();
1235             this.setValue();
1236             if (!which || which  !== 'view') {
1237                 this._trigger('changeDate');
1238             }
1239             var element;
1240             if (this.isInput){
1241                 element = this.element;
1242             }
1243             else if (this.component){
1244                 element = this.element.find('input');
1245             }
1246             if (element){
1247                 element.change();
1248             }
1249             if (this.o.autoclose && (!which || which === 'date')){
1250                 this.hide();
1251             }
1252         },
1253
1254         moveMonth: function(date, dir){
1255             if (!isValidDate(date))
1256                 return this.o.defaultViewDate;
1257             if (!dir)
1258                 return date;
1259             var new_date = new Date(date.valueOf()),
1260                 day = new_date.getUTCDate(),
1261                 month = new_date.getUTCMonth(),
1262                 mag = Math.abs(dir),
1263                 new_month, test;
1264             dir = dir > 0 ? 1 : -1;
1265             if (mag === 1){
1266                 test = dir === -1
1267                     // If going back one month, make sure month is not current month
1268                     // (eg, Mar 31 -> Feb 31 == Feb 28, not Mar 02)
1269                     ? function(){
1270                         return new_date.getUTCMonth() === month;
1271                     }
1272                     // If going forward one month, make sure month is as expected
1273                     // (eg, Jan 31 -> Feb 31 == Feb 28, not Mar 02)
1274                     : function(){
1275                         return new_date.getUTCMonth() !== new_month;
1276                     };
1277                 new_month = month + dir;
1278                 new_date.setUTCMonth(new_month);
1279                 // Dec -> Jan (12) or Jan -> Dec (-1) -- limit expected date to 0-11
1280                 if (new_month < 0 || new_month > 11)
1281                     new_month = (new_month + 12) % 12;
1282             }
1283             else {
1284                 // For magnitudes >1, move one month at a time...
1285                 for (var i=0; i < mag; i++)
1286                     // ...which might decrease the day (eg, Jan 31 to Feb 28, etc)...
1287                     new_date = this.moveMonth(new_date, dir);
1288                 // ...then reset the day, keeping it in the new month
1289                 new_month = new_date.getUTCMonth();
1290                 new_date.setUTCDate(day);
1291                 test = function(){
1292                     return new_month !== new_date.getUTCMonth();
1293                 };
1294             }
1295             // Common date-resetting loop -- if date is beyond end of month, make it
1296             // end of month
1297             while (test()){
1298                 new_date.setUTCDate(--day);
1299                 new_date.setUTCMonth(new_month);
1300             }
1301             return new_date;
1302         },
1303
1304         moveYear: function(date, dir){
1305             return this.moveMonth(date, dir*12);
1306         },
1307
1308         dateWithinRange: function(date){
1309             return date >= this.o.startDate && date <= this.o.endDate;
1310         },
1311
1312         keydown: function(e){
1313             if (!this.picker.is(':visible')){
1314                 if (e.keyCode === 40 || e.keyCode === 27) { // allow down to re-show picker
1315                     this.show();
1316                     e.stopPropagation();
1317         }
1318                 return;
1319             }
1320             var dateChanged = false,
1321                 dir, newDate, newViewDate,
1322                 focusDate = this.focusDate || this.viewDate;
1323             switch (e.keyCode){
1324                 case 27: // escape
1325                     if (this.focusDate){
1326                         this.focusDate = null;
1327                         this.viewDate = this.dates.get(-1) || this.viewDate;
1328                         this.fill();
1329                     }
1330                     else
1331                         this.hide();
1332                     e.preventDefault();
1333                     e.stopPropagation();
1334                     break;
1335                 case 37: // left
1336                 case 39: // right
1337                     if (!this.o.keyboardNavigation)
1338                         break;
1339                     dir = e.keyCode === 37 ? -1 : 1;
1340                     if (e.ctrlKey){
1341                         newDate = this.moveYear(this.dates.get(-1) || UTCToday(), dir);
1342                         newViewDate = this.moveYear(focusDate, dir);
1343                         this._trigger('changeYear', this.viewDate);
1344                     }
1345                     else if (e.shiftKey){
1346                         newDate = this.moveMonth(this.dates.get(-1) || UTCToday(), dir);
1347                         newViewDate = this.moveMonth(focusDate, dir);
1348                         this._trigger('changeMonth', this.viewDate);
1349                     }
1350                     else {
1351                         newDate = new Date(this.dates.get(-1) || UTCToday());
1352                         newDate.setUTCDate(newDate.getUTCDate() + dir);
1353                         newViewDate = new Date(focusDate);
1354                         newViewDate.setUTCDate(focusDate.getUTCDate() + dir);
1355                     }
1356                     if (this.dateWithinRange(newViewDate)){
1357                         this.focusDate = this.viewDate = newViewDate;
1358                         this.setValue();
1359                         this.fill();
1360                         e.preventDefault();
1361                     }
1362                     break;
1363                 case 38: // up
1364                 case 40: // down
1365                     if (!this.o.keyboardNavigation)
1366                         break;
1367                     dir = e.keyCode === 38 ? -1 : 1;
1368                     if (e.ctrlKey){
1369                         newDate = this.moveYear(this.dates.get(-1) || UTCToday(), dir);
1370                         newViewDate = this.moveYear(focusDate, dir);
1371                         this._trigger('changeYear', this.viewDate);
1372                     }
1373                     else if (e.shiftKey){
1374                         newDate = this.moveMonth(this.dates.get(-1) || UTCToday(), dir);
1375                         newViewDate = this.moveMonth(focusDate, dir);
1376                         this._trigger('changeMonth', this.viewDate);
1377                     }
1378                     else {
1379                         newDate = new Date(this.dates.get(-1) || UTCToday());
1380                         newDate.setUTCDate(newDate.getUTCDate() + dir * 7);
1381                         newViewDate = new Date(focusDate);
1382                         newViewDate.setUTCDate(focusDate.getUTCDate() + dir * 7);
1383                     }
1384                     if (this.dateWithinRange(newViewDate)){
1385                         this.focusDate = this.viewDate = newViewDate;
1386                         this.setValue();
1387                         this.fill();
1388                         e.preventDefault();
1389                     }
1390                     break;
1391                 case 32: // spacebar
1392                     // Spacebar is used in manually typing dates in some formats.
1393                     // As such, its behavior should not be hijacked.
1394                     break;
1395                 case 13: // enter
1396                     if (!this.o.forceParse) {
1397                             break;
1398                     }
1399                     focusDate = this.focusDate || this.dates.get(-1) || this.viewDate;
1400                     if (this.o.keyboardNavigation) {
1401                         this._toggle_multidate(focusDate);
1402                         dateChanged = true;
1403                     }
1404                     this.focusDate = null;
1405                     this.viewDate = this.dates.get(-1) || this.viewDate;
1406                     this.setValue();
1407                     this.fill();
1408                     if (this.picker.is(':visible')){
1409                         e.preventDefault();
1410                         if (typeof e.stopPropagation === 'function') {
1411                             e.stopPropagation(); // All modern browsers, IE9+
1412                         } else {
1413                             e.cancelBubble = true; // IE6,7,8 ignore "stopPropagation"
1414                         }
1415                         if (this.o.autoclose)
1416                             this.hide();
1417                     }
1418                     break;
1419                 case 9: // tab
1420                     this.focusDate = null;
1421                     this.viewDate = this.dates.get(-1) || this.viewDate;
1422                     this.fill();
1423                     this.hide();
1424                     break;
1425             }
1426             if (dateChanged){
1427                 if (this.dates.length)
1428                     this._trigger('changeDate');
1429                 else
1430                     this._trigger('clearDate');
1431                 var element;
1432                 if (this.isInput){
1433                     element = this.element;
1434                 }
1435                 else if (this.component){
1436                     element = this.element.find('input');
1437                 }
1438                 if (element){
1439                     element.change();
1440                 }
1441             }
1442         },
1443
1444         showMode: function(dir){
1445             if (dir){
1446                 this.viewMode = Math.max(this.o.minViewMode, Math.min(this.o.maxViewMode, this.viewMode + dir));
1447             }
1448             this.picker
1449                 .children('div')
1450                 .hide()
1451                 .filter('.datepicker-' + DPGlobal.modes[this.viewMode].clsName)
1452                     .show();
1453             this.updateNavArrows();
1454         }
1455     };
1456
1457     var DateRangePicker = function(element, options){
1458         this.element = $(element);
1459         this.inputs = $.map(options.inputs, function(i){
1460             return i.jquery ? i[0] : i;
1461         });
1462         delete options.inputs;
1463
1464         datepickerPlugin.call($(this.inputs), options)
1465             .on('changeDate', $.proxy(this.dateUpdated, this));
1466
1467         this.pickers = $.map(this.inputs, function(i){
1468             return $(i).data('datepicker');
1469         });
1470         this.updateDates();
1471     };
1472     DateRangePicker.prototype = {
1473         updateDates: function(){
1474             this.dates = $.map(this.pickers, function(i){
1475                 return i.getUTCDate();
1476             });
1477             this.updateRanges();
1478         },
1479         updateRanges: function(){
1480             var range = $.map(this.dates, function(d){
1481                 return d.valueOf();
1482             });
1483             $.each(this.pickers, function(i, p){
1484                 p.setRange(range);
1485             });
1486         },
1487         dateUpdated: function(e){
1488             // `this.updating` is a workaround for preventing infinite recursion
1489             // between `changeDate` triggering and `setUTCDate` calling.  Until
1490             // there is a better mechanism.
1491             if (this.updating)
1492                 return;
1493             this.updating = true;
1494
1495             var dp = $(e.target).data('datepicker');
1496
1497             if (typeof(dp) === "undefined") {
1498                 return;
1499             }
1500
1501             var new_date = dp.getUTCDate(),
1502                 i = $.inArray(e.target, this.inputs),
1503                 j = i - 1,
1504                 k = i + 1,
1505                 l = this.inputs.length;
1506             if (i === -1)
1507                 return;
1508
1509             $.each(this.pickers, function(i, p){
1510                 if (!p.getUTCDate())
1511                     p.setUTCDate(new_date);
1512             });
1513
1514             if (new_date < this.dates[j]){
1515                 // Date being moved earlier/left
1516                 while (j >= 0 && new_date < this.dates[j]){
1517                     this.pickers[j--].setUTCDate(new_date);
1518                 }
1519             }
1520             else if (new_date > this.dates[k]){
1521                 // Date being moved later/right
1522                 while (k < l && new_date > this.dates[k]){
1523                     this.pickers[k++].setUTCDate(new_date);
1524                 }
1525             }
1526             this.updateDates();
1527
1528             delete this.updating;
1529         },
1530         remove: function(){
1531             $.map(this.pickers, function(p){ p.remove(); });
1532             delete this.element.data().datepicker;
1533         }
1534     };
1535
1536     function opts_from_el(el, prefix){
1537         // Derive options from element data-attrs
1538         var data = $(el).data(),
1539             out = {}, inkey,
1540             replace = new RegExp('^' + prefix.toLowerCase() + '([A-Z])');
1541         prefix = new RegExp('^' + prefix.toLowerCase());
1542         function re_lower(_,a){
1543             return a.toLowerCase();
1544         }
1545         for (var key in data)
1546             if (prefix.test(key)){
1547                 inkey = key.replace(replace, re_lower);
1548                 out[inkey] = data[key];
1549             }
1550         return out;
1551     }
1552
1553     function opts_from_locale(lang){
1554         // Derive options from locale plugins
1555         var out = {};
1556         // Check if "de-DE" style date is available, if not language should
1557         // fallback to 2 letter code eg "de"
1558         if (!dates[lang]){
1559             lang = lang.split('-')[0];
1560             if (!dates[lang])
1561                 return;
1562         }
1563         var d = dates[lang];
1564         $.each(locale_opts, function(i,k){
1565             if (k in d)
1566                 out[k] = d[k];
1567         });
1568         return out;
1569     }
1570
1571     var old = $.fn.datepicker;
1572     var datepickerPlugin = function(option){
1573         var args = Array.apply(null, arguments);
1574         args.shift();
1575         var internal_return;
1576         this.each(function(){
1577             var $this = $(this),
1578                 data = $this.data('datepicker'),
1579                 options = typeof option === 'object' && option;
1580             if (!data){
1581                 var elopts = opts_from_el(this, 'date'),
1582                     // Preliminary otions
1583                     xopts = $.extend({}, defaults, elopts, options),
1584                     locopts = opts_from_locale(xopts.language),
1585                     // Options priority: js args, data-attrs, locales, defaults
1586                     opts = $.extend({}, defaults, locopts, elopts, options);
1587                 if ($this.hasClass('input-daterange') || opts.inputs){
1588                     var ropts = {
1589                         inputs: opts.inputs || $this.find('input').toArray()
1590                     };
1591                     $this.data('datepicker', (data = new DateRangePicker(this, $.extend(opts, ropts))));
1592                 }
1593                 else {
1594                     $this.data('datepicker', (data = new Datepicker(this, opts)));
1595                 }
1596             }
1597             if (typeof option === 'string' && typeof data[option] === 'function'){
1598                 internal_return = data[option].apply(data, args);
1599             }
1600         });
1601
1602         if (
1603             internal_return === undefined ||
1604             internal_return instanceof Datepicker ||
1605             internal_return instanceof DateRangePicker
1606         )
1607             return this;
1608
1609         if (this.length > 1)
1610             throw new Error('Using only allowed for the collection of a single element (' + option + ' function)');
1611         else
1612             return internal_return;
1613     };
1614     $.fn.datepicker = datepickerPlugin;
1615
1616     var defaults = $.fn.datepicker.defaults = {
1617         autoclose: false,
1618         beforeShowDay: $.noop,
1619         beforeShowMonth: $.noop,
1620         beforeShowYear: $.noop,
1621         calendarWeeks: false,
1622         clearBtn: false,
1623         toggleActive: false,
1624         daysOfWeekDisabled: [],
1625         daysOfWeekHighlighted: [],
1626         datesDisabled: [],
1627         endDate: Infinity,
1628         forceParse: true,
1629         format: 'mm/dd/yyyy',
1630         keyboardNavigation: true,
1631         language: 'en',
1632         minViewMode: 0,
1633         maxViewMode: 2,
1634         multidate: false,
1635         multidateSeparator: ',',
1636         orientation: "auto",
1637         rtl: false,
1638         startDate: -Infinity,
1639         startView: 0,
1640         todayBtn: false,
1641         todayHighlight: false,
1642         weekStart: 0,
1643         disableTouchKeyboard: false,
1644         enableOnReadonly: true,
1645         container: 'body',
1646         immediateUpdates: false,
1647         title: ''
1648     };
1649     var locale_opts = $.fn.datepicker.locale_opts = [
1650         'format',
1651         'rtl',
1652         'weekStart'
1653     ];
1654     $.fn.datepicker.Constructor = Datepicker;
1655     var dates = $.fn.datepicker.dates = {
1656         en: {
1657             days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
1658             daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
1659             daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
1660             months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
1661             monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1662             today: "Today",
1663             clear: "Clear",
1664             titleFormat: "MM yyyy"
1665         }
1666     };
1667
1668     var DPGlobal = {
1669         modes: [
1670             {
1671                 clsName: 'days',
1672                 navFnc: 'Month',
1673                 navStep: 1
1674             },
1675             {
1676                 clsName: 'months',
1677                 navFnc: 'FullYear',
1678                 navStep: 1
1679             },
1680             {
1681                 clsName: 'years',
1682                 navFnc: 'FullYear',
1683                 navStep: 10
1684         }],
1685         isLeapYear: function(year){
1686             return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
1687         },
1688         getDaysInMonth: function(year, month){
1689             return [31, (DPGlobal.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
1690         },
1691         validParts: /dd?|DD?|mm?|MM?|yy(?:yy)?/g,
1692         nonpunctuation: /[^ -\/:-@\[\u3400-\u9fff-`{-~\t\n\r]+/g,
1693         parseFormat: function(format){
1694             if (typeof format.toValue === 'function' && typeof format.toDisplay === 'function')
1695                 return format;
1696             // IE treats \0 as a string end in inputs (truncating the value),
1697             // so it's a bad format delimiter, anyway
1698             var separators = format.replace(this.validParts, '\0').split('\0'),
1699                 parts = format.match(this.validParts);
1700             if (!separators || !separators.length || !parts || parts.length === 0){
1701                 throw new Error("Invalid date format.");
1702             }
1703             return {separators: separators, parts: parts};
1704         },
1705         parseDate: function(date, format, language){
1706             if (!date)
1707                 return undefined;
1708             if (date instanceof Date)
1709                 return date;
1710             if (typeof format === 'string')
1711                 format = DPGlobal.parseFormat(format);
1712             if (format.toValue)
1713                 return format.toValue(date, format, language);
1714             var part_re = /([\-+]\d+)([dmwy])/,
1715                 parts = date.match(/([\-+]\d+)([dmwy])/g),
1716                 part, dir, i;
1717             if (/^[\-+]\d+[dmwy]([\s,]+[\-+]\d+[dmwy])*$/.test(date)){
1718                 date = new Date();
1719                 for (i=0; i < parts.length; i++){
1720                     part = part_re.exec(parts[i]);
1721                     dir = parseInt(part[1]);
1722                     switch (part[2]){
1723                         case 'd':
1724                             date.setUTCDate(date.getUTCDate() + dir);
1725                             break;
1726                         case 'm':
1727                             date = Datepicker.prototype.moveMonth.call(Datepicker.prototype, date, dir);
1728                             break;
1729                         case 'w':
1730                             date.setUTCDate(date.getUTCDate() + dir * 7);
1731                             break;
1732                         case 'y':
1733                             date = Datepicker.prototype.moveYear.call(Datepicker.prototype, date, dir);
1734                             break;
1735                     }
1736                 }
1737                 return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0);
1738             }
1739             parts = date && date.match(this.nonpunctuation) || [];
1740             date = new Date();
1741             var parsed = {},
1742                 setters_order = ['yyyy', 'yy', 'M', 'MM', 'm', 'mm', 'd', 'dd'],
1743                 setters_map = {
1744                     yyyy: function(d,v){
1745                         return d.setUTCFullYear(v);
1746                     },
1747                     yy: function(d,v){
1748                         return d.setUTCFullYear(2000+v);
1749                     },
1750                     m: function(d,v){
1751                         if (isNaN(d))
1752                             return d;
1753                         v -= 1;
1754                         while (v < 0) v += 12;
1755                         v %= 12;
1756                         d.setUTCMonth(v);
1757                         while (d.getUTCMonth() !== v)
1758                             d.setUTCDate(d.getUTCDate()-1);
1759                         return d;
1760                     },
1761                     d: function(d,v){
1762                         return d.setUTCDate(v);
1763                     }
1764                 },
1765                 val, filtered;
1766             setters_map['M'] = setters_map['MM'] = setters_map['mm'] = setters_map['m'];
1767             setters_map['dd'] = setters_map['d'];
1768             date = UTCDate(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
1769             var fparts = format.parts.slice();
1770             // Remove noop parts
1771             if (parts.length !== fparts.length){
1772                 fparts = $(fparts).filter(function(i,p){
1773                     return $.inArray(p, setters_order) !== -1;
1774                 }).toArray();
1775             }
1776             // Process remainder
1777             function match_part(){
1778                 var m = this.slice(0, parts[i].length),
1779                     p = parts[i].slice(0, m.length);
1780                 return m.toLowerCase() === p.toLowerCase();
1781             }
1782             if (parts.length === fparts.length){
1783                 var cnt;
1784                 for (i=0, cnt = fparts.length; i < cnt; i++){
1785                     val = parseInt(parts[i], 10);
1786                     part = fparts[i];
1787                     if (isNaN(val)){
1788                         switch (part){
1789                             case 'MM':
1790                                 filtered = $(dates[language].months).filter(match_part);
1791                                 val = $.inArray(filtered[0], dates[language].months) + 1;
1792                                 break;
1793                             case 'M':
1794                                 filtered = $(dates[language].monthsShort).filter(match_part);
1795                                 val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
1796                                 break;
1797                         }
1798                     }
1799                     parsed[part] = val;
1800                 }
1801                 var _date, s;
1802                 for (i=0; i < setters_order.length; i++){
1803                     s = setters_order[i];
1804                     if (s in parsed && !isNaN(parsed[s])){
1805                         _date = new Date(date);
1806                         setters_map[s](_date, parsed[s]);
1807                         if (!isNaN(_date))
1808                             date = _date;
1809                     }
1810                 }
1811             }
1812             return date;
1813         },
1814         formatDate: function(date, format, language){
1815             if (!date)
1816                 return '';
1817             if (typeof format === 'string')
1818                 format = DPGlobal.parseFormat(format);
1819             if (format.toDisplay)
1820                 return format.toDisplay(date, format, language);
1821             var val = {
1822                 d: date.getUTCDate(),
1823                 D: dates[language].daysShort[date.getUTCDay()],
1824                 DD: dates[language].days[date.getUTCDay()],
1825                 m: date.getUTCMonth() + 1,
1826                 M: dates[language].monthsShort[date.getUTCMonth()],
1827                 MM: dates[language].months[date.getUTCMonth()],
1828                 yy: date.getUTCFullYear().toString().substring(2),
1829                 yyyy: date.getUTCFullYear()
1830             };
1831             val.dd = (val.d < 10 ? '0' : '') + val.d;
1832             val.mm = (val.m < 10 ? '0' : '') + val.m;
1833             date = [];
1834             var seps = $.extend([], format.separators);
1835             for (var i=0, cnt = format.parts.length; i <= cnt; i++){
1836                 if (seps.length)
1837                     date.push(seps.shift());
1838                 date.push(val[format.parts[i]]);
1839             }
1840             return date.join('');
1841         },
1842         headTemplate: '<thead>'+
1843                           '<tr>'+
1844                             '<th colspan="7" class="datepicker-title"></th>'+
1845                           '</tr>'+
1846                             '<tr>'+
1847                                 '<th class="prev">&#171;</th>'+
1848                                 '<th colspan="5" class="datepicker-switch"></th>'+
1849                                 '<th class="next">&#187;</th>'+
1850                             '</tr>'+
1851                         '</thead>',
1852         contTemplate: '<tbody><tr><td colspan="7"></td></tr></tbody>',
1853         footTemplate: '<tfoot>'+
1854                             '<tr>'+
1855                                 '<th colspan="7" class="today"></th>'+
1856                             '</tr>'+
1857                             '<tr>'+
1858                                 '<th colspan="7" class="clear"></th>'+
1859                             '</tr>'+
1860                         '</tfoot>'
1861     };
1862     DPGlobal.template = '<div class="datepicker">'+
1863                             '<div class="datepicker-days">'+
1864                                 '<table class=" table-condensed">'+
1865                                     DPGlobal.headTemplate+
1866                                     '<tbody></tbody>'+
1867                                     DPGlobal.footTemplate+
1868                                 '</table>'+
1869                             '</div>'+
1870                             '<div class="datepicker-months">'+
1871                                 '<table class="table-condensed">'+
1872                                     DPGlobal.headTemplate+
1873                                     DPGlobal.contTemplate+
1874                                     DPGlobal.footTemplate+
1875                                 '</table>'+
1876                             '</div>'+
1877                             '<div class="datepicker-years">'+
1878                                 '<table class="table-condensed">'+
1879                                     DPGlobal.headTemplate+
1880                                     DPGlobal.contTemplate+
1881                                     DPGlobal.footTemplate+
1882                                 '</table>'+
1883                             '</div>'+
1884                         '</div>';
1885
1886     $.fn.datepicker.DPGlobal = DPGlobal;
1887
1888
1889     /* DATEPICKER NO CONFLICT
1890     * =================== */
1891
1892     $.fn.datepicker.noConflict = function(){
1893         $.fn.datepicker = old;
1894         return this;
1895     };
1896
1897     /* DATEPICKER VERSION
1898      * =================== */
1899     $.fn.datepicker.version = '1.5.0';
1900
1901     /* DATEPICKER DATA-API
1902     * ================== */
1903
1904     $(document).on(
1905         'focus.datepicker.data-api click.datepicker.data-api',
1906         '[data-provide="datepicker"]',
1907         function(e){
1908             var $this = $(this);
1909             if ($this.data('datepicker'))
1910                 return;
1911             e.preventDefault();
1912             // component click requires us to explicitly show it
1913             datepickerPlugin.call($this, 'show');
1914         }
1915     );
1916     $(function(){
1917         datepickerPlugin.call($('[data-provide="datepicker-inline"]'));
1918     });
1919
1920 }));