//Important! Nothing much will work if we don't run the Zsetup() function. :)
	onload=Zsetup;
	onresize=setStarters;
	Zobjects = new Array(); 
	Zshadows = new Array(); 
	Zrefresh = howFast;
	loaded=0;
numSuns = 3; numFuels = 7; 
	if (options==2) {numSuns = 2; numFuels = 18; }
	if (options==4) {numSuns = 7; numFuels = 3; }
	Zspots = new Array();
	Zworld = new Array();
fudge_factor = 300;
	sectorWide = 2;
	sectorHigh = 2;
	sectorSize=1200;
Xpos = 0; Ypos = 0;
XposBox = 0; YposBox = 0;

	stickiness = 24;
function setStick(num) {stickiness = num;}

// SETUP FUNCTION! ID,wrapper,top,left,height,width,zindex,dir,speed,visibility
function Zsetup() {
	setMass(fudge_factor);
Zbox = new makeZobject("box",null,118,118,12,120,2);
Ztext = new makeZobject("text",null,118,0,60,'100%',1);
Zcontrol = new makeZobject("control",null,118,118,12,120,3);
Zcontrol2 = new makeZobject("control2",null,118,118,12,120,4);

numSectors = 0;
for (a=0;a<sectorHigh;a++) {
	for (b=0;b<sectorWide;b++) {
	Zworld[numSectors] = new makeZobject("sector_"+a+"_"+b,"galaxy",0,0,sectorSize,sectorSize,numSectors+2);
	numSectors++;
	}}

Zgalaxy = new makeZobject("galaxy",null,0,0,sectorSize*sectorHigh,sectorSize*sectorWide,1);

document.getElementById('box').onmousemove=boxMover;

for (x=0;x<numBugs;x++) {
	sunName = "planet";
	tempNum = Zobjects.length;
	
if (x!=0) {
divText = "<div id=\"planet"+tempNum+"\" class=\"newPlanets\"><img src=/gravity/"+sunName+".gif height=23 width=24></div>"; }
else {
divText = "<div id=\"planet"+tempNum+"\" class=\"newPlanets\"><img src=/gravity/planet2.gif height=23 width=24></div>"; }
document.getElementById("galaxy").insertAdjacentHTML( "beforeEnd", divText);
	//makePlanet(ID,wrapper,top,left,height,width,zindex,velX,velY)
Zobjects[tempNum] = new makePlanet("planet"+tempNum,"galaxy",Zbox.top/2,Zbox.left/2,23,24,2000,0,0,0,'visible',0);

divText = "<div id=\"shadows"+tempNum+"\" class=\"newShadows\"><img src=/gravity/shadow2b.gif height=23 width=24></div>";
document.getElementById("galaxy").insertAdjacentHTML( "beforeEnd", divText);
	//makePlanet(ID,wrapper,top,left,height,width,zindex,velX,velY)
Zshadows[tempNum] = new makePlanet("shadows"+tempNum,"galaxy",Zbox.top/2,Zbox.left/2,23,24,500,0,0,0,'visible',0);
	}
for (x=0;x<numSuns;x++) { makeSun(x); }
for (x=0;x<numFuels;x++) { makeFuel(x); }
scatterStop();
fixObjects();
playPause(1);
loaded=1;
}
// end SETUP FUNCTION 

	playing = 0;
function playPause(num) {
if (num) { if(!playing) { thisInterval = setInterval('updateObj_slime()',Zrefresh); playing=1; }}
else { clearInterval(thisInterval); playing=0; }
}

// run once at start, and then on resize.
function fixObjects() {
//set the background
countUp = 0 ;
for (a=0;a<sectorHigh;a++) {
	for (b=0;b<sectorWide;b++) {
	Zworld[countUp].top = a*sectorSize;
	Zworld[countUp].left = b*sectorSize;
	Zworld[countUp].setZall();
	countUp++;
	}}
Zgalaxy.top = (Zbox.height/2)-((sectorHigh*1200)/2);
Zgalaxy.left = (Zbox.width/2)-((sectorWide*1200)/2);
Zgalaxy.setZall();
	setStarters();
}

function setStarters() {
overallWidth = Zbrowse.width()-100; overallHeight = Zbrowse.height()-100; 
	centerOffset = Math.round((Zbrowse.width()-overallWidth)/2);
	Zbox.top = 50;
	Zbox.left = centerOffset;
	Zbox.width = overallWidth-150;
	Zbox.height = overallHeight;

Zbox.setZall();
	Zcontrol.top = 50;
	Zcontrol.width = 142;
	Zcontrol.left = centerOffset + overallWidth - Zcontrol.width;
	Zcontrol.height = overallHeight;
Zcontrol.setZall();
	Zcontrol2.top = Zcontrol.top+1;
	Zcontrol2.left = Zcontrol.left+1;
	Zcontrol2.width = Zcontrol.width-2;
	Zcontrol2.height = Zcontrol.height-2;
Zcontrol2.setZall();
	Ztext.top = Zbox.height + Zbox.top + 6;
Ztext.setZall();
//alert(Math.abs(Zgalaxy.top) + ", " + Zbox.height + ", " + (2400-Zbox.height-Math.abs(Zgalaxy.top)))
}

function makeRotatingPlanet(num) {
	Zobjects[num].left = Zspots[num].left+60;
	Zobjects[num].top = Zspots[num].top+12;
	Zobjects[num].velX = 0;
	Zobjects[num].velY = -4; 
	Zobjects[num].setZall();
	}

function makeSun(num,mass) {
if (!mass) mass=fudge_factor;
if (!num) num = Zspots.length;
sunText = "<img src=\"/gravity/sphere.gif\" height=49 width=49 id=\"spots" + num + "\" class=\"spots\">";
document.getElementById("galaxy").insertAdjacentHTML( "beforeEnd", sunText);
Zspots[num] = new makePlanet("spots"+num,"galaxy",Zbox.top/2,Zbox.left/2,49,49,600,0,0,1,'visible',mass);
//if (num<Zobjects.length) { makeRotatingPlanet(num); }
placeSpots(num);
	}

		firstTime = 1;
function makeFuel(mass) {
num = Zspots.length;
if (!mass) mass=fudge_factor/2;
sunText = "<img src=\"/gravity/fuel.gif\" height=96 width=96 id=\"fuels" + num + "\" class=\"fuel\">";
document.getElementById("galaxy").insertAdjacentHTML( "beforeEnd", sunText);
Zspots[num] = new makePlanet("fuels"+num,"galaxy",Math.abs(Zgalaxy.top)+(Zbox.height/2)-48,Math.abs(Zgalaxy.left)+(Zbox.width/2)-48,96,96,100,0,0,1,'visible',mass);
 placeSpots(num);checkSpotsCollision(num); while (collided) placeSpots(num);
Zspots[num].setZall();
	}

function setMass(mass,num) {
	if (num != null) Zspots[num].mass = mass;
	else {
for (x=0;x<Zspots.length;x++) { 
if (Zspots[x].ID.indexOf("fuel") != -1) Zspots[x].mass = mass/2;
else Zspots[x].mass = mass;
 }}
 globalMass = Math.sqrt(mass);
	}
	
function scatterStop() {
		for (m=0;m<Zobjects.length;m++) {
Zobjects[m].top = Math.abs(Zgalaxy.top)+Math.round(2000*Math.random())+200; 
Zobjects[m].left = Math.abs(Zgalaxy.left)+Math.round(2000*Math.random())+200;
	Zobjects[m].velX = 2-(Math.random() * 4);
	Zobjects[m].velY = 4-(Math.random() * 8);// 8-(Math.random() * 16);
	Zobjects[m].setZall();
		}
	}

function solarSystem() {
fudge_factor =10;
		for (m=0;m<Zobjects.length;m++) {
Zobjects[m].top = Zspots[0].top +((8*m)+54); 
Zobjects[m].left = Zspots[0].left + ((8*m)+54);
	Zobjects[m].velX = -2;
	Zobjects[m].velY = 1; //
	Zobjects[m].setZall();
		}
	}
function solarSystem2() {
fudge_factor = 16;
		for (m=0;m<Zobjects.length;m++) {
Zobjects[m].top = Zspots[0].top +((8*m)+54); 
Zobjects[m].left = Zspots[0].left + ((8*m)+54);
	Zobjects[m].velX = -2 - ((.2*m));
	Zobjects[m].velY = 2 //
	Zobjects[m].setZall();
		}
	}

function placeSpots(num) {
	//if (!num) return false;
		Zspots[num].top = Math.abs(Zgalaxy.top)+Math.floor(Math.random()*2350);
		Zspots[num].left = Math.abs(Zgalaxy.left)+Math.floor(Math.random()*2350);
		checkSpotsCollision(num)
while (collided) {
		Zspots[num].top = Math.abs(Zgalaxy.top)+Math.floor(Math.random()*2350);
		Zspots[num].left = Math.abs(Zgalaxy.left)+Math.floor(Math.random()*2350);
		checkSpotsCollision(num);
		//window.status=collided;
		}
	Zspots[num].setZall();
}

function checkSpotsCollision(num2) {
	collided = 0;
	for (j=0;j<Zspots.length;j++) {
if (j!=num2) {
  if (Zspots[num2].top < Zspots[j].top) checkH = Zspots[num2].height; else checkH = Zspots[j].height; 
  if (Zspots[num2].left < Zspots[j].left) checkW = Zspots[num2].width; else checkW = Zspots[j].width; 
	if ((Math.abs(Zspots[num2].top-Zspots[j].top)<checkH) &&
	(Math.abs(Zspots[num2].left-Zspots[j].left)<checkW)) {
	collided = 1;
	}
  }
}
}


function changeDir(horz,vert) {
Zobjects[0].velX -= horz*globalMass;
Zobjects[0].velY -= vert*globalMass;
}

//Thanks go to Slime for this wonderful summation of variables and formula...
function updateObj_slime() {
//window.status=Zspots[0].spin;
  for (x=0;x<Zobjects.length;x++) {
	for (n=0;n<Zspots.length;n++) {
 // find distance to sun as a vector:
	distancetosunX = Zobjects[x].left-Zspots[n].left - ((Zspots[n].width/2)-(Zobjects[x].width/2));
	distancetosunY = Zobjects[x].top-Zspots[n].top - ((Zspots[n].height/2)-(Zobjects[x].height/2));
	lengthtosun_squared = (distancetosunX*distancetosunX) + (distancetosunY*distancetosunY);
if (Math.sqrt(lengthtosun_squared)<sectorSize) {
	dirX = distancetosunX/Math.abs(distancetosunX);
	dirY = distancetosunY/Math.abs(distancetosunY);
	Zobjects[x].velX -= ((distancetosunX/lengthtosun_squared)*Zspots[n].mass);//+(dirX*.1);
	Zobjects[x].velY -= ((distancetosunY/lengthtosun_squared)*Zspots[n].mass);//+(dirY*.1);

	if (lengthtosun_squared > 640) {
		diffyX = Math.abs(Zobjects[x].velX) - Math.abs(Zobjects[x].velY);
		diffyY = Math.abs(Zobjects[x].velY) - Math.abs(Zobjects[x].velX);
		if (diffyX > 16) { Zobjects[x].velX *= .99; Zobjects[x].velY *= 1.01; }
		if (diffyY > 16) { Zobjects[x].velY *= .99; Zobjects[x].velX *= 1.01; }
	}
   }
  }
//if (x==0){
Zobjects[x].velX *= .99;Zobjects[x].velY *= .99;
//}
// apply velocity to position
  Zobjects[x].left += Zobjects[x].velX;
  Zobjects[x].top += Zobjects[x].velY;
// set all parameters.
  Zobjects[x].chunk.style.top=Zobjects[x].top;
  Zobjects[x].chunk.style.left=Zobjects[x].left;
  Zshadows[x].chunk.style.top=Zobjects[x].top+20;
  Zshadows[x].chunk.style.left=Zobjects[x].left+20;
  
  }//}

apparentCenterTop = - Zobjects[0].top + (Zbox.height/2);
apparentCenterLeft = - Zobjects[0].left + (Zbox.width/2);

//if ((Math.round(-Zobjects[0].top - Zgalaxy.top) > (-300))&&(Math.round(-Zobjects[0].top - Zgalaxy.top) < (-100))) { stickiness = 999; }

Zgalaxy.top += Math.round((apparentCenterTop - Zgalaxy.top)/stickiness);
if (Zgalaxy.top > 0) flipAll(-1,1); else if (Zgalaxy.top < -1350) flipAll(1,1);

Zgalaxy.left += Math.round((apparentCenterLeft - Zgalaxy.left)/stickiness);
if (Zgalaxy.left > 0) flipAll(-1,0); else if (Zgalaxy.left < -1350) flipAll(1,0); 

Zgalaxy.setZall(); 

window.status= "apparentCenterLeft/apparentCenterTop = " + Math.round(-Zobjects[0].left - Zgalaxy.left) + " / " + Math.round(-Zobjects[0].top - Zgalaxy.top);
}

function flipAll(num,num2) {
if (num2) {
 	for (x=0;x<Zworld.length;x++) { 
	if (Zworld[x].top>0) Zworld[x].top = 0;
	else Zworld[x].top = (sectorHigh*sectorSize)/2;
	Zworld[x].setZall();
	}
  	for (x=0;x<Zobjects.length;x++) { 
	if (Zobjects[x].top>(sectorHigh*sectorSize)/2) Zobjects[x].top -= (sectorHigh*sectorSize)/2;
	else Zobjects[x].top += (sectorHigh*sectorSize)/2;
	Zshadows[x].chunk.style.top=Zobjects[x].top+20;
	Zobjects[x].setZall();
	}
	for (n=0;n<Zspots.length;n++) { 
	if (Zspots[n].top>(sectorHigh*sectorSize)/2) Zspots[n].top -= (sectorHigh*sectorSize)/2;
	else Zspots[n].top += (sectorHigh*sectorSize)/2;
	Zspots[n].setZall();
	}
	Zgalaxy.top += ((sectorHigh*sectorSize)/2)*num;
  }
else {
 	for (x=0;x<Zworld.length;x++) { 
	if (Zworld[x].left>0) Zworld[x].left = 0;
	else Zworld[x].left = (sectorWide*sectorSize)/2;
	Zworld[x].setZall();
	}
  	for (x=0;x<Zobjects.length;x++) { 
	if (Zobjects[x].left>(sectorWide*sectorSize)/2) Zobjects[x].left -= (sectorWide*sectorSize)/2;
	else Zobjects[x].left += (sectorWide*sectorSize)/2;
	Zshadows[x].chunk.style.left=Zobjects[x].left+20;
	Zobjects[x].setZall();
	}
	for (n=0;n<Zspots.length;n++) { 
	if (Zspots[n].left>(sectorWide*sectorSize)/2) Zspots[n].left -= (sectorWide*sectorSize)/2;
	else Zspots[n].left += (sectorWide*sectorSize)/2;
	Zspots[n].setZall();
	}
	Zgalaxy.left += ((sectorWide*sectorSize)/2)*num;
  }

}

	
// ---------------------------------
if (Zflag.IE) {
	document.onmousemove=moveHandler;
	document.onmousedown=StartE;
	document.onmouseup=EndE;
	document.ondragstart=StartE;
	document.ondblclick = dblClick;
	}
else if (Zflag.gecko) {
	document.ondragstart = StartE;
	document.addEventListener("mousedown", StartE, true);
	document.addEventListener("mousemove", moveHandler, true);
	document.addEventListener("mouseup", EndE, true);
	}

function boxMover(e) {
	if (!loaded) return false;
	YposBox = (Zflag.IE)?event.y - Zbox.top :e.pageY - Zbox.top;
	XposBox = (Zflag.IE)?event.x - Zbox.left : e.pageX - Zbox.left;
	}

function moveHandler(e) {
	if (!loaded) return false;
	Ypos = (Zflag.IE)?event.y - Zbox.top:e.pageY - Zbox.top;
	Xpos = (Zflag.IE)?event.x - Zbox.left:e.pageX - Zbox.left;
if (!dragMe) return false;
	Zspots[whichSpot].left = XposBox - Xoffset;
	Zspots[whichSpot].top = YposBox - Yoffset;
	Zspots[whichSpot].setZall();
}
		
	dragMe=false;
function EndE() { if (!loaded) return false;
document.getElementById("shield").style.visibility = "hidden";
	dragMe=false;
	 }
	 
function StartE() { 
	//alert(event.srcElement.id);
	if ((event.srcElement.className=="spots")||(event.srcElement.className=="fuel")){
document.getElementById("shield").style.visibility = "visible";
	whichSpot = event.srcElement.id.substr(5,99);
	Xoffset = Xpos - Zspots[whichSpot].left;
	Yoffset = Ypos - Zspots[whichSpot].top;
	dragMe=true; 
	return false;
	}
}

function dblClick() {
	if (!loaded) return false;
	//scatterStop();
	}


function makePlanet(ID,wrapper,top,left,height,width,zindex,velX,velY,spin,visibility,mass) {
	this.chunk = eval(document.getElementById(ID))
	this.ID = ID ? ID : "Zobject"+(Zobject.length);
	this.DHTML = (wrapper && Zflag.NS) ? eval(layerstart + wrapper + "." 
		+ layerstart + this.ID + layerstyle) : eval(layerstart + this.ID + layerstyle);
	this.top = top ? top:0;
	this.left = left ? left:0;
	this.height = height ? height:0;
	this.width = width ? width:0;
	this.velX = velX ? velX:0;
	this.velY = velY ? velY:0;
	this.zindex = zindex ? zindex:(Zobjects.length+1)*1000;
	this.spin = spin ? spin:0;
	this.visibility = visibility ? visibility:"visible";
	this.mass = mass ? mass:0;
	}
makePlanet.prototype.setZall = function() {
	this.DHTML.top = this.top; 
	this.DHTML.left = this.left;
	this.DHTML.zIndex = this.zindex;
	this.DHTML.height = this.height;
	this.DHTML.width = this.width;
	this.DHTML.visibility = this.visibility;
	}
makePlanet.prototype.shortSetZall = function() {
	this.DHTML.top = this.top; 
	this.DHTML.left = this.left;
	}

