
var ScrollSpy = new Class({

	options: {
		min: 0,
		mode: 'vertical',
		max: 0,
		container: window,
		onEnter: $empty,
		onLeave: $empty,
		onTick: $empty
	},
	
	initialize: function(options) {

		this.setOptions(options);
		this.container = $(this.options.container);
		this.enters = this.leaves = 0;
		this.max = this.options.max;
		
		if(this.max == 0) 
		{ 
			var ss = this.container.getScrollSize();
			this.max = this.options.max = this.options.mode == 'vertical' ? ss.y : ss.x;
		}

		this.addListener();
	},

	addListener: function() {

		this.inside = false;
		this.container.addEvent('scroll',function() {
	
			var position = this.container.getScroll();
			var xy = this.options.mode == 'vertical' ? position.y : position.x;
			if(xy >= this.options.min && xy <= this.max) {

					if(!this.inside) {

						this.inside = true;
						this.enters++;
						this.fireEvent('enter',[position,this.enters]);
					}
					this.fireEvent('tick',[position,this.inside,this.enters,this.leaves]);
			}
			else {
				if(this.inside) 
				{
					this.inside = false;
					this.leaves++;
					this.fireEvent('leave',[position,this.leaves]);
				}
			}
		}.bind(this));
	}
});

ScrollSpy.implement(new Options);
ScrollSpy.implement(new Events);
