

window.onload = winStart;
window.onresize = frogger;
document.onkeypress = defaultKey;

function winStart() {
	initDHTMLAPI();
	hide("gomsg");
	getRawObject("goword").focus();
	if (window.location.search) 
		getRawObject("goword").value = window.location.search.substr(1);
} 	

function defaultKey(evt) {
	if (evt)
		if (evt.keyCode == 13) 
			goclick();
	return true;
}

function goclick(){
	var elem = getRawObject("goword")
	if (elem.value.length > 0) {
		if (elem.value == "bill") window.location.href = "bill/bill.htm";
		else if (elem.value == "green")  window.location.href = "GreenHB.htm";
		else if (elem.value == "beach")  window.location.href = "BeachHouse/default.htm";
		else if (elem.value == "pies")  window.location.href = "pies/default.htm";
		else window.location.href = "noclient.htm";
	}
	return false;
}

// outcolor like a, incolor like a:hover
var outcolor = "#0000ff";
var incolor = "#ff8c00";

function goout (elm) {
	hide(getRawObject("gomsg"));
	getObject(elm).color = outcolor;
}

function goover(elm) {
	if (getRawObject("goword").value.length == 0) {
		show(getRawObject("gomsg"));
		getObject(elm).color = outcolor;
	}
	else getObject(elm).color = incolor;
}


// frogger globals
var faway = 1;		//length of shortest jump
var hasJumped = 0;
var hasCapped = 0;
var frog = new Pos(0, 0);
var cap = new Pos(0, 0);
	
function frogger() {

	var fsize = new Pos(getObjectWidth("jumpfrog"), getObjectHeight("jumpfrog"));
	var dim = new Pos((getInsideWindowWidth() / fsize.x), (getInsideWindowHeight() / fsize.y))
	var nudge = new Pos((fsize.x * ((dim.x % 1) / 2)), (fsize.y * ((dim.y % 1) / 2)));
									   
// insure integers
	dim.set((dim.x - (dim.x % 1)), (dim.y - (dim.y % 1)));
	nudge.set((nudge.x - (nudge.x % 1)), (nudge.y - (nudge.y % 1)));

// fill fgrid only with positions frogAway from current
	var fgrid = new Array();
	for (var dy = 0;  dy < dim.y; dy++) 
		for (var dx = 0;  dx < dim.x; dx++)
			if (!((dy >= (frog.y - faway)) && (dy <= (frog.y + faway)) && (dx >= (frog.x - faway)) && (dx <= (frog.x + faway)))) 
				fgrid[fgrid.length] = new Pos(dx, dy);

	cap.set(frog.x, frog.y);
// pick random position from fgrid
	var indx = Math.round(Math.random() * fgrid.length);
	frog.set(fgrid[indx].x, fgrid[indx].y)

//	var l1  = "<li>index " + indx + "</li>"
//	getRawObject("stats").innerHTML = "<ul>" + l1 + "</ul>";

// shift to new position
	shiftTo("jumpfrog", ((fsize.x * frog.x) + nudge.x), ((fsize.y * frog.y) + nudge.y));
	var capid = "jumpcap";
	if (hasJumped == 0) {
		capid = "base";		
		getObject("basefrog").display = "none";			
		getObject("basecap").display = "inline";	
		getObject("jumpfrog").display = "inline";
		show("jumpfrog");
		hasJumped = -1;		
	} 
	else {
		shiftTo("jumpcap", ((fsize.x * cap.x) + nudge.x), ((fsize.y * cap.y) + nudge.y));
		if (hasCapped == 0) {
			getObject("basecap").display = "none";
			getObject("jumpcap").display = "inline";
			show("jumpcap");
			hasCapped = -1;
		}
	}	
	changeOpac(100, capid);
	changeOpac(0, "jumpfrog");

// pick random duration
// for complementary fades of "frog" and "cap"
	var speed = Math.round((250 + (Math.random() * 1250)) / 100);	
	var timer = 0;
	capid = "'" + capid + "', " + frog.x + ", " + frog.y + ")"
	for(i = 0; i <= 99; i++) {
		setTimeout(("changeTwopac(" + i + ", " + (99 - i) + ", " + capid), (timer * speed));
		timer++;
	}
	
	return false;
}

function Pos(x, y) {
	this.x = x;
	this.y = y;
}

Pos.prototype.set = function (x, y) {
	this.x = x;
	this.y = y;
}

// setTimeout callback: change both elements' opacities
// only fires if called from current position
function changeTwopac(opacity, altopacity, altimage, x, y) {
	if ((x == frog.x) && (y == frog.y)) {
		changeOpac(opacity, "jumpfrog");
		changeOpac(altopacity, altimage);
	}
}
