/**



 */
function Scroller() {
	this.options = {
		area: 20,
		velocity: 1,
		attachEvent: "mouseover",
		detachEvent: "mouseout",
		onChange: function(x, y){
			this.element.scrollLeft(x);
			this.element.scrollTop(y);
		},
		fps: 50
	};
	this.initialize.apply(this, arguments);
}

Scroller.prototype.initialize = function(element, options) {
	this.options=jQuery.extend(this.options, options)
	this.element = element;
	this.listener = element;
	this.timer = null;
	this.bound = {
		attach: this.bind(this.attach),
		detach: this.bind(this.detach),
		getCoords: this.bind(this.getCoords)
	};
}

Scroller.prototype.start = function() {
	this.listener.bind(this.options.attachEvent, this.bound.attach);
	this.listener.bind(this.options.detachEvent, this.bound.detach);
}

Scroller.prototype.stop = function() {
	this.listener.unbind(this.options.attachEvent, this.bound.attach);
	this.listener.unbind(this.options.detachEvent, this.bound.detach);
	this.detach();
	this.timer = this.clearTimer(this.timer);
}

Scroller.prototype.attach = function() {
	this.listener.bind("mousemove", this.bound.getCoords);
}

Scroller.prototype.detach = function() {
	this.listener.unbind("mousemove", this.bound.getCoords);
	this.timer = this.clearTimer(this.timer);
}

Scroller.prototype.bind = function(handler) {
	var t=this;
	return function(event) {
		handler.call(t, event);
	};
}

Scroller.prototype.periodical = function(handler, ms, owner) {
	var id=window.setInterval(function () {
		handler.call(owner);
	}, ms);
	return id;
}

Scroller.prototype.clearTimer = function(timer) {
	window.clearInterval(timer);
	return null;
}

Scroller.prototype.getCoords = function(event) {
	this.page = {x: event.pageX, y: event.pageY};
	if (!this.timer) this.timer = this.periodical(this.scroll, Math.round(1000 / this.options.fps), this);
}

Scroller.prototype.scroll = function() {
	var size = {x: this.element.width(), y: this.element.height()}, 
		scroll = {x: this.element.scrollLeft(), y: this.element.scrollTop()}, 
		pos = {x: this.element.offset().left, y: this.element.offset().top}, 
		scrollSize = {x: this.element[0].scrollWidth, y: this.element[0].scrollHeight},//this.element.getScrollSize(), 
		change = {x: 0, y: 0};
	for (var z in this.page) {
		if (this.page[z] < (this.options.area + pos[z]) && scroll[z] != 0)
			change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
		else if (this.page[z] + this.options.area > (size[z] + pos[z]) && scroll[z] + size[z] != scrollSize[z])
			change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
	}
	if (change.y || change.x) this.options.onChange.call(this, scroll.x + change.x, scroll.y + change.y);
}

/**




 */
Scroller.setListSize = function(element, vertical) {
	var list=$('ul:last', element);
	var li=list.children('li');

	var self = this;
	if (li.size() > 0) {
		var wh = 0;
		li.each(function() {
			wh += !vertical ? $(this).outerWidth() : $(this).outerHeight();
		});

		list.css(!vertical ? 'width' : 'height', wh + 'px');
	}
}

