document.getElementById("loadingMessage").innerHTML = 'Loading encounters...';


function encounterAction(actionToTake)
{
	switch(actionToTake)
	{
		case "evade":
		{
			highlightAttackOption("combatEvadeButton");
			setShipPartHighlight(encounterShip, null);
			getShipPart(playerShip, selectedShipPartID).posture = 'evade';

			break;
		}
		case "attack":
		{
			highlightAttackOption("combatAttackButton");
			if (selectedShipPartID != null) {
//				alert(selectedShipPartID);
				setShipPartHighlight(encounterShip, getShipPart(encounterShip, getShipPart(playerShip, selectedShipPartID).target), true);
			}
			getShipPart(playerShip, selectedShipPartID).posture = 'attack';
			break;
		}
		case "repair":
		{
			highlightAttackOption("combatRepairButton");
			setShipPartHighlight(encounterShip, null);
			getShipPart(playerShip, selectedShipPartID).posture = 'repair';
			break;
		}
		case "flee":
		{
			if (encounterType == "police") {
				if (((Math.random() * (10 + playerShip.adjMaxSpeed - playerShip.attachments.length)) > (Math.random() * (10 + encounterShip.adjMaxSpeed - encounterShip.attachments.length))) && portArray[closestPort].nation != playerGovernment) {
					// if it's a cop, you may be pursued.
					adjustReputation(-(30 + Math.floor(Math.random() *30)), selectedPort);
					showAlertWindow("Halt!", "While running away from the authorities always seems like a right smart move at the time, it takes but a few seconds of heavy laser fire from one's pursuer te sew entirely reasonable doubts as te the wisdom of said course of action.", "Stop <i>shooting</i> at me!");
					encounterAction("combat");
					return;
				}
			
			} else if (encounterType == "pirate") {
//				alert("you: " + (10 + playerShip.adjMaxSpeed - playerShip.attachments.length) + "; them: " + (10 + encounterShip.adjMaxSpeed - encounterShip.attachments.length));
				if ((Math.random() * (10 + playerShip.adjMaxSpeed - playerShip.attachments.length)) > (Math.random() * (10 + encounterShip.adjMaxSpeed - encounterShip.attachments.length))) {
					// if it's a pirate, you may be pursued.
					showAlertWindow("Not Fast Enough", "While ye were wise to make a break for it, yer pursuer somehow guessed that you may just turn tail and fly away as fast as ye can.<br /><br />On the plus side, ye has a chance te shoot yer weapons at something.  Ye like shooting yer weapons, right?", "Not much of a silver lining");
					encounterAction("combat");
					return;
				}
			}

			// otherwise, you get away.
			SwitchWindows('combatOrderButtons', 'combatActiveButtons');
			document.getElementById('battleCanvas').style.display = 'none';
			lossesFromDamage(0);
//			endCombat();
			break;
		}
		case "ignore":
		{
			if (encounterType == "police") {
				if (((Math.random() * -300) - 100) > portArray[closestPort].portHostility && portArray[closestPort].nation != playerGovernment) {
					// if it's a cop and you have a bad rep, there's an increasing chance you'll be attacked.
					showAlertWindow("Oh, dear", "It seems the authorities aren't exactly disposed to ignore you, seeing as you're not exactly the most popular ship in the system.<br /><br />In fact, they seem to be quite keen on tearing yer ship to shreds with large, concentrated blasts of energy.", "May as well return the favor");
					encounterAction("combat");
					return;
				}
//				alert("checking for a random scan...");
				if (Math.random() > 0.8) {
					if ((Coalesce(GetPartStatistic(playerShip, "cargo[6]"), "0") + Coalesce(GetPartStatistic(playerShip, "cargo[7]"), "0")) > 0 && portArray[closestPort].nation != playerGovernment) {
						// if it's a cop, there's a slight chance they'll scan.
						adjustReputation(-(25 + Math.floor(Math.random() *50)), closestPort);
						showAlertWindow("I Swear It's Oregano", "Ooh, bad luck!  It seems that while you were trying to nonchalantly meander past the local authorities, they decided to scan yer ship for illicit cargo.  Unfortunately, ye just happen to be haulin' just that.  Even more unfortunately, they have a \"vaporize on sight\" policy for contraband.  While it reduces internal corruption, it tends to have a nasty effect on the ships carrying said cargo...", "Too late to dump it now");
						encounterAction("combat");
						return;
					} else {
//						alert("scanned but clean");
					}
				} else {
//					alert("no scan.");
				}
			
			} else if (encounterType == "pirate") {
				if ((Math.random()) > 0.2) {
				// if it's a pirate, there's a good chance they'll attack.
					showAlertWindow("Not Se Fast There!", "It seems that yer brilliant plan of trying te sail straight past a bloodthirsty outlaw at a normal cruising speed didn't quite work out as expetced.<br /><br />Ye may want to consider shooting back.", "How rude!");
					encounterAction("combat");
					return;
				}
			}
			
			// otherwise, end combat.
			
			SwitchWindows('combatOrderButtons', 'combatActiveButtons');
			document.getElementById('battleCanvas').style.display = 'none';
			endCombat();
			break;
		}
		case "combat":
		{
			switch (encounterType) {
				case "pirate":
					adjustReputation((5 + Math.floor(Math.random() *25)), selectedPort);
					break;
				case "police":
					adjustReputation(-(20 + Math.floor(Math.random() *25)), selectedPort);
					break;
				case "civilian":
					adjustReputation(-(10 + Math.floor(Math.random() *15)), selectedPort);
					break;			
			}
			drawShipPartToDiv(playerShip, "combatYourShip", "combatYourShipHealth");
			drawShipPartToDiv(encounterShip, "combatTheirShip", "combatTheirShipHealth");
			drawShipPartHealthToDiv(playerShip, "combatYourShipHealth");
			drawShipPartHealthToDiv(encounterShip, "combatTheirShipHealth");
			document.getElementById("combatYourShip").style.setProperty("left", (270 - (playerShip.bgImages.length * 30)) + "px");
			document.getElementById("combatTheirShip").style.setProperty("left", (270 - (encounterShip.bgImages.length * 30)) + "px");
			document.getElementById("combatYourShipHealth").style.setProperty("left", (270 - (playerShip.bgImages.length * 30)) + "px");
			document.getElementById("combatTheirShipHealth").style.setProperty("left", (270 - (encounterShip.bgImages.length * 30)) + "px");
				SwitchWindows('encounterScreen', 'combatScreen');
		}
		case "pause":
		{
			SwitchWindows('combatActiveButtons', 'combatOrderButtons');
			document.getElementById('battleCanvas').style.display = 'none';
			encounterPaused = 1;
			break;
		}
		case "resume":
		{
			SwitchWindows('combatOrderButtons', 'combatActiveButtons');
			document.getElementById('battleCanvas').style.display = 'inline';
			encounterPaused = 0;
			
			setShipPartHighlight(encounterShip, null, false);
			setShipPartHighlight(playerShip, null, false);

			runCombat();
			break;
		}
	}
//	alert("ye've selected: " + actionToTake + "!");
}

function runCombat() {

	var i;
	var actionString;

	var currentCanvas = document.getElementById('battleCanvas');
	var curColor;
	

	if (currentCanvas.getContext)
	{
		var currentContext = currentCanvas.getContext('2d');
		currentContext.clearRect(0,0,currentCanvas.width,currentCanvas.height);
	}
	
	actionString = "";
	if (encounterPaused == 1) {
		return;
	}
	
	if (((20 - playerShip.attachments.length) + GetAdjustedStatistic(playerShip, "maxSpeed"))  > ((20 - encounterShip.attachments.length) + GetAdjustedStatistic(encounterShip, "maxSpeed")))
	{
		// player first
		actionString = playerShip.posture;
		combatAction(playerShip, getShipPart(encounterShip, playerShip.target), playerShip, encounterShip);
		for (i=0; i<playerShip.attachments.length; i++) {
			if (playerShip.attachments[i] != null)
			{
				if (playerShip.attachments[i].armor > 0)
				{
					actionString = actionString + ", " + playerShip.attachments[i].posture;
					combatAction(playerShip.attachments[i], getShipPart(encounterShip, playerShip.attachments[i].target), playerShip, encounterShip);
				} else {
					actionString = actionString + ", (" + playerShip.attachments[i].armor + ")";
				}
			}
		}

//		alert("player first: " + actionString); 

		combatAction(encounterShip, playerShip, encounterShip, playerShip);
		for (i=0; i<encounterShip.attachments.length; i++) {
			if (encounterShip.attachments[i] != null)
			{
				if (encounterShip.attachments[i].armor > 0)
				{
					combatAction(encounterShip.attachments[i], playerShip, encounterShip, playerShip);
				}
			}
		}

	} else {
		// enemy first

		combatAction(encounterShip, playerShip, encounterShip, playerShip);
		for (i=0; i<encounterShip.attachments.length; i++) {
			if (encounterShip.attachments[i] != null)
			{
				if (encounterShip.attachments[i].armor > 0)
				{
					combatAction(encounterShip.attachments[i], playerShip, encounterShip, playerShip);
				}
			}
		}

		actionString = playerShip.posture;
		combatAction(playerShip, getShipPart(encounterShip, playerShip.target), playerShip, encounterShip);
		for (i=0; i<playerShip.attachments.length; i++) {
			if (playerShip.attachments[i] != null)
			{
				if (playerShip.attachments[i].armor > 0)
				{
					actionString = actionString + ", " + playerShip.attachments[i].posture;
					combatAction(playerShip.attachments[i], getShipPart(encounterShip, playerShip.attachments[i].target), playerShip, encounterShip);
				} else {
					actionString = actionString + ", (" + playerShip.attachments[i].armor + ")";
				}
			}
		}
//		alert("enemy first: " + actionString); 
		
		
	}

	for (i=0; i<10; i++) {
		drawExplosion(playerShip, playerShip, currentContext);
		drawExplosion(encounterShip, encounterShip, currentContext);
	}
	
	drawShipPartHealthToDiv(playerShip, "combatYourShipHealth");
	drawShipPartHealthToDiv(encounterShip, "combatTheirShipHealth");
	destroyShip();

	
	setTimeout('runCombat();', 250);


}

function drawExplosion(partToDraw, rootPart, currentContext)
{
	var curColor;
	var iterations;
	var i;
	var tmpX, tmpY;
	if (partToDraw == null)
	{
		return;
	}
	
	if (partToDraw == rootPart) {
		tmpX = rootPart.x + (partToDraw.explosionX + randomInteger(partToDraw.width)-(partToDraw.width/2));
		tmpY = rootPart.y + (partToDraw.explosionY + randomInteger(partToDraw.height)-(partToDraw.height/2));
	} else {
		tmpX = rootPart.x + partToDraw.x + (partToDraw.explosionX + (Math.random()*partToDraw.width)-(partToDraw.width/2));
		tmpY = rootPart.y + partToDraw.y + (partToDraw.explosionY + (Math.random()*partToDraw.height)-(partToDraw.height/2));
	}
	
	if (rootPart == encounterShip)
	{
		// terrible, terrible kludge.  If you tell anybody about this terrible, terrible kludge, I will come to your house, and I will CUT YOU.</bob_ross>
		tmpX = tmpX + (270 - (encounterShip.bgImages.length * 30));
		tmpY = tmpY + 420;
	} else if (rootPart == playerShip) {
		tmpX = tmpX + (270 - (playerShip.bgImages.length * 30));
		tmpY = tmpY + 180;	
	}
	
	if (partToDraw.explosionTime > 0)
	{
//		alert("Boom!");
		if (partToDraw.armor == 0) {
			iterations = 5;
		} else {
			iterations = 1;
		}
		for (i=0; i<iterations; i++)
		{
			curColor = 'rgba(' + (235 + randomInteger(20)) + ', ' + (155 + randomInteger(100)) + ', 0, ' + (partToDraw.explosionTime / 15) + ')';
			currentContext.fillStyle = curColor;
			currentContext.beginPath();
			currentContext.arc(tmpX + (Math.random()*20-10), tmpY + (Math.random()*20-10),(1+randomInteger(partToDraw.explosionTime*(iterations+2))),0,Math.PI*2,true);
			currentContext.fill();
		}
		partToDraw.explosionTime = partToDraw.explosionTime - 1;
//		partToDraw.explosionTime = 0;
	}
	if (partToDraw.attachments == null) {
		return;
	}
	for (i=0; i<partToDraw.attachments.length; i++)
	{
		drawExplosion(partToDraw.attachments[i], rootPart, currentContext);
	}
}

function combatAction(sourcePart, targetPart, sourceRoot, targetRoot) 
{
	var i;
	switch (sourcePart.posture)
	{
		case "attack":
		{
			var damageDone, evasionOdds, alternateTargets, actualTarget;
			
			alternateTargets = new Array();
			if (targetPart == null)
			{
				targetPart = targetRoot;
			}
			if (targetPart.attachments != null) 
			{
				for (i = 0; i < targetPart.attachments.length; i++)
				{
					if (targetPart.attachments[i] != null) 
					{
						if (targetPart.attachments[i].armor != 0)
						{
							alternateTargets.push(targetPart.attachments[i]);
						}
					}
				}
			}

			if (alternateTargets.length == 0)
			{
//				alert("simple actual target: " + actualTarget);
				actualTarget = targetPart;
			} else {
				actualTarget = SelectRandomItemFromArray(alternateTargets);
//				alert("complex actual target: " + actualTarget);
//				alert("complex candidates: " + alternateTargets);
			}
			
			damageDone = GetAdjustedStatistic(sourcePart, "firepower");
			evasionOdds = randomInteger((20 - targetRoot.attachments.length) + GetAdjustedStatistic(targetRoot, "maxSpeed")) - randomInteger((20 - sourceRoot.attachments.length) + GetAdjustedStatistic(sourceRoot, "maxSpeed"));
			
			if (evasionOdds <= 0) {
				return; // attack successfully evaded!
			}
			
			inflictDamage(actualTarget, randomInteger(damageDone));
			if (actualTarget.armor <= 0) {
				// when the current target gets destroyed, auto-target to the main ship.
				sourcePart.target = -1;
			}
			drawShipPartHealth(actualTarget);
			drawShipPartHealth(targetRoot);
			
//			if (actualTarget.armor == 0 && (actualTarget == playerShip || actualTarget == encounterShip)) {
//				destroyShip();
//			}
			
			break;
		}
		case "repair":
		{
			if (getStatisticModifier(sourcePart, "engineering") > 0)
			{
				sourcePart.armor = sourcePart.armor + randomInteger(getStatisticModifier(sourcePart, "engineering"));
				var maxRepair;
				maxRepair = sourcePart.maxArmor / 2;
				maxRepair = maxRepair + ((sourcePart.maxArmor * getStatisticModifier(sourcePart, "engineering"))/100);
				if (maxRepair > sourcePart.maxArmor) {
					maxRepair = sourcePart.maxArmor;
				}
				if (sourcePart.armor > maxRepair)
				{
					sourcePart.armor = maxRepair;
				}
				drawShipPartHealth(sourcePart);
				drawShipPartHealth(sourceRoot);
			}
			break;
		}
		case "evade":
		{
			//don't do anything directly...yet.
			break;
		}
		default:
		{
			alert("bad shipPart.posture: " + sourcePart.posture);
			break;
		}
	}

}

function destroyShip()
{
	var victoryCash;
	var victoryCargoMessage;
	var i;
	
	victoryCargoMessage = "";
	// someone's going down.
	if (playerShip.armor == 0) {
		// sorry, mate.  Yer dead.
		encounterPaused = 1;
		resetDatabase(systemDB);
		SwitchWindows('combatScreen', 'deadScreen');
	} else if (encounterShip.armor == 0) {
		// arr, sweet victory!
//		alert("destroy enemy  ship!");
		encounterPaused = 1;
		victoryCash = Math.floor(encounterShip.baseCost * (0.05 + Math.random()*0.15));
		
		victoryCargoType = -1;
		victoryCargoAmount = 0;
		switch (encounterType) {
			case "pirate":
				adjustReputation((50 + Math.floor(Math.random() *100)), selectedPort);
				break;
			case "police":
				adjustReputation(-(100 + Math.floor(Math.random() *200)), selectedPort);
				break;
			case "civilian":
				adjustReputation(-(50 + Math.floor(Math.random() * 50)), selectedPort);
				break;			
		}
		switch(encounterShip.partType) {
			case "gun": {
				// no good cargo here, but more cash!
				victoryCash = Math.floor(victoryCash * 1.5);
				break;
			}
			case "speed": {
				// a chance of more valuable cargo possible!
				if (Math.random() < 0.5) {
					if (encounterType == "pirate" && (Math.random() < 0.25)) {
						victoryCargoType = randomInteger(5);
						victoryCargoAmount = Math.floor(encounterShip.maxCargo * (0.2 + Math.random()*0.8));
					} else {
						victoryCargoType = 3+randomInteger(4);
						victoryCargoAmount = Math.floor(encounterShip.maxCargo * (0.2 + Math.random()*0.8));
					}
				}
				break;
			}
			case "cargo": 
			default: {
				// undoubtedly cargo here!
				if (encounterType == "pirate" && (Math.random() < 0.25)) {
					victoryCargoType = randomInteger(7);
					victoryCargoAmount = Math.floor(encounterShip.maxCargo * (0.4 + Math.random()*2));
				} else {
					victoryCargoType = randomInteger(4);
					victoryCargoAmount = Math.floor(encounterShip.maxCargo * (0.4 + Math.random()*2));
				}
				break;
			}
		}
		
		if (victoryCargoType == -1) {
			// you can always pilfer a little food...
			victoryCargoType = 0;
		}
		
		if (victoryCargoAmount < 2) {
			victoryCargoAmount = 2 + randomInteger(5);
		}
		
		if (victoryCargoType != -1) {
			victoryCargoMessage = "<br />&nbsp;Ye has the opportunity te take " + victoryCargoAmount + " kilotonnes of " + goodsNames[victoryCargoType].toLowerCase() +", as well.";
		}
		AdjustCash(Math.floor(victoryCash));
		
		// adjust mission countdowns, if applicable
		
		for (i=0; i<currentMissions.length; i++) {
			if (currentMissions[i].targetType != "") {
				if (encounterType == currentMissions[i].targetType && closestPort == currentMissions[i].targetPort) {
					currentMissions[i].targetQuantity = currentMissions[i].targetQuantity - 1;
					if (currentMissions[i].targetQuantity <= 0) {
						currentMissions[i].targetQuantity = 0;
					}
					switch (currentMissions[i].targetType) {
						case "civilian":
							currentMissions[i].missionDescription = "Destroy " + currentMissions[i].targetQuantity + " more civilian ships in the " + portArray[currentMissions[i].targetPort].portName + " system.";
							break;
						case "police":
							currentMissions[i].missionDescription = "Destroy " + currentMissions[i].targetQuantity + " more naval ships in the " + portArray[currentMissions[i].targetPort].portName + " system.";
							break;
						case "pirate":
							currentMissions[i].missionDescription = "Destroy " + currentMissions[i].targetQuantity + " more pirate ships in the " + portArray[currentMissions[i].targetPort].portName + " system.";
							break;
					}
					if (currentMissions[i].targetQuantity <= 0) {
						currentMissions[i].missionDescription = "Return to the " + portArray[currentMissions[i].completionPort].portName + " system to complete this mission."
					}
				}
			}
		}
		
		showConfirmationWindow("Victory!", "Arr, ye've won the battle!<br />&nbsp;<br />Ye've plundered " + victoryCash + " credits from yer adversary." + victoryCargoMessage, "Take what spoils ye can", "Just the cash, thank ye", "battleVictory");

	} else {
		// still going.  Maybe add enemy surrender code here, if he's getting whomped?
	}
}

function lossesFromDamage(workingIndex) {
	
	if (workingIndex >= playerShip.attachments.length) {
		if (encounterShip.armor <= 0) {
			// you won a battle--you may get crewmen out of it!
			addCrewmanFromVictory(0);
		} else {
			// you fled.  No crewmen for you.
			endCombat();
		}
		return;
	}
	
	if (playerShip.attachments[workingIndex].maxArmor == 0) {
		// the part has no armor and therefore doesn't exist in the first place
		lossesFromDamage(workingIndex + 1);
		return;
	}
	
	if (playerShip.attachments[workingIndex].armor > 0) {
		// the shippart is still functional; do nothing
		lossesFromDamage(workingIndex + 1);
		return;
	}
	
	if (playerShip.attachments[workingIndex].curCargo == 0 && playerShip.attachments[workingIndex].curCrew == 0) {
		// eh, there's nothing to lose, so why even check?
		lossesFromDamage(workingIndex + 1);
		return;
	}
	
	//OK, now we test for baaad stuff.
	if (rnd() < 0.1) {
//				alert("collateral damage!");
		showConfirmationWindow("Collateral Damage", "Unfortunately, the damage to yer " + playerShip.attachments[workingIndex].partName + " is severe enough that ye've lost some things of value, namely all the crew and cargo in that particular part of yer ship.", "Arr, hull breaches suck.", "", "encounterPartVent", workingIndex);
		return;
	} else if (rnd() < 0.02) {
		showConfirmationWindow("Does ye feel a draft?", "It would seem that in the heat of th' encounter, yer " + playerShip.attachments[workingIndex].partName + " got blown clear off yer hull.  On the bright side, this frees up a fresh new space on yer ship fer new and exciting things!", "No fair!  Put it back!", "", "encounterPartLoss", workingIndex);
		return;
	} else {
		// Got by OK this time around.
		lossesFromDamage(workingIndex + 1);
		return;
	}
	
}

function endCombat()
{
//	alert("ending combat!");
	inEncounter = 0;
	encounterPaused = 1;
	document.getElementById('confirmScreen').style.display = 'none';
	document.getElementById('encounterScreen').style.setProperty('display', 'none');
	SwitchWindows('combatOrderButtons', 'combatActiveButtons');
	document.getElementById('battleCanvas').style.display = 'none';
	SwitchWindows('combatScreen', 'transitScreen');
	setTimeout(ShipInTransit, 10);
}

function highlightAttackOption(optionToHighlight)
{
	document.getElementById("combatRepairButton").style.setProperty("background-color", "#000000");
	document.getElementById("combatFleeButton").style.setProperty("background-color", "#000000");
	document.getElementById("combatEvadeButton").style.setProperty("background-color", "#000000");
	document.getElementById("combatAttackButton").style.setProperty("background-color", "#000000");
	document.getElementById(optionToHighlight).style.setProperty("background-color", "#006600");
}

function showCombatShipPart() {
	
}

function startEncounter() {

	var shipDescriptor="";
	var governmentAirspace;
	var hullIntegrity;
	
	calculateAdjustedValues(playerShip);
	
	// determine ship's hull integrity
	hullIntegrity = "<br/>Your ship's hull is at " + Math.floor((GetAdjustedStatistic(playerShip, "armor")/GetAdjustedStatistic(playerShip, "maxArmor")) * 100) + "%.";

	if ((Coalesce(GetPartStatistic(playerShip, "cargo[6]"), "0") + Coalesce(GetPartStatistic(playerShip, "cargo[7]"), "0")) > 0) {
		hullIntegrity = hullIntegrity + "  Ye be carrying contraband.";
	}

	if (closestPort == -1) {
		document.getElementById('airspaceMessage').innerHTML = "You are in deep space.  There is no law out here." + hullIntegrity;
		if (encounterType == "police") {
			encounterType = "civilian";
		}
	} else {
		if (portArray[closestPort].nation == -1) {
			document.getElementById('airspaceMessage').innerHTML = "You are in " + portArray[closestPort].portName + " space." + hullIntegrity;
		} else {
			document.getElementById('airspaceMessage').innerHTML = "You are in " + governmentAdjective[portArray[closestPort].nation] + " space, near the " + portArray[closestPort].portName + " system." + hullIntegrity;
		}
	}
	
	
	
	
	
	
	createRandomEncounterShip(Math.floor(-10));
/*
	switch (encounterType) {
		case "pirate": {
			createRandomEncounterShip(4-randomInteger(8));
			break;
		}
		case "police": {
			createRandomEncounterShip(randomInteger(2));
			break;
		}
		case "civilian": {
			createRandomEncounterShip(1-randomInteger(5));
			break;
		}
		default: {
			createRandomEncounterShip(1-randomInteger(7));
			break;
		}
	}
*/	
	createRandomEncounterShip();
	
	switch(encounterShip.partType) {
		case "gun":
		{
			shipDescriptor = hullGunNames[encounterShip.attachments.length-1];
			break;
		}
		case "cargo":
		{
			shipDescriptor = hullCargoNames[encounterShip.attachments.length-1];
			break;
		}
		case "speed":
		{
			shipDescriptor = hullSpeedNames[encounterShip.attachments.length-1];
			break;
		}
	}
	
	switch(encounterType)
	{
		case "pirate":
			switch (encounterShip.partType)
			{
				case "gun":
				{
					shipDescriptor = shipDescriptor + " of the dread pirate ";
					break;
				}
				case "cargo":
				{
					shipDescriptor = shipDescriptor + " of the black marketeer ";
					break;
				}
				
				case "speed":
				{
					shipDescriptor = shipDescriptor + " of the infamous smuggler ";
					break;
				}
			}
			document.getElementById('encounterMessage').innerHTML = "Yarrr! Ye've encountered the <em>" + encounterShip.partName + "</em>, the " + shipDescriptor + encounterShip.crew[0].firstName + " " + encounterShip.crew[0].lastName + "!";
			break;
		case "police":

			if (portArray[closestPort].nation == -1)
			{
				if (startsWithAVowelSound(portArray[closestPort].portName))
				{
					shipDescriptor = "an " + portArray[closestPort].portName + " Navy " + shipDescriptor;
				} else {
					shipDescriptor = "a " + portArray[closestPort].portName + " Navy " + shipDescriptor;
				}
			} else {
				if (startsWithAVowelSound(governmentAdjective[portArray[closestPort].nation]))
				{
					shipDescriptor = "an " + governmentAdjective[portArray[closestPort].nation] + " Navy " + shipDescriptor;
				} else {
					shipDescriptor = "a " + governmentAdjective[portArray[closestPort].nation] + " Navy " + shipDescriptor;
				}
			}

			document.getElementById('encounterMessage').innerHTML = "You've encountered the <em>" + encounterShip.partName + "</em>, " + shipDescriptor + ".";
			break;
		case "civilian":
			document.getElementById('encounterMessage').innerHTML = "You've encountered a civilian " + shipDescriptor + ".";
			break;
	}
	

	selectShipPart(0, "shipManagementImage", "shipManagementHealthImage");	
	
	drawShipPart(encounterShip);
	setShipPartHighlight(playerShip, null);
//	drawShipPart(playerShip);


	drawShipPartToDiv(encounterShip, "encounterTheirShip", "encounterTheirShipHealth");
//	drawShipPartHealthToDiv(encounterShip, "encounterTheirShipHealth");

//	drawShipPart(playerShip, "combatYourShip", 0, 0, true);
//	drawShipPart(encounterShip, "combatTheirShip", 0, 0, true);

//	drawShipPart(playerShip);

//	drawShipPart(encounterShip, "encounterTheirShip", 0, 0, true);
	document.getElementById("encounterTheirShip").style.setProperty("left", (270 - (encounterShip.bgImages.length * 30)) + "px");
	document.getElementById("encounterTheirShipHealth").style.setProperty("left", (270 - (encounterShip.bgImages.length * 30)) + "px");
	
	SwitchWindows('transitScreen', 'encounterScreen');
	
	
	
}

jsFilesLoaded = jsFilesLoaded + 1;
