(function($)
{
	$.fn.particles = function(volume)
	{
		/* Quick function executor */
		exec = 1;

		/* Quick image path assignment; This path is local to the home page */
		images = "./themes/classic-mojo/images/particles/";

		if(exec>0)
		{
			/* Calculate processing time */
			function calcSpeed()
			{
				var dateVarA = new Date();

				for (var a=0, b=1; a<100000; a++) b++;

				var dateVarB = new Date();

				return (dateVarB.getTime() - dateVarA.getTime()) / 1000;
			}

			/* Adjust particle volume according to simulated processing speed */
			if(calcSpeed() < 0.001)
			{
				if(volume > 80)
				{
					var totalParticles = 80;
				}

				else
				{
					var totalParticles = volume;
				}
			}

			else if(calcSpeed() < 0.0025)
			{
				if(volume > 40)
				{
					var totalParticles = 40;
				}

				else
				{
					var totalParticles = volume;
				}
			}
			else if(calcSpeed() < 0.0035)
			{
				var adjustedVolume = volume - ((calcSpeed() * 1000) * 2);
				var totalParticles = Math.round(adjustedVolume);
			}

			else
			{
				totalParticles = 12;
			}

			/* Create the particles container and style it */
			container = $(this);

			container.append('<div id="particles"></div>');

			$("#particles").css(
			{
				"overflow": "hidden",
				"position": "absolute",

				"height": "100%",
				"width": "100%",

				"z-index": "1",
			});

			/* Create and animate the particles */

			container.each(function()
			{
				var particle = function()
				{
					var self = this;

					this.imagePath = images;
					this.imageArray = ["hard-small.png", "hard-medium.png", "hard-small.png", "hard-large.png", "soft-small.png", "soft-large.png"];
					this.imageSelect = this.imageArray[Math.floor(Math.random() * this.imageArray.length)];
					this.imageGenerate = this.imagePath + this.imageSelect;
					this.imageDeploy = document.createElement("img");

					this.newSpeed().newPoint().display().newPoint().fly();
				};

				particle.prototype.display = function()
				{
					$(this.imageDeploy).attr("src", this.imageGenerate).css(
					{
						"position": "absolute",
						"z-index": Math.floor(Math.random() * 20),
						"top": this.Y,
						"left": this.X
					});

					if(!$.browser.msie)
					{
						$(this.imageDeploy).attr("src", this.imageGenerate).css(
						{
							"opacity": 0,
							"position": "absolute",
							"z-index": Math.floor(Math.random() * 20),
							"top": this.Y,
							"left": this.X
						});
					}

					else
					{
						$(this.imageDeploy).attr("src", this.imageGenerate).css(
						{
							"position": "absolute",
							"z-index": Math.floor(Math.random() * 20),
							"top": this.Y,
							"left": this.X
						});
					}

					$("#particles").append(this.imageDeploy);

					return this;
				};

				particle.prototype.fly = function()
				{
					var self = this;

					if(!$.browser.msie)
					{
						opacity = String(Math.round(Math.random() * 100) / 100);

						$(this.imageDeploy).animate(
						{
							"opacity" : opacity,
							"top" : this.Y,
							"left" : this.X
						}, this.speed, function()
						{
							self.newSpeed().newPoint().fly();
						});
					}

					else
					{
						$(this.imageDeploy).animate(
						{
							"top" : this.Y,
							"left" : this.X
						}, this.speed, function()
						{
							self.newSpeed().newPoint().fly();
						});
					}
				};

				particle.prototype.newSpeed = function()
				{
					this.speed = Math.floor((Math.random() * 20) + 5) * 1100;

					return this;
				};

				particle.prototype.newPoint = function()
				{
					if(!$.browser.msie)
					{
//						this.X = Math.floor(Math.random() * (window.innerWidth - 100));
//						this.Y = Math.floor(Math.random() * (window.innerHeight - 100));

						this.X = Math.floor(Math.random() * (window.innerWidth - 100));
						this.Y = Math.floor(Math.random() * (222));
					}

					else
					{
//						this.X = Math.floor(Math.random() * (document.body.offsetWidth - 100));
//						this.Y = Math.floor(Math.random() * (document.body.offsetWidth - 100));

						this.X = Math.floor(Math.random() * (document.body.offsetWidth - 100));
						this.Y = Math.floor(Math.random() * (222));
					}

					return this;
				};

				particle.prototype.random = function(max)
				{
					return Math.ceil(Math.random() * max) - 1;
				}

				var particles = new Array();

				for(i = 0; i<totalParticles; i++)
				{
					particles[i] = new particle();
				}

			});
		}
	}
})(jQuery);
