/*
	jQuery Version:				jQuery 1.3.2
	Plugin Name:				floater V 1.0
	Plugin by: 					Illusion Factory LLC http://www.illusionfactory.com
*/

(function($) {
    $.fn.floater = function(options) {
    
    	// setup default settings
    	var defaults = {
    		time:100,
			maxSpeed:5,
			floatContainerClass: 'floatContainer',
    		floaterClass: 'floater',
    		xOffset: 0,
    		yOffset: 0,
			boxWidth: 958,
			boxHeight: 414
    	},
    
    	// This makes it so the users custom options overrides the default ones
    	settings = $.extend({}, defaults, options);
    	
		return this.each(function() {
			var obj = $(this);
			
			var randLeft = Math.random()*(settings.boxWidth - obj.width());
			var randTop = Math.random()*(settings.boxHeight - obj.height());
			
			
			obj.css('left', randLeft);
			obj.css('top', randTop);
			var thisLeft = randLeft;
			var thisTop = randTop;
			var thisXSpeed = Math.random()*settings.maxSpeed;
			var thisYSpeed = Math.random()*settings.maxSpeed;
			var thisYWhich = 1;
			var thisXWhich = 1;
			
			obj.css('display', 'block');
			
			setInterval(moveIt,settings.time);
			
			
			function moveIt(){
				thisLeft+=(thisXSpeed * thisXWhich);
				thisTop+=(thisYSpeed * thisYWhich);
				obj.css('left',thisLeft+'px');
				obj.css('top',thisTop+'px');
				
				if(thisTop>(settings.boxHeight - obj.height())){
					thisYWhich = -1;
				}
				if(thisLeft>(settings.boxWidth - obj.width())){
					thisXWhich = -1;
				}
				if(thisTop<(settings.yOffset)){
					thisYWhich = 1;
				}
				if(thisLeft<(settings.xOffset)){
					thisXWhich = 1;
				}
			}
			
		}); // END: return this
		
		// returns the jQuery object to allow for chainability.  
        return this;
    };
})(jQuery);
