
var unobtrusive_scroller = new Class({
	initialize: function() {
		this.scroller = null;
		this.y_value = 0;
		
		var _this = this;

		window.addEvent('domready', function() {
			_this.prepare();
		});
	},

	prepare: function() {
        if ($.noConflict) { // Tell jQuery to bugger off if it's loaded.
            $.noConflict();
        }
        
		var scroller_el = $E('.scroller');
		if (scroller_el) {
			var size = scroller_el.getSize();
			var scroll_height = scroller_el.getScrollSize().y;
			var overflow = scroll_height - size.y;
			
			if (overflow > 0) {

				var _this = this;
				scroller_el.addEvent('mouseenter', function() {
					if (_this.scroller != null) {
						_this.scroller.pause();
					}
				});

				scroller_el.addEvent('mouseleave', function() {
					if (_this.scroller != null) {
						_this.scroller.resume();
					}
				});

				var last_element = scroller_el.getLast();
				scroller_el.getChildren().each(function(child) {
					child.clone().set('id', 'news_div').inject(scroller_el);
				});
				//scroller_el.innerHTML += scroller_el.innerHTML;
				var scroll_to_el = last_element.getNext();

				scroller_el.scrollTo(0, 0);

				this.scroller = new Fx.Scroll(scroller_el, {
					'transition': Fx.Transitions.linear,
					'duration': (scroll_height * 70),
					'wheelStops': false,
					'onComplete': function() {
						scroller_el.scrollTo(0, 0);
						//_this.scroller.toElement(scroll_to_el);
						_this.scroller.start(false, scroll_height);
					}
				}).start(false, scroll_height);
				//.toElement(scroll_to_el);
			}
		}
	},
	
	scroll: function(el,y) {
		this.scroller = new Fx.Scroll(el,{
			'transition':Fx.Transitions.linear,
			'duration':(y * 70),
			'wheelStops':false,
			'onComplete': function() {
				var _this = this.options._this;
				var el = this.options._el;
				var y = this.options._y;
				el.scrollTo(0,0);
				_this.scroll(el,y);
			},
			'_this': this,
			'_el': el,
			'_y': y
		});
		this.y_value = y;
		this.scroller.scrollTo(0,y);
	}

});

window.addEvent('domready', function() {
	new unobtrusive_scroller();
});
