var ScrollSidebar = new Class({
	
	Implements: [Options],
	
	options: {
		offsets: { x:0, y:0 },
		mode: 'vertical',
		positionVertical: 'top',
		positionHorizontal: 'right',
		speed: 400,
		startPosition: 0
	},
	
	initialize: function(menu,options) {
		/* initial options */
		this.setOptions(options);
		this.menu = $(menu);
		this.move = this.options.mode == 'vertical' ? 'y' : 'x';
		this.property = this.move == 'y' ? 'positionVertical' : 'positionHorizontal';
		/* ensure a few things */
		var css = { position: 'absolute', display:'block' };
		css[this.options.positionVertical] = this.options.offsets.y;
		css[this.options.positionHorizontal] = this.options.offsets.x;
		this.menu.setStyles(css).set('tween',{ duration: this.options.speed });
		this.lastScroll = $(document.body).getScroll().y;
		/* start listening */
		this.startListeners();
		
	},
	
	startListeners: function() {
		var action = function() {
			var sc = $(document.body).getScroll().y;
			
			
			if((sc > this.options.startPosition) || (sc < this.lastScroll)) {
				var sct = $(document.body).getScroll()[this.move] + (this.options.offsets[this.move]-this.options.startPosition);
				//console.log("sct = " + sct);
				if(sct < this.options.offsets.y) {
					sct = this.options.offsets.y;
				}
				this.setPosition(sct);
			}
			this.lastScroll = sc;
		}.bind(this);
		window.addEvent('scroll',action);
		window.addEvent('load',action);
	},
	
	setPosition: function(move) {
		this.menu.tween(this.options[this.property],move);
		return this;
	}
});
