function Emitter(canvasId, startX, startY)
{
	this.startX = startX;
	this.startY = startY;
	this.color = new Array(255, 255, 255);
	this.size = 1;
	this.gravity = 10;
	this.friction = .9;
	this.particles = new Array();
	this.maxParticles = 60000;
	this.maxBlastParticles = 0;
	this.canvasContext = document.getElementById(canvasId).getContext("2d")

	this.createParticle = function()
	{
		if(this.particles.length > 9000)
		{
			this.particles[0].lifeSpan = 1;
			this.particles[0].velocityX = (Math.random() * 40) - 20;
			this.particles[0].velocityY = (Math.random() * 10) - 15;
			this.particles[0].x = this.startX;
			this.particles[0].y = this.startY;
			this.particles.push(this.particles.shift());
		}
		else
		{
			var particle = new Particle();
			particle.x = this.startX;
			particle.y = this.startY;
			particle.velocityX = (Math.random() *4);
			particle.velocityY = (Math.random() * 50) - 20;
			
			this.particles.push(particle);
		}
	}
	
	this.step = function()
	{
		for(i = 0; i < this.particles.length; i++)
		{			
			this.particles[i].x -= this.particles[i].velocityX;
			this.particles[i].y -= this.particles[i].velocityY;
			this.particles[i].velocityX -= (1);
			this.particles[i].velocityY;
			this.particles[i].velocityY -= this.gravity;
			this.particles[i].lifeSpan;	
			
			if(this.particles[i].lifeSpan < 0) 
			{
				this.particles.splice(i, 1);
			}
		}
	}
	
	this.draw = function()
	{
		this.canvasContext.clearRect(0, 0, 800, 600);
		
		for(i = 0; i < this.particles.length; i++)
		{
			this.canvasContext.fillStyle = 'rgba('+this.color[0]+','+this.color[1]+','+this.color[2]+','+this.particles[i].lifeSpan+')'; 
			this.canvasContext.beginPath();
			this.canvasContext.arc(this.particles[i].x, this.particles[i].y, this.size*this.particles[i].lifeSpan-.5, 0, 1, true); 
			this.canvasContext.fill();
		}
	}
	
	
}


function Particle()
{	
	this.x = 500;
	this.y = 500;
	this.velocityY = 1;
	this.velocityX = 1;
	this.lifeSpan = 1;
}
function stepEmitter()
{
	emitter.step();
	emitter.draw();	
	emitter2.step();
	emitter2.draw();
	emitter3.step();
	emitter3.draw();
}
var particleFirer;
function fireParticle()
{
	for(i = 0; i < maxBlastParticles; i++) { 
		emitter.createParticle(); 
		emitter.createParticle(); 
		emitter.createParticle(); 
		emitter.createParticle(); 
		emitter2.createParticle(); 
		emitter2.createParticle(); 
		emitter2.createParticle(); 
		emitter2.createParticle(); 
		emitter3.createParticle(); 
		emitter3.createParticle(); 
		emitter3.createParticle(); 
		emitter3.createParticle(); 
		
	}
	particleFirer = setTimeout("fireParticle()", Math.random() * 5);
}

//$(document).ready(setTimeout("createEmitter()", 3000));

var emitter;
var delay = 2;
var maxBlastParticles = 5;
var particleInterval;

function createEmitter(){
	emitter = new Emitter('thecanvas', 10, 10);
	emitter2 = new Emitter('thecanvas2', 15, 20);
	emitter3 = new Emitter('thecanvas3', 5, 20);
	fireParticle();
	particleInterval = setInterval('stepEmitter()', 80);
	setTimeout("stopEmitter()", 2000);
}

function stopEmitter(){
	clearTimeout(particleFirer);
	setTimeout("document.getElementById('thecanvas').style.display = 'none'", 2000);
	setTimeout("document.getElementById('thecanvas2').style.display = 'none'", 2000);
	setTimeout("document.getElementById('thecanvas3').style.display = 'none'", 2000);
	//document.getElementById('thecanvas').style.display = 'none';
}
	
