(function($){
	
	/*
	Plugin "global" variable
	used to access the currently
	dragged slider in the document
	mousedown/mousemove/mouseup events.
	
	We need to do the entire sliding
	opretion in the document-events
	instead of the events on handle itself
	so that you can drag the slider
	without keeping the mouse ontop of it
	all the time
	*/
	handleDomObj = false;
	
	callbackFactory = function(obj, opts) {
		
		handle = $(".handle", obj);
		domObj = handle.get(0);
		
		/*
		All elements that require custom
		properties have a cordovan object
		created on them for storage of said 
		properties to avoid naming conflicts
		with other plugins
		*/
		
		if(!domObj.cordovan)
			domObj.cordovan = {}
			
		domObj.cordovan.slider = opts;
		domObj.cordovan.slider.oldPos = 0;
		
		// Only selector we use, which should make this
		// decently fast
		handle.mousedown(function(e){
			handleDomObj = this;
		});
		
		// This allows 
		$(obj).click(function(e){
			handle = $(".handle", obj);
			margin = (e.pageX - $(this).offset().left);
			updateMargin(handle.get(0), margin - (handle.width()/2))
			return false;
		});
		
	};
	
	updateMargin = function(domObj, margin) {
		// Wrap it
		handle = $(domObj);
		
		// Width parent (slider background) width
		parentWidth = handle.parent().width();
		
		// Adjust our margin so that it's within bounds (larger then or equal to 0
		// and less then or equal to the width of the parent
		margin = margin > 0 ? (margin > parentWidth ? parentWidth : margin) : 0;
		
		// Set our new margin (moves the elment in the ui)
		handle.css("margin-left", margin);
		
		// If we have a callback function
		if(domObj.cordovan.slider.callback) {
			
			// call it, first argument is the slider background object
			// (the handles parent) and the second argument is the
			// percentage of the slider, i.e 0 - 100%
			domObj.cordovan.slider.callback(handle.parent(), 
				margin <= 0 ? 
					0 // if we are less then or equal to zero our percentage is 0
					: // but if we're not ...
					margin / handle.parent().width() // ... calculate the percentage as 
													 // a float between 0.0 and 1.0
			);
			
		}
		
	};
	
	$(document).mousedown(function(e){
		if(handleDomObj) {
			// Reset the "old position" to the current
			// position so we know where to begin
			handleDomObj.cordovan.slider.oldPos = e.pageX;
			
			// Do not propagate this event further up the chain
			// this will stop the default browser behaviour which
			// will screw us over in certain browsers
			return false;
		}
	});
	
	$(document).mousemove(function(e){
		
		// Only do this if we're actually
		// dragging something otherwise it would be
		// well... stupid.
		if(handleDomObj) {
			
			// Wrap the naked DOMElement in a jquery object for conveniance
			handle = $(handleDomObj);
			
			// Our movement is equal to the current mouse position subtracted with the old mouse position
			movement = e.pageX - handleDomObj.cordovan.slider.oldPos;
			
			// Calculate our new margin value = (old margin parsed to int) + (movement we have made)
			margin = parseInt(handle.css("margin-left")) + movement;
			
			// Move the handle
			updateMargin(handleDomObj, margin);
			
			// Set the current position as the "old" position 
			// for the next event
			handleDomObj.cordovan.slider.oldPos = e.pageX;
			
			// Do not propgate this event further up the chain
			// this stops the default browser behaviour which
			// fucks us up in certain browsers
			return false;
		}
		
	});
	
	$(document).mouseup(function(e){
		// When the mouse is released reset
		// the handleDomObj because we're not dragging
		// anything
		handleDomObj = false;
	});
	
	$.fn.cordovanSlider = function(opts){
		$(this).each(function(n, obj){
			callbackFactory(obj, opts);
		})
	};
	
})(jQuery);
