/************************************************************\
 *        (c) 2009, Bluebear Internet Data Solutions        *
 *    http://www.bluebear.nl/        http://www.tic4.nl/    *
 *                                                          *
 *                    Licensed under the                    *
 * Creative Commons Attribution-Share Alike 3.0 Netherlands *
 *      http://creativecommons.org/licenses/by-sa/3.0/      *
\************************************************************/
var bbFixedBlock = new Class({
	initialize: function(elem, cont) {
		this.elem = $(elem);
		this.cont = $(cont);
		this.pageOffset = this.elem.getPosition();
		this.contOffset = this.elem.getPosition(this.cont);
		if (this.cont.getStyle('position') == 'static') {
			this.cont.setStyle('position','relative');
		}
		this.elem.setStyles({
			'position': 'absolute',
			'top': this.contOffset.y,
			'left': this.contOffset.x
		});
		window.addEvents({
			'scroll': this.stayFixed.bind(this)
		});
	},
	stayFixed: function() {
		//if (window.getScroll().y - this.contOffset.y > this.contOffset.y) {
		if (window.getScroll().y - this.pageOffset.y > 0) {
			var newOffset = window.getScroll().y - this.pageOffset.y + this.contOffset.y;
			this.elem.set('tween', {duration: '200',link: 'cancel'}).tween('top', newOffset);
		} else if (this.elem.getPosition().y > this.pageOffset.y) {
			this.elem.set('tween', {duration: '200',link: 'cancel'}).tween('top', this.contOffset.y);
		}
	}
});