var MooFloater = new Class({
    Implements: [Options, Events],
	options: {
			width: '200px',
			height: '30px',
			position: 'top-right',
			glidespeed: 6,
			offsetx: 10,
			offsety: 10,
			topboundary: 0,
			bottomboundary: 9999
	},
	
	initialize: function(id, options){
		this.setOptions(options);
		this.container = id;
		this.container.setStyle('position','relative');
		this.container.setStyle('width',this.options.width);
		this.container.setStyle('height',this.options.height);
		//this.options.topboundary = this.container.getParent().getParent().getTop().toInt();
		this.options.topboundary = this.container.getTop().toInt();
		this.options.bottomboundary = 0;
		var position = this.options.position.split('-');
		switch(position[0]) {
			case 'top':
				this.container.setStyle('top', this.options.offsety+'px');
				break;
			case 'bottom':
				this.container.setStyle('bottom', this.options.offsety+'px');
				break;
		}
		switch(position[1]) {
			case 'left':
				this.container.setStyle('left', this.options.offsetx+'px');
				break;
			case 'right':
				this.container.setStyle('right', this.options.offsetx+'px');
				break;
		}
		this.floater = this.startFloat.bind(this).delay(100, window);
		window.addEvent('scroll', this.onScrollHandler.bind(this));
		window.addEvent('resize', this.onScrollHandler.bind(this));
	},

	onScrollHandler: function(){
		if (this.floater == null){
			this.floater = this.startFloat.bind(this).delay(100, window);
		}else{
			this.floater = $clear(this.floater);
			this.floater = null;
			this.floater = this.startFloat.bind(this).delay(100, window);
		}
	},
	
	startFloat: function(){
		glidespeed = (this.options.glidespeed*100);
		position = this.options.position.split('-');
		if(this.options.topboundary < 1) this.options.topboundary = this.container.getTop().toInt();
		if(this.options.bottomboundary < 1) {
			if(this.container.getParent().getParent().getStyle('height').toInt() < this.options.topboundary) {
				this.options.bottomboundary = this.container.getParent().getParent().offsetHeight + this.container.getParent().getParent().getTop().toInt() + 0;
			} else {
				this.options.bottomboundary = this.container.getParent().getParent().getStyle('height').toInt() + this.container.getParent().getParent().getTop().toInt() + 0;
			}
		}
		switch(position[0]) {
			case 'top':
				var floatTop = new Fx.Morph(this.container, {duration:glidespeed});
				if(window.getScrollTop() > this.options.topboundary ) {
					//console.log(window.getScrollTop(), this.options.bottomboundary, this.options.height.toInt(), this.options.topboundary);
					//alert(this.options.bottomboundary + '|' + this.options.height.toInt() + '|' + this.options.offsety);
					if(window.getScrollTop() < (this.options.bottomboundary - this.options.height.toInt()) - this.options.offsety) {
						floatTop.start({'top':window.getScrollTop()+this.options.offsety-this.options.topboundary});
					} else {
						floatTop.start({'top':this.options.bottomboundary - (this.options.topboundary + this.options.height)});
					}
				} else {
					floatTop.start({'top':this.options.offsety});
				}
				break;
			case 'bottom':
				var floatBottom = new Fx.Morph(this.container, {duration:glidespeed});
				floatBottom.start({'bottom': - window.getScrollTop()+this.options.offsety});
				break;
		}
		switch(position[1]) {
			case 'left':
				var floatLeft = new Fx.Morph(this.container, {duration:glidespeed});
				floatLeft.start({'left':Window.getScrollLeft()+this.options.offsetx});
				break;
			case 'right':
				var floatRight = new Fx.Morph(this.container, {duration:glidespeed});
				floatRight.start({'right': - Window.getScrollLeft()+this.options.offsetx});
				break;
		}
	}
});

MooFloater.implement(new Chain);
