/*
	Plugin created by Etienne TREMEL for BSO - October 2011
	
	Easing preview available here: http://jqueryui.com/demos/effect/easing.html
*/

(function($){
	var methods = {
		init: function(options) {
			var defaults = {
				speed:1500,
				nbrObjMax:20,
				xDistance:100,
				yDistance:100,
				easingTop:'linear',
				easingLeft:'easeInOutQuad' //linear, swing, easeInQuad, easeOutQuad, easeInOutQuad, easeInCubic, easeOutCubic, easeInOutCubic, easeInQuart, easeOutQuart, easeInOutQuart, easeInQuint, easeOutQuint, easeInOutQuint, easeInSine, easeOutSine, easeInOutSine, easeInExpo, easeOutExpo, easeInOutExpo, easeInCirc, easeOutCirc, easeInOutCirc, easeInElastic, easeOutElastic, easeInOutElastic, easeInBack, easeOutBack, easeInOutBack, easeInBounce, easeOutBounce, easeInOutBounce
			};
			
			var options = $.extend(defaults, options);
			
			var objects = [];
			var timeout;
			var nbrObjMax = options.nbrObjMax;
			var xDistance = options.xDistance;
			var yDistance = options.yDistance;
			var speed = options.speed;
			var easingTop = options.easingTop;
			var easingLeft = options.easingLeft;
			
			var target = $(this);
			var container = $('<div class="rainWrapper" style="width:100%; height:100%;" />');

			target.children().each(function(index, item) {
				objects.push(item);
			});
			
			target.empty();
			target.html(container);
			
			addObject(nbrObjMax/2, true);
			
			function addObject(number, playAfter) {
				if(container.children().size() < number) {
					for(var i=0; i<number-container.children().length; i++) {
						var randomSelector = Math.floor(Math.random()*objects.length);
						var obj = $(objects[randomSelector]).clone();
						var initLeft = container.width() - Math.floor(Math.random()*container.width());
						var initTop = -Math.floor(Math.random()*container.height());
						obj.css({
							'top':initTop+'px',
							'left':initLeft+'px',
							'position':'absolute'
						});
						container.append(obj);
					}
				}
				if(playAfter) playRainingObjects();
			}
			
			function playRainingObjects() {
				
				addObject(nbrObjMax,false);
			
				container.children().each(function() {
					var nextLeft;
					var leftOrRight = Math.floor(Math.random()*2);
					if(leftOrRight==1) {
						leftOrRight = '+=';
					} else {
						leftOrRight = '-=';
					}
					var nextLeft = Math.floor(Math.random()*xDistance);
					var nextTop = yDistance;
					
					if(parseInt($(this).css('top'))+nextTop > container.height()) {
						$(this).animate({
							'top':'+='+yDistance,
							'left': leftOrRight+nextLeft+'px'
						}, speed, function() {
							$(this).remove();
							var randomSelector = Math.floor(Math.random()*objects.length);
							var obj = $(objects[randomSelector]).clone();
							var initLeft = container.width() - Math.floor(Math.random()*container.width());
							var initTop = -Math.floor(Math.random()*container.height());
							obj.css({
								'top':initTop+'px',
								'left':initLeft+'px',
								'position':'absolute'
							});
							container.append(obj);
							$(obj).animate({
								'top': '+='+nextTop+'px',
								'left': leftOrRight+nextLeft+'px'
							}, { 
								duration: speed, 
								specialEasing: {
									'top': easingTop,
									'left':	easingLeft
								}
							});
						});
					} else {
						$(this).animate({
							'top': '+='+nextTop+'px',
							'left': leftOrRight+nextLeft+'px'
						}, { 
							duration: speed, 
							specialEasing: {
								'top': easingTop,
								'left':	easingLeft
							}
						});
					}
					
				});
				
				timeout = setTimeout(function() {
					playRainingObjects();
				}, speed);
			}
		},
		destroy: function() {
			$(this).find('.rainWrapper').remove();  
			//return this.each(function(){
				//$(this).removeData();
				//$(this).die();
				//console.log($(this).html());
				/*$(this).children().each(function() {
					$(this).remove();
				});*/
			//})
			/*clearTimeout(timeout);
			container.each(function() {
				$(this).stop(true,true);
				$(this).remove();
			});
			objects=[];*/
		}
	};
	
	$.fn.rainingObject = function( method ) {
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
		}    
	};
})(jQuery);
