/*
* jQuery UI Numeric Up/Down v1.0
*
* Copyright 2010, Tony Kramer
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* Dependencies:
* - jQuery (1.4.2)
* - jQuery-ui (1.8.6 - core, widget, button)
*/
(function(e){function i(a){if(typeof a!=="number")a=Number(a);if(isNaN(a))return 0;return a}function j(a){return a.replace(m,"\\$1")}e.widget("ui.numeric",{options:{disabled:false,buttons:true,keyboard:true,showCurrency:false,currencySymbol:"$",emptyValue:0,minValue:false,maxValue:false,smallIncrement:1,increment:5,largeIncrement:10,format:{format:"0",decimalChar:".",thousandsChar:","}},_adjustmentFlag:false,_keyDownFlag:false,_timer:null,_name:"numeric",_value:0,_create:function(){var a=this,b=a.options,
c=a.widget();if(c.attr("type").toLowerCase()!=="text")throw"numeric widget can only be applied to text inputs.";a._checkFormat();a._name=c.attr("id")||c.attr("name");a._value=a._getInputValue(c.attr("value"));if(b.minValue!==false&&a._value<b.minValue)a._value=b.minValue;if(b.maxValue!==false&&a._value>b.maxValue)a._value=b.maxValue;a._setInputValue(a._value);c.wrap(e('<div class="ui-widget ui-corner-all ui-numeric" />'));b.disabled&&a._setOption("disabled",true);b.showCurrency&&a._createCurrency();
b.buttons&&a._createButtons();c.bind({keydown:function(d){return a._onKeyDown(d)},keyup:function(d){return a._onKeyUp(d)},change:function(d){return a._onChange(d)}})},destroy:function(){var a=this,b=a.widget();b.unbind({keydown:function(c){return a._onKeyDown(c)},keyup:function(c){return a._onKeyUp(c)},change:function(c){return a._onChange(c)}});a.options.showCurrency&&e("#"+a._name+"_currency").remove();a.options.buttons&&e("#"+a._name+"_buttons").remove();b.unwrap();e.Widget.prototype.destroy.apply(a)},
_createCurrency:function(){this.widget().before(e("<div/>").attr("id",this._name+"_currency").addClass("ui-numeric-currency").html(this.options.currencySymbol))},_createButtons:function(){function a(f,h){if(f.which==32||f.which==13){b(f,h);f.target.focus()}}function b(f,h){(h?d:g).blur();var k=c._getIncrement(f.ctrlKey,f.shiftKey);c._adjustValueRecursive(h?-k:k)}var c=this,d=e('<button type="button">Up</button>').bind({keydown:function(f){a(f,false)},keyup:function(){clearTimeout(c._timer)},mousedown:function(f){b(f,
false)},mouseup:function(){clearTimeout(c._timer)}}).button({text:false,label:"Up",icons:{primary:"ui-icon-triangle-1-n"}}),g=e('<button type="button">Down</button>').bind({keydown:function(f){a(f,true)},keyup:function(){clearTimeout(c._timer)},mousedown:function(f){b(f,true)},mouseup:function(){clearTimeout(c._timer)}}).button({text:false,label:"Down",icons:{primary:"ui-icon-triangle-1-s"}});c.widget().after(e("<div/>").attr("id",c._name+"_buttons").addClass("ui-numeric-buttons").append(d).append(g))},
_setOption:function(a,b){var c=this.options;switch(a){case "disabled":var d=this.widget();d.parent()[b?"addClass":"removeClass"]("ui-numeric-disabled ui-state-disabled").attr("aria-disabled",b);this._adjustmentFlag=true;b?d.attr({disabled:"disabled",value:""}):d.attr({disabled:"",value:this._format(this._value)});this._adjustmentFlag=false;if(c.buttons)e("#"+this._name+"_buttons button").button(b?"disable":"enable");break;case "emptyValue":c.emptyValue=b;this._setValue(this._value);break;case "minValue":c.minValue=
b===false?false:i(b);c.minValue!==false&&this._value<c.minValue&&this._setValue(c.minValue);break;case "maxValue":c.maxValue=b===false?false:i(b);c.maxValue!==false&&this._value>c.maxValue&&this._setValue(c.maxValue);break;case "format":c.format=b;this._checkFormat();this._setValue(this._value);break;case "showCurrency":if(b&&!c.showCurrency)this._createCurrency();else!b&&c.showCurrency&&e("#"+this._name+"_currency").remove();c.showCurrency=b;break;case "currencySymbol":c.currencySymbol=b;c.showCurrency&&
e("#"+this._name+"_currency").html(b);break;case "buttons":if(b&&!c.buttons)this._createButtons();else!b&&c.buttons&&e("#"+this._name+"_buttons").remove();c.buttons=b;break;default:e.Widget.prototype._setOption.apply(this,a,b)}return this},_checkFormat:function(){var a=this.options;a.format=typeof a.format==="string"?{format:a.format,decimalChar:".",thousandsChar:","}:e.extend({format:"0",decimalChar:".",thousandsChar:","},a.format)},_getInputValue:function(a){a=a.replace(RegExp(j(this.options.format.thousandsChar),
"g"),"");if(this.options.format.decimalChar!==".")a=a.replace(RegExp(j(this.options.format.decimalChar),"g"),".");return i(a)},_setInputValue:function(a){this._adjustmentFlag=true;this.widget().attr("value",this._format(a));this._adjustmentFlag=false},_setValue:function(a){a=i(a);if(this.options.minValue!==false&&a<this.options.minValue)a=this.options.minValue;if(this.options.maxValue!==false&&a>this.options.maxValue)a=this.options.maxValue;this._value=a;this.options.disabled||this._setInputValue(a)},
_format:function(a){var b=this.options;return isNaN(a)||b.emptyValue!==false&&a===b.emptyValue?"":e.formatNumber(a,b.format)},_getIncrement:function(a,b){if(a)return this.options.smallIncrement;else if(b)return this.options.largeIncrement;return this.options.increment},_adjustValue:function(a){if(!this.options.disabled){this._setValue(this._value+a);this.select()}},_adjustValueRecursive:function(a){e.ui.numeric._current=this;e.ui.numeric._timerCallback(a,true)},_onKeyDown:function(a){var b=this.options;
if(!b.disabled){switch(a.which){case 109:case 110:case 190:this._keyDownFlag=true;return;case 38:b.keyboard&&this._adjustValue(this._getIncrement(a.ctrlKey,a.shiftKey));return;case 40:b.keyboard&&this._adjustValue(-this._getIncrement(a.ctrlKey,a.shiftKey));return;case 33:b.keyboard&&this._adjustValue(b.largeIncrement);return;case 34:b.keyboard&&this._adjustValue(-b.largeIncrement);return;case 65:case 67:case 86:case 88:case 89:case 90:if(a.ctrlKey)return}if(!(a.which<=47&&a.which!=32||a.which>=91&&
a.which<=95||a.which>=112&&[188,190,191,192,219,220,221,222].indexOf(a.which)==-1))if(!(a.which>=48&&a.which<=57||a.which>=96&&a.which<=105)){a.preventDefault();a.stopPropagation()}}},_onKeyUp:function(){this._keyDownFlag=false},_onChange:function(a){!this._adjustmentFlag&&!this._keyDownFlag&&this._setValue(this._getInputValue(a.target.value))},value:function(a){if(a===undefined)return this._value;this._setValue(a);return this},select:function(){this.options.disabled||this.widget().select();return this}});
e.ui=e.ui||{};e.ui.numeric._current=null;e.ui.numeric._timerCallback=function(a,b){clearTimeout(e.ui.numeric._current._timer);e.ui.numeric._current._adjustValue(a);e.ui.numeric._current._timer=setTimeout("jQuery.ui.numeric._timerCallback("+a+",false)",b?1E3:50)};var m=RegExp("(\\/|\\.|\\*|\\+|\\?|\\||\\(|\\)|\\[|\\]|\\{|\\}|\\\\)","g");e.formatNumber=function(a,b){a=i(a);if(typeof b==="string")b={format:b};b=e.extend({format:null,decimalChar:".",thousandsChar:","},b);if(typeof b.format!=="string"||
b.format.length<=0)return a.toString();b.decimalChar=typeof b.decimalChar!=="string"||b.decimalChar.length<=0?".":b.decimalChar;b.thousandsChar=typeof b.thousandsChar!=="string"?",":b.thousandsChar;if(b.decimalChar.length>1)throw"NumberFormatException: Can not have multiple characters as the decimal character.";if(b.thousandsChar.length>1)throw"NumberFormatException: Can not have multiple characters as the thousands separator.";var c=b.format.indexOf(b.decimalChar);if(c>=0&&b.format.indexOf(b.decimalChar,
c+1)>=0)throw"NumberFormatException: Format string has multiple decimal characters.";var d=a.toString().replace(/-/g,""),g=b.format.replace(RegExp("[^0#"+j(b.decimalChar)+"]","g"),"");d=d.indexOf(b.decimalChar)<0?[d,""]:d.split(b.decimalChar);g=c<0?[g,""]:g.split(b.decimalChar);if(parseInt(a)===0)d[0]=g[0].indexOf("0")>=0?"0":"";if(b.format.indexOf(b.thousandsChar)>=0&&d[0].length>3){var f=[],h=d[0].length,k=Math.floor(h/3),l=d[0].length%3||3;for(c=0;c<h;c+=l){if(c!=0)l=3;f[f.length]=d[0].substr(c,
l);k-=1}d[0]=f.join(b.thousandsChar)}if(g[1].length>0)if(d[1].length>0){g=g[1].split("");f=d[1].split("");for(c=0;c<f.length;c++){if(c>=g.length)break;g[c]=f[c]}d[1]=g.join("").replace(/#/g,"")}else{c=0;for(d[1]="";g[1].charAt(c)==="0";){d[1]+="0";c++}}else d[1]="";d=d[1].length<=0?d[0]:d[0]+b.decimalChar+d[1];if(a<0)d="-"+d;return b.format.replace(RegExp("[0|#|"+j(b.thousandsChar)+"|"+j(b.decimalChar)+"]+"),d)}})(jQuery);

