(function(j){function i(a){var b=j.data(a,"timespinner").options;j(a).spinner(b);j(a).unbind(".timespinner");j(a).bind("click.timespinner",function(){var c=0;if(this.selectionStart!=null){c=this.selectionStart}else{if(this.createTextRange){var e=a.createTextRange();var d=document.selection.createRange();d.setEndPoint("StartToStart",e);c=d.text.length}}if(c>=0&&c<=2){b.highlight=0}else{if(c>=3&&c<=5){b.highlight=1}else{if(c>=6&&c<=8){b.highlight=2}}}l(a)}).bind("blur.timespinner",function(){k(a)})}function l(d){var e=j.data(d,"timespinner").options;var a=0,b=0;if(e.highlight==0){a=0;b=2}else{if(e.highlight==1){a=3;b=5}else{if(e.highlight==2){a=6;b=8}}}if(d.selectionStart!=null){d.setSelectionRange(a,b)}else{if(d.createTextRange){var c=d.createTextRange();c.collapse();c.moveEnd("character",b);c.moveStart("character",a);c.select()}}j(d).focus()}function g(e,a){var b=j.data(e,"timespinner").options;if(!a){return null}var c=a.split(b.separator);for(var d=0;d<c.length;d++){if(isNaN(c[d])){return null}}while(c.length<3){c.push(0)}return new Date(1900,0,0,c[0],c[1],c[2])}function k(b){var c=j.data(b,"timespinner").options;var d=j(b).val();var e=g(b,d);if(!e){e=g(b,c.value)}if(!e){c.value="";j(b).val("");return}var f=g(b,c.min);var p=g(b,c.max);if(f&&f>e){e=f}if(p&&p<e){e=p}var a=[r(e.getHours()),r(e.getMinutes())];if(c.showSeconds){a.push(r(e.getSeconds()))}var q=a.join(c.separator);c.value=q;j(b).val(q);function r(m){return(m<10?"0":"")+m}}function h(c,d){var f=j.data(c,"timespinner").options;var a=j(c).val();if(a==""){a=[0,0,0].join(f.separator)}var b=a.split(f.separator);for(var e=0;e<b.length;e++){b[e]=parseInt(b[e],10)}if(d==true){b[f.highlight]-=f.increment}else{b[f.highlight]+=f.increment}j(c).val(b.join(f.separator));k(c);l(c)}j.fn.timespinner=function(a,b){if(typeof a=="string"){var c=j.fn.timespinner.methods[a];if(c){return c(this,b)}else{return this.spinner(a,b)}}a=a||{};return this.each(function(){var d=j.data(this,"timespinner");if(d){j.extend(d.options,a)}else{j.data(this,"timespinner",{options:j.extend({},j.fn.timespinner.defaults,j.fn.timespinner.parseOptions(this),a)});i(this)}})};j.fn.timespinner.methods={options:function(a){var b=j.data(a[0],"timespinner").options;return j.extend(b,{value:a.val()})},setValue:function(a,b){return a.each(function(){j(this).val(b);k(this)})},getHours:function(a){var b=j.data(a[0],"timespinner").options;var c=a.val().split(b.separator);return parseInt(c[0],10)},getMinutes:function(a){var b=j.data(a[0],"timespinner").options;var c=a.val().split(b.separator);return parseInt(c[1],10)},getSeconds:function(a){var b=j.data(a[0],"timespinner").options;var c=a.val().split(b.separator);return parseInt(c[2],10)||0}};j.fn.timespinner.parseOptions=function(a){var b=j(a);return j.extend({},j.fn.spinner.parseOptions(a),{separator:b.attr("separator"),showSeconds:(b.attr("showSeconds")?b.attr("showSeconds")=="true":undefined),highlight:(parseInt(b.attr("highlight"))||undefined)})};j.fn.timespinner.defaults=j.extend({},j.fn.spinner.defaults,{separator:":",showSeconds:false,highlight:0,spin:function(a){h(this,a)}})})(jQuery);