// ver 1.10 2005-05-03 tac
//
var sc_isIE = document.all != null;

function esiScroller(name,w,vmin,vmax,pval,pgInc,cbfn)
{
  this.id = name;
  this.min = vmin;
  this.max = vmax;
  this.cbf = cbfn;
  if (pval < vmin) pval = vmin;
  if (pval > vmax) pval = vmax;
  this.val = pval;
  this.page = pgInc;
  this.width = w-16;
  this.pos = Math.round(this.width * (this.val- this.min) / (this.max - this.min));
  this.imageDir = "../resources/images";
 
	this.DragDelta = function () {
		if ( sc_isIE ) 
			return this.ieDelta;
		else
			return this.nsDelta;
	}
	
	this.DragWith = function () {
		return 16;
	}
	
	this.SetUpDrag = function (nsDelta, ieDelta) {
		this.nsDelta = nsDelta;
		this.ieDelta = ieDelta;		
	}
	
	this.SetUpDragAll = function (delta) {
		this.nsDelta = delta;
		this.ieDelta = delta;
		
	}

  this.Value = function () {
	if (arguments.length > 0) {
		var n = arguments[0];
		if (n < this.min) n = this.min;
		if (n > this.max) n = this.max;
		this.val = n;
		this.pos = Math.round(this.width * (this.val- this.min) / (this.max - this.min));
		document.getElementById(this.id + "_mk").style.left = this.pos +"px";
	}
	return this.val;
  }

	this.ImageDir = function(){
		if(arguments.length == 0)
			return this.imageDir;
		else
			this.imageDir = (arguments[0]);
	}
	
  this.SetPos = function (x) {
    document.getElementById(this.id + "_mk").style.left = x +"px";
    this.pos = x;
    var v = Math.round(this.pos * (this.max - this.min) / this.width) + this.min;
    if (v != this.val) { this.val = v; this.OnChange();}
  }
  
  this.SnapToPage = function () {
    var r = this.val % this.page;
    if (r > 0) {
      var n = this.val;
	  if (r > this.page/2)
		n += this.page-r;
	else
		n -= r;
	  if (n < this.min) n = this.min;
	  if (n > this.max) n = this.max;
	  if (n != this.val) {
	    this.val = n;
    this.pos = Math.round(this.width * (this.val- this.min) / (this.max - this.min));   
        document.getElementById(this.id + "_mk").style.left = this.pos +"px";
    this.OnChange();
        return true;
      }
    }
    return false;
  }
  
  this.OnChange = function () {
    if (this.cbf != null)
		this.cbf(this);
  }
  
	this.Init = function (){
			var bt = null;
			bt = document.getElementById(this.id + "_mk");
			if( bt != null){
				bt.style.left = this.pos +"px";
				bt.style.top = "0px";
				bt.style.position = "relative";
			}
	
		this.nsDelta = 0;
		this.ieDelta = 0;
	
	}
}

var e_X,e_dragElm = null,e_mxo = 0;

function esiScrl_NSgetOffX(Element) {
  var ox = 0;
  while (Element.offsetParent){
	ox += Element.offsetLeft ;
	Element = Element.offsetParent ;
  }
  return ox;
}

function esiScroller_drag (evt) {
  if (sc_isIE) evt = event; 
  var bt = (!sc_isIE) ? evt.which : evt.button;
  if ((bt & 1)==0) { e_dragElm = null; return true; }
  var x = evt.clientX - e_X + window.document.body.scrollLeft - e_mxo;
 
  //window.status = "MV x:"+x + " s:" + bt
  var sc = esiScroller_Find(e_dragElm.id.substring(0,3));
  if (x < 0) x = 0;
  if (x > sc.width) x = sc.width;
	sc.SetPos(x); 
  return true;
}


function esiScroller_startDrag (element, evt) {
  
    var name  = element.id.substring(0,3);
	
	var sc = esiScroller_Find(name);

	e_X = esiScrl_NSgetOffX(element) + sc.DragDelta();
	
  var x  = evt.clientX - e_X + window.document.body.scrollLeft;
  
  var mx = parseInt(document.getElementById(name + "_mk").style.left,10);
  
	//window.status = "x: "+ x + " mx:" + mx + " mx + dw: " + mx + sc.DragWith();
  
	if ( ( x >= mx) && ( x <= mx + sc.DragWith())){
	if (sc_isIE) element.setCapture();
	element.onmousemove = esiScroller_drag;
	e_dragElm = element;
	e_mxo = x-mx;
  }
  return true;
}


function esiScroller_stopDrag (element, evt) {
  if (e_dragElm == null) return false;
  if (sc_isIE) element.releaseCapture();
  element.onmousemove = null;
  var sc = esiScroller_Find(e_dragElm.id.substring(0,3));
  if (sc.page > 1) sc.SnapToPage();
  e_dragElm = null;
  return true;
}

function esiScroller_up(elm) {
  var sc = esiScroller_Find(elm.id.substring(0,3));
  if (sc) { sc.Value(sc.val + sc.page); if (!sc.SnapToPage()) sc.OnChange();}
}

function esiScroller_down(elm) {
  var sc = esiScroller_Find(elm.id.substring(0,3));
  if (sc) { sc.Value(sc.val - sc.page); if (!sc.SnapToPage()) sc.OnChange();}
}

