/* choo.js - The JavaScript that drives the BuckoSoft home page.
 * See also: http://www.buckosoft.com/j/indexchoo.js
 * $Id: choo.js,v 1.4 2008/02/14 21:29:27 dick Exp $
 */
var	loaded=false;
var baseSpeed=3;
var lspeed=2;
var cookieSpeed=baseSpeed;
var dn=0;
var up=1;
var st=0;
var en=1;
var tr;
var trclip;
var curtr;		// current track
var curdir;		// direction
var curofs;		// percentage through track
var curdest;	// Altoona?
var clickDest;
var hasArrived;
var action;
var dest,desd,desu;
var baseURL;
function point(x,y) { this.length = 2; this.x = x; this.y = y; }
function line(l,s,e) {
	this.length = 3;
	this.l = l; 	// pic to use for moving
	this.s = s; 	// start point
	this.e = e; 	// end point
}
function Tpos(d,u) {
	this.length = 3;
	this.d = d;		// pic and coords for moving down the line
	this.u = u;		// pic and coords for moving up the line
	this.tlen = 0;	// no track length yet (calc later)
}
function Dest(d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,d17,d18,d19,d20,d21,d22,d23) {
	this.length = 24;
	this[0]=d0;this[1]=d1;this[2]=d2;this[3]=d3;this[4]=d4;this[5]=d5;this[6]=d6;this[7]=d7;this[8]=d8;
	this[9]=d9;this[10]=d10;this[11]=d11;this[12]=d12;this[13]=d13;this[14]=d14;this[15]=d15;this[16]=d16;
	this[17]=d17;this[18]=d18;this[19]=d19;this[20]=d20;this[21]=d21;this[22]=d22;this[23]=d23;
}
with (this.location) {baseURL = href.substring (0,href.lastIndexOf ("/") + 1)}

var locos = new Array();
locos[8] = new Image(100,100);
locos[8].src = baseURL + "g/loco/dt.gif";
locos[0] = new Image(100,100);
locos[0].src = baseURL + "g/loco/l.gif";
locos[1] = new Image(100,100);
locos[1].src = baseURL + "g/loco/r.gif";
locos[2] = new Image(100,100);
locos[2].src = baseURL + "g/loco/dl.gif";
locos[3] = new Image(100,100);
locos[3].src = baseURL + "g/loco/dr.gif";
locos[4] = new Image(100,100);
locos[4].src = baseURL + "g/loco/ul.gif";
locos[5] = new Image(100,100);
locos[5].src = baseURL + "g/loco/ur.gif";
//locos[6] = new Image(100,100);
//locos[6].src = baseURL + "g/loco/d.gif";
locos[7] = new Image(100,100);
locos[7].src = baseURL + "g/loco/u.gif";

dest = new Array();
desd = new Array();
desu = new Array();
//                  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
dest[0] = new Dest(-1, 0, 0, 2, 3, 4, 5, 6, 7, 7,  9, 10, 11,  9, 11, 12,  4, 16, 17, 17, 19, 20, 20, 22);
desd[0] = new Dest(up,dn,up,up,up,up,up,up,up,up, up, up, up, up, up, up, up, up, up, up, up, up, up, up);
dest[1] = new Dest( 1,-1, 1, 2, 3, 4, 5, 6, 7, 7,  9, 10, 11,  9, 11, 12,  4, 16, 17, 17, 19, 20, 20, 22);
desd[1] = new Dest(dn,up,up,up,up,up,up,up,up,up, up, up, up, up, up, up, up, up, up, up, up, up, up, up);
dest[8] = new Dest( 2, 2, 3, 4, 5, 6, 7,  8,-1, 8, 9, 10, 11,  9, 11, 12,  4, 16, 17, 17, 19, 20, 20, 22);
desd[8] = new Dest(dn,dn,dn,dn,dn,dn,dn,dn,dn,up, up, up, up, up, up, up, up, up, up, up, up, up, up, up);
dest[13]= new Dest( 2, 2, 3, 4, 5, 6, 7, 9, 9,13, 13, 10, 11, -1, 11, 12,  4, 16, 17, 17, 19, 20, 20, 22);
desd[13]= new Dest(dn,dn,dn,dn,dn,dn,dn,dn,up,dn, up, up, up, dn, up, up, up, up, up, up, up, up, up, up);
dest[14]= new Dest(2, 2, 3, 4, 5, 6, 7,  9, 9,10, 11, 14, 11, 10, -1, 12,  4, 16, 17, 17, 19, 20, 20, 22);
desd[14]= new Dest(dn,dn,dn,dn,dn,dn,dn,dn,up,dn, dn, dn, up, up, dn, up, up, up, up, up, up, up, up, up);
dest[15]= new Dest(2, 2, 3, 4, 5, 6, 7,  9, 9,10, 11, 12, 15, 10, 12, -1,  4, 16, 17, 17, 19, 20, 20, 22);
desd[15]= new Dest(dn,dn,dn,dn,dn,dn,dn,dn,up,dn, dn, dn, dn, up, up, dn, up, up, up, up, up, up, up, up);
dest[18]= new Dest(2,  2, 3, 4,16, 4, 5, 6, 7, 7,  9, 10, 11,  9, 11, 12, 17, 18, -1, 18, 19, 20, 20, 22);
desd[18]= new Dest(dn,dn,dn,dn,dn,up,up,up,up,up, up, up, up, up, up, up, dn, dn, dn, up, up, up, up, up);
dest[21]= new Dest(2,  2, 3, 4,16, 4, 5, 6, 7, 7,  9, 10, 11,  9, 11, 12, 17, 19, 19, 20, 21, -1, 21, 22);
desd[21]= new Dest(dn,dn,dn,dn,dn,up,up,up,up,up, up, up, up, up, up, up, dn, dn, up, dn, dn, dn, up, up);
dest[23]= new Dest(2,  2, 3, 4,16, 4, 5, 6, 7, 7,  9, 10, 11,  9, 11, 12, 17, 19, 19, 20, 22, 22, 23, -1);
desd[23]= new Dest(dn,dn,dn,dn,dn,up,up,up,up,up, up, up, up, up, up, up, dn, dn, up, dn, dn, up, dn, dn);

clickDest=-1;
desu[0] = "theoffice/";
desu[1] = "gallery/";
desu[8] = "personal/";
desu[13]= "free/";
desu[14]= "xpilot/";
desu[15]= "http://lists.buckosoft.com:8080/";
desu[18]= "etc/";
desu[21]= "pinball/";
desu[23]= "sdh/";

tr = new Array();
tr[0] = new Tpos(new line(8, new point(97,11), new point(97,191)),
				 new line(8, new point(97,191), new point(97,11)));
tr[1] = new Tpos(new line(0, new point(665,170), new point(100,170)),
				 new line(1, new point(90,170), new point(665,170)));
tr[2] = new Tpos(new line(0, new point(100,170), new point(20,170)),
				 new line(1, new point(10,170), new point(90,170)));
tr[3] = new Tpos(new line(2, new point(15,168), new point(-22,204)),
				 new line(5, new point(-40,215), new point(-3,175)));
tr[4] = new Tpos(new line(8, new point(-25,220), new point(-25,240)),
				 new line(7, new point(-25,250), new point(-25,227)));
tr[5] = new Tpos(new line(8, new point(-25,240), new point(-25,467)),
				 new line(7, new point(-25,470), new point(-25,250)));
tr[6] = new Tpos(new line(3, new point(-29,450), new point(35,515)),
				 new line(4, new point(61,529), new point(-10,460)));
tr[7] = new Tpos(new line(1, new point(35,520), new point(220,520)),
				 new line(0, new point(230,520), new point(45,520)));
tr[8] = new Tpos(new line(1, new point(220,520), new point(610,520)),
				 new line(0, new point(610,520), new point(230,520)));
tr[9] = new Tpos(new line(4, new point(247,525), new point(224,501)),
				 new line(3, new point(203,492), new point(225,514)));
tr[10]= new Tpos(new line(4, new point(224,501), new point(180,460)),
				 new line(3, new point(164,454), new point(201,490)));
tr[11]= new Tpos(new line(0, new point(174,460), new point(155,460)),
				 new line(1, new point(142,460), new point(161,460)));
tr[12]= new Tpos(new line(4, new point(170,460), new point(114,411)),
				 new line(3, new point(90,396), new point(147,453)));
tr[13]= new Tpos(new line(0, new point(210,498), new point(110,498)),
				 new line(1, new point(100,498), new point(200,498)));
tr[14]= new Tpos(new line(0, new point(155,460), new point(45,460)),
				 new line(1, new point(45,460), new point(142,460)));
tr[15]= new Tpos(new line(0, new point(99,402), new point(54,402)),
				 new line(1, new point(54,402), new point(89,402)));
tr[16]= new Tpos(new line(3, new point(-29,224), new point(5,260)),
				 new line(4, new point(26,270), new point(-10,234)));
tr[17]= new Tpos(new line(1, new point(4,265), new point(275,265)),
				 new line(0, new point(288,265), new point(15,265)));
tr[18]= new Tpos(new line(1, new point(275,265), new point(668,265)),
				 new line(0, new point(668,265), new point(288,265)));
tr[19]= new Tpos(new line(3, new point(282,263), new point(340,320)),
				 new line(4, new point(363,332), new point(304,274)));
tr[20]= new Tpos(new line(1, new point(330,325), new point(419,325)),
				 new line(0, new point(430,325), new point(345,325)));
tr[21]= new Tpos(new line(1, new point(419,325), new point(525,325)),
				 new line(0, new point(525,325), new point(430,325)));
tr[22]= new Tpos(new line(3, new point(423,320), new point(490,387)),
				 new line(4, new point(515,400), new point(447,334)));
tr[23]= new Tpos(new line(1, new point(490,393), new point(525,393)),
				 new line(0, new point(526,393), new point(500,393)));

function setClickDest(newDest) {
	lspeed *= 3;
	clickDest = newDest;
	if (hasArrived && clickDest == curdest)
		location.href = desu[clickDest];
}
function setDest(newDest) {
//	compassTextHide();

	if (!loaded)
		return;
	if (curdest == newDest)
		return;
	curdest = newDest;
	curdir = desd[curdest][curtr];
	hasArrived = false;
	lspeed = baseSpeed;
	if (curdir == dn)
		document.images[3].src = locos[tr[curtr].d.l].src;
	else
		document.images[3].src = locos[tr[curtr].u.l].src;
}
function setTrack(newTrack, newDir) {
	curtr = newTrack;
	if (newDir == dn) {
		document.images[3].src = locos[tr[curtr].d.l].src;
		document.getElementById("loco").style.top  = tr[curtr].d.s.y;
		document.getElementById("loco").style.left = tr[curtr].d.s.x;
	} else {
		document.images[3].src = locos[tr[curtr].u.l].src;
		document.getElementById("loco").style.top  = tr[curtr].u.s.y;
		document.getElementById("loco").style.left = tr[curtr].u.s.x;
	}
	setClip();
}
function startTR() {
	var i;
	for (i=0; i<24; i++) {
		var rise, run, slope;
		rise = tr[i].d.e.x-tr[i].d.s.x;
		run  = tr[i].d.e.y-tr[i].d.s.y;
		if (!run) {
			tr[i].tlen = Math.abs(rise);
			continue;
		}
		slope = rise/run;
		tr[i].tlen = Math.abs((1/(Math.cos(Math.atan(slope))))*run);
	}

	curtr = 0;
	curofs = 0;
	setTrack(0, dn);
	setDest(8);
	lspeed = baseSpeed;
	//lspeed = 0;
	//curofs = tr[0].tlen;
	//curofs = tr[1].tlen;
	//setTrack(1, dn);
	action = window.setInterval("ChooTimer()", 10);
}
function ChooTimer() {
	MoveChoo();
	compassTimer();
}
function MoveChoo() {
	if (hasArrived)
		return;
	var trc = tr[curtr];
	var mx, my;
	if (curdir == dn) {
		curofs += lspeed;
		if (curofs > trc.tlen) {
			NewTrack();
			return;
		}
		trc = tr[curtr];
		mx = trc.d.s.x + (trc.d.e.x-trc.d.s.x)*(curofs/trc.tlen);
		my = trc.d.s.y + (trc.d.e.y-trc.d.s.y)*(curofs/trc.tlen);
	} else {
		curofs -= lspeed;
		if (curofs < 0) {
			NewTrack();
			return;
		}
		mx = trc.u.e.x - (trc.u.e.x-trc.u.s.x)*(curofs/trc.tlen);
		my = trc.u.e.y - (trc.u.e.y-trc.u.s.y)*(curofs/trc.tlen);
	}
	document.getElementById("loco").style.top  = my;
	document.getElementById("loco").style.left = mx;
	setClip();
}
function NewTrack() {
	if (dest[curdest][curtr] == -1) {
		hasArrived = true;
		if (clickDest == curdest)
			location.href = desu[clickDest];
		else 
			clickDest = -1;
		return;
	}
	curtr = dest[curdest][curtr];
	if (desd[curdest][curtr] == dn) {
		curofs = 0;
		curdir = dn;
		MoveChoo();
		document.images[3].src = locos[tr[curtr].d.l].src;
	}
	else {
		curofs = tr[curtr].tlen;
		curdir = up;
		MoveChoo();
		document.images[3].src = locos[tr[curtr].u.l].src;
	}
}
var hasClip = false;
function setClip() {
	if (curtr == 0 || curtr == 1) {
		var trc, mx, my;
		trc = tr[curtr];
		if (curtr == 0) {
			my = trc.d.s.y + (trc.d.e.y-trc.d.s.y)*(curofs/trc.tlen);
			var tclip = 119-my; if (my < 0) my = 0;
			document.getElementById("loco").style.clip = "rect(" + tclip + " 900 400 0)";
		}
		else {
			mx = trc.d.s.x + (trc.d.e.x-trc.d.s.x)*(curofs/trc.tlen);
			var tclip = 653-mx; 
			if (tclip > 100) {
				if (hasClip)
					document.getElementById("loco").style.clip = "rect( 0 100 100 0)";
				hasClip = false;
				return;
			}
			document.getElementById("loco").style.clip = "rect(0 " + tclip + " 400 0)";
		}
		hasClip = true;
	}
	else {
		if (hasClip)
			document.getElementById("loco").style.clip = "rect( 0 100 100 0)";
		hasClip = false;
	}
}

function init() {
	/*
	var curline = tr[23].u;
	document.images[1].src  = locos[curline.l].src;
	document.getElementById("loco").style.top  = curline.e.y;
	document.getElementById("loco").style.left = curline.e.x;
	*/
	loaded=true;
	startTR();
	compassInit();
}

function unload() {
	saveCookie();
	compassUnload();
}

function setSpeedUp() {
	baseSpeed += 2;
	lspeed = baseSpeed;
}

function setSpeedDn() {
	baseSpeed -= 2;
	if (baseSpeed < 1)
		baseSpeed = 1;
	lspeed = baseSpeed;
}

function saveCookie() {
	window.status = "cs=" + cookieSpeed + " bs=" + baseSpeed;
	if (cookieSpeed == baseSpeed)
		return;
	var expdate = new Date();
	FixCookieDate(expdate); // Correct for Mac date bug - call only once for given Date object!
	expdate.setTime(expdate.getTime() + 2592000000); // 24 hrs from now 
	SetCookie("LocoSpeed", baseSpeed, expdate, "/");
}

