/** 
 * @license jQuery UI Spinner 1.20
 *
 * Copyright (c) 2009-2010 Brant Burnett
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * This file is part of MoonCake, an Admin template build for sale at ThemeForest.
 * For questions, suggestions or support request, please mail me at maimairel@yahoo.com
 * Modified for use in MoonCake while waiting for the final version of jQuery-UI 1.9 Spinner
 *
 * Changes:
 * 1. Removed the native mousewheel support to use Brandon Aaron's mousewheel plugin
 * 2. Removed inline css for the input
 * 3. Put the input inside the wrapper
 * 4. Converted all class names for Bootstrap use
 *
 */

;(function($, undefined) {

var 
	keyCode = $.ui.keyCode,
	up = keyCode.UP,
	down = keyCode.DOWN,
	right = keyCode.RIGHT,
	left = keyCode.LEFT,
	pageUp = keyCode.PAGE_UP,
	pageDown = keyCode.PAGE_DOWN,
	home = keyCode.HOME,
	end = keyCode.END,
	
	msie = $.browser.msie,
	
	// namespace for events on input
	eventNamespace = '.uispinner', 
	
	// only these special keys will be accepted, all others will be ignored unless CTRL or ALT are pressed
	validKeys = [up, down, right, left, pageUp, pageDown, home, end, keyCode.BACKSPACE, keyCode.DELETE, keyCode.TAB],
	
	// stores the currently focused spinner
	// Note: due to oddities in the focus/blur events, this is part of a two-part system for confirming focus
	// this must set to the control, and the focus variable must be true
	// this is because hitting up/down arrows with mouse causes focus to change, but blur event for previous control doesn't fire
	focusCtrl;
	
$.widget('ui.spinner', {
	options: {
		min: null,
		max: null,
		allowNull: false, 
		
		group: '',
		point: '.',
		prefix: '',
		suffix: '',
		places: null, // null causes it to detect the number of places in step
		
		defaultStep: 1, // real value is 'step', and should be passed as such.  This value is used to detect if passed value should override HTML5 attribute
		largeStep: 10,
		mouseWheel: true,
		increment: 'slow',
		showOn: 'always',
		upIconClass: "icon-caret-up",
		downIconClass: "icon-caret-down",
		
		format: function(num, places) {
			var options = this,
				regex = /(\d+)(\d{3})/,
				result = ((isNaN(num) ? 0 : Math.abs(num)).toFixed(places)) + '';
				
			for (result = result.replace('.', options.point); regex.test(result) && options.group; result=result.replace(regex, '$1'+options.group+'$2')) {};
			return (num < 0 ? '-' : '') + options.prefix + result + options.suffix;
		},
		
		parse: function(val) {
			var options = this;
			
			if (options.group == '.')
				val = val.replace('.', '');
			if (options.point != '.')
				val = val.replace(options.point, '.');
			return parseFloat(val.replace(/[^0-9\-\.]/g, ''));
		}
	},
	
	// * Widget fields *
	// curvalue - current value
	// places - currently effective number of decimal places
	// counter - number of spins at the current spin speed
	// incCounter - index within options.increment of the current spin speed
	// selfChange - indicates that change event is being fired by the widget, so don't reprocess input value
	// focused - this spinner currently has the focus

	_create: function() {
		// shortcuts
		var self = this,
			input = self.element,
			type = input.attr('type');
			
		if (!input.is('input') || ((type != 'text') && (type != 'number'))) {
			console.error('Invalid target for ui.spinner');
			return;
		}
		
		self._procOptions();
		self._createButtons(input);

        input.addClass('spinner-input');

		if (!input.is(':enabled'))
			self.disable();
	},
	
	_createButtons: function(input) {
		var self = this,
			options = self.options,
			showOn = options.showOn,
			box = $.support.boxModel,
			wrapper = self.wrapper = input
				.wrap('<span class="spinner-container"></span>').parent(),
			btnContainer = self.btnContainer = $(
				'<span class="spinner-btn-group btn-group btn-group-vertical">' + 
					'<span class="btn-spinner-up btn"><span class="'+options.upIconClass+'"></span></span>' + 
					'<span class="btn-spinner-down btn"><span class="'+options.downIconClass+'"></span></span>' + 
				'</span>'),

			// object shortcuts
			upButton, downButton, buttons, icons,

			hoverDelay,
			hoverDelayCallback,
			
			// current state booleans
			hovered, inKeyDown, inSpecialKey, inMouseDown,
						
			// used to reverse left/right key directions
			rtl = input[0].dir == 'rtl';

		wrapper.append(btnContainer);
		
		buttons = self.buttons = btnContainer.find('.btn');
		upButton = buttons[0];
		downButton = buttons[1];
		
		// apply className before doing any calculations because it could affect them
        if (input.attr("class") !== undefined) {
            wrapper.addClass(input.attr("class"));
        }

		// set width of btnContainer to be the same as the buttons
		if (showOn != 'always')
			btnContainer.css('opacity', 0);
		
		/* Event Bindings */

		// bind hover events to show/hide buttons
		if (showOn == 'hover' || showOn == 'both')
			buttons.add(input)
				.bind('mouseenter' + eventNamespace, function() {
					setHoverDelay(function() {
						hovered = true;
						if (!self.focused || (showOn == 'hover')) // ignore focus flag if show on hover only
							self.showButtons();
					});
				})
				
				.bind('mouseleave' + eventNamespace, function hoverOut() {
					setHoverDelay(function() {
						hovered = false;
						if (!self.focused || (showOn == 'hover')) // ignore focus flag if show on hover only
							self.hideButtons();
					});
				});

	
		buttons
			.mousedown(mouseDown)
			.mouseup(mouseUp)
			.mouseout(mouseUp);
			
		if (msie)
			// fixes dbl click not firing second mouse down in IE
			buttons.dblclick(function() {
					if (!options.disabled) {
						// make sure any changes are posted
						self._change();
						self._doSpin((this === upButton ? 1 : -1) * options.step);
					}
					
					return false;
				}) 
				
				// fixes IE8 dbl click selection highlight
				.bind('selectstart', function() {return false;});
				
		input.bind('keydown' + eventNamespace, function(e) {
					var dir, large, limit,
						keyCode = e.keyCode; // shortcut for minimization
					if (e.ctrl || e.alt) return true; // ignore these events
					
					if (isSpecialKey(keyCode))
						inSpecialKey = true;
					
					if (inKeyDown) return false; // only one direction at a time, and suppress invalid keys
					
					switch (keyCode) {
						case up:
						case pageUp:
							dir = 1;
							large = keyCode == pageUp;
							break;
							
						case down:
						case pageDown:
							dir = -1;
							large = keyCode == pageDown;
							break;
							
						case right:
						case left:
							dir = (keyCode == right) ^ rtl ? 1 : -1;
							break;
							
						case home:
							limit = self.options.min;
							if (limit != null) self._setValue(limit);
							return false;
							
						case end:
							limit = self.options.max;
							limit = self.options.max;
							if (limit != null) self._setValue(limit);
							return false;
					}
					
					if (dir) { // only process if dir was set above
						if (!inKeyDown && !options.disabled) {
							keyDir = dir;
	
							inKeyDown = true;
							self._startSpin(dir, large);
						}
						
						return false;
					}
				})
				
			.bind('keyup' + eventNamespace, function(e) {
					if (e.ctrl || e.alt) return true; // ignore these events
					
					if (isSpecialKey(keyCode))
						inSpecialKey = false;
					
					switch (e.keyCode) {
						case up:
						case right:
						case pageUp:
						case down:
						case left:
						case pageDown:
							self._stopSpin();
							inKeyDown = false;
							return false;
					}
				})
				
			.bind('keypress' + eventNamespace, function(e) {
					if (invalidKey(e.keyCode, e.charCode)) return false;
				})
				
			.bind('change' + eventNamespace, function() { self._change(); })
			
			.bind('focus' + eventNamespace, function() {
					function selectAll() {
						self.element.select();
					}

					msie ? selectAll() : setTimeout(selectAll, 0); // add delay for Chrome, but breaks IE8
					self.focused = true;
					focusCtrl = self;
					if (!hovered && (showOn == 'focus' || showOn == 'both')) // hovered will only be set if hover affects show
						self.showButtons();
				})
				
			.bind('blur' + eventNamespace, function() {
					self.focused = false;
					if (!hovered && (showOn == 'focus' || showOn == 'both')) // hovered will only be set if hover affects show
						self.hideButtons();
				});
				
		function isSpecialKey(keyCode) {
			for (var i=0; i<validKeys.length; i++) // predefined list of special keys
				if (validKeys[i] == keyCode) return true;
				
			return false;
		}
			
		function invalidKey(keyCode, charCode) {
			if (inSpecialKey) return false;				
			
			var ch = String.fromCharCode(charCode || keyCode),
				options = self.options;
				
			if ((ch >= '0') && (ch <= '9') || (ch == '-')) return false;
			if (((self.places > 0) && (ch == options.point))
				|| (ch == options.group)) return false;
						
			return true;
		}
		
		// used to delay start of hover show/hide by 100 milliseconds
		function setHoverDelay(callback) {
			if (hoverDelay) {
				// don't do anything if trying to set the same callback again
				if (callback === hoverDelayCallback) return;
				
				clearTimeout(hoverDelay);
			}
			
			hoverDelayCallback = callback;
			hoverDelay = setTimeout(execute, 100);
			
			function execute() {
				hoverDelay = 0;
				callback();
			}
		}
			
		function mouseDown() {
			if (!options.disabled) {
				var input = self.element[0],
					dir = (this === upButton ? 1 : -1);
					
				input.focus();
				input.select();
				
				inMouseDown = true;
				self._startSpin(dir);
			}

			return false;
		}
		
		function mouseUp() {
			if (inMouseDown) {
				self._stopSpin();
				inMouseDown = false;
			}
			return false;
		}
	},
	
	_procOptions: function() {
		var self = this,
			input = self.element,
			options = self.options,
			min = options.min,
			max = options.max,
			step = options.step,
			places = options.places,
			temp;
			
		// setup increment based on speed string
		if (options.increment == 'slow')
			options.increment = [{count: 1, mult: 1, delay: 250},
								 {count: 3, mult: 1, delay: 100},
								 {count: 0, mult: 1, delay: 50}];
		else if (options.increment == 'fast')
			options.increment = [{count: 1, mult: 1, delay: 250},
								 {count: 19, mult: 1, delay: 100},
								 {count: 80, mult: 1, delay: 20},
								 {count: 100, mult: 10, delay: 20},
								 {count: 0, mult: 100, delay: 20}];

		if ((min == null) && ((temp = input.attr('min')) != null))
			min = parseFloat(temp);
		
		if ((max == null) && ((temp = input.attr('max')) != null))
			max = parseFloat(temp);
		
		if (!step && ((temp = input.attr('step')) != null))
			if (temp != 'any') {
				step = parseFloat(temp);
				options.largeStep *= step;
			}
		options.step = step = step || options.defaultStep;

		// Process step for decimal places if none are specified
		if ((places == null) && ((temp = step + '').indexOf('.') != -1))
			places = temp.length - temp.indexOf('.') - 1;
		self.places = places;
		
		if ((max != null) && (min != null)) {
			// ensure that min is less than or equal to max
			if (min > max) min = max;
		}
					
		options.min = min;
		options.max = max;
		
		// ensures that current value meets constraints
		self._change();
		
		if (options.mouseWheel && $.fn.mousewheel) {
			input.mousewheel(self._mouseWheel);
		}
	},
		
	_mouseWheel: function(e, delta) {
		var self = $.data(this, 'spinner');
		if (!self.options.disabled && self.focused && (focusCtrl === self)) {
			// make sure changes are posted
			self._change();
			self._doSpin(delta * self.options.step);
			return false;
		}
	},
	
	// sets an interval to call the _spin function
	_setTimer: function(delay, dir, large) {
		var self = this;
		self._stopSpin();
		self.timer = setInterval(fire, delay);
		
		function fire() {
			self._spin(dir, large);
		}
	},
	
	// stops the spin timer
	_stopSpin: function() {
		if (this.timer) {
			clearInterval(this.timer);
			this.timer = 0;
		}
	},
	
	// performs first step, and starts the spin timer if increment is set
	_startSpin: function(dir, large) {
		// shortcuts
		var self = this,
			options = self.options,
			increment = options.increment;
			
		// make sure any changes are posted
		self._change();
		self._doSpin(dir * (large ? self.options.largeStep : self.options.step));
		
		if (increment && increment.length > 0) {		
			self.counter = 0;
			self.incCounter = 0;
			self._setTimer(increment[0].delay, dir, large);
		}
	},
	
	// called by timer for each step in the spin
	_spin: function(dir, large) {
		// shortcuts
		var self = this,
			increment = self.options.increment,
			curIncrement = increment[self.incCounter];
		
		self._doSpin(dir * curIncrement.mult * (large ? self.options.largeStep : self.options.step));
		self.counter++;

		if ((self.counter > curIncrement.count) && (self.incCounter < increment.length-1)) {
			self.counter = 0;
			curIncrement = increment[++self.incCounter];
			self._setTimer(curIncrement.delay, dir, large);
		}
	},
	
	// actually spins the timer by a step
	_doSpin: function(step) {
		// shortcut
		var self = this,
			value = self.curvalue;
			
		if (value == null)
			value = (step > 0 ? self.options.min : self.options.max) || 0;
		
		self._setValue(value + step);
	},
	
	// Parse the value currently in the field
	_parseValue: function() {
		var value = this.element.val();
		return value ? this.options.parse(value, this.element) : null;
	},
	
	_validate: function(value) {
		var options = this.options,
			min = options.min,
			max = options.max;

		if ((value == null) && !options.allowNull)
			value = this.curvalue != null ? this.curvalue : min || max || 0; // must confirm not null in case just initializing and had blank value

		if ((max != null) && (value > max))
			return max;
		else if ((min != null) && (value < min))
			return min;
		else
			return value;
	},
	
	_change: function() {
		var self = this, // shortcut
			value = self._parseValue(),
			min = self.options.min,
			max = self.options.max;
			
		// don't reprocess if change was self triggered
		if (!self.selfChange) {
			if (isNaN(value))
				value = self.curvalue;

			self._setValue(value, true);
		}
	},
	
	// overrides _setData to force option parsing
	_setOption: function(key, value) {
		$.Widget.prototype._setOption.call(this, key, value);
		this._procOptions();
	},
	
	increment: function() {
		this._doSpin(this.options.step);
	},
	
	decrement: function() {
		this._doSpin(-this.options.step);
	},
	
	showButtons: function(immediate) {
		var btnContainer = this.btnContainer.stop();
		if (immediate)
			btnContainer.css('opacity', 1);
		else
			btnContainer.fadeTo('fast', 1);
	},
	
	hideButtons: function(immediate) {
		var btnContainer = this.btnContainer.stop();
		if (immediate)
			btnContainer.css('opacity', 0);
		else
			btnContainer.fadeTo('fast', 0);
		this.buttons.removeClass(hover);
	},
	
	// Set the value directly
	_setValue: function(value, suppressFireEvent) {
		var self = this;
		
		self.curvalue = value = self._validate(value);
		self.element.val(value != null ? 
			self.options.format(value, self.places, self.element) :
			'');
		
		if (!suppressFireEvent) {
			self.selfChange = true;
			self.element.change();
			self.selfChange = false;
		}
	},

	// Set or retrieve the value
	value: function(newValue) {
		if (arguments.length) {
			this._setValue(newValue);
			
			// maintains chaining
			return this.element;
		}

		return this.curvalue;
	},

	enable: function() {
		this.buttons.removeClass(disabled);
		this.element[0].disabled = false;
		$.Widget.prototype.enable.call(this);
	},
	
	disable: function() {
		this.buttons.addClass(disabled)
			// in case hover class got left on
			.removeClass(hover);
			
		this.element[0].disabled = true;
		$.Widget.prototype.disable.call(this);
	},
	
	destroy: function(target) {
		this.wrapper.remove();
		this.element.unbind(eventNamespace).removeClass('spinner-input');
		
		if ($.fn.mousewheel) {
			this.element.unmousewheel();
		}		
		
		$.Widget.prototype.destroy.call(this);
	}	
});

})( jQuery );
