/* http://keith-wood.name/timeEntry.html Time entry for jQuery v1.3.1. Written by Keith Wood (kbwood@virginbroadband.com.au) June 2007. Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. Please attribute the author if you use it. */ (function($){function TimeEntry(){this._nextId=0;this._inst=[];this._disabledInputs=[];this.regional=[];this.regional['']={show24Hours:false,separator:':',ampmPrefix:'',ampmNames:['AM','PM'],spinnerTexts:['Now','Previous field','Next field','Increment','Decrement']};this._defaults={appendText:'',showSeconds:false,timeSteps:[1,1,1],initialField:0,useMouseWheel:true,defaultTime:null,minTime:null,maxTime:null,spinnerImage:'timeEntry.png',spinnerSize:[20,20,8],spinnerIncDecOnly:false,spinnerRepeat:[500,250],beforeShow:null};$.extend(this._defaults,this.regional[''])}$.extend(TimeEntry.prototype,{markerClassName:'hasTimeEntry',_register:function(a){var b=this._nextId++;this._inst[b]=a;return b},_getInst:function(a){return this._inst[a]||a},setDefaults:function(a){extendRemove(this._defaults,a||{})},_doFocus:function(a){var b=(a.nodeName&&a.nodeName.toLowerCase()=='input'?a:this);if($.timeEntry._lastInput==b){return}if($.timeEntry._isDisabledTimeEntry(b)){return}var c=$.timeEntry._getInst(b._timeId);c._input=$(b);$.timeEntry._focussed=true;$.timeEntry._lastInput=b;$.timeEntry._blurredInput=null;var d=c._get('beforeShow');extendRemove(c._settings,(d?d(b):{}));c._parseTime()},_doBlur:function(a){$.timeEntry._blurredInput=$.timeEntry._lastInput;$.timeEntry._lastInput=null},_doClick:function(a){var b=a.target;var c=$.timeEntry._getInst(b._timeId);if(!$.timeEntry._focussed){var d=c._get('separator').length+2;c._field=0;if($.browser.msie){var e=b.value;var f=a.clientX+$.timeEntry._findScroll(a.srcElement)[0]-$.timeEntry._findPos(a.srcElement)[0];for(var g=0;g<=Math.max(1,c._secondField,c._ampmField);g++){var h=(g!=c._ampmField?(g*d)+2:(c._ampmField*d)+c._get('ampmPrefix').length+c._get('ampmNames')[0].length);b.value=e.substring(0,h);var i=b.createTextRange();if(f=48){return true}var b=$.timeEntry._getInst(this._timeId);switch(a.keyCode){case 9:return(a.shiftKey?b._previousField(true):b._nextField(true));case 35:if(a.ctrlKey){b._setValue('')}else{b._field=Math.max(1,b._secondField,b._ampmField);b._adjustField(0)}break;case 36:if(a.ctrlKey){b._setTime()}else{b._field=0;b._adjustField(0)}break;case 37:b._previousField(false);break;case 38:b._adjustField(+1);break;case 39:b._nextField(false);break;case 40:b._adjustField(-1);break;case 46:b._setValue('');break}return false},_doKeyPress:function(a){var b=String.fromCharCode(a.charCode==undefined?a.keyCode:a.charCode);if(b<' '){return true}var c=$.timeEntry._getInst(this._timeId);c._handleKeyPress(b);return false},_doMouseWheel:function(a,b){b=($.browser.opera?-b/Math.abs(b):b);var c=$.timeEntry._getInst(this._timeId);c._adjustField(b);a.preventDefault()},_connectTimeEntry:function(b,c){var d=$(b);if(d.is('.'+this.markerClassName)){return}var e=c._get('spinnerImage');var f=c._get('spinnerText');var g=c._get('spinnerSize');var h=c._get('appendText');var i=(!e?null:$(''));d.wrap('').after(h?''+h+'':'').after(i||'');d.addClass(this.markerClassName).bind('focus.timeEntry',this._doFocus).bind('blur.timeEntry',this._doBlur).bind('click.timeEntry',this._doClick).bind('keydown.timeEntry',this._doKeyDown).bind('keypress.timeEntry',this._doKeyPress);if($.browser.mozilla){d.bind('input.timeEntry',function(a){c._parseTime()})}if($.browser.msie){d.bind('paste.timeEntry',function(a){setTimeout(function(){c._parseTime()},1)})}if(c._get('useMouseWheel')&&$.fn.mousewheel){d.mousewheel(this._doMouseWheel)}d[0]._timeId=c._id;if(i){i.mousedown(this._handleSpinner).mouseup(this._endSpinner).mouseout(this._endSpinner).mousemove(this._describeSpinner);i[0]._timeId=c._id}},_enableTimeEntry:function(a){this._enableDisable(a,false)},_disableTimeEntry:function(a){this._enableDisable(a,true)},_enableDisable:function(b,c){var d=$.timeEntry._getInst(b._timeId);if(!d){return}b.disabled=c;if(b.nextSibling&&b.nextSibling.nodeName.toLowerCase()=='span'){$.timeEntry._changeSpinner(d,b.nextSibling,(c?5:-1))}$.timeEntry._disabledInputs=$.map($.timeEntry._disabledInputs,function(a){return(a==b?null:a)});if(c){$.timeEntry._disabledInputs[$.timeEntry._disabledInputs.length]=b}},_isDisabledTimeEntry:function(a){for(var i=0;i=3&&f[0]){$.timeEntry._timer=setTimeout(function(){$.timeEntry._repeatSpinner(d,e)},f[0]);$(b).one('mouseout',$.timeEntry._releaseSpinner).one('mouseup',$.timeEntry._releaseSpinner)}},_actionSpinner:function(a,b){switch(b){case 0:a._setTime();break;case 1:a._previousField(false);break;case 2:a._nextField(false);break;case 3:a._adjustField(+1);break;case 4:a._adjustField(-1);break}},_repeatSpinner:function(a,b){$.timeEntry._lastInput=$.timeEntry._blurredInput;this._actionSpinner(a,b);this._timer=setTimeout(function(){$.timeEntry._repeatSpinner(a,b)},a._get('spinnerRepeat')[1])},_releaseSpinner:function(a){clearTimeout($.timeEntry._timer)},_endSpinner:function(a){var b=$.timeEntry._getSpinnerTarget(a);var c=$.timeEntry._getInst(b._timeId);if(!$.timeEntry._isDisabledTimeEntry(b.previousSibling)){$.timeEntry._changeSpinner(c,b,-1)}if(!$.browser.opera){$.timeEntry._lastInput=$.timeEntry._blurredInput}if($.timeEntry._lastInput){c._showField()}},_getSpinnerTarget:function(a){return(a.target?a.target:a.srcElement)},_getSpinnerRegion:function(a,b){var c=this._getSpinnerTarget(b);var d=this._findPos(c);var e=this._findScroll(c);var f=a._get('spinnerIncDecOnly');var g=(f?99:b.clientX+e[0]-d[0]-($.browser.msie?1:0));var h=b.clientY+e[1]-d[1]-($.browser.msie?1:0);var i=a._get('spinnerSize');var j=(f?99:i[0]-g);var k=i[1]-h;if(i[2]>0&&Math.abs(g-j)<=i[2]&&Math.abs(h-k)<=i[2]){return 0}var l=Math.min(g,h,j,k);return(l==g?1:(l==j?2:(l==h?3:4)))},_changeSpinner:function(a,b,c){$(b).css('background-position','-'+((c+1)*a._get('spinnerSize')[0])+'px 0px')},_findPos:function(a){var b=curTop=0;if(a.offsetParent){b=a.offsetLeft;curTop=a.offsetTop;while(a=a.offsetParent){var c=b;b+=a.offsetLeft;if(b<0){b=c}curTop+=a.offsetTop}}return[b,curTop]},_findScroll:function(a){var b=false;$(a).parents().each(function(){b|=$(this).css('position')=='fixed'});if(b&&!$.browser.opera){return[0,0]}var c=($.browser.opera?document.body.scrollLeft:a.scrollLeft);var d=($.browser.opera?document.body.scrollTop:a.scrollTop);if(!$.browser.opera){while(a=a.parentNode){c+=a.scrollLeft||0;d+=a.scrollTop||0}}return[c,d]}});function TimeEntryInstance(a){this._id=$.timeEntry._register(this);this._selectedHour=0;this._selectedMinute=0;this._selectedSecond=0;this._field=0;this._input=null;this._settings=extendRemove({},a||{})}$.extend(TimeEntryInstance.prototype,{_get:function(a){return(this._settings[a]!=null?this._settings[a]:$.timeEntry._defaults[a])},_parseTime:function(){var a=this._extractTime();var b=this._get('showSeconds');if(a){this._selectedHour=a[0];this._selectedMinute=a[1];this._selectedSecond=a[2]}else{var c=this._constrainTime();this._selectedHour=c[0];this._selectedMinute=c[1];this._selectedSecond=(b?c[2]:0)}this._secondField=(b?2:-1);this._ampmField=(this._get('show24Hours')?-1:(b?3:2));this._lastChr='';this._field=Math.max(0,Math.min(Math.max(1,this._secondField,this._ampmField),this._get('initialField')));if(this._input.val()!=''){this._showTime()}},_extractTime:function(){var a=(this._input?this._input.val():'');var b=this._get('separator');var c=a.split(b);if(b==''&&a!=''){c[0]=a.substring(0,2);c[1]=a.substring(2,4);c[2]=a.substring(4,6)}var d=this._get('ampmNames');var e=this._get('show24Hours');if(c.length>=2){var f=!e&&(a.indexOf(d[0])>-1);var g=!e&&(a.indexOf(d[1])>-1);var h=parseInt(c[0],10);h=(isNaN(h)?0:h);h=((f||g)&&h==12?0:h)+(g?12:0);var i=parseInt(c[1],10);i=(isNaN(i)?0:i);var j=(c.length>=3?parseInt(c[2],10):0);j=(isNaN(j)||!this._get('showSeconds')?0:j);return this._constrainTime([h,i,j])}return null},_constrainTime:function(a){var b=(a!=null);if(!b){var c=this._determineTime(this._get('defaultTime'))||new Date();a=[c.getHours(),c.getMinutes(),c.getSeconds()]}var d=false;var e=this._get('timeSteps');for(var i=0;i1){a[i]=Math.round(a[i]/e[i])*e[i];d=true}}return a},_showTime:function(){var a=this._get('show24Hours');var b=this._get('separator');var c=(this._formatNumber(a?this._selectedHour:((this._selectedHour+11)%12)+1)+b+this._formatNumber(this._selectedMinute)+(this._get('showSeconds')?b+this._formatNumber(this._selectedSecond):'')+(a?'':this._get('ampmPrefix')+this._get('ampmNames')[(this._selectedHour<12?0:1)]));this._setValue(c);this._showField()},_showField:function(){if(!this._input){return}var a=this._input[0];var b=this._get('separator');var c=b.length+2;var d=(this._field!=this._ampmField?(this._field*c):(this._ampmField*c)-b.length+this._get('ampmPrefix').length);var e=d+(this._field!=this._ampmField?2:this._get('ampmNames')[0].length);if(a.setSelectionRange){a.setSelectionRange(d,e)}else if(a.createTextRange){var f=a.createTextRange();f.moveStart('character',d);f.moveEnd('character',e-this._input.val().length);f.select()}if(!a.disabled){a.focus()}},_formatNumber:function(a){return(a<10?'0':'')+a},_setValue:function(a){this._input.val(a);this._input.trigger('change')},_previousField:function(a){var b=(this._input.val()==''||this._field==0);if(!b){this._field--}this._showField();this._lastChr='';return(b&&a)},_nextField:function(a){var b=(this._input.val()==''||this._field==Math.max(1,this._secondField,this._ampmField));if(!b){this._field++}this._showField();this._lastChr='';return(b&&a)},_adjustField:function(a){if(this._input&&this._input.val()==''){a=0}var b=this._get('timeSteps');this._setTime(new Date(0,0,0,this._selectedHour+(this._field==0?a*b[0]:0)+(this._field==this._ampmField?a*12:0),this._selectedMinute+(this._field==1?a*b[1]:0),this._selectedSecond+(this._field==this._secondField?a*b[2]:0)))},_setTime:function(a){a=this._determineTime(a);var b=this._constrainTime(a?[a.getHours(),a.getMinutes(),a.getSeconds()]:null);a=new Date(0,0,0,b[0],b[1],b[2]);var a=this._normaliseTime(a);var c=this._normaliseTime(this._determineTime(this._get('minTime')));var d=this._normaliseTime(this._determineTime(this._get('maxTime')));a=(c&&ad?d:a));this._selectedHour=a.getHours();this._selectedMinute=a.getMinutes();this._selectedSecond=a.getSeconds();this._showTime()},_determineTime:function(h){var i=function(a){var b=new Date();b.setTime(b.getTime()+a*1000);return b};var j=function(a){var b=new Date();var c=b.getHours();var d=b.getMinutes();var e=b.getSeconds();var f=/([+-]?[0-9]+)\s*(s|S|m|M|h|H)?/g;var g=f.exec(a);while(g){switch(g[2]||'s'){case's':case'S':e+=parseInt(g[1]);break;case'm':case'M':d+=parseInt(g[1]);break;case'h':case'H':c+=parseInt(g[1]);break}g=f.exec(a)}b=new Date(0,0,10,c,d,e,0);if(/^!/.test(a)){if(b.getDate()>10){b=new Date(0,0,10,23,59,59)}else if(b.getDate()<10){b=new Date(0,0,10,0,0,0)}}return b};return(h?(typeof h=='string'?j(h):(typeof h=='number'?i(h):h)):null)},_normaliseTime:function(a){if(!a){return null}a.setFullYear(2001);a.setMonth(1-1);a.setDate(26);return a},_handleKeyPress:function(a){if(a==this._get('separator')){this._nextField(false)}else if(a>='0'&&a<='9'){var b=(this._lastChr+a)*1;var c=this._get('show24Hours');var d=(this._field==0&&((c&&b<24)||(b>=1&&b<=12))?b+(!c&&this._selectedHour>=12?12:0):this._selectedHour);var e=(this._field==1&&b<60?b:this._selectedMinute);var f=(this._field==this._secondField&&b<60?b:this._selectedSecond);var g=this._constrainTime([d,e,f]);this._setTime(new Date(0,0,0,g[0],g[1],g[2]));this._lastChr=a}else if(!this._get('show24Hours')){var h=this._get('ampmNames');if((a==h[0].substring(0,1).toLowerCase()&&this._selectedHour>=12)||(a==h[1].substring(0,1).toLowerCase()&&this._selectedHour<12)){var i=this._field;this._field=this._ampmField;this._adjustField(+1);this._field=i;this._showField()}}}});function extendRemove(a,b){$.extend(a,b);for(var c in b){if(b[c]==null){a[c]=null}}return a}$.fn.timeEntry=function(e){var f=Array.prototype.slice.call(arguments,1);if(typeof e=='string'&&(e=='isDisabled'||e=='getTime')){return $.timeEntry['_'+e+'TimeEntry'].apply($.timeEntry,[this[0]].concat(f))}return this.each(function(){var a=this.nodeName.toLowerCase();if(a=='input'){if(typeof e=='string'){$.timeEntry['_'+e+'TimeEntry'].apply($.timeEntry,[this].concat(f))}else{var b=null;for(attrName in $.timeEntry._defaults){var c=this.getAttribute('time:'+attrName);if(c){b=b||{};try{b[attrName]=eval(c)}catch(err){b[attrName]=c}}}var d=(d&&!b?d:new TimeEntryInstance(!b?e:$.extend(b,e)));$.timeEntry._connectTimeEntry(this,d)}}})};$(document).ready(function(){$.timeEntry=new TimeEntry()})})(jQuery);