/*
 *
 * To make Slider work:
 *     o incluce wz_dragdrop.js
 *     o call SET_DHTML() (from the wz_dragdrop library) before creating any slider
 *
 */

function Slider(x,y,w) {

	this.id = ++Slider.counter;
	
	// You must set the container for the slider
	// before create its track and thumb.
	this.container = document.body;
	
	this.track = null;
	this.thumb = null;
	
	// make sure we always have something to show
	this.x = (arguments.length>0) ? x : 20;
	this.y = (arguments.length>1) ? y : 20;
	this.w = (arguments.length>2) ? w : Slider.defaultWidth;
	this.trackColor = "#FF0000";
	this.thumbColor = "#0000FF";
	this.thumbWidth = 0;
	
	this.trackClass = Slider.defaultTrackClass;
	this.thumbClass = Slider.defaultThumbClass;
	this.wzThumb = null;
	
	this.centerThumb = true;
	
	// callback called while thumb is being dragged. If
	// you're only interested in the final position of the
	// thumb (once it has been released), use the onStop
	// callback.
	this.onSlide = Slider.defaultOnSlide;
	// callback called once the thumb has been released
	this.onStop = Slider.defaultOnStop;
	// Both callbacks are passed one argument: the slider
	// object (this) itself - which you can ask for the
	// position of its thumb (getQ()).


}


Slider.counter = 0;

// If you're going to instantiate multiple sliders,
// you may want to set these variables:
Slider.defaultWidth = 200;
Slider.defaultTrackClass = null;
Slider.defaultThumbClass = null;
Slider.defaultOnSlide = null;
Slider.defaultOnStop = null;

// the thumb of the most recently operated slider
Slider.thumb = null;

Slider.onPick = function() { Slider.thumb = dd.obj; }

Slider.onDrag = function() {
	var thumb = Slider.thumb;
	var slider = thumb.manager;
	if(slider.onSlide != null) {
		slider.onSlide(slider);
	}
}

Slider.onDrop = function() {
	var thumb = Slider.thumb;
	var slider = thumb.manager;
	if(slider.onStop != null) {
		slider.onStop(slider);
	}
}

Slider.prototype.setDraggable = function(b) {
	this.wzThumb.setDraggable(b);
}

Slider.prototype.createTrack = function(height,top) {
	var h = (arguments.length>0) ? height : 6;
	var y = (arguments.length>1) ? top : (this.y-(h/2));
	this.track = div();
	this.track.id = "sldr-track" + this.id;
	var s = this.track.style;
	if(this.trackClass!=null) {
		this.track.className = this.trackClass;
	} else {
		s.backgroundColor = this.trackColor;
	}
	s.position = "absolute";
	s.left = this.x + PX;
	s.top = y + PX;
	s.width = this.w + PX;
	s.height = h + PX;
	
	this.container.appendChild(this.track);

	ADD_DHTML(this.track.id);
	dd.elements[this.track.id].setDraggable(false);

}

Slider.prototype.createThumb = function(width,height,top) {
	
	var w = (arguments.length>0) ? width : 8;
	this.thumbWidth = w;
	var h = (arguments.length>1) ? height : 26;
	var y = (arguments.length>2) ? top : (this.y-(h/2));
	
	this.thumb = div();
	this.thumb.id = "sldr-thumb" + this.id;
	var s = this.thumb.style;
	if(this.thumbClass!=null) {
		this.thumb.className = this.thumbClass;
	} else {
		s.backgroundColor = this.thumbColor;
	}
	s.position = "absolute";
	s.left = (this.x - Math.round(w/2)) + PX;
	s.top = y + PX;
	s.width = w + PX;
	s.height = h + PX;
	
	this.container.appendChild(this.thumb);
	
	var offset = 0;
	if(!dd.elements[this.container.id]) {
		ADD_DHTML(this.container.id);
		dd.elements[this.container.id].setDraggable(false);
	}
	offset = (this.container ==document.body) ? 0 : dd.elements[this.container.id].defx;
	
	ADD_DHTML(this.thumb.id);

	var wz = dd.elements[this.thumb.id];
	wz.maxoffl = 0;
	wz.maxoffr = this.w;
	wz.maxofft = 0;
	wz.maxoffb = 0;
	// the Dialog and the Walter Zorn object both need to
	// have references to eachother.
	wz.manager = this;
	this.wzThumb = wz;

	wz.setPickFunc(Slider.onPick);
	wz.setDragFunc(Slider.onDrag);
	wz.setDropFunc(Slider.onDrop);

	if(this.centerThumb) {
		var center = wz.x + (this.w/2);
		wz.moveTo(center,wz.y);
	}
	
}

// Get position of the slider as a fraction of its width
// (0 means it's dragged all the way to the left; 1 means
// it's dragged all the way to the right
Slider.prototype.getQ = function() {
	var x = ((this.wzThumb.x - this.wzThumb.defx)/this.w);
	return Math.min(Math.max(x,0),1);
}

// Set position of the slider as a fraction of its width
Slider.prototype.setQ = function(f) {
	if((f!=parseFloat(f))||(f<0)||(f>1)) {
		alert("invalid position for slider");
	}
	//var x = (this.x - Math.round(this.thumbWidth/2) + Math.round(f*this.w));
	var x = this.wzThumb.defx + (f*this.w);
	this.wzThumb.moveTo(x,this.wzThumb.y);
}

var PX = "px";
function div() { return document.createElement("div"); }
