var a = 40;
var timeFactor = 300;
var size = 1;
var arcb = 0;
var arcv = 180;
var vb = 0;
var vv = 110;

var flakeColours = ["FFBBBB","BBFFBB","BBBBFF","BBFFFF","FFBBFF","FFFFBB"];

var numFlakes = 24;
var maxFlakes = 96;

var userAgent = navigator.userAgent;
var ns=false;
var ns6=false;
var ie=false;
var moz=false;

if (userAgent.indexOf("MSIE") != -1) { ie = true; }
else if (userAgent.indexOf("Netscape6") != -1) { ns6 = true; }
else if (userAgent.indexOf("Gecko") != -1) { moz = true; }
else { ns = true; }

if (ns6) {numFlakes = 18;}

var xmouse = 200;
var ymouse = 200;
var maxL, maxR, maxD, maxU;

function getLimits() {
	if (ie) {
		maxL = document.body.scrollLeft;
		maxR = document.body.scrollLeft+document.body.clientWidth-size;
		maxU = document.body.scrollTop;
		maxD = document.body.scrollTop+document.body.clientHeight-size;
	}
	else if (ns || ns6 || moz) {
		maxL = window.pageXOffset;
		maxR = window.pageXOffset+window.innerWidth-size - 15;
		maxU = window.pageYOffset;
		maxD = window.pageYOffset+window.innerHeight-size;
	}

}

function Mouse(evnt){

	if (ie) {
		ymouse = event.y+document.body.scrollTop;
		xmouse = event.x+document.body.scrollLeft;		
	}
	else if (ns6 || moz || ns) {
		ymouse = evnt.pageY;
		xmouse = evnt.pageX;
	}
	
	getLimits();

	if (xmouse == null || xmouse < maxL || xmouse > maxR) {
		xmouse = Math.random()*(maxR-maxL)+maxL;
	}
	if (ymouse == null || ymouse > maxD) {
		ymouse = Math.random()*(maxD-maxU)+maxU;
	}

}

if (ie) {
	document.onmousemove=Mouse;
}
else if (ns6 || ns || moz) {
	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove=Mouse;
}

for (i = 1; i <= maxFlakes; i++) {

	colour = flakeColours[(i-1)% 6];

	if (ns) {
		document.write('<LAYER NAME="nsFlake'+i+'" height="'+size+'" width="'+size+'" bgcolor="#'+colour+'"> </LAYER>');
		document.layers["nsFlake" + i].visibility = false;
	}
	else if (ie) {
		document.write('<div id="ieFlake'+i+'" style="position:absolute;top:-10px;left:-10px;height:'+size+'px;width:'+size+'px;font-size:0pt;background-color:#'+colour+';"></div>');
	}
	else if (ns6) {
		document.write('<div id="ns6Flake'+i+'" style="position:absolute;top:-10px;left:-10px;height:'+size+'px;width:'+size+'px;font-size:0pt;background-color:#'+colour+';"></div>');
	}
	else if (moz) {
		document.write('<div id="mozFlake'+i+'" style="position:absolute;top:-10px;left:-10px;height:'+size+'px;width:'+size+'px;font-size:0pt;background-color:#'+colour+';"></div>');
	}

}


var c = new Confetti();

for (i = 1; i <= maxFlakes; i++) {
	var element = null;
	
	if (ns) element = document.layers['nsFlake'+i]
	else if (ie) element = document.all['ieFlake' + i].style
	else if (ns6) element = document.getElementById("ns6Flake" + i).style
	else if (moz) element = document.getElementById("mozFlake" + i).style;
	
	f = new Flake("flake" + i, arcb + Math.random()*arcv, vb + Math.random() * vv, element );

	if (ns) {
		f.element.clip.height = size;
		f.element.clip.width = size;
	}
	else if (ie || ns6 || moz) {
		f.element.height="" + size + "px";
		f.element.width="" + size + "px";
	}
	c.add(f);
}

window.onload=throwConfetti;






function Flake(name, theta, v, element)
{
	this.name = name;
	this.theta = theta;
	this.v = v;

	this.x0 = -100;
	this.y0 = -100;
	this.x = -100;
	this.y = -100;

	this.time = 0;
	this.elapsedTime = 0;

	with (Math) {
		this.vx = v*round(cos(theta*PI/180)*100)/100;
		this.vy = v*round(sin(theta*PI/180)*100)/100;
	}

	this.moveFlake = moveFlake;
	this.throwFlake = throwFlake;

	this.element = element;
	this.element.top = this.x;
	this.element.left = this.y;
	
	this.thrown = false;
	
}

function throwFlake(x, y, t) {

	this.x0 = x;
	this.y0 = y;
	this.x = x;
	this.y = y;

	this.theta = arcb + Math.random()*arcv, 
	this.v = vb + Math.random() * vv

	this.time = t;

	with (Math) {
		this.vx = this.v*round(cos(this.theta*PI/180)*100)/100;
		this.vy = this.v*round(sin(this.theta*PI/180)*100)/100;
	}

	if (ns) {
		this.element.clip.height = size;
		this.element.clip.width = size;
	}
	else if (ie || ns6 || moz) {
		this.element.height="" + size + "px";
		this.element.width="" + size + "px";
	}

	this.thrown = true;
	if (ns) this.element.visibility=true;

}

function moveFlake() {

	if (!this.thrown)
	{
		return;
	}

	this.elapsedTime = (new Date() - this.time) / timeFactor;

	this.x = this.x0 + this.vx*this.elapsedTime;
	this.y = this.y0 - this.vy*this.elapsedTime + 0.5*a*this.elapsedTime*this.elapsedTime;

	if (this.x > maxR || this.x < maxL || this.y > maxD) {
		this.thrown = false;
		c.count--;
		this.element.left=-100;
		this.element.top=-100;
	}
	else {
		this.element.left = this.x;
		this.element.top = this.y;
	}

}



function Confetti()
{
	
	this.timer = null;
	this.flakes = new Array();

	this.add = addFlake;
	this.remove = removeFlake;
	this.throwFlakes = throwFlakes;
	this.isEmpty = isEmpty;
	
	this.count=0;
	
}

function addFlake(f) {
	this.flakes[this.flakes.length] = f;
}

function removeFlake(f) {
	this.flakes[f.name] = null;
}

function throwFlakes() {

	t = new Date().getTime();
	
	for (i = 0; i < numFlakes; i++) {
		this.flakes[i].throwFlake(xmouse, ymouse, t);
		this.count++;
	}

	moveFlakes(this);
}

function isEmpty() {
	return (this.count <= 0);
}


function moveFlakes(c) {
	for (i = 0; i < numFlakes; i++) {
		c.flakes[i].moveFlake();
	}
	
	if (c.isEmpty()) {
		clearTimeout(c.timer);
		throwConfetti();
	}
	else {
		c.timer = setTimeout("moveFlakes(c)", 25);
	}
}


function throwConfetti() {

	getLimits();

	if (xmouse == null) {xmouse=0;}
	if (ymouse == null) {ymouse=0;}

	c.throwFlakes();

}


function stopConfetti() {
	for (i = 1; i <= numFlakes; i++) {
		if (ns) {
			document.layers["nsFlake" + i].top = -10;
			document.layers["nsFlake" + i].left = -10;
		}
		else if (ie) {
			document.all['ieFlake' + i].style.top = -10
			document.all['ieFlake' + i].style.left = -10
		}
		else if (ns6) {
			document.getElementById("ns6Flake" + i).style.top = "-10px";
			document.getElementById("ns6Flake" + i).style.left = "-10px";
		}
		else if (moz) {
			document.getElementById("mozFlake" + i).style.top = "-10px";
			document.getElementById("mozFlake" + i).style.left = "-10px";
		}
	}
	clearTimeout(c.timer);
	c.count = 0;
	
}




