var frameindex = 3;
var wagonFrames = new Array;
wagonFrames[0] = "/images/westwardtrail/wagontrain0.png";
wagonFrames[1] = "/images/westwardtrail/wagontrain1.png";
wagonFrames[2] = "/images/westwardtrail/wagontrain2.png";
wagonFrames[3] = "/images/westwardtrail/wagontrain3.png";

var deerindex = 0;
var deerFrames = new Array;
deerFrames[0] = "/images/westwardtrail/deer0.png";
deerFrames[1] = "/images/westwardtrail/deer0.png";
deerFrames[2] = "/images/westwardtrail/deer1.png";
deerFrames[3] = "/images/westwardtrail/deer1.png";
deerFrames[4] = "/images/westwardtrail/deer2.png";
deerFrames[5] = "/images/westwardtrail/deer2.png";
deerFrames[6] = "/images/westwardtrail/deer3.png";
deerFrames[7] = "/images/westwardtrail/deer3.png";

var deerLeftFrames = new Array;
deerLeftFrames[0] = "/images/westwardtrail/deer-left0.png";
deerLeftFrames[1] = "/images/westwardtrail/deer-left0.png";
deerLeftFrames[2] = "/images/westwardtrail/deer-left1.png";
deerLeftFrames[3] = "/images/westwardtrail/deer-left1.png";
deerLeftFrames[4] = "/images/westwardtrail/deer-left2.png";
deerLeftFrames[5] = "/images/westwardtrail/deer-left2.png";
deerLeftFrames[6] = "/images/westwardtrail/deer-left3.png";
deerLeftFrames[7] = "/images/westwardtrail/deer-left3.png";


var hunterindex = 0;
var hunterFrames = new Array;
hunterFrames[0] = "/images/westwardtrail/hunter0.png";
hunterFrames[1] = "/images/westwardtrail/hunter1.png";
hunterFrames[2] = "/images/westwardtrail/hunter2.png";
hunterFrames[3] = "/images/westwardtrail/hunter3.png";
hunterFrames[4] = "/images/westwardtrail/hunter4.png";
hunterFrames[5] = "/images/westwardtrail/hunter5.png";
hunterFrames[6] = "/images/westwardtrail/hunter6.png";
hunterFrames[7] = "/images/westwardtrail/hunter7.png";
hunterFrames[8] = "/images/westwardtrail/hunter8.png";

var firing = 0;
var aimdirection = 0;
var meat = 0;
var bullets = 0;
var animalnum = 1;

walkroutine = new Animator({duration: 2000, interval: 100, transition: Animator.tx.linear});
walkroutine.addSubject(drawWalking);

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function keepwalking ()
{
	document.getElementById('walkbutton').style.visibility = 'hidden';
	var sendList = new Array();
	sendList['command'] = 'keepwalking';
	sendList['type'] = 'ajax';
	var poststr = packageMessage(sendList);
	postMessage('/cgi-bin/westwardtrail.pl',poststr,'walkresult');
}

function walkresult (forminput)
{
	// Hide the previous turn's alerts
	document.getElementById('alertbox').style.visibility = 'hidden';

	// Draw the next landmark
	document.getElementById('landmark').src = '/images/westwardtrail/' + forminput['landmarkimg'];
	document.getElementById('landmark').style.left = forminput['landmarkoffset'];
	document.getElementById('landmark').style.display = '';
	
	// Change the landscape based on weather
	if (forminput['landscape'] > 0)
	{
		document.getElementById('landscape').src = '/images/westwardtrail/landscape' + forminput['landscape'] + '.jpg';
	}
	
	// Draw the storms
	if (forminput['storm'] == 0){document.getElementById('storm').style.left = '-1000px';}
	if (forminput['storm'] >= 1){document.getElementById('storm').style.left = '130px'; document.getElementById('storm').src = '/images/westwardtrail/storm' + forminput['storm'] + '.png';}
	
	// Do nothing if you're broken down
	if (forminput['brokendown'] == 1)
	{
		document.getElementById('walkbutton').style.visibility = 'visible';
		document.getElementById('alertbox').style.visibility = 'visible';
		document.getElementById('alertbox').innerHTML = "<span style=\"color: #ff0000; background-color: #000000; font-weight: bold\">" + forminput['problemtext'] + "</span>";
		if (forminput['wagonstatus'] != 'nooxen'){document.getElementById('repairbutton').style.visibility = 'visible';}
		document.getElementById('wagontrain').src = '/images/westwardtrail/' + forminput['wagonstatus'] + '.png';
		return 0;
	}

	// Start the animation
	var walkspeed = 150;
	var walktime = 2400;
	if (forminput['walkspeed'] == 2){walkspeed = 100; walktime = 2000;}
	if (forminput['walkspeed'] == 3){walkspeed = 50; walktime = 1500;}
	if (forminput['storm'] >= 1){walkspeed = 200; walktime = 1000;}
	walkroutine = new Animator({duration: walktime, interval: walkspeed, transition: Animator.tx.linear, onComplete: function() {walkdone(forminput);}});
	walkroutine.addSubject(drawWalking);
	walkroutine.play();
	
	// Update the player stats
	document.getElementById('distancecell').innerHTML = forminput['position'] + " miles";
	document.getElementById('foodcell').innerHTML = forminput['food'] + " lbs.";
	document.getElementById('weathercell').innerHTML = forminput['weather'];
	document.getElementById('datecell').innerHTML = forminput['dateword'];
	
	// Stop at landmarks
	if (forminput['landmarkfunction']){eval(forminput['landmarkfunction'] + '();');}
}

function walkdone (forminput)
{
	if (forminput['alerttext'])
	{
		document.getElementById('alertbox').style.visibility = 'visible';
		document.getElementById('alertbox').innerHTML = "<span style=\"color: #ff0000; background-color: #000000; font-weight: bold\">" + forminput['alerttext'] + "</span>";
	}
	if (forminput['showbroken'] == 1)
	{
		document.getElementById('wagontrain').src = '/images/westwardtrail/' + forminput['wagonstatus'] + '.png';
		document.getElementById('repairbutton').style.visibility = 'visible';
	}
	document.getElementById('walkbutton').style.visibility = 'visible';
}

function repair ()
{
	var sendList = new Array();
	sendList['command'] = 'repairwagon';
	sendList['type'] = 'ajax';
	var poststr = packageMessage(sendList);
	postMessage('/cgi-bin/westwardtrail.pl',poststr,'repairresult');
}
function repairresult (forminput)
{
	document.getElementById('alertbox').style.visibility = 'visible';
	if (forminput['success'] == 1)
	{
		document.getElementById('alertbox').innerHTML = "<span style=\"color: #00ff00; background-color: #000000; font-weight: bold\">" + forminput['alerttext'] + "</span>";
		document.getElementById('repairbutton').style.visibility = 'hidden';
		document.getElementById('wagontrain').src = wagonFrames[0];
	}
	else
	{
		document.getElementById('alertbox').innerHTML = "<span style=\"color: #ff0000; background-color: #000000; font-weight: bold\">" + forminput['alerttext'] + "</span>";
	}
}

function drawWalking ()
{
	document.getElementById('wagontrain').src = wagonFrames[frameindex];
	frameindex = (frameindex + 1) % 4;
	document.getElementById('landscape').style.left = parseInt(document.getElementById('landscape').style.left) + 2;
	document.getElementById('landmark').style.left = parseInt(document.getElementById('landmark').style.left) + 2;
}

function river1 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached the Kansas River Crossing.  The river may be several feet deep in the middle.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"river1\" /><input type=\"radio\" name=\"riverchoice\" value=\"caulk\" checked=\"checked\" /> Caulk the wagon and float across<br /><input type=\"radio\" name=\"riverchoice\" value=\"ford\" /> Attempt to ford the river<br /><input type=\"radio\" name=\"riverchoice\" value=\"ferry\" /> Hire a ferry to take you across for $50.00<br /><br /><input type=\"submit\" value=\"Let's Go!\" /></form></div>";

}

function river2 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached the Big Blue River Crossing.  The river is shallow and narrow.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"river2\" /><input type=\"radio\" name=\"riverchoice\" value=\"caulk\" checked=\"checked\" /> Caulk the wagon and float across<br /><input type=\"radio\" name=\"riverchoice\" value=\"ford\" /> Attempt to ford the river<br /><br /><input type=\"submit\" value=\"Let's Go!\" /></form></div>";
}

function river3 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached the Green River Crossing.  The river is wide and deep.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"river3\" /><input type=\"radio\" name=\"riverchoice\" value=\"caulk\" checked=\"checked\" /> Caulk the wagon and float across<br /><input type=\"radio\" name=\"riverchoice\" value=\"ford\" /> Attempt to ford the river<br /><input type=\"radio\" name=\"riverchoice\" value=\"ferry\" /> Hire a ferry to take you across for $75.00<br /><br /><input type=\"submit\" value=\"Let's Go!\" /></form></div>";

}

function river4 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached the Snake River Crossing.  The river is deep and the current is strong.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"river4\" /><input type=\"radio\" name=\"riverchoice\" value=\"caulk\" checked=\"checked\" /> Caulk the wagon and float across<br /><input type=\"radio\" name=\"riverchoice\" value=\"ford\" /> Attempt to ford the river<br /><input type=\"radio\" name=\"riverchoice\" value=\"indians\" /> Hire Indians to help you across for 5 sets of clothing<br /><br /><input type=\"submit\" value=\"Let's Go!\" /></form></div>";

}

function fort1 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Fort Kearney.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"fort1\" /><input type=\"radio\" name=\"fortchoice\" value=\"store\" /> Buy supplies at the store<br /><input type=\"radio\" name=\"fortchoice\" value=\"rest\" /> Rest in the comfort of the fort<br /><input type=\"radio\" name=\"fortchoice\" value=\"continue\" checked=\"checked\" /> Continue traveling the trail<br /><br /><input type=\"submit\" value=\"Submit Action\" /></form></div>";
}

function fort2 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Fort Laramie.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"fort2\" /><input type=\"radio\" name=\"fortchoice\" value=\"store\" /> Buy supplies at the store<br /><input type=\"radio\" name=\"fortchoice\" value=\"rest\" /> Rest in the comfort of the fort<br /><input type=\"radio\" name=\"fortchoice\" value=\"continue\" checked=\"checked\" /> Continue traveling the trail<br /><br /><input type=\"submit\" value=\"Submit Action\" /></form></div>";
}

function fort3 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Fort Hall.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"fort3\" /><input type=\"radio\" name=\"fortchoice\" value=\"store\" /> Buy supplies at the store<br /><input type=\"radio\" name=\"fortchoice\" value=\"rest\" /> Rest in the comfort of the fort<br /><input type=\"radio\" name=\"fortchoice\" value=\"continue\" checked=\"checked\" /> Continue traveling the trail<br /><br /><input type=\"submit\" value=\"Submit Action\" /></form></div>";
}

function fort4 ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Fort Boise.  What do you want to do?</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"fort4\" /><input type=\"radio\" name=\"fortchoice\" value=\"store\" /> Buy supplies at the store<br /><input type=\"radio\" name=\"fortchoice\" value=\"rest\" /> Rest in the comfort of the fort<br /><input type=\"radio\" name=\"fortchoice\" value=\"continue\" checked=\"checked\" /> Continue traveling the trail<br /><br /><input type=\"submit\" value=\"Submit Action\" /></form></div>";
}

function chimneyrock ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Chimney Rock.  From this point on, the trail becomes more rugged as you head toward the Rocky Mountains.</b><br /><br /><b>Think you're old enough to drive a car?</b>  12 years old is the minimum in some areas!<br /><b><a href=\"http://www.car-insurance-for-teenagers.com\">Find out more &gt;&gt;</a></b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"chimneyrock\" /><br /><br /><input type=\"submit\" value=\"Keep Going!\" /></form></div>";
}
function independencerock ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Independence Rock.</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"independencerock\" /><br /><br /><input type=\"submit\" value=\"Keep Going!\" /></form></div>";
}
function southpass ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached South Pass.</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"southpass\" /><br /><br /><input type=\"submit\" value=\"Keep Going!\" /></form></div>";
}
function sodasprings ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>You've reached Soda Springs.</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"sodasprings\" /><input type=\"radio\" name=\"sodachoice\" value=\"rest\" /> Drink from the spring and rest<br /><input type=\"radio\" name=\"sodachoice\" value=\"continue\" checked=\"checked\" /> Continue traveling the trail<br /><br /><input type=\"submit\" value=\"Keep Going!\" /></form></div>";
}
function oregoncity ()
{
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>Congratulations, you win!  You've reached Oregon City.</b><br /><br /><form action=\"/cgi-bin/westwardtrail.pl\" method=\"post\"><input type=\"hidden\" name=\"command\" value=\"oregoncity\" /><br /><br /><input type=\"submit\" value=\"Compute final score -&gt;\" /></form></div>";
}
function gameover ()
{
	deleteCookie('wtusername','/','globalgamenetwork.com');
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>All members of your party have died!  You lose!</b><br /><br />If you like on-line strategy games, try some of our other games at the Global Game Network.  We have on-line Risk, chess, strategy/war games, economy games, country building games, and more.  Sign-up for free at <a href=\"http://www.globalgamenetwork.com\">www.GlobalGameNetwork.com</a><br /><br /><a href=\"/cgi-bin/westwardtrail.pl?command=startgame\">Play Again</a> | <a href=\"http://www.globalgamenetwork.com\">Exit Game</a></div>";
}
function timesup ()
{
	deleteCookie('wtusername','/','globalgamenetwork.com');
	document.getElementById('fullscene').style.visibility = 'visible';
	document.getElementById('fullscene').innerHTML = "<div style=\"margin: 20px\"><b>Severe winter weather has snowed you in.  You had to resort to cannibalism before finally dying of hunger.  You lose!</b><br /><br /><a href=\"/cgi-bin/westwardtrail.pl?command=startgame\">Play Again</a> | <a href=\"http://www.globalgamenetwork.com\">Exit Game</a></div>";
}

function showHunting ()
{
	//document.onkeydown = keyCheck;	//Get the key press
	//document.getElementById('fullscene').style.visibility = 'visible';
	//document.getElementById('fullscene').innerHTML = "<div style=\"z-index: 20\">hi<img src=\"/images/westwardtrail/hunter0.png\" id=\"hunter\" name=\"hunter\" style=\"position: absolute; left: 275px; top: 225px\" /><img src=\"/images/westwardtrail/bullet.png\" id=\"bullet\" name=\"bullet\" style=\"position: absolute; left: 10px; top: 10px\" /></div>";
}

function keyCheck(e)
{
	var KeyID = (window.event) ? event.keyCode : e.keyCode;

	switch(KeyID)
	{
		case 32:
		fireShot();
		break;
		case 37:
		hunterindex = hunterindex - 1;
		if (hunterindex < 0){hunterindex = 8;}
		break;
		case 39:
		hunterindex = hunterindex + 1;
		if (hunterindex > 8){hunterindex = 0;}
		break;
	}
	
	document.getElementById('hunter').src = hunterFrames[hunterindex];

}

function fireShot ()
{
	if (firing == 0)
	{
		firing = 1;
		bullets += 1;
		aimdirection = hunterindex;
		document.getElementById('bullet').style.left = parseInt(document.getElementById('hunter').style.left) + 45;
		document.getElementById('bullet').style.top = parseInt(document.getElementById('hunter').style.top) + 45;
		bulletshot = new Animator({duration: 800, interval: 10, transition: Animator.tx.linear, onComplete: function() {firing = 0;}});
		bulletshot.addSubject(drawBullet);
		bulletshot.play();
	}
}

function drawBullet ()
{
	if (aimdirection == 0){vectorx = -5; vectory = 0;}
	if (aimdirection == 1){vectorx = -5; vectory = -3;}
	if (aimdirection == 2){vectorx = -5; vectory = -5;}
	if (aimdirection == 3){vectorx = 0; vectory = -5;}
	if (aimdirection == 4){vectorx = 5; vectory = -4;}
	if (aimdirection == 5){vectorx = 5; vectory = 1;}
	if (aimdirection == 6){vectorx = 2; vectory = 4;}
	if (aimdirection == 7){vectorx = -1; vectory = 5;}
	if (aimdirection == 8){vectorx = -5; vectory = 5;}
	
	document.getElementById('bullet').style.left = parseInt(document.getElementById('bullet').style.left) + vectorx;
	document.getElementById('bullet').style.top = parseInt(document.getElementById('bullet').style.top) + vectory;
	
	// Check for hit
	if ((parseInt(document.getElementById('bullet').style.left) > parseInt(document.getElementById(animalname).style.left)) && (parseInt(document.getElementById('bullet').style.left) < parseInt(document.getElementById(animalname).style.left) + 90) && (parseInt(document.getElementById('bullet').style.top) > parseInt(document.getElementById(animalname).style.top)) && (parseInt(document.getElementById('bullet').style.top) < parseInt(document.getElementById(animalname).style.top) + 90))
	{
		meat = 10 + Math.floor(40*Math.random());
		var hitdeer = "hitdeer";
		if ((animalname == "deer2") || (animalname == "deer3")){hitdeer = "hitdeer-left";}
		document.getElementById(hitdeer).style.left = parseInt(document.getElementById(animalname).style.left);
		document.getElementById(hitdeer).style.top = parseInt(document.getElementById(animalname).style.top);
		document.getElementById(animalname).style.visibility = 'hidden';
	}
	
}

function moveAnimals ()
{
	animalname = Math.floor(4*Math.random());
	animalname = 'deer' + animalname;
	animalrun = new Animator({duration: 3000, interval: 10, transition: Animator.tx.linear, onComplete: function() {document.getElementById(animalname).style.left = 615; doneHunting();}});
	animalrun.addSubject(deerRun);
	animalrun.play();
}

function deerRun ()
{
	var newframe = deerFrames[deerindex];
	if ((animalname == "deer2") || (animalname == "deer3")){newframe = deerLeftFrames[deerindex];}
	document.getElementById(animalname).src = newframe;
	deerindex = (deerindex + 1) % 8;
	var offset = 0 - 5;
	if (animalname == "deer0"){offset = 0 - 7;}
	if ((animalname == "deer2") || (animalname == "deer3")){offset = 5;}
	document.getElementById(animalname).style.left = parseInt(document.getElementById(animalname).style.left) + offset;
}

function doneHunting ()
{
	var sendList = new Array();
	sendList['command'] = 'huntresults';
	sendList['food'] = meat;
	sendList['bullets'] = bullets;
	sendList['type'] = 'ajax';
	var poststr = packageMessage(sendList);
	postMessage('/cgi-bin/westwardtrail.pl',poststr,'donothing');
	
	document.getElementById('fullscene').innerHTML = '<div style=\"z-index: 20\">You killed ' + meat + ' lbs of meat.<br /><br /><a href=\"/cgi-bin/westwardtrail.pl\">Continue</a></div>';
}

function donothing (forminput)
{
}