var myScroller = new Class({

	Implements: [Events, Options],

	options: {
		area: 20,
		velocity: 1,
		delay:250,
		onChange: function(x, y){
			this.element.scrollTo(x, y);
		}
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		this.mousemover = ([window, document].contains(element)) ? $(document.body) : this.element;
	},

	/*
	Property: start
		The scroller starts listening to mouse movements.
	*/

	start: function(){
		this.coord = this.getCoords.bindWithEvent(this);
		this.mousemover.addListener('mousemove', this.coord);
	},

	/*
	Property: stop
		The scroller stops listening to mouse movements.
	*/

	stop: function(){
		this.mousemover.removeListener('mousemove', this.coord);
		this.timer = $clear(this.timer);
	},

	getCoords: function(event){
	    if (this.element == window) {
    		this.pageX = event.clientX;
    		this.pageY = event.clientY;
    	}
    	else {
    		this.pageX = event.pageX;
    		this.pageY = event.pageY;
    	}
		//var x = this.scroll.periodical(this.delay, this);
		if (!this.timer) this.timer = this.scroll.periodical(this.delay, this);
	},

	scroll: function(){
		var el = this.element.getSize();
		var pos = this.element.getOffsets();
        var scr = this.element.getScroll();
        var sz = this.element.getScrollSize();
        if (!$chk(this.pageX))
            this.pageX = event.clientX;
        if (!$chk(this.pageY))
            this.pageY = event.clientY;
		var change = {'x': 0, 'y': 0};
		    z='x';
			if (this.pageX < (this.options.area + pos[z]) && scr[z] != 0)
				change[z] = (this.pageX - this.options.area - pos[z]) * this.options.velocity;
			else if (this.pageX + this.options.area > (el[z] + pos[z]) && scr[z] + el[z] != sz[z])
				change[z] = (this.pageX - el[z] + this.options.area - pos[z]) * this.options.velocity;
		    z='y';
			if (this.pageY < (this.options.area + pos[z]) && scr[z] != 0)
				change[z] = (this.pageY - this.options.area - pos[z]) * this.options.velocity;
			else if (this.pageY + this.options.area > (el[z] + pos[z]) && scr[z] + el[z] != sz[z])
				change[z] = (this.pageY - el[z] + this.options.area - pos[z]) * this.options.velocity;

		if (change.y || change.x) 
		    this.fireEvent('onChange', [scr.x + change.x, scr.y + change.y]);
	}

});

myScroller.implement(new Events, new Options);

window.addEvent('domready', function() {
    try {
        var coldiv = $('content');
        if ($chk(coldiv)) {
            var scroll2 = new myScroller('content', {area: 100, velocity: .5}); 
            // Mousemove
            coldiv.addEvent('mouseover', scroll2.start.bind(scroll2));
            coldiv.addEvent('mouseout', scroll2.stop.bind(scroll2)); 
	    }
    }
    catch(e) { 
        alert("an error occurred");
    }
});

