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


function SetPortsVisible()
{

	var i;
	
	for (i=0; i < portArray.length; i++)
	{
		if (portArray[i].portFamiliarity == 0)
		{
			document.getElementById('mapPort' + i).style.setProperty('display', 'none');
			document.getElementById('mapLabel' + i).style.setProperty('display', 'none');
		} else {
			document.getElementById('mapPort' + i).style.setProperty('display', 'block');
			document.getElementById('mapLabel' + i).style.setProperty('display', 'block');
		}
		if (portArray[i].isCapital)
		{
			document.getElementById('mapLabel' + i).style.setProperty('font-weight', 'bold');
			document.getElementById('mapLabel' + i).style.setProperty('text-decoration', 'underline');
		} else {
			document.getElementById('mapLabel' + i).style.setProperty('font-weight', 'normal');
			document.getElementById('mapLabel' + i).style.setProperty('text-decoration', 'none');
		}
		if (portArray[i].nation == -1)
		{
			document.getElementById('mapLabel' + i).style.setProperty('color', '#999999');
		} else {
			document.getElementById('mapLabel' + i).style.setProperty('color', governmentColors[portArray[i].nation]);
		}
	}

}

function SetNationality(portIndex, governmentIndex)
{
	if (portArray[portIndex].nation == -1)
	{
		portArray[portIndex].nation = governmentIndex;
	}
}

function drawQuadratics(pointsToDraw, pointsPerShape, workingScale, currentContext)
{
	var i, j, tmpIndex;
	for(i=0; i<pointsToDraw.length; i=i+(pointsPerShape*6)) {
		currentContext.beginPath();
		
		currentContext.moveTo(pointsToDraw[(i+(pointsPerShape*6)-2)]*workingScale, pointsToDraw[(i+(pointsPerShape*6)-2)]*workingScale);
		for (j=0; j < pointsPerShape; j++) {
			tmpIndex = i + (j*6);
//			currentContext.bezierCurveTo(pointsToDraw[(tmpIndex+0)]*workingScale, pointsToDraw[(tmpIndex+1)]*workingScale, pointsToDraw[(tmpIndex+2)]*workingScale, pointsToDraw[(tmpIndex+3)]*workingScale, pointsToDraw[(tmpIndex+4)]*workingScale, pointsToDraw[(tmpIndex+5)]*workingScale);
			currentContext.lineTo(pointsToDraw[(tmpIndex+4)]*workingScale, pointsToDraw[(tmpIndex+5)]*workingScale);
		}
		currentContext.closePath();
		currentContext.fill();
	}
}

function drawQuadraticsNew(arrayX, arrayY, arrayCp1X, arrayCp1Y, arrayCp2X, arrayCp2Y, pointsPerShape, workingScale, currentContext)
{
	for(i=0; i<(arrayX.length / pointsPerShape); i++) {
		currentContext.beginPath();
		
		currentContext.moveTo(arrayX[(i+(pointsPerShape)-1)]*workingScale, arrayY[(i+(pointsPerShape)-1)]*workingScale);
		for (j=0; j <= pointsPerShape; j++) {
			tmpIndex = j + (i*pointsPerShape);
//			currentContext.lineTo(arrayX[tmpIndex]*workingScale, arrayY[tmpIndex]*workingScale);
			currentContext.bezierCurveTo(arrayX[tmpIndex]*workingScale, arrayY[tmpIndex]*workingScale, arrayCp1X[tmpIndex]*workingScale, arrayCp1Y[tmpIndex]*workingScale, arrayCp2X[tmpIndex]*workingScale, arrayCp2Y[tmpIndex]*workingScale);
		}
		currentContext.closePath();
//		currentContext.stroke();
		currentContext.fill();
	}
}

function drawPortPlanet(portToDraw, canvasID, isReallyACanvas)
{
	var currentCanvas = document.getElementById(canvasID);
	var workingScale;
	var i;
	var sphereOverlay;
	
	if (isReallyACanvas == null) {
		currentCanvas = document.getElementById(canvasID);
	} else {
		currentCanvas = canvasID;
	}
	
	if (currentCanvas.getContext)
	{
//		alert("drawing planet! color = " + portArray[portToDraw].baseColor);
		var currentContext = currentCanvas.getContext('2d');
		workingScale = currentCanvas.width;
		currentContext.clearRect(0,0,workingScale,workingScale);
		
		
		currentContext.beginPath();
		currentContext.arc((workingScale/2),(workingScale/2),(workingScale/2.1),0,Math.PI*2,true);
		currentContext.clip();
		if (portArray[portToDraw] == null) {
			alert("Oops!  Null! " + portToDraw);
		}
		currentContext.fillStyle = portArray[portToDraw].baseColor;
		currentContext.fillRect(0, 0, workingScale, workingScale);
		currentContext.closePath();
		currentContext.fill();
		
		// draw "continents"
		
		currentContext.fillStyle = portArray[portToDraw].continentColor;
//		drawQuadratics(portArray[portToDraw].landMass, 5, workingScale, currentContext);
		drawQuadraticsNew(portArray[portToDraw].landMassX, portArray[portToDraw].landMassY, portArray[portToDraw].landMassCp1X, portArray[portToDraw].landMassCp1Y, portArray[portToDraw].landMassCp2X, portArray[portToDraw].landMassCp2Y, 9, workingScale, currentContext);
		// draw the dark overlay
		sphereOverlay = currentContext.createRadialGradient(workingScale,workingScale,0,0,0,workingScale);
		sphereOverlay.addColorStop(0, "rgba(0, 0, 0, 0.5)");
		sphereOverlay.addColorStop(1, "rgba(0, 0, 0, 0)");
		currentContext.fillStyle = sphereOverlay;
		currentContext.fillRect(0, 0, workingScale, workingScale);
		// draw the light overlay
		sphereOverlay = currentContext.createRadialGradient(0,0,0,workingScale,workingScale,workingScale);
		sphereOverlay.addColorStop(0, "rgba(255, 255, 255, 0.5)");
		sphereOverlay.addColorStop(1, "rgba(255, 255, 255, 0)");
		currentContext.fillStyle = sphereOverlay;
		currentContext.fillRect(0, 0, workingScale, workingScale);
	}

}

function drawPortIcon(portToDraw)
{
	var currentCanvas = document.getElementById('mapLabel'+portToDraw);
	var curNation = portArray[portToDraw].nation;
	var curColor;
	var i;
	

	if (currentCanvas.getContext)
	{
		var hasActiveMission=false;
		var currentContext = currentCanvas.getContext('2d');
		currentContext.clearRect(0,0,28,28);
//		currentContext.fillRect(0,0,28,28);

		for (i=0; i<7; i++) {
			if (currentMissions[i] != null) {
				if (currentMissions[i].targetPort == portToDraw && currentMissions[i].missionStatus == "current") {
					hasActiveMission = true;
				}
			}
		}
	

		if (portArray[portToDraw].portFamiliarity < 500000) {
			curColor = "#666666";
			currentContext.lineWidth = 3
			currentContext.strokeStyle = curColor;
			currentContext.beginPath();
			currentContext.arc(14,14,12,0,Math.PI*2,true);
			currentContext.stroke();
			if (hasActiveMission == true) {
				// highlight the planet for deliveries
				curColor = "#999900";
				currentContext.lineWidth = 6
				currentContext.strokeStyle = curColor;
				currentContext.strokeRect(0, 0, 28, 28);
//				currentContext.strokeRect(2, 2, 24, 24);
			}
		} else {		

			currentContext.save();
			drawPortPlanet(portToDraw, 'mapLabel'+portToDraw);
			currentContext.restore();
			if (portArray[portToDraw].hostility < 100 || portArray[portToDraw].security < .75) {
				currentContext.fillStyle = "#333333";
				currentContext.beginPath();
				currentContext.arc(14,14,12,0,Math.PI*2,true);
				currentContext.stroke();
			}

			if (hasActiveMission == true) {
				// highlight the planet for deliveries
				curColor = "#999900";
				currentContext.lineWidth = 6
				currentContext.strokeStyle = curColor;
				currentContext.strokeRect(0, 0, 28, 28);
//				currentContext.strokeRect(2, 2, 24, 24);
			}
	
			if (curNation == -1)
			{
				curColor = "rgba(1, 1, 1, 0)";
			} else {
				curColor = governmentColors[curNation];
			}
			
			if (portArray[portToDraw].isCapital) {
				currentContext.lineWidth = 5;
			} else {
				curColor = "rgba(1, 1, 1, 0)";
			}
			
			
			currentContext.strokeStyle = curColor;
			currentContext.fillStyle = curColor;
			currentContext.beginPath();
			currentContext.arc(14,14,12,0,Math.PI*2,true);
			currentContext.stroke();
		}		
	}
	

	
	if (portToDraw == currentPort) {
		curColor = "#00FF00";
		currentContext.lineWidth = 6
		currentContext.strokeStyle = curColor;
		currentContext.beginPath();
		currentContext.arc(14,14,18,0,Math.PI*2,true);
		currentContext.stroke();
	}
	
	if (portToDraw == selectedPort) {
		curColor = "#FF0000";
		currentContext.lineWidth = 3
		currentContext.strokeStyle = curColor;
		currentContext.beginPath();
		currentContext.arc(14,14,18,0,Math.PI*2,true);
		currentContext.stroke();
	}
	
}



function SelectPortOnMap(dstPort)
{
	var tmpPort;
//	alert("clicked port: " + dstPort);
	tmpPort = selectedPort;

	if (selectedPort != -1)
	{
//		document.getElementById('mapPort' + selectedPort).style.backgroundImage = 'url(images/mapDot.png);';
		selectedPort = -1;
		drawPortIcon(tmpPort);
	}
	
	selectedPort = dstPort;
	drawPortIcon(selectedPort);

//	document.getElementById('mapPort' + selectedPort).style.setProperty('background-image', 'url(images/mapDotTarget.png)');
//	document.getElementById('mapPort' + currentPort).style.setProperty('background-image', 'url(images/mapDotCurrent.png)');
	document.getElementById('selectedPortName').innerHTML = portArray[selectedPort].portName;
//	document.getElementById('selectedPortDistance').innerHTML = GetRange(portArray[selectedPort].x, portArray[currentPort].x, portArray[selectedPort].y, portArray[currentPort].y);
	document.getElementById('selectedPortDistance').innerHTML = GetRangeBetweenPorts(selectedPort, currentPort);
//	SetText("maximumJumpDistance", GetAdjustedStatistic(playerShip, "fuel"));
	if (playerShip != null) {
		SetText("maximumJumpDistance", playerShip.fuel);
	}
	SetRouteSecurityText(portArray[selectedPort].routeSecurity);
	setRouteReputationText(portArray[selectedPort].portHostility);

	if (portArray[selectedPort].nation == -1)
	{
		SetText("routeGovernment", "Independent");
	} else {
		SetText("routeGovernment", governmentNames[portArray[selectedPort].nation]);
	}
	document.getElementById('cancelTheJump').style.setProperty('display', 'none');

	if (playerShip != null) {
		if (document.getElementById('selectedPortDistance').innerHTML <= playerShip.fuel)
		{
			document.getElementById('makeTheJump').style.setProperty('display', 'inline');
		} else {
			document.getElementById('makeTheJump').style.setProperty('display', 'none');
		}
		if (Math.floor(document.getElementById('selectedPortDistance').innerHTML) <= 0) 
		{
			document.getElementById('makeTheJump').style.setProperty('display', 'none');
			document.getElementById('cancelTheJump').style.setProperty('display', 'inline');
		}
	}
	
	
}

function SetRouteSecurityText(tgtSecurity)
{
	if (tgtSecurity < .25)
	{
		SetText("routeSecurity", "No Man's Land");
	}
	else if (tgtSecurity < .5)
	{
		SetText("routeSecurity", "Lawless");
	}
	else if (tgtSecurity < .75)
	{
		SetText("routeSecurity", "Dangerous");
	}
	else if (tgtSecurity < .90)
	{
		SetText("routeSecurity", "Risky");
	}
	else if (tgtSecurity < 1.10)
	{
		SetText("routeSecurity", "Lightly guarded");
	}
	else if (tgtSecurity < 1.25)
	{
		SetText("routeSecurity", "Well patrolled");
	}
	else if (tgtSecurity < 1.50)
	{
		SetText("routeSecurity", "Heavily protected");
	}
	else
	{
		SetText("routeSecurity", "Completely secure");
	}
}

function setRouteReputationText(tgtReputation) {

	if (tgtReputation < -2000) {
		SetText("routeReputation", "Supervillian");
	} else if (tgtReputation < -1600) {
		SetText("routeReputation", "Crime Lord");
	} else if (tgtReputation < -1400) {
		SetText("routeReputation", "Infamous");
	} else if (tgtReputation < -1200) {
		SetText("routeReputation", "Notorious");
	} else if (tgtReputation < -1000) {
		SetText("routeReputation", "Felon");
	} else if (tgtReputation < -500) {
		SetText("routeReputation", "Wanted");
	} else if (tgtReputation < -250) {
		SetText("routeReputation", "Disliked");
	} else if (tgtReputation < -100) {
		SetText("routeReputation", "Unwelcome");
	} else if (tgtReputation < 50) {
		SetText("routeReputation", "Neutral");
	} else if (tgtReputation < 50) {
		SetText("routeReputation", "Welcome");
	} else if (tgtReputation < 150) {
		SetText("routeReputation", "Liked");
	} else if (tgtReputation < 400) {
		SetText("routeReputation", "Trusted");
	} else if (tgtReputation < 800) {
		SetText("routeReputation", "Respected");
	} else if (tgtReputation < 1200) {
		SetText("routeReputation", "Honored");
	} else if (tgtReputation < 1600) {
		SetText("routeReputation", "Revered");
	} else if (tgtReputation < 2000) {
		SetText("routeReputation", "Hero");
	} else {
		SetText("routeReputation", "Legendary Hero");
	}

}

function adjustReputation(amountToAdjustBy, targetPort) {

	var reputationAdjustment;
	var i;
	
	if (portArray[targetPort].portHostility > 0 && amountToAdjustBy < 0) {
		//bad actions -really- hurt good karma...
		portArray[targetPort].portHostility = portArray[targetPort].portHostility * 2 / 3
	}
	
	reputationAdjustment = amountToAdjustBy * Math.random();
	
	portArray[targetPort].portHostility += reputationAdjustment
	
	if (portArray[targetPort].nation != -1) {
		for (i=0; i<portArray.length;i++) {
			if (i != targetPort && portArray[i].nation == portArray[targetPort].nation) {
				portArray[i].portHostility += (reputationAdjustment * (Math.random() * .25));
			}
		}
	}

}

function GetRangeBetweenPorts(portA, portB)
{
	return GetRange(portArray[portA].x, portArray[portB].x, portArray[portA].y, portArray[portB].y);
}

function JumpToSelectedSystem()
{

	var i;

	if (document.getElementById('selectedPortDistance').innerHTML <= GetAdjustedStatistic(playerShip, "fuel"))
	{
//		autoSaveGame();
		distanceTravelled = 0;
		distanceToTravel = Math.floor(document.getElementById('selectedPortDistance').innerHTML);
		setTimeout('ShipInTransit();', 250);
		playerShip.fuel = playerShip.adjMaxFuel
 - Math.floor(document.getElementById('selectedPortDistance').innerHTML);
		document.getElementById('closeTransitScreen').style.display='none';
		document.getElementById('progressTravel').style.setProperty('width', '0px');
		document.getElementById('mapPort' + currentPort).style.backgroundImage = 'url(images/mapDot.png);';
//		if (currentPort != selectedPort)
//		{
//			SetPrices();
//		}
		if (playerCash > (playerShip.adjMaxFuel - playerShip.fuel))
		{
//			alert("playerShip.maxAdjFuel=" + playerShip.adjMaxFuel + " playerShip.fuel=" + playerShip.fuel);
			AdjustCash(-(playerShip.adjMaxFuel - playerShip.fuel));
			playerShip.fuel = playerShip.adjMaxFuel;
		} else {
			playerShip.fuel = playerShip.fuel + playerCash;
			playerCash = 0;
			AdjustCash(0);
		}
		
		var tmpPort;
		departurePort = currentPort;
		tmpPort = currentPort;
		currentPort = selectedPort;
//		alert(tmpPort);
		drawPortIcon(tmpPort);
		currentPort = tmpPort;
		
		// if running a mission in the target system that involves killing, give a man a target when he gets there!
		
		encountersAtArrival = 0;
		encounterTypeAtArrival = "civilian";
		for(i=0; i<currentMissions.length; i++) {
			if (currentMissions[i].missionStatus == "current" && currentMissions[i].targetPort == selectedPort && currentMissions[i].targetType != "") {
				encounterTypeAtArrival = currentMissions[i].targetType;
				encountersAtArrival = encountersAtArrival + 3;
				// if we're trying to conquer, make it interesting...
				if (currentMissions[i].successTargetGovernment != portArray[selectedPort].nation) {
					encountersAtArrival = encountersAtArrival + 2;
				}
			}
		}
		
		


		SetCurrentPort(selectedPort);
		SwitchWindows('mapScreen', 'transitScreen');
	}
	
}

function SetCurrentPort(newPort)
{
	var currentCanvas = document.getElementById("mapTerritory");
	var workingScale;
	var i;
	currentPort = newPort;
	SelectPortOnMap(currentPort);
	document.getElementById('currentPortName').innerHTML = portArray[currentPort].portName;
	document.getElementById('currentPortName2').innerHTML = portArray[currentPort].portName;
	document.getElementById('primaryDescriptor').innerHTML = primaryPlanetDescriptors[portArray[currentPort].primaryDescriptor];
	document.getElementById('secondaryDescriptor').innerHTML = secondaryPlanetDescriptors[portArray[currentPort].secondaryDescriptor];
	if (playerShip != null) {
		SetText("maximumJumpDistance", playerShip.fuel);
	}
	UpdateNeighboringPorts(currentPort);
	document.getElementById("gotoBar").style.display = "block";
		
		
	// draw territory shapes
	
	if (currentCanvas.getContext)
	{
//		alert("drawing planet! color = " + portArray[portToDraw].baseColor);
		var currentContext = currentCanvas.getContext('2d');
		workingScale = currentCanvas.width;
	}
		currentContext.clearRect(0,0,workingScale,workingScale);

		// draw explored space
		currentContext.save();
		currentContext.beginPath();
	for (i=0; i<portArray.length; i++)
	{
		if (portArray[i].portFamiliarity >= 500000)
		{
//			alert("draw!");
			currentContext.moveTo(portArray[i].x, portArray[i].y);
			currentContext.arc(portArray[i].x, portArray[i].y, 100, 0, Math.PI*2, true);
		}
	}
	currentContext.clip();
	currentContext.fillStyle = "rgba(150, 150, 150, 1.0)";
	currentContext.fillRect(0, 0, currentCanvas.width, currentCanvas.height);
	
	// draw national space
	for (i=0; i<portArray.length; i++)
	{
		if (portArray[i].portFamiliarity >= 0) // we know nationality so long as a planet is visible
		{
//			alert("draw!");
			currentContext.setGlobal
			currentContext.beginPath();
			if (portArray[i].nation >= 0)
			{
				currentContext.fillStyle = governmentColors[portArray[i].nation];
			} else {
				currentContext.fillStyle = "rgba(50, 50, 50, 1.0)";
			}
			currentContext.moveTo(portArray[i].x, portArray[i].y);

//			if ((portArray[i].hostileInfluence/1.5) < portArray[i].influence)
//			{
				if (portArray[i].nation != -1)
				{
					currentContext.arc(portArray[i].x, portArray[i].y, portArray[i].hostileInfluence/1.5, 0, Math.PI*2, true);
				}
//			} else {
//				if (portArray[i].nation != -1)
//				{
//					currentContext.arc(portArray[i].x, portArray[i].y, portArray[i].influence, 0, Math.PI*2, true);
//				}
//			}
/*
			if ((portArray[(portArray[i].nearestNeighbor)].nation != portArray[i].nation) || (portArray[i].nation < 0))
			{
				currentContext.arc(portArray[i].x, portArray[i].y, portArray[i].influence/2, 0, Math.PI*2, true);
			} else {
				currentContext.arc(portArray[i].x, portArray[i].y, portArray[i].influence, 0, Math.PI*2, true);
			}
*/
			currentContext.fill();
		}
	}
	
	currentContext.fillStyle = "rgba(0, 0, 0, 0.8)";
	currentContext.fillRect(0, 0, currentCanvas.width, currentCanvas.height);

	currentContext.restore();
	
	
	
	
	// prep recruitable crewmen
	barCrewmenSeed = Math.floor(10000000 * Math.random());
	populateBarCrewmen(barCrewmenSeed);


}


function UpdateNeighboringPorts(curPort)
{
	var i;
	portArray[curPort].portFamiliarity += 500000;

	for (i = 0; i<portArray[curPort].nearNeighbors.length; i++)
	{
		portArray[portArray[curPort].nearNeighbors[i]].portFamiliarity += 1;
	}
	
	for (i = 0; i<portArray[curPort].farNeighbors.length; i++)
	{
		portArray[portArray[curPort].farNeighbors[i]].portFamiliarity += 1;
	}
}

function ShipInTransit()
{
	var i;
	if (inEncounter == 1)
	{
		return;	// don't travel while in an encounter...
	}
	if (distanceTravelled >= distanceToTravel)
	{
		distanceTravelled = distanceToTravel;
		IncrementProgress(distanceTravelled, distanceToTravel, 'progressTravel');
		if (encountersAtArrival > 0) {
//			alert("encounter!  " + encountersAtArrival);
			encountersAtArrival = encountersAtArrival - 1;
			encounterType = encounterTypeAtArrival;
			calculateNearestSystem();
			startEncounter();
		} else {
			setTimeout('completeJump();', 50);
		}
	} else {
		// repair ship bit by bit
		
		repairShipInTransit(playerShip, 0);
		
		IncrementProgress(distanceTravelled, distanceToTravel, 'progressTravel');
		distanceTravelled = distanceTravelled + (GetAdjustedStatistic(playerShip, "maxSpeed") * .1);
		AddToCurrentStardate(1);
		testForEncounter();
		
		setTimeout('ShipInTransit();', 50);
	}

}

function calculateNearestSystem() {
	var curX, curY, i, shortestDistance, tmpDistance;
	

	curX = portArray[departurePort].x + ((portArray[selectedPort].x - portArray[departurePort].x) * (distanceTravelled / distanceToTravel))
	
	curY = portArray[departurePort].y + ((portArray[selectedPort].y - portArray[departurePort].y) * (distanceTravelled / distanceToTravel))
	
	closestPort = -1;
	shortestDistance = 2100000000;
	
	for (i=0; i<portArray.length; i++) {
		tmpDistance = GetRange(curX, portArray[i].x, curY, portArray[i].y);
		if (tmpDistance < shortestDistance) {
			closestPort = i;
			shortestDistance = tmpDistance;
		}
	}
	
	if (portArray[closestPort].nation != -1) {
		// all good, continue
	} else if (shortestDistance > 100) {
		closestPort = -1;
	}
}

function completeJump()
{
	calculateAdjustedValues(playerShip);
	drawShipPart(playerShip);
	SelectGoods(-1);
	createRandomMissions();
	shipyardSeed = Math.floor(10000000 * Math.random());
	populateShipyard(shipyardSeed);
	drawPortPlanet(currentPort, 'planetCanvas');
	document.getElementById('closeTransitScreen').style.display='block';
//	testMissionDeadlines();
	dbSaveState();

}

function testForEncounter()
{
	var encounterNumber;
	var i;

//	if (Math.random() < 0.015)
	if (Math.random() < 0.05)
	{
		// random encounter!
		calculateNearestSystem();
		inEncounter = 1;
		
		// find the nearest port and use its airspace.

		if (closestPort == -1) {
			if (rnd() < 0.60) {
				encounterType = 'pirate';
				startEncounter();
			} else {
				encounterType = 'civilian';
				startEncounter();
			}
		} else {
		
			encounterNumber = (rnd() + portArray[closestPort].routeSecurity);
			if (encounterNumber < 1.30)
			{
	//			alert("Pirate encounter! " + portArray[selectedPort].routeSecurity + ", " + encounterType);
				encounterType = 'pirate';
				startEncounter();
			} else if (encounterNumber > 2) {
	//			alert("Security force encounter! " + portArray[selectedPort].routeSecurity + ", " + encounterType);
				encounterType = 'police';
				startEncounter();
			} else {
	//			alert("Trader encounter! " + portArray[selectedPort].routeSecurity + ", " + encounterType);
				encounterType = 'civilian';
				startEncounter();
			}
		}
	}
	
}

function AddToCurrentStardate(amtToAdd, isInitialRun)
{
	var i;
	currentStardate += amtToAdd;
	
	// tick down the days on missions
	
	if (isInitialRun == null)
	{
		for (i=0; i<7; i++) {
			currentMissions[i].timeLimit = currentMissions[i].timeLimit - amtToAdd;
		}
		
		if (currentStardate % 100 == 0)
		{
			SetPrices();
		}
	}
	
	if ((currentStardate % 1000) < 10)
	{
		SetText("displayStardate", Math.floor(currentStardate/1000) + '.00' + currentStardate % 1000);
	} else if ((currentStardate % 1000) < 100)
	{
		SetText("displayStardate", Math.floor(currentStardate/1000) + '.0' + currentStardate % 1000);
	} else {
		SetText("displayStardate", Math.floor(currentStardate/1000) + '.' + currentStardate % 1000);
	}
//	SetText("tradeStardate", Math.floor(currentStardate/1000) + '.' + currentStardate % 100);
}

function IncrementProgress(curDistanceCovered, totalDistanceToCover, progressBarId)
{
	if (totalDistanceToCover == 0)
	{
		document.getElementById(progressBarId).style.setProperty('width', '400px');
	} else {
		// BUG: ships that are too slow (<0 speed) will throw an exception here.
		document.getElementById(progressBarId).style.setProperty('width', (((curDistanceCovered/totalDistanceToCover) * 400))+'px');
	}
//	alert(curDistanceCovered + ", " + totalDistanceToCover);
}

function calculatePortTerritory()
{
	var i, j, tmpRangeToNeighbor;
	for (i=0;i<portArray.length;i++)
	{
		for (j=0;j<portArray.length;j++)
		{
			if (j != i)
			{
				tmpRangeToNeighbor = GetRangeBetweenPorts(i, j);
				if (tmpRangeToNeighbor < portArray[i].influence) {
					// new nearest neighbor
					portArray[i].nearestNeighbor = j;
					portArray[i].influence = tmpRangeToNeighbor;
//					portArray[j].nearestNeighbor = i;
//					portArray[j].influence = tmpRangeToNeighbor;
				}

				if (portArray[i].nation != portArray[j].nation)
				{
					if (tmpRangeToNeighbor < portArray[i].hostileInfluence) 
					{
						// new nearest "hostile" neighbor
						portArray[i].nearestHostile = j;
						portArray[i].hostileInfluence = tmpRangeToNeighbor;
					}
				}
			}
		}
//		portArray[i].portFamiliarity = 500000; // debug to test mapping
	}
}

jsFilesLoaded = jsFilesLoaded + 1;

