var Slider = {
	min: 100,
	max: 350,
	initial: 145,
	value: 145,
	butons_step: 10,
	initialize: function () {
		this.btn_decrease = $('sl_btn_decrease');

		this.btn_increase = $('sl_btn_increase');

		this.element_osd = $('sl_osd');
		this.element_osd.onclick = this.reset.bindAsEventListener(this);

		this.sl_main = $('sl_main');
		this.slider_width = this.sl_main.offsetWidth - 12;
		var temp = this.sl_main.getElementsByTagName('IMG');
		this.sl_left = $('slider_left');
		this.sl_cursor = $('slider_cursor');
		this.sl_right = $('slider_right');

		this.sl_cursor.onmousedown = this.startSliding.bindAsEventListener(this);
		this.sliding = false;

		this.btn_decrease.onclick = this.OnClickDecrease.bindAsEventListener(this);
		this.btn_increase.onclick = this.OnClickIncrease.bindAsEventListener(this);

		Event.observe(this.sl_main, "mousewheel", this.onMouseWheel.bindAsEventListener(this));
	},

	OnClickDecrease: function () {
		this.setValue(Math.max(this.value - this.butons_step, this.min));
		rc.resizeResults(this.value);
	},

	OnClickIncrease: function () {
		this.setValue (Math.min(this.value + this.butons_step, this.max));
		rc.resizeResults(this.value);
	},

	onMouseWheel: function (event) {
		var d = Event.wheelDelta(event);
		if (d == 0) return true;
		if (d < 0)
			this.OnClickDecrease();
		else
			this.OnClickIncrease();
	},

	reset: function () {
		this.setValue(this.initial);
		rc.resizeResults(this.value);
	},

	setValue: function (value) {
		this.value = value;
		var nx = (this.slider_width)*(this.value - this.min)/(this.max-this.min) ;
		this.sl_left.style.width = Math.max(0, nx - 9) + "px";
		this.sl_right.style.width = Math.max(0, (this.slider_width - 2 - Math.max(9, nx))) + "px";
		this.element_osd.innerHTML = Math.round( 100 * (this.value - this.min) / (this.initial - this.min) ) + "%";
	},

	startSliding: function (event) {
		this.eventMouseUp   = this.endSliding.bindAsEventListener(this);
    	this.eventMouseMove = this.updateSliding.bindAsEventListener(this);
  		var a = Position.cumulativeOffset(this.sl_main);
  		this.slbar_left = a[0] + 6;
  		this.slbar_right = a[0] + this.slider_width;
  		Event.observe(document, "mouseup", this.eventMouseUp);
  		Event.observe(document, "mousemove", this.eventMouseMove);
  		this.sliding = true;
  		Event.stop(event);
	},

	updateSliding: function (event) {
		if (!this.sliding) return;
		Event.stop(event);
		var nx = Event.pointerX(event);

		if (nx < this.slbar_left)
			nx = this.slbar_left;
		if (nx > this.slbar_right)
			nx = this.slbar_right;
		nx = nx - this.slbar_left;
		if (nx > 5) nx = nx-5;
		this.sl_left.style.width = nx + "px";
		this.sl_right.style.width = (this.slider_width - nx - 9) + "px";

		this.value = Math.ceil(this.sl_left.offsetWidth*(this.max-this.min) / (this.slider_width - 9)) + this.min;
		this.element_osd.innerHTML = Math.round( 100 * (this.value - this.min) / (this.initial - this.min) ) + "%";
		rc.resizeResults(this.value);
 	},

	endSliding: function (event) {
		Event.stop(event);
		this.sliding = false;
		Event.stopObserving(document, "mouseup", this.eventMouseUp);
  		Event.stopObserving(document, "mousemove", this.eventMouseMove);
	}
}