var current_scrolling;

function scroller(element, slider, num_displayed, count, callback, scrollbar_up, scrollbar_down, scroll_press_class ) {
	
	this.element = element;
	this.scroll_bar = new handle(slider);
	this.scroll_bar.parent_slider = this;
	this.up = scrollbar_up;
	this.down = scrollbar_down;
	this.scroll_press_class = scroll_press_class;
	
	this.element.obj_ref = this;
	this.up.obj_ref = this;
	this.down.obj_ref = this;
	
	this.scroll_bar.draw();
	this.scroll_bar.element.style.top = '0px';
	
	this.callback = callback;
	
	this.num_displayed = num_displayed;
	this.count = count;
	
	var height = (num_displayed / count) * this.element.offsetHeight;
	if ( height < 20 ) {
		height = 20;
	} else if ( height > this.element.offsetHeight ) {
		height = this.element.offsetHeight;
	}
	
	this.scroll_bar.element.style.height = height;
	
	if ( this.element.addEventListener ) {
		this.element.addEventListener("click", this.click, false);
		scrollbar_up.addEventListener("mousedown", this.mousedown, false);
		scrollbar_down.addEventListener("mousedown", this.mousedown, false);
	} else {
		this.element.attachEvent('onclick', this.click, false);
		scrollbar_up.attachEvent("onmousedown", this.mousedown, false);
		scrollbar_down.attachEvent("onmousedown", this.mousedown, false);
	}
	

	this.interval_id = 0;
	this.old_scroll_class = '';
	this.current_pos = 0;
}

scroller.prototype.moved = function (x, y, element)
{
	
	if ( this.count < this.num_displayed ) {
		return;
	}
	var multiplier = (this.count - this.num_displayed) / (this.element.offsetHeight - this.scroll_bar.element.offsetHeight);
	
	current = Math.round(multiplier * y);
	if ( this.num_displayed == this.count ) {
		y = 0;
		current = 0;
	} else if ( current < 0 ) {
		y = 0;
		current = 0;
	} else if ( current > (this.count - this.num_displayed) ) {
		y = (this.count - this.num_displayed) / multiplier;
		current = this.count - this.num_displayed;
		if ( current < 0 ) {
			current = 0;
			y=0;
		}
	}
	
	this.scroll_bar.element.style.top = y;
	
	if ( this.current_pos != current ) {
		this.callback(current);
		this.current_pos = current;
	}

};

scroller.prototype.mousedown = function (e)
{
	if (isIE) {
		current_scrolling = event.srcElement.parentNode.obj_ref;
		elem = event.srcElement.parentNode;
	}
	else {
		elem = this;
		current_scrolling = this.obj_ref;
	}

	if ( elem.addEventListener ) {
		elem.addEventListener("mouseup", current_scrolling.mouseup, false);
		elem.addEventListener("mouseout", current_scrolling.mouseup, false);
	} else {
		elem.attachEvent("onmouseup", current_scrolling.mouseup, false);
		elem.attachEvent("onmouseout", current_scrolling.mouseup, false);
	}
	if ( elem == current_scrolling.down ) {
		current_scrolling.interval_id = setInterval('scroller_move(1)', 100);
	} else {
		current_scrolling.interval_id = setInterval('scroller_move(-1)', 100);
	}
	current_scrolling.old_scroll_class = elem.className;
	elem.className = current_scrolling.scroll_press_class;
	return false;
};

scroller.prototype.mouseup = function (e)
{
	if (isIE) {
		e.srcElement.className = current_scrolling.old_scroll_class;
	}
	else {
		this.className = current_scrolling.old_scroll_class;
	}
	this.className = current_scrolling.old_scroll_class;
	clearInterval(current_scrolling.interval_id);
};

scroller_move = function (rows)
{
	current_scrolling.current_pos += rows;
	var multiplier = (current_scrolling.count - current_scrolling.num_displayed) / (current_scrolling.element.offsetHeight - current_scrolling.scroll_bar.element.offsetHeight);
	y = current_scrolling.scroll_bar.element.offsetTop - get_offsetTop(current_scrolling.scroll_bar.element.parentNode);
	
	new_pos = current_scrolling.current_pos;
	if ( current_scrolling.current_pos < 0 ) {
		current_scrolling.current_pos = 0;
	} else if ( current_scrolling.current_pos > (current_scrolling.count - current_scrolling.num_displayed) ) {
		current_scrolling.current_pos = (current_scrolling.count - current_scrolling.num_displayed);
		if ( current_scrolling.current_pos < 0 ) {
			current_scrolling.current_pos = 0;
		}
	}
	
	y = Math.round(current_scrolling.current_pos / multiplier);
	

	current_scrolling.current_pos = Math.round(current_scrolling.current_pos);
	ret_value = Array(2);
	ret_value[0] = 0;
	ret_value[1] = y;
	current_scrolling.callback(current_scrolling.current_pos);
	current_scrolling.scroll_bar.move_to(ret_value);

};

scroller.prototype.click = function(e)
{
	var is_bar = false;
	if ( e.target == this )
		is_bar = true;
		var elem = this;
	if ( e.srcElement ) {
		if ( e.srcElement.obj_ref.num_displayed != undefined ) {
			is_bar = true;
			var elem = e.srcElement;
		}
	}
	
	if ( is_bar ) {

		y = e.clientY + (document.documentElement.scrollTop + document.body.scrollTop);
		true_pos = get_offsetTop(elem.obj_ref.scroll_bar.element);
		new_pos = elem.obj_ref.current_pos;
		var multiplier = (elem.obj_ref.count - elem.obj_ref.num_displayed) / (elem.obj_ref.element.offsetHeight - elem.obj_ref.scroll_bar.element.offsetHeight);
		if ( y > true_pos ) {
			new_pos += elem.obj_ref.num_displayed;
		} else {
			new_pos -= elem.obj_ref.num_displayed;
		}
		
		if ( new_pos < 0 ) {
			new_pos = 0;
		} else if ( new_pos > (elem.obj_ref.count - elem.obj_ref.num_displayed) ) {
			new_pos = (elem.obj_ref.count - elem.obj_ref.num_displayed);
			if ( new_pos < 0 ) {
				new_pos = 0;
			}	
		}
		
		elem.obj_ref.current_pos = new_pos;
		elem.obj_ref.callback(new_pos);
		ret_value = Array(2);
		ret_value[0] = 0;
		ret_value[1] = new_pos / multiplier;
		elem.obj_ref.scroll_bar.move_to(ret_value);

		
		
		
	}
};

scroller.prototype.resize = function(count)
{
	this.count = count;
	var height = (this.num_displayed / this.count) * this.element.offsetHeight;
	if ( height < 20 ) {
		height = 20;
	} else if ( height > this.element.offsetHeight ) {
		height = this.element.offsetHeight;
	}
	this.scroll_bar.element.style.height = height;
	
	this.scroll_bar.element.style.top = '0px';
	this.current_pos = 0;
};

scroller.prototype.redraw_start = function()
{
	this.scroll_bar.redraw_start();
};

scroller.prototype.redraw_frame = function()
{
	this.scroll_bar.redraw_frame();
};