
<!-- Begin

// Parameters
var n = 5; // number of planets
var step_size = 40; // smaller number moves the snow faster
var star = "http://www.seaofechoes.com/js/circle_black.gif";
var planet = "http://www.seaofechoes.com/js/circle_white.gif";

// Window size
var ie4up = (document.all) ? 1 : 0;
var i, doc_width = 80, doc_height = 80;
if (ie4up) {
	doc_width = 80;
	doc_height = 80;
}

var xmax = doc_width/2;
var ymax = doc_height/2;
var zmax = Math.min(doc_width,doc_height);

var xoff = Math.floor(doc_width/2);
var yoff = Math.floor(doc_height/2);
var zoff = Math.floor(zmax/2);

var star_size=0;
var planet_size=5;

function draw_star () {
	// Central star

	document.write("<div id=\"star\" ");
	document.write("style=\"POSITION: relative; ");
	document.write("        Z-INDEX: "+ yoff +";");
	document.write("        VISIBILITY: visible; filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25; ");
	document.write("        TOP: "+ (zoff-star_size/2)+ ";");
	document.write("        LEFT: "+ (xoff-star_size/2)+ ";");
	document.write("      \" >");
	document.write("	<img src="+star+" style=\"border: none; padding:none; \"  height="+star_size+">");
	//document.write(yoff);
	document.write("</div>");
}

// Planet state variables - in the coordinate space of the star
var x = new Array();
var y = new Array();
var z = new Array();

var vx = new Array();
var vy = new Array();
var vz = new Array();

var g = 1; // acceleration constant
var eccentricity = 0.1; // deviation from eliptical
var z_eccentricity = 0.1; // elliptical constant
var radius = 0.5; // elliptical constant
var orbit = 0;
var temperature = 1; // gas temperature
var planet_mass=1;
var star_mass=10;

// Initial value of variables
function init_planets () {
	var sum_vx=0;
	var sum_vy=0;
	var sum_vz=0;

	for (i = 0; i < n; ++ i) {  // iterate for every dot
		x[i] = radius * (Math.random()*2*xmax - xoff);
		y[i] = radius * (Math.random()*2*ymax - yoff);
		z[i] = z_eccentricity*  (Math.random()*2*zmax - zoff);

		// Make sure all initial orbits are circular
		// by relating the initial velocities
		if (orbit) {
			// for a circular orbit, the velocity must be proportional
			// to the square of the distance from the center
			r_squared =  x[i]*x[i] + y[i]*y[i] + z[i]*z[i];
			r = Math.sqrt(r_squared);
			v = Math.sqrt(g)/r;

			vx[i] = y[i]*v*(1 - eccentricity*(Math.random()-0.5));
			vy[i] = -x[i]*v*(1 - eccentricity*(Math.random()-0.5));
			vz[i] = z_eccentricity*(Math.random()-0.5)/r_squared;
		}
		else {
			if (i < n-1) {
				vx[i] = temperature * (Math.random()-0.5);
				vy[i] = temperature * (Math.random()-0.5);
				vz[i] = temperature * (Math.random()-0.5);

				// make sure the total momentum is zero
				sum_vx += vx[i];
				sum_vy += vy[i];
				sum_vz += vz[i];
			}
			else {
				// make sure momentum is zero
				vx[i] = -sum_vx;
				vy[i] = -sum_vy;
				vz[i] = -sum_vz;
			}
		}

		document.write("<div id=\"planet"+i+"\" ");
		document.write("style=\"POSITION: relative; ");
		document.write("        Z-INDEX: "+ (yoff-y[i]) +";");
		document.write("        VISIBILITY: visible; BORDER: none; MARGIN: 0px; PADDING: none;");
		document.write("        BACKGROUND: transparent;  filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25;");
		document.write("        TOP: "+ (z[i]+zoff-planet_size/2)+ ";");
		document.write("        LEFT: "+ (x[i]+xoff-planet_size/2)+ ";");
		document.write("      \" >");
		document.write("	<img id=\"planet_img"+i+"\" src="+planet+"  style=\"border:none; padding:1px; \" height="+planet_size+">");

		// attach some text to the planet for debug 
		//document.write("<form id=\"planet_text"+i +"\" >");
		//document.write("	<input id=\"planet_btn"+i+"\" type=button value=\"undef\" >");
		//document.write("</form>");

		document.write("</div>");
	}
}


// Display a planet based on its x,y,z positions
// Initially set up the camera on the y axis pointing to the origin.
camera_x = 0;
camera_y = Math.min(doc_width,doc_height)/2;
camera_z = 0
camera_r = Math.sqrt(camera_x*camera_x + camera_y*camera_y +camera_z*camera_z);

var camera_vx = 0;
var camera_vy = -1;
var camera_vz = 0;

function draw_planet (i) {
	var dx = x[i]-camera_x;
	var dy = y[i]-camera_y;
	var dz = z[i]-camera_z;

	var distance_to_planet = Math.sqrt (dx*dx+dy*dy+dz*dz);
	var apparent_size = planet_size * camera_r/distance_to_planet;

	document.all["planet_img"+i].height = apparent_size;
	document.all["planet"+i].style.pixelLeft = x[i] + xoff-apparent_size/2;

	//document.all["planet"+i].style.pixelTop = z[i] + zoff-apparent_size/2;
	document.all["planet"+i].style.pixelTop = y[i] + yoff-apparent_size/2;
	document.all["planet"+i].style.zindex = yoff - x[i];

	//document.all["planet_btn"+i].value = yoff - x[i];
}


// IE main animation function 
function orbitIE() {

	// Compute position of each dot
	for (i = 0; i < n; ++ i) {
		r= Math.sqrt (x[i]*x[i] + y[i]*y[i] + z[i]*z[i]);

		// Now change velocity	
		// Compute attraction to the central star
		var star=1;

		if (star) {
			// Check for collision
			if (r < 2.0) {
				vx[i] = -vx[i];
				vy[i] = -vy[i];
				vz[i] = -vz[i];
			}
			else {
				// the "r" term should be r^3, but that allows
				// planets to slingshot away too easily
				vx[i] -= star_mass*g*x[i]/(r*r);
				vy[i] -= star_mass*g*y[i]/(r*r);
				vz[i] -= star_mass*g*z[i]/(r*r);
			}
		}

		// Now compute the attraction to each other planet
		for (j = 0; j < n; ++ j) {  // iterate for every dot
			if (i!=j) {
				var dx = x[j] - x[i];
				var dy = y[j] - y[i];
				var dz = z[j] - z[i];
				r = Math.sqrt(dx*dx+dy*dy+dz*dz);

				if (r > 1) {
					vx[i] += planet_mass*g*dx/(r*r);
					vy[i] += planet_mass*g*dy/(r*r);
					vz[i] += planet_mass*g*dz/(r*r);
				}
				// Compute half an elastic collision
				else {
					// ...
				}
			}
		}
	}

	bounce_loss = -0.9;

	// Update positions 
	for (i = 0; i < n; ++ i) {  // iterate for every dot
		x[i] += vx[i];
		y[i] += vy[i];
		z[i] += vz[i];

		// Check for out of bounds
		if (x[i] >= xoff-5 || x[i] <= -xoff) {
			vx[i] = - bounce_loss * vx[i];
		}
		if (y[i] >= yoff-5 || y[i] <= -yoff) {
			vy[i] = - bounce_loss * vy[i];
		}
		if (z[i] > zoff-5 || z[i] < -zoff) {
			vz[i] = - bounce_loss * vz[i];
		}

		draw_planet(i);
	}		

	setTimeout("orbitIE()", step_size);
}



if (ie4up) {

	document.write("<div id=\"bbox\" ");
	document.write("style=\"POSITION: relative; ");
	document.write("        Z-INDEX: "+ yoff +";");
	document.write("        VISIBILITY: visible; filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25; ");
	document.write("        TOP: "+ 0 + ";");
	document.write("        LEFT: "+ 0 +";");
//	document.write("        HEIGHT: "+ 100 + ";");
//	document.write("        WIDTH: "+ 100 + ";");
	document.write("      \" >");
	//document.write(yoff);
	document.write("</div>");

	if(star_mass > 0) { 
		//draw_star();
	}
	init_planets();
	orbitIE();
}


// End -->

