//This file and its algorithms copyright (C) 2007 DragonLord Enterprises, Inc. DBA Sequoia Consulting.
//All rights reserved.
//Copying, distribution, or substantially similar derivative works
//strictly prohibited without prior license.
//(818) 810-6671
//Feb 06 '09  IE requires local variables to explicitly have a var in front.
//            Also can't say Obj.class = "House".
//            IE does not honor setattribute = "STYLE", have to do things the long way.
//            And this means...
// can't do this:    //Houses[i].mytitlediv.setAttribute("STYLE", 
//                   //   "position: absolute; top: 20px; z-index: 10; width: 120px; text-align: center; ");
//  must do this:/    //Houses[i].mytitlediv.style.background = "#EEEEFF";
//    Houses[i].mytitlediv.style.position = "absolute";
//    Houses[i].mytitlediv.style.top = "20px";
//    Houses[i].mytitlediv.style.left = "2px";    
//    Houses[i].mytitlediv.style.zIndex = 10;
//    Houses[i].mytitlediv.style.width = "120px";
//    Houses[i].mytitlediv.style.textAlign = "center";
	//Houses[i].class = "House";   //IE did not like this line, does not like assigning class.
//Feb 07 '09 Under IE, appendChild violently breaks the page load when called from JS inside a table, it seems.
//              JS MUST be loaded at the end of the file, not in the middle of a table.
//Feb 07 '09  setInterval of 0 heartbeat broken under IE, freezes.

var isIE = 0;   //Feb 06 '09

/////////////////////////////////////////////////////////////////////
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a 0  (was -1)
// (indicating the use of another browser).
{
  var rv = 0; //-1; // Return value assumes failure.  WAS -1
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
//function checkVersion()
//{
//  var msg = "You're not using Internet Explorer.";
//  var ver = getInternetExplorerVersion();
//
//  if ( ver > -1 )





var arena = document.getElementById("arena");
var arena2 = document.getElementById("arena2");

var ball = new Image();
ball.src = "../images/balls/100red.gif";
var ball2 = new Image();
ball2.src = "../images/balls/100blue.gif";
//arena.appendChild(ball);
//arena2.appendChild(ball2);

var dispbuf = arena;
var drawbuf = arena2;

function bufferswap ()
{
	dispbuf.parentNode.replaceChild(drawbuf, dispbuf);
	var temp = dispbuf;
	dispbuf = drawbuf;
	drawbuf = temp;	
	dispbuf.style.height = 360;
	

	//	for(var i=0; i<ballcount; i++)
	//	{
	//		var ball = balls[i];
	//		temp = ball.front;
	//		ball.front = ball.back;
	//		ball.back = temp;
	//	}	
	
}

//480 x 360
var ballcount = 102;
var balls =
[
{ x: -88, y:12, z:0, vx: 1, vy: 2, vz: 3,   rad:8, front:new Image(), back:new Image()},
{ x: -68, y:20, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: -62, y:40, z:0, vx: 1, vy: 2, vz: 3,  rad:14, front:new Image(), back:new Image()},
{ x: -55, y:5, z:0, vx: 1, vy: 2, vz: 3,  rad:14, front:new Image(), back:new Image()},
{ x: -45, y:45, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: -37, y:60, z:0, vx: 1, vy: 2, vz: 3,  rad:14, front:new Image(), back:new Image()},
{ x: -41, y:70, z:0, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x: -20, y:65, z:0, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x: -30, y:30, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: -37, y:10, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x:-8, y:30, z:0, vx: 1, vy: 2, vz: 3,  rad:14, front:new Image(), back:new Image()},
{ x:-2, y:10, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: 10, y:8, z:0, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x:-18, y:0, z:0, vx: 1, vy: 2, vz: 3,  rad:14, front:new Image(), back:new Image()},

{ x:-15, y:-22, z: 10, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},

{ x:-16, y:-15, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x:-28, y:-16, z:0, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
//{ x:-15, y:-22, z: -10, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x:-25, y:-28, z:20, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x:-37, y:-35, z:30, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
  { x: -8, y:-45, z:20, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x:-5, y:-35, z:10, vx:1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},

//{ x: -8, y:-45, z:20, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 3, y:-50, z:30, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
  { x: 5, y:-32, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: 0, y:-19, z:-8, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
//{ x: 5, y:-32, z:0, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: 13, y:-37, z:3, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x: 28, y:-26, z:7, vx: 1, vy: 2, vz: 3,  rad:15, front:new Image(), back:new Image()},
{ x: 30, y:-37, z:14, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x: 20, y:-18, z:14, vx: 1, vy: 2, vz: 3,  rad:8, front:new Image(), back:new Image()},
{ x: 30, y:-15, z:-5, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 45, y:-15, z:0, vx: 1, vy: 2, vz: 3,  rad:22, front:new Image(), back:new Image()},
{ x: 45, y:-30, z:5, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 45, y:  0, z:5, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 60, y:-15, z:-5, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 70, y:-4, z:-15, vx: 1, vy: 2, vz: 3,  rad:22, front:new Image(), back:new Image()},
{ x: 80, y:-12, z:-15, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 80, y: 7, z:-25, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 60, y: 4, z:-15, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 60, y: 15, z:-20, vx: 1, vy: 2, vz: 3,  rad:22, front:new Image(), back:new Image()},
{ x: 75, y: 15, z:-30, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 45, y: 15, z:-30, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},

{ x: 60, y:30, z:-15, vx: 1, vy: 2, vz: 3,  rad:13, front:new Image(), back:new Image()},
{ x: 20, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 30, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 40, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 50, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 60, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 70, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 80, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x: 90, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:100, y:50, z:0, vx: 1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-10, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-20, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-30, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-40, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-50, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-60, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-70, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-80, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-90, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},
{ x:-100, y:50, z:0, vx: -1, vy: 2, vz: 3,  rad:5, front:new Image(), back:new Image()},

{ x: 10, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:10, front:new Image(), back:new Image()},
{ x: 20, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 30, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 40, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 50, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 60, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 70, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 80, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 90, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:100, y:-50, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-10, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:10, front:new Image(), back:new Image()},
{ x:-20, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-30, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-40, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-50, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-60, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-70, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-80, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-90, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-100, y:-50, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},

{ x: 10, y:-70, z:0, vx: 1, vy: -4, vz: -10,  rad:10, front:new Image(), back:new Image()},
{ x: 20, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 30, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 40, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 50, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 60, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 70, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 80, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x: 90, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:100, y:-70, z:0, vx: 1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-10, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-20, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-30, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-40, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-50, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-60, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-70, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-80, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-90, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-100, y:-70, z:0, vx: -1, vy: -2, vz:-3,  rad:5, front:new Image(), back:new Image()},

{ x:-90, y:-70, z:0, vx: -1, vy: -2, vz: -3,  rad:5, front:new Image(), back:new Image()},
{ x:-100, y:-70, z:0, vx: -1, vy: -2, vz:-3,  rad:5, front:new Image(), back:new Image()},

];

function setupballs()
{	var i;
	balls[-1] = {  z:-1000000 };  // backstop for sort.  
	for(i=0; i<16; i++)
	{
		var ball  = balls[i*6];
		var ball2 = balls[i*6+1];	
		var ball3 = balls[i*6+2];
		var ball4 = balls[i*6+3];	
		var ball5 = balls[i*6+4];	
		var ball6 = balls[i*6+5];						

		ball.front.style.position = "absolute";
		arena.appendChild(ball.front);
		ball2.front.style.position = "absolute";
		arena.appendChild(ball2.front);
		ball3.front.style.position = "absolute";
		arena.appendChild(ball3.front);
		ball4.front.style.position = "absolute";
		arena.appendChild(ball4.front);
		ball5.front.style.position = "absolute";
		arena.appendChild(ball5.front);
		ball6.front.style.position = "absolute";
		arena.appendChild(ball6.front);										
		

		ball.back.style.position = "absolute";	
		arena2.appendChild(ball.back);	
		ball2.back.style.position = "absolute";	
		arena2.appendChild(ball2.back);	
		ball3.back.style.position = "absolute";	
		arena2.appendChild(ball3.back);	
		ball4.back.style.position = "absolute";	
		arena2.appendChild(ball4.back);	
		ball5.back.style.position = "absolute";	
		arena2.appendChild(ball5.back);	
		ball6.back.style.position = "absolute";	
		arena2.appendChild(ball6.back);	
		
		ball.rad  = 15; 
		ball2.rad = 15; 
		ball3.rad = 15; 
		ball4.rad = 15; 
		ball5.rad = 15; 
		ball6.rad = 15; 
		
		ball.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "purple.gif";
		ball.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "purple.gif";		

		ball4.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "purple.gif";
		ball4.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "purple.gif";		

		ball5.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "dkgrey.gif";
		ball5.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "dkgrey.gif";		

		ball6.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "dkgrey.gif";
		ball6.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "dkgrey.gif";		

		switch( i%8 )
		{
		case 0: case 5:
		ball2.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "yellow.gif";
		ball2.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "yellow.gif";		

		ball3.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "blue.gif";
		ball3.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "blue.gif";
		break;	
		case 1: case 2: case 3:
		ball2.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "red.gif";
		ball2.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "red.gif";		

		ball3.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "green.gif";
		ball3.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "green.gif";
		break;	
		case 4: case 6:
		ball2.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "blue.gif";
		ball2.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "blue.gif";		

		ball3.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "yellow.gif";
		ball3.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "yellow.gif";
		break;
		case 7: default:
		ball2.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "green.gif";
		ball2.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "green.gif";		

		ball3.front.src = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "red.gif";
		ball3.back.src  = "http://www.seqcon.com/images/balls/" + (ball.rad*2) + "red.gif";
		break;	
		}								
	}
}

var phase=0;
var deg_to_rad = 0.017453293;
function simulate()
{	var i;

	phase += 5;

	for(i=0; i<16; i++)
	{
		var ball  = balls[i*6];
		var ball2 = balls[i*6+1];	
		var ball3 = balls[i*6+2];
		var ball4 = balls[i*6+3];	
		var ball5 = balls[i*6+4];	
		var ball6 = balls[i*6+5];	
		
		ball.y = ball2.y = ball3.y = ball4.y = (i-8) * 11 * 2;
		ball5.y = ball6.y = (i-8) * 11 * 2  + 11;
		
		var angle = i * 30 + phase;
		var sin = Math.sin( angle * deg_to_rad );
		var cos = Math.cos( angle * deg_to_rad ); 
		var cutoff = Math.cos(angle * deg_to_rad / 8) * 8 + 8;
		var dist1 = 15 + ((cutoff > i) ? (cutoff-i) * 10 : 0 );
		var dist2 = dist1 + 30 ;
		
		ball.x = -cos * dist2;  ball2.x = -cos * dist1;  ball3.x = cos * dist1;  ball4.x = cos * dist2;
		ball.z = -sin * dist2;  ball2.z = -sin * dist1;  ball3.z = sin * dist1;  ball4.z = sin * dist2;
//		ball.x += ball.y;  ball2.x += ball2.y;  ball3.x += ball3.y;  ball4.x += ball4.y;


		
		angle = (i + 0.5) * 30 + phase;
		sin = Math.sin( angle * deg_to_rad );
		cos = Math.cos( angle * deg_to_rad ); 
		dist1 = 15   + ((cutoff > i+0.5) ? (-i-0.5 + cutoff) * 10 : 0 );
		var dist2 = dist1 + 30;
		
		ball5.x = -cos * dist2;  ball6.x = cos * dist2;
		ball5.z = -sin * dist2;  ball6.z = sin * dist2;
//		ball5.x += bally5.y;  ball6.x += ball6.y;
	}
	
}

//zIndex is not working for some reason.  
//So we need to roll our own.
//  Zdist is distance from camera.
//  We therefore sort our tiles back to front, 0 to count-1.
//  Tile # -1 is at +infinity, and is a "property", not an array element.
//  O(n) if balls are already in order.
function zsortballs()
{	var i, j, tempball, tempz;

	return;
	
	for(j=1; j<ballcount; j++)
	{
		i = j-1;
		tempball = balls[j];
		while( tempball.z < balls[i].z )
		{
			balls[i+1] = balls[i];
			i--;
		}
		if( i < j-1 )
		{
			balls[i+1] = tempball;
			arena.insertBefore( tempball.front, balls[i+2].front );
			arena2.insertBefore( tempball.back, balls[i+2].back  );
		
		}	
	}
}


var perspective = 0;
var jitter = 0;
function drawstuff()
{	var i;
if(0){ //(perspective){
		if(dispbuf == arena)
		{
			for(i=0; i<ballcount; i++)
			{
				//drawball( balls[i] );
				var ball = balls[i];	
				var z = ball.z;	
				var diam = (ball.rad * 2000)/(1100 + z);
				ball.front.src = "../html/images/balls/" + Math.round(diam) + "red.gif";
				ball.front.style.left = ((ball.x * 1000)/(1100+z) + 100)*479/200 -diam/2 + "px";
				ball.front.style.top  = (100 - (ball.y * 1000)/(1100+z))*359/200 -diam/2 + "px";
				ball.front.zIndex = 0; //-z * 10;  	
			}
			zsortballs();
		}
		else
		{
			for(i=0; i<ballcount; i++)
			{
				//drawball( balls[i] );
				var ball = balls[i];
				var z = ball.z;	
				var diam = (ball.rad * 2000)/(1100 + z);						
				ball.back.src = "../html/images/balls/" + Math.round(diam) + "red.gif";
				ball.back.style.left = ((ball.x * 1000)/(1100+z) + 100)*479/200 -diam/2 + "px";
				ball.back.style.top  = (100 - (ball.y * 1000)/(1100+z))*359/200 -diam/2 + "px";
				ball.back.zIndex = 0; //-ball.z * 10;  	
			}
			zsortballs();
		}
	}else{	//perspective
		if(dispbuf == arena)
		{
			if( jitter )  //only jitter balls on front.
			{
				for(i=0; i<ballcount; i++)
				{
					var ball = balls[i];	
					ball.front.style.left = (ball.x + 100)*200/200 + 400/2 -ball.rad   + ((ball.x % ball.rad) % 3) -1 + "px";
					ball.front.style.top  = (100 - ball.y)*200/200 + 140/2 -ball.rad   + ((ball.y % ball.rad) % 3) -1 + "px";
					ball.front.style.zIndex = Math.floor( 1000 -ball.z * 10);  	
				}
			}
			else
			{
				for(i=0; i<ballcount; i++)
				{
					var ball = balls[i];	
					ball.front.style.left = (ball.x + 100)*200/200 + 400/2 -ball.rad   + "px";
					ball.front.style.top  = (100 - ball.y)*200/200 + 140/2 -ball.rad   + "px";
					ball.front.style.zIndex =  Math.floor( 1000 -ball.z * 10);  	
				}
			}
			//zsortballs();				
		}
		else
		{
			for(i=0; i<ballcount; i++)
			{
				var ball = balls[i];
				ball.back.style.left = (ball.x + 100)*200/200 + 400/2 -ball.rad + "px";
				ball.back.style.top  = (100 - ball.y)*200/200 + 140/2  -ball.rad + "px";
				ball.back.style.zIndex =  Math.floor( 1000 -ball.z * 10);  	
			}
			//zsortballs();			
		}
	}//perspective	
}

var movement=1;
//var theDate = new Date();
//var start = theDate.getTime();
function heartbeat()
{
//setupballs();

	if(movement)
	simulate();
	drawstuff();
	bufferswap ();

//	t=setTimeout("heartbeat()",0);  //deprecated.
}

isIE = getInternetExplorerVersion();
setupballs();
//drawstuff();
//heartbeat();  //deprecated. Use next.
setInterval ( "heartbeat()", 1 );  //Time in msec.   0 doesn't work on IE.




