(function ($) {
	$.fn.extend({
		priceSlider: function(settings) {
			settings = $.extend({
				minValue: 0,
				maxValue: 100000,
				startMin: null,
				startMax: null,
				step: 1000,
				format: function (value) {
					return '$' + (((isNaN(parseFloat(value)) ? 0.00 : parseFloat(value)) < 0) ? '-' : '') + (function (s) { return (s === (s=s.replace(/^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/, '$1$2,$3')) ? s : arguments.callee(s)); })((parseInt((Math.abs((isNaN(parseFloat(value)) ? 0.00 : parseFloat(value))) + .005) * 100) / 100).toString());
				},
				change: function (value, range) {}
			}, settings);
			
			return $(this).each(function() {
				var currentMin = settings.startMin || settings.minValue,
					currentMax = settings.startMax || settings.maxValue,
					onStartMin = currentMin,
					onStartMax = currentMax,
					slide = function(e, ui) {
						currentMin = jQuery(slider).slider('values', 0);
						currentMax = jQuery(slider).slider('values', 1);
						$('em', ui.handle).html(settings.format(ui.value));
					},
					stop = function(e, ui) {
						setTimeout(function() {
							if ((onStartMin !== currentMin) || (onStartMax !== currentMax)) {
								settings.change.apply(slider, [null, [jQuery(slider).slider('values', 0), jQuery(slider).slider('values', 1)]]);
							}
						}, 100);
					},
					start = function() {
						onStartMin = currentMin;
						onStartMax = currentMax;
					},
					slider = $('.priceSlider', this).slider({
						min: settings.minValue,
						max: settings.maxValue,
						step: settings.step,
						range: true,
						slide: slide,
						start: start,
						stop: stop
					});
				
				$(slider).slider('values', 1, currentMax);
				$(slider).slider('values', 0, currentMin);
				$('.min em', slider).html(settings.format(currentMin));
				$('.max em', slider).html(settings.format(currentMax));
				$('.ui-slider-handle', this).css({visibility: 'visible'});
			});
		}
	});
})(jQuery);
