/*Scroller2: Bases on the mootools scroller by mootools
By; Frank Teunisse
*/
var Scroller2 = new Class({

	options: {
		area: 20,
		velocity: 1,
		scrolltime: 2000,
		scrollinterval: 4000,
		name: "scroll2",
		onChange: function(x, y){
			this.element.scrollTo(x, y);
		}
	},

	initialize: function(element, options){
		var elementname = element;
		this.setOptions(options);
		this.element = $(element);

		this.autoscrolldelta = 162;
		
		this.timeout = window.setTimeout(this.options.name+'.autoscroll()',this.options.scrollinterval);
		this.mousemover = ([window, document].contains(element)) ? $(document.body) : this.element;
		
		this.scrollfx = new Fx.Scroll(elementname, {
		wait: false,
		duration: this.options.scrolltime,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut
		});
		
		//Voor het weergeven/verbergen van de pijltjes
		var el = this.element.getSize();
		if(el.scroll['y'] <= 0) $('omhoog').style.visibility = 'hidden'; else $('omhoog').style.visibility = 'visible';
		if(el.scroll['y'] >= (el.scrollSize['y']-el.size['y'])) $('omlaag').style.visibility = 'hidden'; else $('omlaag').style.visibility = 'visible';
		
	},

	/*
	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){
		this.page = (this.element == window) ? event.client : event.page;
		if (!this.timer) this.timer = this.scroll.periodical(50, this);
	},

	scroll: function(){
		if(!(this.timeout == null)) this.timeout = window.clearTimeout(this.timeout);
		this.scrollfx.stop();
		
		var el = this.element.getSize();
		var pos = this.element.getPosition();
		
		var change = {'x': 0, 'y': 0};

		if (this.page['y'] < (this.options.area + pos['y']) && el.scroll['y'] != 0)
			change['y'] = (this.page['y'] - this.options.area - pos['y']) * this.options.velocity;
		else if (this.page['y'] + this.options.area > (el.size['y'] + pos['y']) && el.scroll['y'] + el.size['y'] != el.scrollSize['y'])
			change['y'] = (this.page['y'] - el.size['y'] + this.options.area - pos['y']) * this.options.velocity;
		
		//Voor het weergeven/verbergen van de pijltjes
		if(el.scroll['y'] == 0)
			{
			$('omhoog').style.visibility = 'hidden'; 
			this.autoscrolldelta = 162;
			}
		else $('omhoog').style.visibility = 'visible';
		if(el.scroll['y'] == (el.scrollSize['y']-el.size['y']))
			{
			$('omlaag').style.visibility = 'hidden'; 
			this.autoscrolldelta = -162;
			}
		else $('omlaag').style.visibility = 'visible';

		if (change.y || change.x) this.fireEvent('onChange', [el.scroll.x + change.x, el.scroll.y + change.y]);
		
		this.timeout = window.setTimeout(this.options.name+'.autoscroll()',this.options.scrollinterval);
	},
	
	autoscroll: function(){
		var el = this.element.getSize();
		this.scrollfx.scrollTo(0,el.scroll['y']+this.autoscrolldelta);
		
		//Voor het weergeven/verbergen van de pijltjes
		if(el.scroll['y']+this.autoscrolldelta <= 0) 
			{
			$('omhoog').style.visibility = 'hidden'; 
			this.autoscrolldelta = 162;
			}
		else $('omhoog').style.visibility = 'visible';
		if(el.scroll['y']+this.autoscrolldelta >= (el.scrollSize['y']-el.size['y'])) 
			{
			$('omlaag').style.visibility = 'hidden'; 
			this.autoscrolldelta = -162;
			}
		else $('omlaag').style.visibility = 'visible';
		
		this.timeout = window.setTimeout(this.options.name+'.autoscroll()',this.options.scrollinterval);

	}

});

Scroller2.implement(new Events, new Options);
