export host selection per service, RT#17914
[freeside.git] / rt / share / html / NoAuth / js / ui.timepickr.js
1 /*
2   jQuery utils - @VERSION
3   http://code.google.com/p/jquery-utils/
4
5   (c) Maxime Haineault <haineault@gmail.com> 
6   http://haineault.com
7
8   MIT License (http://www.opensource.org/licenses/mit-license.php
9
10 */
11
12 (function($){
13      $.extend($.expr[':'], {
14         // case insensitive version of :contains
15         icontains: function(a,i,m){return (a.textContent||a.innerText||jQuery(a).text()||"").toLowerCase().indexOf(m[3].toLowerCase())>=0;}
16     });
17
18     $.iterators = {
19         getText:  function() { return $(this).text(); },
20         parseInt: function(v){ return parseInt(v, 10); }
21     };
22
23         $.extend({ 
24
25         // Returns a range object
26         // Author: Matthias Miller
27         // Site:   http://blog.outofhanwell.com/2006/03/29/javascript-range-function/
28         range:  function() {
29             if (!arguments.length) { return []; }
30             var min, max, step;
31             if (arguments.length == 1) {
32                 min  = 0;
33                 max  = arguments[0]-1;
34                 step = 1;
35             }
36             else {
37                 // default step to 1 if it's zero or undefined
38                 min  = arguments[0];
39                 max  = arguments[1]-1;
40                 step = arguments[2] || 1;
41             }
42             // convert negative steps to positive and reverse min/max
43             if (step < 0 && min >= max) {
44                 step *= -1;
45                 var tmp = min;
46                 min = max;
47                 max = tmp;
48                 min += ((max-min) % step);
49             }
50             var a = [];
51             for (var i = min; i <= max; i += step) { a.push(i); }
52             return a;
53         },
54
55         // Taken from ui.core.js. 
56         // Why are you keeping this gem for yourself guys ? :|
57         keyCode: {
58             BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, CONTROL: 17, DELETE: 46, DOWN: 40,
59             END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT:  45, LEFT: 37,
60             NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108, 
61             NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, 
62             PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38
63         },
64         
65         // Takes a keyboard event and return true if the keycode match the specified keycode
66         keyIs: function(k, e) {
67             return parseInt($.keyCode[k.toUpperCase()], 10) == parseInt((typeof(e) == 'number' )? e: e.keyCode, 10);
68         },
69         
70         // Returns the key of an array
71         keys: function(arr) {
72             var o = [];
73             for (k in arr) { o.push(k); }
74             return o;
75         },
76
77         // Redirect to a specified url
78         redirect: function(url) {
79             window.location.href = url;
80             return url;
81         },
82
83         // Stop event shorthand
84         stop: function(e, preventDefault, stopPropagation) {
85             if (preventDefault)  { e.preventDefault(); }
86             if (stopPropagation) { e.stopPropagation(); }
87             return preventDefault && false || true;
88         },
89
90         // Returns the basename of a path
91         basename: function(path) {
92             var t = path.split('/');
93             return t[t.length] === '' && s || t.slice(0, t.length).join('/');
94         },
95
96         // Returns the filename of a path
97         filename: function(path) {
98             return path.split('/').pop();
99         }, 
100
101         // Returns a formated file size
102         filesizeformat: function(bytes, suffixes){
103             var b = parseInt(bytes, 10);
104             var s = suffixes || ['byte', 'bytes', 'KB', 'MB', 'GB'];
105             if (isNaN(b) || b === 0) { return '0 ' + s[0]; }
106             if (b == 1)              { return '1 ' + s[0]; }
107             if (b < 1024)            { return  b.toFixed(2) + ' ' + s[1]; }
108             if (b < 1048576)         { return (b / 1024).toFixed(2) + ' ' + s[2]; }
109             if (b < 1073741824)      { return (b / 1048576).toFixed(2) + ' '+ s[3]; }
110             else                     { return (b / 1073741824).toFixed(2) + ' '+ s[4]; }
111         },
112
113         fileExtension: function(s) {
114             var tokens = s.split('.');
115             return tokens[tokens.length-1] || false;
116         },
117         
118         // Returns true if an object is a String
119         isString: function(o) {
120             return typeof(o) == 'string' && true || false;
121         },
122         
123         // Returns true if an object is a RegExp
124                 isRegExp: function(o) {
125                         return o && o.constructor.toString().indexOf('RegExp()') != -1 || false;
126                 },
127
128         isObject: function(o) {
129             return (typeof(o) == 'object');
130         },
131         
132         // Convert input to currency (two decimal fixed number)
133                 toCurrency: function(i) {
134                         i = parseFloat(i, 10).toFixed(2);
135                         return (i=='NaN') ? '0.00' : i;
136                 },
137
138         /*-------------------------------------------------------------------- 
139          * javascript method: "pxToEm"
140          * by:
141            Scott Jehl (scott@filamentgroup.com) 
142            Maggie Wachs (maggie@filamentgroup.com)
143            http://www.filamentgroup.com
144          *
145          * Copyright (c) 2008 Filament Group
146          * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
147          *
148          * Description: pxToEm converts a pixel value to ems depending on inherited font size.  
149          * Article: http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/
150          * Demo: http://www.filamentgroup.com/examples/pxToEm/          
151          *                                                      
152          * Options:                                                                     
153                 scope: string or jQuery selector for font-size scoping
154                 reverse: Boolean, true reverses the conversion to em-px
155          * Dependencies: jQuery library                                           
156          * Usage Example: myPixelValue.pxToEm(); or myPixelValue.pxToEm({'scope':'#navigation', reverse: true});
157          *
158          * Version: 2.1, 18.12.2008
159          * Changelog:
160          *              08.02.2007 initial Version 1.0
161          *              08.01.2008 - fixed font-size calculation for IE
162          *              18.12.2008 - removed native object prototyping to stay in jQuery's spirit, jsLinted (Maxime Haineault <haineault@gmail.com>)
163         --------------------------------------------------------------------*/
164
165         pxToEm: function(i, settings){
166             //set defaults
167             settings = jQuery.extend({
168                 scope: 'body',
169                 reverse: false
170             }, settings);
171             
172             var pxVal = (i === '') ? 0 : parseFloat(i);
173             var scopeVal;
174             var getWindowWidth = function(){
175                 var de = document.documentElement;
176                 return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
177             };  
178             
179             /* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
180                 For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size.      
181                 When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
182                 to get an accurate em value. */
183                         
184             if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
185                 var calcFontSize = function(){          
186                     return (parseFloat($('body').css('font-size'))/getWindowWidth()).toFixed(3) * 16;
187                 };
188                 scopeVal = calcFontSize();
189             }
190             else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); }
191                     
192             var result = (settings.reverse === true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
193             return result;
194         }
195         });
196
197         $.extend($.fn, { 
198         type: function() {
199             try { return $(this).get(0).nodeName.toLowerCase(); }
200             catch(e) { return false; }
201         },
202         // Select a text range in a textarea
203         selectRange: function(start, end){
204             // use only the first one since only one input can be focused
205             if ($(this).get(0).createTextRange) {
206                 var range = $(this).get(0).createTextRange();
207                 range.collapse(true);
208                 range.moveEnd('character',   end);
209                 range.moveStart('character', start);
210                 range.select();
211             }
212             else if ($(this).get(0).setSelectionRange) {
213                 $(this).bind('focus', function(e){
214                     e.preventDefault();
215                 }).get(0).setSelectionRange(start, end);
216             }
217             return $(this);
218         },
219
220         /*-------------------------------------------------------------------- 
221          * JQuery Plugin: "EqualHeights"
222          * by:  Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
223          *
224          * Copyright (c) 2008 Filament Group
225          * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
226          *
227          * Description: Compares the heights or widths of the top-level children of a provided element 
228                 and sets their min-height to the tallest height (or width to widest width). Sets in em units 
229                 by default if pxToEm() method is available.
230          * Dependencies: jQuery library, pxToEm method  (article: 
231                 http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)                                                      
232          * Usage Example: $(element).equalHeights();
233                 Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
234          * Version: 2.1, 18.12.2008
235          *
236          * Note: Changed pxToEm call to call $.pxToEm instead, jsLinted (Maxime Haineault <haineault@gmail.com>)
237         --------------------------------------------------------------------*/
238
239         equalHeights: function(px){
240             $(this).each(function(){
241                 var currentTallest = 0;
242                 $(this).children().each(function(i){
243                     if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
244                 });
245                 if (!px || !$.pxToEm) { currentTallest = $.pxToEm(currentTallest); } //use ems unless px is specified
246                 // for ie6, set height since min-height isn't supported
247                 if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
248                 $(this).children().css({'min-height': currentTallest}); 
249             });
250             return this;
251         },
252
253         // Copyright (c) 2009 James Padolsey
254         // http://james.padolsey.com/javascript/jquery-delay-plugin/
255         delay: function(time, callback){
256             jQuery.fx.step.delay = function(){};
257             return this.animate({delay:1}, time, callback);
258         }        
259         });
260 })(jQuery);
261
262 /*
263   jQuery strings - 0.4
264   http://code.google.com/p/jquery-utils/
265   
266   (c) Maxime Haineault <haineault@gmail.com>
267   http://haineault.com   
268
269   MIT License (http://www.opensource.org/licenses/mit-license.php)
270
271   Implementation of Python3K advanced string formatting
272   http://www.python.org/dev/peps/pep-3101/
273
274   Documentation: http://code.google.com/p/jquery-utils/wiki/StringFormat
275   
276 */
277 (function($){
278     var strings = {
279         strConversion: {
280             // tries to translate any objects type into string gracefully
281             __repr: function(i){
282                 switch(this.__getType(i)) {
283                     case 'array':case 'date':case 'number':
284                         return i.toString();
285                     case 'object': // Thanks to Richard Paul Lewis for the fix
286                         var o = []; 
287                         var l = i.length;
288                         for(var x=0;x<l;x++) {
289                           o.push(x+': '+this.__repr(i[x]));
290                         } 
291                         return o.join(', ');                        
292                     case 'string': 
293                         return i;
294                     default: 
295                         return i;
296                 }
297             },
298             // like typeof but less vague
299             __getType: function(i) {
300                 if (!i || !i.constructor) { return typeof(i); }
301                 var match = i.constructor.toString().match(/Array|Number|String|Object|Date/);
302                 return match && match[0].toLowerCase() || typeof(i);
303             },
304             // Jonas Raoni Soares Silva (http://jsfromhell.com/string/pad)
305             __pad: function(str, l, s, t){
306                 var p = s || ' ';
307                 var o = str;
308                 if (l - str.length > 0) {
309                     o = new Array(Math.ceil(l / p.length)).join(p).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2)) + str + p.substr(0, l - t);
310                 }
311                 return o;
312             },
313             __getInput: function(arg, args) {
314                  var key = arg.getKey();
315                 switch(this.__getType(args)){
316                     case 'object': // Thanks to Jonathan Works for the patch
317                         var keys = key.split('.');
318                         var obj = args;
319                         for(var subkey = 0; subkey < keys.length; subkey++){
320                             obj = obj[keys[subkey]];
321                         }
322                         if (typeof(obj) != 'undefined') {
323                             if (strings.strConversion.__getType(obj) == 'array') {
324                                 return arg.getFormat().match(/\.\*/) && obj[1] || obj;
325                             }
326                             return obj;
327                         }
328                         else {
329                             // TODO: try by numerical index                    
330                         }
331                     break;
332                     case 'array': 
333                         key = parseInt(key, 10);
334                         if (arg.getFormat().match(/\.\*/) && typeof args[key+1] != 'undefined') { return args[key+1]; }
335                         else if (typeof args[key] != 'undefined') { return args[key]; }
336                         else { return key; }
337                     break;
338                 }
339                 return '{'+key+'}';
340             },
341             __formatToken: function(token, args) {
342                 var arg   = new Argument(token, args);
343                 return strings.strConversion[arg.getFormat().slice(-1)](this.__getInput(arg, args), arg);
344             },
345
346             // Signed integer decimal.
347             d: function(input, arg){
348                 var o = parseInt(input, 10); // enforce base 10
349                 var p = arg.getPaddingLength();
350                 if (p) { return this.__pad(o.toString(), p, arg.getPaddingString(), 0); }
351                 else   { return o; }
352             },
353             // Signed integer decimal.
354             i: function(input, args){ 
355                 return this.d(input, args);
356             },
357             // Unsigned octal
358             o: function(input, arg){ 
359                 var o = input.toString(8);
360                 if (arg.isAlternate()) { o = this.__pad(o, o.length+1, '0', 0); }
361                 return this.__pad(o, arg.getPaddingLength(), arg.getPaddingString(), 0);
362             },
363             // Unsigned decimal
364             u: function(input, args) {
365                 return Math.abs(this.d(input, args));
366             },
367             // Unsigned hexadecimal (lowercase)
368             x: function(input, arg){
369                 var o = parseInt(input, 10).toString(16);
370                 o = this.__pad(o, arg.getPaddingLength(), arg.getPaddingString(),0);
371                 return arg.isAlternate() ? '0x'+o : o;
372             },
373             // Unsigned hexadecimal (uppercase)
374             X: function(input, arg){
375                 return this.x(input, arg).toUpperCase();
376             },
377             // Floating point exponential format (lowercase)
378             e: function(input, arg){
379                 return parseFloat(input, 10).toExponential(arg.getPrecision());
380             },
381             // Floating point exponential format (uppercase)
382             E: function(input, arg){
383                 return this.e(input, arg).toUpperCase();
384             },
385             // Floating point decimal format
386             f: function(input, arg){
387                 return this.__pad(parseFloat(input, 10).toFixed(arg.getPrecision()), arg.getPaddingLength(), arg.getPaddingString(),0);
388             },
389             // Floating point decimal format (alias)
390             F: function(input, args){
391                 return this.f(input, args);
392             },
393             // Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise
394             g: function(input, arg){
395                 var o = parseFloat(input, 10);
396                 return (o.toString().length > 6) ? Math.round(o.toExponential(arg.getPrecision())): o;
397             },
398             // Floating point format. Uses exponential format if exponent is greater than -4 or less than precision, decimal format otherwise
399             G: function(input, args){
400                 return this.g(input, args);
401             },
402             // Single character (accepts integer or single character string).   
403             c: function(input, args) {
404                 var match = input.match(/\w|\d/);
405                 return match && match[0] || '';
406             },
407             // String (converts any JavaScript object to anotated format)
408             r: function(input, args) {
409                 return this.__repr(input);
410             },
411             // String (converts any JavaScript object using object.toString())
412             s: function(input, args) {
413                 return input.toString && input.toString() || ''+input;
414             }
415         },
416
417         format: function(str, args) {
418             var end    = 0;
419             var start  = 0;
420             var match  = false;
421             var buffer = [];
422             var token  = '';
423             var tmp    = (str||'').split('');
424             for(start=0; start < tmp.length; start++) {
425                 if (tmp[start] == '{' && tmp[start+1] !='{') {
426                     end   = str.indexOf('}', start);
427                     token = tmp.slice(start+1, end).join('');
428                     if (tmp[start-1] != '{' && tmp[end+1] != '}') {
429                         var tokenArgs = (typeof arguments[1] != 'object')? arguments2Array(arguments, 2): args || [];
430                         buffer.push(strings.strConversion.__formatToken(token, tokenArgs));
431                     }
432                     else {
433                         buffer.push(token);
434                     }
435                 }
436                 else if (start > end || buffer.length < 1) { buffer.push(tmp[start]); }
437             }
438             return (buffer.length > 1)? buffer.join(''): buffer[0];
439         },
440
441         calc: function(str, args) {
442             return eval(format(str, args));
443         },
444
445         repeat: function(s, n) { 
446             return new Array(n+1).join(s); 
447         },
448
449         UTF8encode: function(s) { 
450             return unescape(encodeURIComponent(s)); 
451         },
452
453         UTF8decode: function(s) { 
454             return decodeURIComponent(escape(s)); 
455         },
456
457         tpl: function() {
458             var out = '';
459             var render = true;
460             // Set
461             // $.tpl('ui.test', ['<span>', helloWorld ,'</span>']);
462             if (arguments.length == 2 && $.isArray(arguments[1])) {
463                 this[arguments[0]] = arguments[1].join('');
464                 return $(this[arguments[0]]);
465             }
466             // $.tpl('ui.test', '<span>hello world</span>');
467             if (arguments.length == 2 && $.isString(arguments[1])) {
468                 this[arguments[0]] = arguments[1];
469                 return $(this[arguments[0]]);
470             }
471             // Call
472             // $.tpl('ui.test');
473             if (arguments.length == 1) {
474                 return $(this[arguments[0]]);
475             }
476             // $.tpl('ui.test', false);
477             if (arguments.length == 2 && arguments[1] == false) {
478                 return this[arguments[0]];
479             }
480             // $.tpl('ui.test', {value:blah});
481             if (arguments.length == 2 && $.isObject(arguments[1])) {
482                 return $($.format(this[arguments[0]], arguments[1]));
483             }
484             // $.tpl('ui.test', {value:blah}, false);
485             if (arguments.length == 3 && $.isObject(arguments[1])) {
486                 return (arguments[2] == true) 
487                     ? $.format(this[arguments[0]], arguments[1])
488                     : $($.format(this[arguments[0]], arguments[1]));
489             }
490         }
491     };
492
493     var Argument = function(arg, args) {
494         this.__arg  = arg;
495         this.__args = args;
496         this.__max_precision = parseFloat('1.'+ (new Array(32)).join('1'), 10).toString().length-3;
497         this.__def_precision = 6;
498         this.getString = function(){
499             return this.__arg;
500         };
501         this.getKey = function(){
502             return this.__arg.split(':')[0];
503         };
504         this.getFormat = function(){
505             var match = this.getString().split(':');
506             return (match && match[1])? match[1]: 's';
507         };
508         this.getPrecision = function(){
509             var match = this.getFormat().match(/\.(\d+|\*)/g);
510             if (!match) { return this.__def_precision; }
511             else {
512                 match = match[0].slice(1);
513                 if (match != '*') { return parseInt(match, 10); }
514                 else if(strings.strConversion.__getType(this.__args) == 'array') {
515                     return this.__args[1] && this.__args[0] || this.__def_precision;
516                 }
517                 else if(strings.strConversion.__getType(this.__args) == 'object') {
518                     return this.__args[this.getKey()] && this.__args[this.getKey()][0] || this.__def_precision;
519                 }
520                 else { return this.__def_precision; }
521             }
522         };
523         this.getPaddingLength = function(){
524             var match = false;
525             if (this.isAlternate()) {
526                 match = this.getString().match(/0?#0?(\d+)/);
527                 if (match && match[1]) { return parseInt(match[1], 10); }
528             }
529             match = this.getString().match(/(0|\.)(\d+|\*)/g);
530             return match && parseInt(match[0].slice(1), 10) || 0;
531         };
532         this.getPaddingString = function(){
533             var o = '';
534             if (this.isAlternate()) { o = ' '; }
535             // 0 take precedence on alternate format
536             if (this.getFormat().match(/#0|0#|^0|\.\d+/)) { o = '0'; }
537             return o;
538         };
539         this.getFlags = function(){
540             var match = this.getString().matc(/^(0|\#|\-|\+|\s)+/);
541             return match && match[0].split('') || [];
542         };
543         this.isAlternate = function() {
544             return !!this.getFormat().match(/^0?#/);
545         };
546     };
547
548     var arguments2Array = function(args, shift) {
549         var o = [];
550         for (l=args.length, x=(shift || 0)-1; x<l;x++) { o.push(args[x]); }
551         return o;
552     };
553     $.extend(strings);
554 })(jQuery);
555
556 /*
557   jQuery ui.timepickr - @VERSION
558   http://code.google.com/p/jquery-utils/
559
560   (c) Maxime Haineault <haineault@gmail.com> 
561   http://haineault.com
562
563   MIT License (http://www.opensource.org/licenses/mit-license.php
564
565   Note: if you want the original experimental plugin checkout the rev 224 
566
567   Dependencies
568   ------------
569   - jquery.utils.js
570   - jquery.strings.js
571   - jquery.ui.js
572   
573 */
574
575 (function($) {
576
577 $.tpl('timepickr.menu',   '<div class="ui-helper-reset ui-timepickr ui-widget" />');
578 $.tpl('timepickr.row',    '<ol class="ui-timepickr-row ui-helper-clearfix" />');
579 $.tpl('timepickr.button', '<li class="{className:s}"><span class="ui-state-default">{label:s}</span></li>');
580
581 $.widget('ui.timepickr', {
582     plugins: {},
583     _create: function() {
584         this._dom = {
585             menu: $.tpl('timepickr.menu'),
586             row:  $.tpl('timepickr.menu')
587         };
588         this._trigger('initialize');
589         this._trigger('initialized');
590     },
591
592     _trigger: function(type, e, ui) {
593         var ui = ui || this;
594         $.ui.plugin.call(this, type, [e, ui]);
595         return $.Widget.prototype._trigger.call(this, type, e, ui);
596     },
597
598     _createButton: function(i, format, className) {
599         var o  = format && $.format(format, i) || i;
600         var cn = className && 'ui-timepickr-button '+ className || 'ui-timepickr-button';
601         return $.tpl('timepickr.button', {className: cn, label: o}).data('id', i)
602                 .bind('mouseover', function(){
603                     $(this).siblings().find('span')
604                         .removeClass('ui-state-hover').end().end()
605                         .find('span').addClass('ui-state-hover');
606                 });
607
608     },
609
610     _addRow: function(range, format, className, insertAfter) {
611         var ui  = this;
612         var btn = false;
613         var row = $.tpl('timepickr.row').bind('mouseover', function(){
614             $(this).next().show();
615         });
616         $.each(range, function(idx, val){
617             ui._createButton(val, format || false).appendTo(row);
618         });
619         if (className) {
620             $(row).addClass(className);
621         }
622         if (this.options.corners) {
623              row.find('span').addClass('ui-corner-'+ this.options.corners);
624         }
625         if (insertAfter) {
626             row.insertAfter(insertAfter);
627         }
628         else {
629             ui._dom.menu.append(row);
630         }
631         return row;
632     },
633
634     _setVal: function(val) {
635         val = val || this._getVal();
636         if (!(val.h==='' && val.m==='')) {        
637             this.element.data('timepickr.initialValue', val);
638             this.element.val(this._formatVal(val));        
639         }
640         if(this._dom.menu.is(':hidden')) {
641             this.element.trigger('change');
642         }
643     },
644
645     _getVal: function() {
646         var ols = this._dom.menu.find('ol');
647         function get(unit) {
648             var u = ols.filter('.'+unit).find('.ui-state-hover:first').text();
649             return u || ols.filter('.'+unit+'li:first span').text();
650         }
651         return {
652             h: get('hours'),
653             m: get('minutes'),
654             s: get('seconds'),
655             a: get('prefix'),
656             z: get('suffix'),
657             f: this.options['format'+ this.c],
658             c: this.c
659         };
660     },
661
662     _formatVal: function(ival) {
663         var val = ival || this._getVal();
664         val.c = this.options.convention;
665         val.f = val.c === 12 && this.options.format12 || this.options.format24;
666         return (new Time(val)).getTime();
667     },
668
669     blur: function() {
670         return this.element.blur();      
671     },
672
673     focus: function() {
674         return this.element.focus();      
675     },
676     show: function() {
677         this._trigger('show');
678         this.element.trigger(this.options.trigger);
679     },
680     hide: function() {
681         this._trigger('hide');
682         this._dom.menu.hide();
683     }
684
685 });
686
687 // These properties are shared accross every instances of timepickr 
688 $.extend($.ui.timepickr.prototype, {
689     version:     '@VERSION',
690     //eventPrefix: '',
691     //getter:      '',
692     options:    {
693         convention:  24, // 24, 12
694         trigger:     'mouseover',
695         format12:    '{h:02.d}:{m:02.d} {z:s}',
696         format24:    '{h:02.d}:{m:02.d}',
697         hours:       true,
698         prefix:      ['am', 'pm'],
699         suffix:      ['am', 'pm'],
700         prefixVal:   false,
701         suffixVal:   true,
702         rangeHour12: $.range(1, 13),
703         rangeHour24: [$.range(0, 12), $.range(12, 24)],
704         rangeMin:    $.range(0, 60, 15),
705         rangeSec:    $.range(0, 60, 15),
706         corners:     'all',
707         // plugins
708         core:        true,
709         minutes:     true,
710         seconds:     false,
711         val:         false,
712         updateLive:  true,
713         resetOnBlur: true,
714         keyboardnav: true,
715         handle:      false,
716         handleEvent: 'click'
717     }
718 });
719
720 $.ui.plugin.add('timepickr', 'core', {
721     initialized: function(e, ui) {
722         var menu = ui._dom.menu;
723         var pos  = ui.element.position();
724
725         menu.insertAfter(ui.element).css('left', pos.left);
726
727         if (!$.boxModel) { // IE alignement fix
728             menu.css('margin-top', ui.element.height() + 8);
729         }
730         
731         ui.element
732             .bind(ui.options.trigger, function() {
733                 ui._dom.menu.show();
734                 ui._dom.menu.find('ol:first').show();
735                 ui._trigger('focus');
736                 if (ui.options.trigger != 'focus') {
737                     ui.element.focus();
738                 }
739                 ui._trigger('focus');
740             })
741             .bind('blur', function() {
742                 ui.hide();
743                 ui._trigger('blur');
744             });
745
746         menu.find('li').bind('mouseover.timepickr', function() {
747             ui._trigger('refresh');
748         });
749     },
750     refresh: function(e, ui) {
751         // Realign each menu layers
752         ui._dom.menu.find('ol').each(function(){
753             var p = $(this).prev('ol');
754             try { // .. to not fuckup IE
755                 $(this).css('left', p.position().left + p.find('.ui-state-hover').position().left);
756             } catch(e) {};
757         });
758     }
759 });
760
761 $.ui.plugin.add('timepickr', 'hours', {
762     initialize: function(e, ui) {
763         if (ui.options.convention === 24) {
764             // prefix is required in 24h mode
765             ui._dom.prefix = ui._addRow(ui.options.prefix, false, 'prefix'); 
766
767             // split-range
768             if ($.isArray(ui.options.rangeHour24[0])) {
769                 var range = [];
770                 $.merge(range, ui.options.rangeHour24[0]);
771                 $.merge(range, ui.options.rangeHour24[1]);
772                 ui._dom.hours = ui._addRow(range, '{0:0.2d}', 'hours');
773                 ui._dom.hours.find('li').slice(ui.options.rangeHour24[0].length, -1).hide();
774                 var lis   = ui._dom.hours.find('li'); 
775
776                 var show = [
777                     function() {
778                         lis.slice(ui.options.rangeHour24[0].length).hide().end()
779                            .slice(0, ui.options.rangeHour24[0].length).show()
780                            .filter(':visible:first').trigger('mouseover');
781
782                     },
783                     function() {
784                         lis.slice(0, ui.options.rangeHour24[0].length).hide().end()
785                            .slice(ui.options.rangeHour24[0].length).show()
786                            .filter(':visible:first').trigger('mouseover');
787                     }
788                 ];
789
790                 ui._dom.prefix.find('li').bind('mouseover.timepickr', function(){
791                     var index = ui._dom.menu.find('.prefix li').index(this);
792                     show[index].call();
793                 });
794             }
795             else {
796                 ui._dom.hours = ui._addRow(ui.options.rangeHour24, '{0:0.2d}', 'hours');
797                 ui._dom.hours.find('li').slice(12, -1).hide();
798             }
799         }
800         else {
801             ui._dom.hours  = ui._addRow(ui.options.rangeHour12, '{0:0.2d}', 'hours');
802             // suffix is required in 12h mode
803             ui._dom.suffix = ui._addRow(ui.options.suffix, false, 'suffix'); 
804         }
805     }});
806
807 $.ui.plugin.add('timepickr', 'minutes', {
808     initialize: function(e, ui) {
809         var p = ui._dom.hours && ui._dom.hours || false;
810         ui._dom.minutes = ui._addRow(ui.options.rangeMin, '{0:0.2d}', 'minutes', p);
811     }
812 });
813
814 $.ui.plugin.add('timepickr', 'seconds', {
815     initialize: function(e, ui) {
816         var p = ui._dom.minutes && ui._dom.minutes || false;
817         ui._dom.seconds = ui._addRow(ui.options.rangeSec, '{0:0.2d}', 'seconds', p);
818     }
819 });
820
821 $.ui.plugin.add('timepickr', 'val', {
822     initialized: function(e, ui) {
823         ui._setVal(ui.options.val);
824     }
825 });
826
827 $.ui.plugin.add('timepickr', 'updateLive', {
828     refresh: function(e, ui) {
829         ui._setVal();
830     }
831 });
832
833 $.ui.plugin.add('timepickr', 'resetOnBlur', {
834     initialized: function(e, ui) {
835         ui.element.data('timepickr.initialValue', ui._getVal());
836         ui._dom.menu.find('li > span').bind('mousedown.timepickr', function(){
837             ui.element.data('timepickr.initialValue', ui._getVal()); 
838         });
839     },
840     blur: function(e, ui) {
841         ui._setVal(ui.element.data('timepickr.initialValue'));
842     }
843 });
844
845 $.ui.plugin.add('timepickr', 'handle', {
846     initialized: function(e, ui) {
847         $(ui.options.handle).bind(ui.options.handleEvent + '.timepickr', function(){
848             ui.show();
849         });
850     }
851 });
852
853 $.ui.plugin.add('timepickr', 'keyboardnav', {
854     initialized: function(e, ui) {
855         ui.element
856             .bind('keydown', function(e) {
857                 if ($.keyIs('enter', e)) {
858                     ui._setVal();
859                     ui.blur();
860                 }
861                 else if ($.keyIs('escape', e)) {
862                     ui.blur();
863                 }
864             });
865     }
866 });
867
868 var Time = function() { // arguments: h, m, s, c, z, f || time string
869     if (!(this instanceof arguments.callee)) {
870         throw Error("Constructor called as a function");
871     }
872     // arguments as literal object
873     if (arguments.length == 1 && $.isObject(arguments[0])) {
874         this.h = arguments[0].h || 0;
875         this.m = arguments[0].m || 0;
876         this.s = arguments[0].s || 0;
877         this.c = arguments[0].c && ($.inArray(arguments[0].c, [12, 24]) >= 0) && arguments[0].c || 24;
878         this.f = arguments[0].f || ((this.c == 12) && '{h:02.d}:{m:02.d} {z:02.d}' || '{h:02.d}:{m:02.d}');
879         this.z = arguments[0].z || 'am';
880     }
881     // arguments as string
882     else if (arguments.length < 4 && $.isString(arguments[1])) {
883         this.c = arguments[2] && ($.inArray(arguments[0], [12, 24]) >= 0) && arguments[0] || 24;
884         this.f = arguments[3] || ((this.c == 12) && '{h:02.d}:{m:02.d} {z:02.d}' || '{h:02.d}:{m:02.d}');
885         this.z = arguments[4] || 'am';
886         
887         this.h = arguments[1] || 0; // parse
888         this.m = arguments[1] || 0; // parse
889         this.s = arguments[1] || 0; // parse
890     }
891     // no arguments (now)
892     else if (arguments.length === 0) {
893         // now
894     }
895     // standards arguments
896     else {
897         this.h = arguments[0] || 0;
898         this.m = arguments[1] || 0;
899         this.s = arguments[2] || 0;
900         this.c = arguments[3] && ($.inArray(arguments[3], [12, 24]) >= 0) && arguments[3] || 24;
901         this.f = this.f || ((this.c == 12) && '{h:02.d}:{m:02.d} {z:02.d}' || '{h:02.d}:{m:02.d}');
902         this.z = 'am';
903     }
904     return this;
905 };
906
907 Time.prototype.get        = function(p, f, u)    { return u && this.h || $.format(f, this.h); };
908 Time.prototype.getHours   = function(unformated) { return this.get('h', '{0:02.d}', unformated); };
909 Time.prototype.getMinutes = function(unformated) { return this.get('m', '{0:02.d}', unformated); };
910 Time.prototype.getSeconds = function(unformated) { return this.get('s', '{0:02.d}', unformated); };
911 Time.prototype.setFormat  = function(format)     { return this.f = format; };
912 Time.prototype.getObject  = function()           { return { h: this.h, m: this.m, s: this.s, c: this.c, f: this.f, z: this.z }; };
913 Time.prototype.getTime    = function()           { return $.format(this.f, {h: this.h, m: this.m, suffix: this.z}); }; // Thanks to Jackson for the fix.
914 Time.prototype.parse      = function(str) { 
915     // 12h formats
916     if (this.c === 12) {
917         // Supported formats: (can't find any *official* standards for 12h..)
918         //  - [hh]:[mm]:[ss] [zz] | [hh]:[mm] [zz] | [hh] [zz] 
919         //  - [hh]:[mm]:[ss] [z.z.] | [hh]:[mm] [z.z.] | [hh] [z.z.]
920         this.tokens = str.split(/\s|:/);    
921         this.h = this.tokens[0] || 0;
922         this.m = this.tokens[1] || 0;
923         this.s = this.tokens[2] || 0;
924         this.z = this.tokens[3] || '';
925         return this.getObject();
926     }
927     // 24h formats
928     else { 
929         // Supported formats:
930         //  - ISO 8601: [hh][mm][ss] | [hh][mm] | [hh]  
931         //  - ISO 8601 extended: [hh]:[mm]:[ss] | [hh]:[mm] | [hh]
932         this.tokens = /:/.test(str) && str.split(/:/) || str.match(/[0-9]{2}/g);
933         this.h = this.tokens[0] || 0;
934         this.m = this.tokens[1] || 0;
935         this.s = this.tokens[2] || 0;
936         this.z = this.tokens[3] || '';
937         return this.getObject();
938     }
939 };
940
941 })(jQuery);