/************************************** Splendid *************************************
 * Created By:		Richard Schrieber
 * Creation Date:	21st June 2007
 * Edited ----------------------------------------------------------------------------
 *      By:    Steve Doggett           On:  5th September 2007
 * Description -----------------------------------------------------------------------
 *      This file is used to give the slider functionality
 *      It's based on the jquery.drag.js, but this one works ok
 *      It's written as a jQuery extension
 *      Uses the JQuery plugin (http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.1.4.pack.js)
 *
 *      splendidSlider:
 *          calculatePosition(e)
 *          calculateValue(pos)
 *          onMouseMove(e)
 *          onMouseUp(e)
 *          splendidSliderSetValues(xVal, yVal)
 *          splendidSliderGetValues()
 *          splendidSliderSetValues(xVal, yVal)
 *
 **************************************************************************************/

/****
 * This is the re-written jQuery.drag.js to actually do all the work
 ****/
(function($) {
	$.fn.splendidSlider = function(settings) {
		
		settings = jQuery.extend({
			onSlide: function() {},
			onChange: function() {},
			handle: this,
			limit: null,
			fractions: null,
			values: [0, 0]
			
		}, settings);
		
		var handle = $(settings.handle);
		var h = {};
		var maxTop = this.height() - handle.height();
		var maxLeft = this.width() - handle.width();
		
		var lastValues = [0, 0];
		
		if (settings.fractions) {
			var fractionsHeight = maxTop/settings.fractions;
			var fractionsWidth  = maxLeft/settings.fractions;
		}
		
		var calculatePosition = function(e) {
			var top = Math.max(h.oY + (settings.limit == "horizontal" ? 0 : e.pageY - h.pY), 0);
			if (top > maxTop) top = maxTop;
			var left = Math.max(h.oX + (settings.limit == "vertical" ? 0 : e.pageX - h.pX), 0);
			if (left > maxLeft) left = maxLeft;
			
			if (settings.fractions) {
				top = fractionsHeight ? Math.round(top/fractionsHeight)*fractionsHeight : 0;
				left = fractionsWidth ? Math.round(left/fractionsWidth)*fractionsWidth : 0;
			}
			
			return [left, top];
		};
		
		var calculateValue = function(pos) {
			return [maxLeft ? pos[0]/maxLeft : 0, maxTop ? pos[1]/maxTop : 0];
		};

		var onMouseMove = function(e) {
			
			var pos = calculatePosition(e);
			
			handle.css({
				"left": pos[0],
				"top":  pos[1]
			});
			var newValues = calculateValue(pos);

			settings.onSlide(newValues[0], newValues[1]);
			
			return false;
		};
		
		var onMouseUp = function (e){
			$().unbind('mousemove', onMouseMove).unbind('mouseup', onMouseUp);
			var newValues = calculateValue(calculatePosition(e));
			
			if (newValues[0] == lastValues[0] && newValues[1] == lastValues[1]) {
				// nothing changed, so no redraw and no Update on changed value
				return;
			}
			lastValues = newValues;
			
			settings.onChange(lastValues[0], lastValues[1]);
		};
		
		handle.bind("mousedown", function(e) {
			
			h = {
				oX: parseInt(handle.css("left")) || 0,
				oY: parseInt(handle.css("top")) || 0,
				pX: e.pageX,
				pY: e.pageY
			};
			
			$().mousemove(onMouseMove).mouseup(onMouseUp);
			return false;
		});
		
		
		this.splendidSliderSetValues = function(xVal, yVal) {
			
			var top = Math.max(maxTop*yVal, 0);
			var left = Math.max(maxLeft*xVal, 0);
			
			if (top > maxTop) top = maxTop;
			if (left > maxLeft) left = maxLeft;
			
			
			if (settings.fractions) {
				top = fractionsHeight ? Math.round(top/fractionsHeight)*fractionsHeight : 0;
				left = fractionsWidth ? Math.round(left/fractionsWidth)*fractionsWidth : 0;
			}
			
			var newValues = calculateValue([left, top]);
			if (newValues[0] == lastValues[0] && newValues[1] == lastValues[1]) {
				// nothing changed, so no redraw and no Update on changed value
				return;
			}
			
			lastValues = newValues;
			
			handle.css({
				"left": left,
				"top":  top
			});


			settings.onChange(lastValues[0], lastValues[1]);
		};
		
		this.splendidSliderGetValues = function() {
			return lastValues;
		};
		
		this.splendidSliderSetValues(settings.values[0], settings.values[1]);
		
		return this;
	};

})(jQuery);