// JavaScript Document
var _startX = 0; // mouse starting positions 
var _startY = 0; 
var _offsetX = 0; // current element offset 
var _offsetY = 0; 
var _dragElement; // needs to be passed from OnMouseDown to OnMouseMove 
var _dragType; // needs to be passed from OnMouseDown to OnMouseMove 
var _oldZIndex = 0; // we temporarily increase the z-index during drag 

InitDragDrop(); 
function InitDragDrop() { 
  document.onmousedown = OnMouseDown; 
  document.onmouseup = OnMouseUp; 
}

function OnMouseDown(e) 
{ 
  // IE doesn't pass the event object 
  if (e == null) e = window.event; 
  
  // IE uses srcElement, others use target
  var target = e.target != null ? e.target : e.srcElement; 
  
  // for IE, left click == 1 
  // for Firefox, left click == 0 
  if ((e.button == 1 && window.event != null || e.button == 0) && (target.className == 'slideMin' || target.className == 'slideMax')) { 
  
    // grab the mouse position 
    _startX = e.clientX; 
	//_startY = e.clientY; 
	
	// grab the clicked element's position 
	_offsetX = ExtractNumber(target.style.left); 
	//_offsetY = ExtractNumber(target.style.top); 
	
	// bring the clicked element to the front while it is being dragged 
	_oldZIndex = target.style.zIndex; target.style.zIndex = 10000; 
	
	
	// we need to access the element in OnMouseMove
	if(target.className == 'slideMin') _dragType = "mini";
	if(target.className == 'slideMax') _dragType = "maxi";
	_dragElement = target; 
	
	switch (_dragElement.parentNode.id){
		case "caratSlider": 
		_dragValues = carat;
		break;
		case "priceSlider": 
		_dragValues = price;
		break;
		case "depthSlider": 
		_dragValues = depth;
		break;
		case "tableSlider": 
		_dragValues = table;
		break;
		default: return false;
	}
	
	// tell our code to start moving the element with the mouse 
	document.onmousemove = OnMouseMove; 
	
	// cancel out any text selections 
	document.body.focus(); 
	
	// prevent text selection in IE 
	document.onselectstart = function () { return false; }; 
	// prevent IE from trying to drag an image 
	target.ondragstart = function() { return false; }; 
	// prevent text selection (except IE) 
	return false;
  } 
	
}


function OnMouseMove(e) 
{ 
  if (e == null) var e = window.event; 
	
	// this is the actual "drag code" 
	if (_dragType == "mini"){
	  lower = 0;
      upper =_dragValues.upper-22;
	} else if (_dragType == "maxi"){
		lower = _dragValues.lower+22;
        upper = 215;
		
	}
    leftval = _offsetX + e.clientX - _startX;
    leftval = Math.min(leftval,upper);
    leftval = Math.max(lower,leftval);
	
	_dragElement.style.left = leftval+"px";
	if (_dragType == "mini"){
	  _dragValues.minBox.value =_dragValues.setSlideValueMin(leftval);
	  _dragValues.presMin = _dragValues.minBox.value;
	  _dragValues.currentMin = leftval;
	} else if (_dragType == "maxi"){
		_dragValues.maxBox.value =_dragValues.setSlideValueMax(leftval);
		_dragValues.presMax = _dragValues.maxBox.value;
		_dragValues.currentMax = leftval;
	}
	
	//_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px'; 
	//_dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';  
}


function OnMouseUp(e) { 
  if (_dragElement != null) { 
    _dragElement.style.zIndex = _oldZIndex; 
	
	// we're done with these events until the next OnMouseDown 
	document.onmousemove = null; 
	document.onselectstart = null;
	sValueString=_dragElement.style.left.toString();
	sValueString = sValueString.replace(/px/, "");
	iValue = parseInt(sValueString,10);
	if (_dragType == "mini"){
	  _dragValues.lower = iValue;
	} else if (_dragType == "maxi"){
		_dragValues.upper = iValue;
	}
	
	_dragElement.ondragstart = null; 
	// this is how we know we're not dragging 
	_dragElement = null; 
	_dragType = null;
	//_dragValues = null;
	
	if (do_ajax >0) {pagenbr=1;ajaxCall();}
  } 
}

function ExtractNumber(value) { 
  var n = parseInt(value); 
  return n == null || isNaN(n) ? 0 : n; 
} 

// this is simply a shortcut for the eyes and fingers 
function $(id) { 
  return document.getElementById(id); 
}


