document.write("<link href='/www2009/slider/css/main.css' rel='stylesheet' type='text/css' />");
var currentCursorObj=null;//当前使用的游标
var callBackResult=new Object();//返回值对像
var callMouseUp=null;
//鼠标点击游标,将此游标激活成当前游标
function sliderDowning(e)
{
	e=e || window.event;
	currentCursorObj=e.srcElement || e.target;
}
//松开鼠标,当前游标设为空
function sliderUping(mouseupActiva)
{
	if(currentCursorObj && mouseupActiva)
	{
		mouseupActiva.call(window,callBackResult);
	}
	else if(currentCursorObj && callMouseUp)
	{
		callMouseUp.call(window,callBackResult);
	}
	callMouseUp=null;
	currentCursorObj=null;
}
//获取鼠标的坐标
function getMouseCoordinate(ev){
  if(ev.pageX || ev.pageY){
    return {x:ev.pageX, y:ev.pageY};
  }
  return {
    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
    y:ev.clientY + document.body.scrollTop - document.body.clientTop
  };
}
function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
	return offset;
}
(function($){
	$.fn.slider=function(options){
		var id='';
		var minDefaultLeft=0;//最小值的left值
		var maxDefaultLeft=0;//最大值的left值
		this.each(function(){
			id=this.id;
		});
		options=$.extend({
			minValue:0,
			maxValue:100,
			minDefaultValue:null,
			maxDefaultValue:null,
			width:121,
			callback:null,
			mouseup:null
		},options);
		options.minDefaultValue=(options.minDefaultValue==null || options.minDefaultValue<options.minValue || options.minDefaultValue>=options.maxValue?options.minValue:options.minDefaultValue);
		options.maxDefaultValue=(options.maxDefaultValue==null || options.maxDefaultValue>options.maxValue || options.maxDefaultValue<=options.minValue?options.maxValue:options.maxDefaultValue);
		
		callBackResult.min=options.minDefaultValue;
		callBackResult.max=options.maxDefaultValue;
		
		minDefaultLeft=Math.round((options.minDefaultValue-options.minValue)/(options.maxValue-options.minValue)*options.width);
		maxDefaultLeft=Math.round((options.maxDefaultValue-options.minValue)/(options.maxValue-options.minValue)*options.width);
		//鼠标移动事件
		var sliderMoving=function(e)
		{
			if(currentCursorObj)
			{
				var scaleObj=currentCursorObj.parentNode.childNodes[0].childNodes[0];//刻度尺
				var scaleWidth=scaleObj.offsetWidth;//刻度尺的宽度
				var cursorWidth=currentCursorObj.offsetWidth;
				var moveValue=getMouseCoordinate(e).x-getLeft(scaleObj);//移动的距离
				if(moveValue>scaleWidth){callMouseUp=options.mouseup;moveValue=scaleWidth};
				if(moveValue<0){callMouseUp=options.mouseup;moveValue=0;}
				currentCursorObj.style.left=(scaleObj.offsetLeft-cursorWidth/2+moveValue)+"px";
				var minPoint=(getLeft($('#cursor1_'+id)[0])-getLeft(scaleObj)+cursorWidth/2)/scaleWidth;
				var maxPoint=(getLeft($('#cursor2_'+id)[0])-getLeft(scaleObj)+cursorWidth/2)/scaleWidth;
				if(minPoint>maxPoint)
				{
					var temp;
					temp=minPoint;
					minPoint=maxPoint;
					maxPoint=temp;
				}
				var minValue=Math.round(options.minValue+(options.maxValue-options.minValue)*minPoint);
				var maxValue=Math.round(options.minValue+(options.maxValue-options.minValue)*maxPoint);
				callBackResult.min=minValue;
				callBackResult.max=maxValue;
				if(options.callback)//回调函数
				{
					options.callback.call(window,callBackResult);
				}
			}
		};
		var str = '<div class="slider">'
			+ '<div class="line_main"><div class="scale" id="scale_'+id+'" style="width: '+options.width+'px;"></div></div>'
			+ '<div class="cursor1" id="cursor1_'+id+'" onmousedown="sliderDowning(arguments[0])" onmouseup="sliderUping('+options.mouseup+')"></div><div class="cursor2" id="cursor2_'+id+'" onmousedown="sliderDowning(arguments[0])" onmouseup="sliderUping('+options.mouseup+')"></div>'
			+ '</div>';
		return this.each(function(){
			$(this).html(str).mousemove(sliderMoving);
			$('#cursor1_'+id)[0].style.left=($('#scale_'+id)[0].offsetLeft-$('#cursor1_'+id)[0].offsetWidth/2+minDefaultLeft)+"px";//设置第一个游标的默认值
			$('#cursor2_'+id)[0].style.left=($('#scale_'+id)[0].offsetLeft-$('#cursor2_'+id)[0].offsetWidth/2+maxDefaultLeft)+"px";//设置第二个游标的默认值
			document.onmouseup=sliderUping;
		});
	};
})(jQuery)