/**
*
*   Floating / flying boxes with easing and motion-blur in jQuery
*   Author:      Marcell Jusztin
*   Date:        12th July, 2010
*   URL:         http://www.marcelljusztin.com
*   Version:     0.2
*
*/

jQuery.fn.floatingBox = function ( userOptions ) {
	
	options = jQuery.extend ({
		parent    : 'container',
		stage     : 'container',
		scale     : 0.3,
		xOffset   : 0,
		yOffset   : 0,
		blur      : true,
		isText    : false,
		blurColor : '#888',
		frameRate : 33
	}, userOptions);
 
	
	var parent = options.parent;
	var stage = options.stage;
	var scale = options.scale;
	var xOffset = options.xOffset;
	var yOffset = options.yOffset;
	var blur = options.blur;
	var isText = options.isText;
	var blurColor = options.blurColor;
	var frameRate = options.frameRate;
	
	var midX = $('#' + stage).width() / 2;
	var midY = $('#' + stage).height() / 2;
	var _x = midX;
	var _y = midY;
	var xx = midX;
	var yy = midY;
	
	var objectId = $(this).attr('id');
	$('#' + objectId).css('position','absolute');
	
	shadowAmount = 0;
		
	window["timer" + objectId] = window.setInterval(update,frameRate);
	
			
	$('#' + parent).mousemove(function(event){
		
		_x = event.pageX; 
		_y = event.pageY;
				
		if( shadowAmount < 5 && blur == true ) shadowAmount += scale;
				
	});
	
	factor = scale * 0.5;	
	
	function update() {
			
			xx += (((_x - midX) * -scale) - xx) * factor;
			yy += (((_y - midY) * -scale) - yy) * factor;
			$('#' + objectId).css('left', xx + xOffset + $('#' + parent).position().left);
			$('#' + objectId).css('top', yy + yOffset + $('#' + parent).position().top);
			if(blur){
				if(!isText){
					$('#' + objectId).css('box-shadow', '0px 0px ' + shadowAmount + 'px ' + blurColor );
					$('#' + objectId).css('-moz-box-shadow', '0px 0px ' + shadowAmount + 'px ' + blurColor );
					$('#' + objectId).css('-webkit-box-shadow', '0px 0px ' + shadowAmount + 'px ' + blurColor );
					$('#' + objectId).css('-o-box-shadow', '0px 0px ' + shadowAmount + 'px ' + blurColor );
				}else{
					$('#' + objectId).css('text-shadow', '0px 0px ' + shadowAmount + 'px ' + blurColor );
				}
				if(shadowAmount > 0 ) shadowAmount -= scale;
			}
			
		
	}
	
	
}
