(function($) {

	$.fn.slider3 = function(options) 
	{

		var opts = $.extend({}, $.fn.slider3.defaults, options);

		return this.each(function() 
		{
			var $select = $(this);
			var _select = this;
			_select.ticks = new Array();

			var $container = $('<div class="slider"><img src="/us/p2images/common/slider-bar.gif" class="bar" alt="" /><span class="range"></span></div>').insertAfter($select);
			var $handles = $('<span class="left"><span class="label"></span><span class="anchor"></span></span><span class="right"><span class="label"></span><span class="anchor"></span></span><br/><br/><div class="hiddenobj">L:<span class="LValue" name="LValue" id="LValue"></span><br/>R:<span class="RValue" name="RValue" id="RValue"></span><input class="LIDX" name="LIDX" id="LIDX" value=""/><input class="RIDX" name="RIDX" id="RIDX" value=""/></div>').appendTo($container);

			var $left = $handles.filter('.left');
			var $right = $handles.filter('.right')
			var $range = $container.find('.range');

			var $leftVal = $container.find('.LValue');
			var $rightVal = $container.find('.RValue');
			var $LIDX = $container.find('.LIDX');
			var $RIDX = $container.find('.RIDX');

			var _width = $container.width();
			var _handleWidth = $handles.width();
			var _offset = opts.tweaks.margin + (_handleWidth/2);
			
			var _min = Infinity;
			var _max = 0;

			$select.find('option').each(function(i)
			{
				_select.ticks[i] = {value:i,pixel:0};
			});
	
			_min = 0;
			_max = ($select.find('option').length-1);

			var _factor = (_max-_min)/_width;
			
			for(var i=0; i < _select.ticks.length; i++)
			{
				_select.ticks[i].pixel = Math.round((_select.ticks[i].value-_min)/_factor);
				$container.prepend('<span class="tick" style="left:'+_select.ticks[i].pixel+'px;"></span>');
			}

			$left.attr('slidermin',0-_handleWidth).attr('slidermax',_width-_handleWidth).attr('slidervalue',_min).find('.label').text($select.find('option')[_min].text);
			$leftVal.text($select.find('option')[_min].text);
			$LIDX.val(_min);

			$right.attr('slidermin',0).attr('slidermax',_width).attr('slidervalue',_max).find('.label').text($select.find('option')[_max].text);
			$rightVal.text($select.find('option')[_max].text);
			$RIDX.val(_max);

			$container.mousemove(function(e)
			{
				_offset = (getRealOffsetLeft(document.getElementById("content_p2"))+64) + (_handleWidth/2);

				if($left.find('span.anchor').get(0).clicked)
				{
					if(parseInt(e.pageX-_offset) >= parseInt($left.attr('slidermin')) && parseInt(e.pageX-_offset) <= parseInt($left.attr('slidermax')))
					//if(parseInt(e.pageX) >= parseInt($left.attr('slidermin')) && parseInt(e.pageX) <= parseInt($left.attr('slidermax')))
					{
						if(parseInt(e.pageX-_offset) <= parseInt($right.css('left').replace('px',''))-_handleWidth)
						{
							$left.get(0).style.left = e.pageX-_offset+"px";

							var _value = snapTo(parseInt($left.get(0).style.left)+_handleWidth,_select.ticks).value;

							$left.attr('slidervalue',_value).find('.label').text($select.find('option')[_value].text);
							$range.trigger('slider:change');

							$leftVal.text($select.find('option')[_value].text);
							$LIDX.val(_value);
							
						}
					}
				}

				if($right.find('span.anchor').get(0).clicked)
				{
					if(parseInt(e.pageX-_offset) >= parseInt($right.attr('slidermin')) && parseInt(e.pageX-_offset) <= parseInt($right.attr('slidermax')))
					{
						if(parseInt(e.pageX-_offset) >= parseInt($left.css('left').replace('px',''))+_handleWidth)
						{
							$right.get(0).style.left = e.pageX-_offset+"px";

							var _value = snapTo($right.get(0).style.left,_select.ticks).value;

							$right.attr('slidervalue',_value).find('.label').text($select.find('option')[_value].text);
							$range.trigger('slider:change');

							$rightVal.text($select.find('option')[_value].text);
							$RIDX.val(_value);
						}
					}
				}
			});

			$handles.find('span.anchor').mousedown(function()
			{
				this.clicked = true;
			});

			$('body').bind('mouseup',function()
			{
				$handles.find('span.anchor').trigger('slider:release');
			});

			$container.hover(function(){},function()
			{
				$handles.find('span.anchor').trigger('slider:release');
			});

			$handles.find('span.anchor').bind('slider:release',function()
			{
				if(this.clicked)
				{
					this.clicked = false;

					var $handle = $(this).parent('span');
					var _value = $handle.attr('slidervalue');
					var _pixel = null;

					for(var i=0; i < _select.ticks.length; i++)
					{
						(_select.ticks[i].value == _value) ? _pixel = _select.ticks[i].pixel : null;
					}

					var _side = ($handle.hasClass('left')) ? 'left' : 'right';

					if(_pixel != null)
					{
						(_side == 'left') ? $handle.get(0).style.left = (_pixel-(_handleWidth-opts.tweaks.left))+"px" : $handle.get(0).style.left = (_pixel-opts.tweaks.right)+"px";
						
						$handle.attr('slidervalue',_value).find('.label').text($select.find('option')[_value].text);

						$range.trigger('slider:change');
						try { changeSlider($select, $LIDX, $RIDX); } catch (e) {}
					}
				}
			});

			$range.bind('slider:change',function()
			{
				$range.css({left:parseInt($left.css('left'))+5,width:parseInt($right.css('left'))-parseInt($left.css('left'))});
			});

			$container.bind('slider:reset',function()
			{
				$left.attr('slidervalue',_min).css({left:-(_handleWidth-opts.tweaks.left)}).find('.label').text($select.find('option')[_min].text);
				$right.attr('slidervalue',_max).css({left:_width-opts.tweaks.right}).find('.label').text($select.find('option')[_max].text);

				$leftVal.text($select.find('option')[_min].text);
				$LIDX.val(_min);

				$rightVal.text($select.find('option')[_max].text);
				$RIDX.val(_max);

				$range.trigger('slider:change');
			});

			$container.bind('slider:setValue', function(event, _TValue) 
			{
				var TmpV = _TValue.split("‡");
				var _leftX = _min, _rightX = _max;
				var _lvX = TmpV[0], _rvX = TmpV[1];
				var _Leftpixel = null;
				var _Rigtpixel = null;

				if (_lvX == "") { _leftX = _min; }
				if (_rvX == "") { _rightX = _max; }

				$select.find("option").each(function(i){
					if ($select.find("option").eq(i).val() == _lvX) {
						_leftX = i;
					}
					if ($select.find("option").eq(i).val() == _rvX) {
						_rightX = i;
					}
				});
				
				$left.attr('slidermin',0-_handleWidth).attr('slidermax',_width-_handleWidth).attr('slidervalue',_leftX).find('.label').text($select.find('option')[_leftX].text);
				$leftVal.text($select.find('option')[_leftX].text);
				$LIDX.val(_leftX);

				$right.attr('slidermin',0).attr('slidermax',_width).attr('slidervalue',_rightX).find('.label').text($select.find('option')[_rightX].text);
				$rightVal.text($select.find('option')[_rightX].text);
				$RIDX.val(_rightX);


				for(var i=0; i < _select.ticks.length; i++)
				{
					(_select.ticks[i].value == _leftX) ? _Leftpixel = _select.ticks[i].pixel : null;
					(_select.ticks[i].value == _rightX) ? _Rigtpixel = _select.ticks[i].pixel : null;
				}

				$left.get(0).style.left = (_Leftpixel - _handleWidth) +"px";
				$right.get(0).style.left = (_Rigtpixel+1) +"px";

				$range.trigger('slider:change');
			});

			$container.find('.bar,.range,.tick').click(function(e)
			{	
				return;
				var _snap = snapTo(parseInt(e.pageX-opts.tweaks.margin),_select.ticks);

				if(_snap != null)
				{	
					if(Math.abs(parseInt($left.css('left'))-_snap.pixel) <  Math.abs(parseInt($right.css('left'))-_snap.pixel))
					{
						$left.get(0).style.left = (_snap.pixel-(_handleWidth-opts.tweaks.left))+"px";
						$left.attr('slidervalue',_snap.value).find('.label').text(_snap.value);
					}
					else
					{
						$right.get(0).style.left = (_snap.pixel-opts.tweaks.right)+"px";
						$right.attr('slidervalue',_snap.value).find('.label').text(_snap.value);
					}

					$range.trigger('slider:change');
				}
			});
		});
	};
	
	function snapTo(left,ticks)
	{
		var _candidate = null;
		var _diff = Infinity;

		for(var i=0; i < ticks.length; i++)
		{
			if(Math.abs(ticks[i].pixel-parseInt(left)) < _diff)
			{
				_diff = Math.abs(ticks[i].pixel-parseInt(left));
				_candidate = ticks[i];
			}
		}

		return _candidate;
	};

	function getRealOffsetLeft(el) {
		return el ? el.offsetLeft + getRealOffsetLeft(el.offsetParent) : 0;
	};

	$.fn.slider3.defaults = 
	{
		tweaks: {left:0, right:-1, margin: getRealOffsetLeft(document.getElementById("content_p2"))+76}
	};

})(jQuery);
