var Scroller = Class.create({
	initialize: function(container){
		this.container = $(container);
		
		if(!this.container.firstChild)
		    return false;

		this.step      = 1,
		this.refresh   = 10;
		this.interval  = null;
		this.disabled  = 0;
		this.children  = this.container.childElements();

		this.container.observe('mouseover', this.stop.bind(this));
		this.container.observe('mouseout' , this.start.bind(this));

		var contentTotalScrollWidth = this.container.scrollWidth + this.container.offsetWidth;
		var first     = this.children[0];
		var current   = first;
		var tmp_width = 0;

		while(this.container.scrollWidth < contentTotalScrollWidth)
		{
			var before = this.container.scrollWidth;
			tmp_width += this.container.appendChild($(current.cloneNode(1))).scrollWidth;
			current = current.next() || first;
			var after = this.container.scrollWidth;
			if(after <= before)
				break;
		}
		this.offset_diff = tmp_width - this.container.offsetWidth;

		this.start();
	},

	start: function() {
		if(!this.interval) {
			this.interval = window.setInterval(function() {
				var next_possible_offset = this.container.scrollLeft + this.step;
				var max_possible_offset  = this.container.scrollWidth-this.container.offsetWidth;
				var set_offset = 0;

				if(next_possible_offset >= max_possible_offset)
					set_offset = this.offset_diff;
				else
					set_offset = next_possible_offset % max_possible_offset;

				this.container.scrollLeft = set_offset;
			}.bind(this), this.refresh);
		}
	},

	stop: function() {
		window.clearInterval(this.interval);
		this.interval = null;
	}
});
