(function($){
	function floatLayer(object, params) {
		this.object = object;
		
		switch(params.speed) {
			case 'fast': 
				this.steps = 5; 
				break;
			case 'normal': 
				this.steps = 10; 
				break;
			case 'slow': 
				his.steps = 20; 
				break;
			default:
				this.steps = 10;
		};
		
		var offset = this.object.offset();
		
		this.currentY = offset.top;
		this.origY = typeof(params.y) == "string" ?  this.currentY : params.y;
		this.object.css({'position':'absolute' , 'top':this.currentY });
	}
	
	floatLayer.prototype.updateLocation = function() {
		this.updatedY = $(window).scrollTop()+ this.origY;
		this.dy = Math.abs(this.updatedY - this.currentY );
		
		return this.dy;
	}
	
	floatLayer.prototype.move = function() {
		if( this.object.css("position") != "absolute" ) return;
		
		var cy = 0;
		
		if( this.dy > 0 ) {
			if( this.dy < this.steps / 2 )
				cy = (this.dy >= 1) ? 1 : 0;
			else
				cy = Math.round(this.dy/this.steps);
			
			if( this.currentY < this.updatedY )
				this.currentY += cy;
			else
				this.currentY -= cy;
		}
		
		this.object.css({ 'top': this.currentY });
	}

	$.floatMgr = {
		
		FOArray: new Array() ,
		
		timer: null ,
		
		initializeFO: function(object,params) {
			var settings =  $.extend({
				y: 0 ,
				speed: 'normal'	},params||{});
			var newFO = new floatLayer(object,settings);
			
			$.floatMgr.FOArray.push(newFO);
			
			if( !$.floatMgr.timer ) $.floatMgr.adjustFO();
			
			if( !$.floatMgr.registeredEvents ) {
				$(window).bind("resize", $.floatMgr.onChange);
				$(window).bind("scroll", $.floatMgr.onChange);
				$.floatMgr.registeredEvents = true;
			}		
		} , 
		
		adjustFO: function() {
			$.floatMgr.timer = null;
			
			var moveFO = false;
			
			for( var i = 0 ; i < $.floatMgr.FOArray.length ; i++ ) {
				 FO = $.floatMgr.FOArray[i];
				 if( FO.updateLocation() )  moveFO = true;
			}
			
			if( moveFO ) {
				for( var i = 0 ; i < $.floatMgr.FOArray.length ; i++ ) {
					FO = $.floatMgr.FOArray[i];
					FO.move();
				}
				
				if( !$.floatMgr.timer ) $.floatMgr.timer = setTimeout($.floatMgr.adjustFO,50);
			}
		}	,
		
		onChange: function() {
			if( !$.floatMgr.timer ) $.floatMgr.adjustFO();
		}
	};

	$.fn.floatObject = function(params) {
		var obj = this.eq(0);
		
		$.floatMgr.initializeFO(obj,params);
		
		if( $.floatMgr.timer == null ) $.floatMgr.adjustFO();
		
		return obj;
	};
})(jQuery);