//Copyright© 2004 Hongjun Zhou. Firework animation. All rights reserved.
var hexChars = "0123456789ABCDEF";

range=100;

function Dec2Hex (d) {
	var a = d % 16;
	var b = (d - a)/16;
	hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
	return hex;
}

//color
var color="";

var width=700;
var height=500;
var timerID = null;
var nf=20;
var g=9.8;
var scale=0.1;
var timestep=50;
if (parseInt(navigator.appVersion)>3) {

	top.resizeTo(width+300,width+250);
}

//initail projectile angle/velocty
var v0=Math.sqrt(height*(2.0*g));

//initial shooting origin
x0=new Array(nf);
y0=new Array(nf);
x=new Array(nf);
y=new Array(nf);
theta=new Array(nf);
t=new Array(nf);

winW=width+50;
winH=height+50;


window.onload=startclock;

for (i=0;i<nf;i++) {
	y0[i]=height-1;
	y[i]=y0[i];
	x0[i]=width*Math.random();
	theta[i]=3.14159265*Math.random();
	t[i]=0;
}



function stopclock() {
	if(timerID != null)
	clearTimeout(timerID);

}

function startclock() {
	// Make sure the clock is stopped
	if (timerID != null) {
			clearTimeout(timerID);
	}
	runTimer();

}

function runTimer() {
//projectile trajectory: x=v0*cos(theta0*t)
//                       y=v0*sin(theta0*t)-1/2*g*t^2
//max height: h=(v0^2/2g)*sin(theta)^2
	for (i=0;i<nf;i++) {
		if (y[i] > height || y[i] < 0 || x[i] > width || x[i] < 0) {
			theta[i]=3.14159265*0.5*(1.0+0.7*(0.5-Math.random()));
			x0[i]=width/2+100.0*Math.random();
			t[i]=0;
			y[i]=y0[i];
			x[i]=x0[i];
			rr=Math.round(Math.random()*255);
			gg=Math.round(Math.random()*255);
			bb=Math.round(Math.random()*255);
			color=Dec2Hex(rr)+Dec2Hex(gg)+Dec2Hex(bb);
			document.getElementById(i).style.color="#"+color;

		} else {
			x[i]=x0[i]+v0*t[i]*scale*Math.cos(theta[i]);
			y[i]=y0[i]-v0*t[i]*scale*(Math.sin(theta[i]))+0.5*g*t[i]*t[i]*scale*scale;
			document.getElementById(i).style.left=Math.round(x[i])+"px";
			document.getElementById(i).style.top=Math.round(y[i])+"px";
			t[i]=t[i]+1;
		}
	}

	timerID = setTimeout("runTimer()",timestep);

}

