AsciiUI_Slider = function (elem,initObj) {
	this.widgetName = 'Slider';
	this.margin=1;
	this.handle='|';
	this.slider=elem.firstChild.nodeValue;
	this.charWidth = elem.offsetWidth/this.slider.length;
	this.minValue = 0;
	this.maxValue = 99;
	this.steps = 0;
	this.value = 0;
	this.disabled = false;
	this.dragging = false;
	this.constructor (elem,initObj);
	
	this.updateRange();
	this.redraw();
}

AsciiUI_Slider.prototype = new _AsciiUI;

AsciiUI_Slider.prototype.updateRange = function () {
	this.range = this.slider.length-this.margin*2-this.handle.length;
	this.mouseRange = this.range*this.charWidth;
}

AsciiUI_Slider.prototype.snapValue = function () {
	if (this.steps>0) this.value=Math.round(this.multiplier()*(this.steps-1))*((this.maxValue-this.minValue)/(this.steps-1))+this.minValue;
}

AsciiUI_Slider.prototype.redraw = function () {
	this.elem.innerHTML = this.render();
}

AsciiUI_Slider.prototype.multiplier = function () {
	var retVal = (this.value-this.minValue)/(this.maxValue-this.minValue);
	retVal = retVal<0?0:retVal>1?1:retVal;
	return retVal;
}

AsciiUI_Slider.prototype.render = function () {
	var position = Math.round(this.range*this.multiplier());
	var retVal = this.slider.substr(0,position+this.margin) + this.renderHandle() + this.slider.substr(position+this.margin+this.handle.length);
	return retVal;
}

AsciiUI_Slider.prototype.renderHandle = function () {
	var retVal;
	if (!this.disabled && !this.dragging) {
		retVal = '<span style="cursor:pointer;" onmousedown="AsciiUI.startDrag(event,\''+this.objId+'\');">' + this.handle + '</span>';
	}  else {
		retVal = this.handle;
	}
	return retVal;
}

AsciiUI_Slider.prototype.startDrag = function (e) {
	this.dragging = true;
	this.redraw();
	this.startMouseX = e.screenX;
	this.minMouseX = this.startMouseX - (this.mouseRange*this.multiplier());
	this.maxMouseX = this.minMouseX + this.mouseRange;

	document.onmousemove = function (e) {
		var e = e?e:event
		var o = AsciiUI.dragObj;
		var multiplier = (e.screenX-o.minMouseX)/(o.maxMouseX-o.minMouseX);
		multiplier = multiplier<0?0:multiplier>1?1:multiplier;
		o.value = o.minValue + (multiplier*(o.maxValue-o.minValue));
		o.snapValue();
		o.redraw();
	}
}

AsciiUI_Slider.prototype.stopDrag = function () {
	this.dragging = false;
	document.onmousemove = null;
	this.redraw();
}