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


function purchaseSelectedShipPart() {
	if (document.getElementById("shipyardButton").style.display == "inline")
	{
		showConfirmationWindow("Can't take it with ye", "Just so yer aware, the shipyard'll be keepin' anything of value still te be found in yer ship, such as cargo, crewmwen and the like.<br />&nbsp;<br />Furthermore, any attachments ye may have will be sold with the ship.", "Buy the ship already!", "Maaaaybe not just yet", "purchaseShip", selectedShipyardPart);
	} else {
		showConfirmationWindow("Caveat Emptorrr", "In the interests of avoiding any unpleasant surprises, we feel it only fair te warn ye' that any cargo or crew still inside the part of the ship yer upgradin' will become the shipyard's sole property under the \"finder's keepers\" rule.", "Fair enough.  Buy it!", "Lemme double-check it...", "purchaseShipPart", selectedShipyardPart);
	}
}

function UpdateShipPartSelectbox(idToUpdate)
{

	var tmpOption;
	var i;
	while (document.getElementById(idToUpdate).childNodes[0]) {
    document.getElementById(idToUpdate).removeChild(document.getElementById(idToUpdate).childNodes[0]);
	}
		tmpOption = document.createElement("option");
		tmpOption.setAttribute("value", -1);
		if (idToUpdate == "crewManagementShipComponents")
		{
			tmpOption.appendChild(document.createTextNode("Ship's Core"));
		} else {
			tmpOption.appendChild(document.createTextNode("Core + Components"));
		}
		document.getElementById(idToUpdate).appendChild(tmpOption);
	for (i=0; i<playerShip.attachments.length; i++)
	{
		tmpOption = document.createElement("option");
		tmpOption.setAttribute("value", i);
		if (playerShip.attachments[i] == null)
		{
			tmpOption.appendChild(document.createTextNode("  (empty)"));
		} else {
			tmpOption.appendChild(document.createTextNode(playerShip.attachments[i].partName));
		}
		document.getElementById(idToUpdate).appendChild(tmpOption);
	}
}

function InitializeManageShipScreen()
{
	var tmpOption;
	var i;
	
//	UpdateShipPartSelectbox("attachedComponents");
	/*
	while (document.getElementById('attachedComponents').childNodes[0]) {
    document.getElementById('attachedComponents').removeChild(document.getElementById('attachedComponents').childNodes[0]);
	}
		tmpOption = document.createElement("option");
		tmpOption.setAttribute("value", -1);
		tmpOption.appendChild(document.createTextNode("Ship + Components"));
		document.getElementById('attachedComponents').appendChild(tmpOption);
	for (i=0; i<playerShip.attachments.length; i++)
	{
		tmpOption = document.createElement("option");
		tmpOption.setAttribute("value", i);
		if (playerShip.attachments[i] == null)
		{
			tmpOption.appendChild(document.createTextNode("(empty slot)"));
		} else {
			tmpOption.appendChild(document.createTextNode(playerShip.attachments[i].partName));
		}
		document.getElementById('attachedComponents').appendChild(tmpOption);
	}
	*/
	document.getElementById('managementName').innerHTML = playerShipName;

	drawShipPart(playerShip);
//	alert("shipManagementImage div: " + document.getElementById('shipManagementImage'));
	UpdatePartDetails(playerShip, 'shipManagementImage', 'shipManagementHealthImage');
	setShipPartHighlight(playerShip, null);
	
	
							   
}
function GetRepairCost(rootPart)
{
	var i;
	var maxArmor;
	var curArmor;
	var baseCost;
	var returnValue=0;
	
	if (rootPart == null)
	{
		return 0;
	}

	if (rootPart.attachments != null)
	{
		for (i=0; i< rootPart.attachments.length; i++)
		{
			returnValue += GetRepairCost(rootPart.attachments[i]);
		}
	}
	
	maxArmor = GetAdjustedStatistic(rootPart, "maxArmor", "none", true);
	curArmor = GetAdjustedStatistic(rootPart, "armor", "none", true);
	baseCost = GetAdjustedStatistic(rootPart, "baseCost", "none", true);
	
	if (baseCost < 3000) {
		baseCost = 3000;
	}
	
	returnValue += Math.floor(((maxArmor - curArmor) / maxArmor * (baseCost * 0.1)));
//	alert("Repair cost: " + returnValue + " maxArmor = " + maxArmor + ", armor = " + curArmor + ", baseCost = " + baseCost);
	
	if (isNaN(returnValue))
	{
		return 0;
	} else {
		return returnValue;
	}
	
}

function createRandomBlankHull(sourceSeed) {

	var tmpSeed = null;
	if (sourceSeed != null)
	{
		tmpSeed = randomNumberSeed;
		randomNumberSeed = sourceSeed;
	}
	
	var shipSize = 1 + randomInteger(15.9999);
	if (isCrippledFreeVersion == true) {
		shipSize = 1 + randomInteger(5.9999);
	}
	var tmpShip;
	
	tmpShip = new ShipPart(shipSize + "-slot hull", "", 1 + randomInteger((20-shipSize)), 1 + randomInteger(20 * shipSize), 1 + randomInteger(shipSize), 50 + randomInteger(15*shipSize), 25 + (randomInteger(15 * shipSize)), 50 + randomInteger(20 * shipSize), 0, 0, shipSize, ((5000 * (1.4 ^ shipSize)) + randomInteger(5000 * (1.4 ^ shipSize)) * ((portArray[currentPort].routeSecurity + 0.5))), encounterShip, "attack");
	
	tmpShip.baseCost = calculateShipPartValue(tmpShip);
	tmpShip.baseCost = tmpShip.baseCost * (portArray[currentPort].routeSecurity + 0.5);
	tmpShip.adjBaseCost = tmpShip.adjBaseCost * (portArray[currentPort].routeSecurity + 0.5);
	
	if (tmpSeed != null) {
		randomNumberSeed = tmpSeed;
	}
//	alert("blank hull created: " + shipSize + "-slot hull");
	return tmpShip;
	
}

function calculateShipPartValue(targetPart) {
	var valueFactor;
	var stupidMathDoesntWork;
	
	valueFactor = 1000;
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.maxSpeed * 100), 1.4), 0);
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.maxFuel * 30), 1.4), 0);
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.maxCargo * 10), 1.4), 0);
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.maxCrew * 500), 1.4), 0);
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.maxArmor * 30), 1.2), 0);
	valueFactor = valueFactor + Coalesce(Math.pow((targetPart.firepower * 40), 1.4), 0);
	
	stupidMathDoesntWork = targetPart.attachments.length;
	stupidMathDoesntWork = stupidMathDoesntWork + 1;
	stupidMathDoesntWork = Math.pow(stupidMathDoesntWork, 1.4);
	

	valueFactor = valueFactor * stupidMathDoesntWork;
//	alert("calculated value: " + valueFactor + ", attachments: " + stupidMathDoesntWork);
	return Math.floor(valueFactor/10);
}

function createRandomEncounterShip(crewmanBonus) {

// set up your ship's frame

//	alert("start setup");
	var shipSize = 1 + randomInteger(15.9999);
	var i;
	var gunWeight=0, speedWeight=0, cargoWeight=0;
	
	if (crewmanBonus == null) {
		crewmanBonus = 0;
	}
	
	encounterShip = new ShipPart(SelectRandomItemFromArray(shipNames), "", 1 + randomInteger((20-shipSize)), 1 + randomInteger(20 * shipSize), 1 + randomInteger(shipSize), 50 + randomInteger(15*shipSize), 25 + (randomInteger(15 * shipSize)), 50 + randomInteger(20 * shipSize), 0, 0, shipSize, 2500 + randomInteger(5000 * shipSize), encounterShip, "attack");

	encounterShip.baseCost = calculateShipPartValue(encounterShip);
	encounterShip.adjBaseCost = encounterShip.baseCost;

//	alert("ship created: size = " + shipSize);
	for(i=0; i<shipSize-1;i++)
	{
		encounterShip.attachments[i] = createRandomShipAttachment();
		
		if (rnd() > 0.85) {
			// null this ship attachment out.
			encounterShip.attachments[i] = createNullAttachment();
		}
		
		encounterShip.attachments[i].rootPart = encounterShip;
		if (encounterShip.attachments[i].firepower > 0)
		{
			gunWeight++;
		} else if (encounterShip.attachments[i].maxCargo > 0) {
			cargoWeight++;
		} else if (encounterShip.attachments[i].maxSpeed > 0) {
			speedWeight++;
		}
	}
	
	if (gunWeight > cargoWeight && gunWeight > speedWeight) {
		encounterShip.partType = "gun";
	} else if (speedWeight > gunWeight && speedWeight > cargoWeight) {
		encounterShip.partType = "speed";
	} else {
		encounterShip.partType = "cargo";
	}
	
//	alert("attachments added; crew size = " + encounterShip.crew.length);
	for(i=0;i<encounterShip.crew.length;i++)
	{
		encounterShip.crew[i] = CreateRandomCrewman(crewmanBonus);
	}
	calculateAdjustedValues(encounterShip)
//	alert("entering draw for " + encounterShip);
	drawShipPart(encounterShip);
//	alert("exiting draw for " + encounterShip);
	
//document.getElementById("shipManagementImage").appendChild(playerShip.drawDiv);
//	alert("Done!");
}

function createNullAttachment() {
	return new ShipPart("(empty)", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, null, "attack");
}


function populateShipyard(sourceSeed) {
	
	var i;
	var tmpSeed = null;
	if (sourceSeed != null)
	{
		tmpSeed = randomNumberSeed;
		randomNumberSeed = sourceSeed;
	}
	
	
	
	for (i = 0; i < availableShipParts.length; i++) {
		availableShipParts[i] = createRandomShipAttachment();
		drawShipPart(availableShipParts[i]);
	}
	
	for (i = 0; i < availableShips.length; i++) {
		availableShips[i] = createRandomBlankHull();
		drawShipPart(availableShips[i]);
	}

	drawAllShipyardParts();

	if (tmpSeed != null) {
		randomNumberSeed = tmpSeed;
	}
}

function getPurchasePrice(sellingPart, buyingPart) {
	
	var priceDifference;
	
	priceDifference = Math.floor(GetPartStatistic(sellingPart, "baseCost", "children") * (1 - (getStatisticModifier(buyingPart, "haggling") / 600)));
	priceDifference = priceDifference - Math.floor(GetPartStatistic(buyingPart, "baseCost", "children") * (1 - (getStatisticModifier(buyingPart, "haggling") / 600)));
	priceDifference = priceDifference + GetRepairCost(sellingPart);
	return priceDifference;
}

function drawShipyardButton(sourceShipPart, targetDiv, shipPartIndex) 
{
	var tmpDiv, tmpDiv2;
	var statsString;
	
	var tmpTargetPart;
	var priceDifference;
	
	tmpTargetPart = getShipPart(playerShip, selectedShipPartID);
	if (selectedShipPartID == -1) {
		tmpTargetPart = playerShip;
	}
	
//	statsString = "AMR: " + sourceShipPart.maxArmor + " " + getBaseStatisticDifference(sourceShipPart, tmpTargetPart, "maxArmor") + "; ";
	statsString = sourceShipPart.maxArmor + "/";
	statsString = statsString + sourceShipPart.firepower + "; ";
	statsString = statsString + sourceShipPart.maxSpeed + "/";
	statsString = statsString + sourceShipPart.maxFuel + "; ";
	statsString = statsString + sourceShipPart.maxCargo + "; ";
	statsString = statsString + sourceShipPart.maxCrew;
	
	// need to do some custom price checking here: your current ship's haggle bonus should apply to the new ship, as well.
	priceDifference = getPurchasePrice(sourceShipPart, tmpTargetPart);
	
	tmpDiv = document.createElement("div");
	document.getElementById(targetDiv).appendChild(tmpDiv);

	if (sourceShipPart.attachments.length == 0) {
		tmpDiv.setAttribute("class", "shipPartButton");
	} else {
		tmpDiv.setAttribute("class", "shipHullButton");
	}

	tmpDiv2 = document.createElement("div");
	tmpDiv2.style.setProperty("position", "relative");
	if (sourceShipPart.attachments.length == 0) {
		tmpDiv2.style.setProperty("left", "120px");
		tmpDiv2.style.setProperty("top", "4px");
		tmpDiv2.style.setProperty("width", "480px");
	} else {
		tmpDiv2.style.setProperty("left", "10px");
		tmpDiv2.style.setProperty("top", "4px");
		tmpDiv2.style.setProperty("width", "590px");
	}
	tmpDiv2.appendChild(document.createTextNode(sourceShipPart.partName + ": " + priceDifference + "cr "));
	tmpDiv.appendChild(tmpDiv2);
	
	tmpDiv2 = document.createElement("div");
	tmpDiv2.style.setProperty("position", "relative");
	if (sourceShipPart.attachments.length == 0) {
		tmpDiv2.style.setProperty("left", "120px");
		tmpDiv2.style.setProperty("top", "2px");
		tmpDiv2.style.setProperty("width", "480px");
		tmpDiv2.style.setProperty("text-align", "left");
	} else {
		tmpDiv2.style.setProperty("left", "0px");
		tmpDiv2.style.setProperty("top", "-18px");
		tmpDiv2.style.setProperty("width", "590px");
		tmpDiv2.style.setProperty("text-align", "right");
	}
	tmpDiv2.style.setProperty("font-weight", "normal");
	tmpDiv2.appendChild(document.createTextNode(statsString));
	tmpDiv.appendChild(tmpDiv2);
	
	tmpDiv.style.setProperty("background-color", "#FFFFFF");
	if (selectedShipyardPart == shipPartIndex) {
		tmpDiv.style.setProperty("background-color", "#FFFFCC");
//		alert("price diff: " + priceDifference + "; playerCash: " + playerCash);
		if (priceDifference <= playerCash) {
			document.getElementById('purchaseShipPartButton').style.display = 'inline';
		}
	}
	
	tmpDiv2 = document.createElement("div");
	tmpDiv.appendChild(tmpDiv2);
	tmpDiv2.id = "shipYardButton_" + sourceShipPart.ID;
	tmpDiv2.style.setProperty("position", "relative");
	tmpDiv2.style.setProperty("left", "0px");
	if (sourceShipPart.attachments.length == 0) {
		tmpDiv2.style.setProperty("top", "-42px");
		tmpDiv2.style.setProperty("width", "100px");
	} else {
		tmpDiv2.style.setProperty("top", "-24px");
		tmpDiv2.style.setProperty("width", "600px");
	}
	drawShipPartToDiv(sourceShipPart, tmpDiv2.id, null);

	tmpDiv.setAttribute("onClick", "selectShipyardPart(" + shipPartIndex +");");
	

}

function selectShipyardPart(partIndex) {
	var i;
	selectedShipyardPart = partIndex;	
	drawAllShipyardParts();
}

function drawAllShipyardParts() {
	var i;
	ClearAllChildren("shipPartsAvailable");
	ClearAllChildren("shipHullsAvailable");
	document.getElementById('purchaseShipPartButton').style.display = 'none';
	if (document.getElementById("shipyardButton").style.display == "none") {
		for (i = 0; i < availableShipParts.length; i++) {
			drawShipyardButton(availableShipParts[i], "shipPartsAvailable", i);
		}
	} else {
		for (i = 0; i < availableShips.length; i++) {
			drawShipyardButton(availableShips[i], "shipHullsAvailable", i);
		}
	}
}



function createRandomShipAttachment()
{
	//ShipPart(shipName, maxSpeed, maxCargo, maxCrew, maxFuel, firepower, maxArmor, x, y, maxAttachments, baseCost)
	var returnPart, partName, partNumber;
	var partSpeed=0, partCargo=0, partCrew=0, partFuel=0, partFirepower=0, partArmor=0, partCost=0, partDisposition;
	var tmpShip;
	var randomRoll;

	partDisposition = "repair";
	
	randomRoll = randomInteger(70);
	
	if (randomRoll <= 9)
	{
		// gun part;
		partName = '"' + SelectRandomItemFromArray(shipPartGunNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 10 + randomInteger(50);
		partFirepower = 15 + randomInteger(100);
		partCrew = randomInteger(2);
		partDisposition = "attack";
	}
	else if (randomRoll <= 10 && isCrippledFreeVersion != true)
	{
		// powerful gun part;
		partName = '"' + SelectRandomItemFromArray(shipPartGunNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 30 + randomInteger(80);
		partFirepower = 35 + randomInteger(200);
		partCrew = 1 + randomInteger(3);
		partDisposition = "attack";
	}
	else if (randomRoll <= 19)
	{
		// armor part;
		partName = '"' + SelectRandomItemFromArray(shipPartArmorNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 50 + randomInteger(200);
		partSpeed = 0 - randomInteger(3);
		partFirepower = 5 + randomInteger(30);
		partCrew = randomInteger(2);
		partDisposition = "attack";
	}
	else if (randomRoll <= 20 && isCrippledFreeVersion != true)
	{
		// powerful armor part;
		partName = '"' + SelectRandomItemFromArray(shipPartArmorNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 150 + randomInteger(400);
		partSpeed = 0 - randomInteger(10);
		partFirepower = 15 + randomInteger(50);
		partCrew = 1 + randomInteger(3);
		partDisposition = "attack";
	}
	else if (randomRoll <= 29)
	{
		// cargo part;
		partName = '"' + SelectRandomItemFromArray(shipPartCargoNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 10 + randomInteger(50);
		partSpeed = 0 - randomInteger(3);
		partCargo = 25 + randomInteger(50);
		partCrew = randomInteger(2);
	}
	else if (randomRoll <= 30 && isCrippledFreeVersion != true)
	{
		// large cargo part;
		partName = '"' + SelectRandomItemFromArray(shipPartCargoNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 30 + randomInteger(80);
		partSpeed = 0 - randomInteger(10);
		partCargo = 50 + randomInteger(150);
		partCrew = 1 + randomInteger(3);
	}
	else if (randomRoll <= 39)
	{
		// engine part;
		partName = '"' + SelectRandomItemFromArray(shipPartEngineNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 10 + randomInteger(50);
		partFuel = 2 + randomInteger(5);
		partSpeed = 5 + randomInteger(5);
		partCrew = randomInteger(2);
		partDisposition = "evade";
	}
	else if (randomRoll <= 40 && isCrippledFreeVersion != true)
	{
		// powerful engine part;
		partName = '"' + SelectRandomItemFromArray(shipPartEngineNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 20 + randomInteger(100);
		partFuel = 5 + randomInteger(10);
		partSpeed = 10 + randomInteger(10);
		partCrew = 1 + randomInteger(3);
		partDisposition = "evade";
	}
	else if (randomRoll <= 49)
	{
		// fuel part;
		partName = '"' + SelectRandomItemFromArray(shipPartFuelNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 10 + randomInteger(50);
		partSpeed = 0 - randomInteger(2);
		partFuel = 10 + randomInteger(15);
		partCrew = randomInteger(2);
	}
	else if (randomRoll <= 50 && isCrippledFreeVersion != true)
	{
		// large fuel part;
		partName = '"' + SelectRandomItemFromArray(shipPartFuelNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 25 + randomInteger(75);
		partSpeed = 0 - randomInteger(5);
		partFuel = 15 + randomInteger(40);
		partCrew = 1 + randomInteger(3);
	}
	else if (randomRoll <= 59)
	{
		// crew part;
		partName = '"' + SelectRandomItemFromArray(shipPartCrewNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 10 + randomInteger(50);
		partSpeed = 0 - randomInteger(2);
		partCrew = 2 + randomInteger(2);
	}
	else if (randomRoll <= 60 && isCrippledFreeVersion != true)
	{
		// large crew part;
		partName = '"' + SelectRandomItemFromArray(shipPartCrewNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partArmor = 25 + randomInteger(75);
		partSpeed = 0 - randomInteger(5);
		partCrew = 4 + randomInteger(4);
	}
	else if (randomRoll <= 69)
	{
		// combo part;
		partName = '"' + SelectRandomItemFromArray(shipPartComboNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partFirepower = 15 + randomInteger(50);
		partArmor = 30 + randomInteger(50);
		partCargo = 15 + randomInteger(15);
		partSpeed = -1 + randomInteger(6);
		partFuel = 5 + randomInteger(20);
		partCrew = 1 + randomInteger(2);
		partDisposition = "attack";
	}
	else if (randomRoll <= 70 && isCrippledFreeVersion != true)
	{
		// freakin' awesome combo part;
		partName = '"' + SelectRandomItemFromArray(shipPartComboNames) + '"';
		partNumber = SelectRandomItemFromArray(shipPartPrefix) + randomInteger(999);
		partFirepower = 30 + randomInteger(150);
		partArmor = 75 + randomInteger(200);
		partCargo = 50 + randomInteger(50);
		partSpeed = 3 + randomInteger(9);
		partFuel = 15 + randomInteger(30);
		partCrew = 6 + randomInteger(2);
		partDisposition = "attack";
	}
	
	partCost = 0;
	
	tmpShip = new ShipPart(partName, partNumber, partSpeed, partCargo, partCrew, partFuel, partFirepower, partArmor, 0, 0, 0, partCost, null, partDisposition);

	tmpShip.baseCost = calculateShipPartValue(tmpShip);
	tmpShip.baseCost = tmpShip.baseCost * (portArray[currentPort].routeSecurity + 0.5);
	tmpShip.adjBaseCost = tmpShip.baseCost;

	return tmpShip;

}

function OrderRepairs()
{
	var partToRepair;
	
//	var curValue = document.getElementById('attachedComponents').options[document.getElementById('attachedComponents').selectedIndex].value;
	var partToCheck;
	
//	if (curValue == -1)
//	{
//		partToRepair = playerShip;
//	} else {
//		partToRepair = playerShip.attachments[curValue];	
//	}

//	alert(selectedShipPartID);
	if (selectedShipPartID == -1)
	{
		partToRepair = playerShip;
	} else {
		partToRepair = getShipPart(playerShip, selectedShipPartID);
	}
//	alert(partToRepair);

	if (GetRepairCost(partToRepair) > playerCash) 
	{
		showAlertWindow("Check yer wallet!", "It'd cost ye " +  GetRepairCost(partToRepair) + " credits to make the repairs ye requested.  Sadly, ye can't afford these repairs, and the mechanics simply won't accept happy thoughts and eternal gratitude as legal tender.", "Nice try, though.");
	} else if (GetRepairCost(partToRepair) == 0) {
		showAlertWindow("If it Ain't Broke", "While we applaud yer enthusiasm for running a tight ship and generally favor the indiscriminate taking of money from eager souls, we're beginnin' te worry that ye may be going just a wee bit overboard.  There's nothing wrong with this part of the ship, so ye can't exactly have it repaired now, can ye?", "I'll show ye overboard, punk.");
	} else {
		showConfirmationWindow("Order Repairs?", "It'll cost ye " + GetRepairCost(partToRepair) + " credits te make these repairs.  OK?", "Arr, go ahead.", GetRepairCost(partToRepair) + "?  I'll do it meself!", "shipRepairs", selectedShipPartID);

	}
}

function restorePartArmor(curPart)
{
	var i;
	if (curPart == null)
	{
		return;
	}
	for (i = 0; i < curPart.attachments.length; i++)
	{
		restorePartArmor(curPart.attachments[i]);
	}
	curPart.armor = curPart.maxArmor;
	
}

function inflictDamage(shipPart, damageDone)
{
	var armorModifier;
	
	armorModifier = GetAdjustedStatistic(shipPart, "maxArmor", "none")  / GetAdjustedStatistic(shipPart, "maxArmor", "none");
	
	armorModifier = armorModifier / 10;
	
//	alert("doing damage: damageDone = " + damageDone + ", modifier = " + armorModifier);
	damageDone = damageDone * armorModifier;
		
	shipPart.explosionTime = Math.ceil(damageDone*5);
	if (shipPart.explosionTime > 10)
	{
		shipPart.explosionTime = 10;
	}
	
//	alert(damageDone);
	
	shipPart.armor = shipPart.armor - damageDone;
	if (shipPart.armor <= 0)
	{
		shipPart.armor = 0;
		if (shipPart.rootPart != null) {
//			alert("boom!" + shipPart.rootPart.partName);
			calculateAdjustedValues(shipPart.rootPart);
		}
	}
}

function repairShipInTransit(targetPart, repairHoldover) {
	var i, maxRepairs;
	
	maxRepairs = (targetPart.maxArmor * 65) / 100;
	maxRepairs = maxRepairs +  ((targetPart.maxArmor * getStatisticModifier(targetPart, "engineering"))/100);
	
	if (maxRepairs > targetPart.maxArmor) {
		maxRepairs = targetPart.maxArmor;
	}
	
	repairsMade = randomInteger(getStatisticModifier(targetPart, "engineering")/5) + repairHoldover;

//	if (targetPart == playerShip) {
//		alert("repairs made to " + targetPart.partName + ": " + repairsMade + " engineering = " + getStatisticModifier(targetPart, "engineering"));
//	}
	
	if (targetPart.armor > 0) {
		if (targetPart.armor < maxRepairs) {
			targetPart.armor = targetPart.armor + repairsMade;
//			alert("repairs made to " + targetPart.partName + ": " + repairsMade + " engineering = " + getStatisticModifier(targetPart, "engineering"));
			if (targetPart.armor > maxRepairs) {
				repairsMade = targetPart.armor - maxRepairs;
				targetPart.armor = maxRepairs;
			} else {
				repairsMade = 0;
			}
		}
		for (i=0; i<targetPart.attachments.length; i++) {
			repairsMade = repairShipInTransit(targetPart.attachments[i], repairsMade);
		}
	} else {
//		alert("No repairs here: " + targetPart.partName);
		repairsMade = repairHoldover;
	}
	return repairsMade;
	
	
	
}

function UpdatePartDetails(partToCheck, targetDiv, targetHealthDiv)
{
//	var curValue = document.getElementById('attachedComponents').options[document.getElementById('attachedComponents').selectedIndex].value;
//	var partToCheck;
	
//	if (curValue == -1)
//	{
//		partToCheck = playerShip;
//	} else {
//		partToCheck = playerShip.attachments[curValue];	
//	}
	
	if (inEncounter == 0) {
		
		document.getElementById('currentItemCargo').innerHTML = GetAdjustedStatistic(partToCheck, "curCargo");
		document.getElementById('currentItemMaxCargo').innerHTML = GetAdjustedStatistic(partToCheck, "maxCargo");
		
		document.getElementById('currentItemSpeed').innerHTML = GetAdjustedStatistic(partToCheck, "maxSpeed");
	
		document.getElementById('currentItemFuel').innerHTML = GetAdjustedStatistic(partToCheck, "fuel");
		document.getElementById('currentItemMaxFuel').innerHTML = GetAdjustedStatistic(partToCheck, "maxFuel");
		
		document.getElementById('currentItemCrew').innerHTML = GetAdjustedStatistic(partToCheck, "curCrew");
		document.getElementById('currentItemMaxCrew').innerHTML = GetAdjustedStatistic(partToCheck, "maxCrew");
		
		document.getElementById('currentItemFirepower').innerHTML = GetAdjustedStatistic(partToCheck, "firepower");
	
		document.getElementById('currentItemArmor').innerHTML = GetAdjustedStatistic(partToCheck, "armor");
		document.getElementById('currentItemMaxArmor').innerHTML = GetAdjustedStatistic(partToCheck, "maxArmor");
		
		if (partToCheck == playerShip) {	
			document.getElementById('currentItemMaxFuel').innerHTML = playerShip.adjMaxFuel;
			document.getElementById('currentItemFuel').innerHTML = playerShip.fuel;
			document.getElementById('shipManagementTitle').innerHTML = "Ship + all components";
			document.getElementById('shipManagementPartNumber').innerHTML = "";
		} else {
			document.getElementById('shipManagementTitle').innerHTML = GetAdjustedStatistic(partToCheck, "partName");
			document.getElementById('shipManagementPartNumber').innerHTML = GetAdjustedStatistic(partToCheck, "partNumber");
		}
	} else {
		// we're in an encounter.  Do stuff!
		if (partToCheck.firepower == 0)
		{
			document.getElementById("combatAttackButton").style.display = 'none';
		} else {
			document.getElementById("combatAttackButton").style.display = 'inline';		
		}
		if (partToCheck.maxSpeed <= 0)
		{
			document.getElementById("combatEvadeButton").style.display = 'none';
		} else {
			document.getElementById("combatEvadeButton").style.display = 'inline';		
		}
		if (getStatisticModifier(partToCheck, "engineering") <= 0)
		{
			document.getElementById("combatRepairButton").style.display = 'none';
		} else {
			document.getElementById("combatRepairButton").style.display = 'inline';		
		}
		
		if (partToCheck.armor <= 0) {
			document.getElementById("combatAttackButton").style.display = 'none';
			document.getElementById("combatEvadeButton").style.display = 'none';
			document.getElementById("combatRepairButton").style.display = 'none';
		}
		switch (partToCheck.posture)
		{
			case "attack":
			{
				highlightAttackOption("combatAttackButton");
				var targetPart;
//				alert("current target ID: " + partToCheck.target);
				targetPart = getShipPart(encounterShip, partToCheck.target);
				if (targetPart == null) {
					partToCheck.target = encounterShip.ID;
//					alert("Unknown target: setting to main encounter ship, ID = " + partToCheck.target);
				}
//				alert("attempting highlight: ID=" + partToCheck.target);
				setShipPartHighlight(encounterShip, getShipPart(encounterShip, partToCheck.target), true);
				break;
			}
			case "evade":
			{
				setShipPartHighlight(encounterShip, null);
				highlightAttackOption("combatEvadeButton");
				break;
			}
			case "repair":
			{
				setShipPartHighlight(encounterShip, null);
				highlightAttackOption("combatRepairButton");
				break;
			}
		}
	}

//	alert("UpdatePartDetails: " + targetDiv);
	drawShipPartToDiv(playerShip, targetDiv, targetHealthDiv);
	drawShipPartHealthToDiv(playerShip, targetHealthDiv);

	document.getElementById(targetDiv).style.left = (270 - (playerShip.bgImages.length * 30)) + "px";
	document.getElementById(targetHealthDiv).style.left = (270 - (playerShip.bgImages.length * 30)) + "px";
}

function setShipPartHighlight(workingShipPart, shipPartToHighlight, isTarget) {

	var i;
	if (workingShipPart == shipPartToHighlight) 
	{
//		alert("highlight! ID = " + workingShipPart.ID);
		if (isTarget == true)
		{
			workingShipPart.highlightDiv.setAttribute("class", "targetHighlight");
		} else {
			workingShipPart.highlightDiv.setAttribute("class", "shipHighlight");
		}
		workingShipPart.highlightDiv.style.display = 'inline';
	} else {
		workingShipPart.highlightDiv.style.display = 'none';
	}
	
	for (i = 0; i < workingShipPart.attachments.length; i++)
	{
		if (workingShipPart.attachments[i] != null) {
			setShipPartHighlight(workingShipPart.attachments[i], shipPartToHighlight, isTarget);
		}
	}
		
}

function getShipPart(rootShipPart, IDtoGet)
{

	var i;
	var returnValue;
	
	if (IDtoGet == null || IDtoGet == -1) {
		return rootShipPart;
	}
	
	returnValue = null;
	if (rootShipPart == null) {
		return null;
	}
	
	if (rootShipPart.ID == IDtoGet) {
		return rootShipPart;
	}
	
	for (i=0; i<rootShipPart.attachments.length; i++)
	{
		if (rootShipPart.attachments[i] != null) {
			returnValue = getShipPart(rootShipPart.attachments[i], IDtoGet);
			if (returnValue != null) {
				return returnValue;
			}
		}
	}
	
	return null;

}

function selectShipPart(shipPartID, targetDiv, targetHealthDiv)
{

	var selectedShipPart;
	
//	alert("selected ship part.  ID = " + shipPartID + ", targetDiv = " + targetDiv);
	
	
	selectedShipPart = getShipPart(playerShip, shipPartID);

	if (selectedShipPart != null)
	{
		selectedShipPartID = shipPartID;
		setShipPartHighlight(playerShip, selectedShipPart);
		UpdatePartDetails(selectedShipPart, targetDiv, targetHealthDiv);
		
		if (selectedShipPart == playerShip) {
			// display the "buy ship" button
			document.getElementById("shipyardButton").style.display = "inline";
			document.getElementById("shipyardPartButton").style.display = "none";
		} else {
			// display the "upgrade part" button
			document.getElementById("shipyardButton").style.display = "none";
			document.getElementById("shipyardPartButton").style.display = "inline";			
		}
		drawAllShipyardParts();
	} else {
		selectedShipPart = getShipPart(encounterShip, shipPartID);
		if (selectedShipPart == null)
		{
			return;
		}
//		alert("arr, this be the enemy ship!");
		// you're clicking the enemy ship; make your currently selected part target the part you just clicked.
		if (getShipPart(playerShip, selectedShipPartID) != null) {
			getShipPart(playerShip, selectedShipPartID).target = shipPartID;
//		alert("targeting ship part: " + getShipPart(playerShip, selectedShipPartID).target);
			setShipPartHighlight(encounterShip, selectedShipPart, true);
		}
	}

	if (selectedShipPart == null)
	{
		return;
	}

//	drawShipPartToDiv(playerShip, document.getElementById("shipManagementImage"));
	// we'll always assume player ship, as we're not allowing the selection of enemy ship parts.
}

function drawShipPart(partToDraw) {
	var i, j, k;
	var healthWidth;
	var healthContainerWidth;
	var healthLeft;
	var healthDiv;
	var healthContainerDiv;
	var highlightDiv;
	var tmpDiv;
	
	for (i = 0; i < partToDraw.attachments.length; i++)
	{
		if (partToDraw.attachments[i] != null) {
			partToDraw.attachments[i].x = ((Math.floor(i/2) * 60) + 30);
			if (i % 2 == 1) {
				partToDraw.attachments[i].shipPosition = "l";
				partToDraw.attachments[i].y = 40;
			} else {
				partToDraw.attachments[i].shipPosition = "u";
				partToDraw.attachments[i].y = -40;
			}
			drawShipPart(partToDraw.attachments[i]);
		}
	}
	
	ClearAllChildren(partToDraw.bgDiv);
	ClearAllChildren(partToDraw.midDiv);
	ClearAllChildren(partToDraw.fgDiv);
	
	healthDiv = document.createElement("div");
	healthDivContainer = document.createElement("div");
	partToDraw.width = ((partToDraw.bgImages.length + 1) * 60);
	partToDraw.height = 120;

	partToDraw.highlightDiv = document.createElement("div");	
	partToDraw.highlightDiv.setAttribute("class", "shipHighlight");
	partToDraw.highlightDiv.style.setProperty("display", "none");
	if (partToDraw.attachments.length == 0) {
		setDivDimensions(partToDraw.highlightDiv, partToDraw.x, partToDraw.y, partToDraw.width, partToDraw.height);
	} else if (partToDraw.attachments.length == 1) {
		setDivDimensions(partToDraw.highlightDiv, partToDraw.x, (partToDraw.y - 30), partToDraw.width, (partToDraw.height + 60));
	} else {
		setDivDimensions(partToDraw.highlightDiv, partToDraw.x, (partToDraw.y - 60), partToDraw.width, (partToDraw.height + 120));
	}
	setDivDimensions(partToDraw.fgDiv, partToDraw.x, partToDraw.y, partToDraw.width, partToDraw.height);
	setDivDimensions(partToDraw.midDiv, partToDraw.x, partToDraw.y, partToDraw.width, partToDraw.height);
	setDivDimensions(partToDraw.bgDiv, partToDraw.x, partToDraw.y, partToDraw.width, partToDraw.height);

	if (partToDraw.partName == "(empty)" && partToDraw != playerShip) {
		// if an empty playerShip attachment, draw this part faintly, and add the clickable FG!
		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:0px;left:0px;background-image:url(images/ships/placeholderStructure2" + partToDraw.shipPosition + ".png);filter:alpha(opacity=30);");
		partToDraw.fgDiv.appendChild(tmpDiv);
		return;
	}
	
	var tmpRandomSeed = randomNumberSeed;
	randomNumberSeed = partToDraw.imageSeed;
	for (i=0; i<partToDraw.bgImages.length; i++)
	{
/*
		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:0px;left:"+ (i*60)+"px;background-image:url(images/ships/a"+ (partToDraw.bgImages[i]) +".png);");
		partToDraw.bgDiv.appendChild(tmpDiv);

		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:0px;left:"+(i*60)+"px;background-image:url(images/ships/b"+ (partToDraw.midImages[i]) +".png);");
		partToDraw.midDiv.appendChild(tmpDiv);

		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:0px;left:"+(i*60)+"px;background-image:url(images/ships/c"+ (partToDraw.fgImages[i]) +".png);");
		partToDraw.fgDiv.appendChild(tmpDiv);
*/
		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
//		if (partToDraw == playerShip || partToDraw == encounterShip) {
//			tmpDiv.setAttribute("style", "position:absolute;top:0px;left:"+(i*60)+"px;background-image:url(images/ships/structure2.png);");
//		} else {
			tmpDiv.setAttribute("style", "position:absolute;top:0px;left:"+(i*60)+"px;background-image:url(images/ships/structure2" + partToDraw.shipPosition + ".png);");
//		}
		partToDraw.midDiv.appendChild(tmpDiv);

		for (j=0; j<partToDraw.maxArmor; j=j+5) {
			tmpDiv = document.createElement("img");
//			tmpDiv.setAttribute("class", "shipImage");
			tmpDiv.setAttribute("src", "images/ships/armor"+ (randomInteger(4)+1) +".png");
			tmpDiv.setAttribute("style", "position:absolute;top:" + (randomInteger(80)+10) + "px;left:"+((i*60) + (randomInteger(80)+10))+"px;");
			if (rnd() > 0.3) {
				partToDraw.bgDiv.appendChild(tmpDiv);
			} else {
				partToDraw.fgDiv.appendChild(tmpDiv);
			}
		}
		
		
	}

// jets
		if (partToDraw.maxSpeed > 0) {
			var tmpX, tmpY;
			tmpX = (randomInteger(i*60) + (randomInteger(0)+10));
			if (partToDraw.shipPosition == "") {
				tmpX = 0;
				tmpY = (50 + randomInteger(20) - partToDraw.maxSpeed);
			} else if (partToDraw.shipPosition == "u") {
				tmpY = (10 + randomInteger(20));
			} else {
				tmpY = (90 + randomInteger(20) - partToDraw.maxSpeed);
			}
			for (j=0; j<partToDraw.maxSpeed; j=j+8) {
				tmpDiv = document.createElement("img");
	//			tmpDiv.setAttribute("class", "shipImage");
				tmpDiv.setAttribute("src", "images/ships/jet.png");
				tmpDiv.setAttribute("style", "position:absolute;top:" + tmpY + "px;left:"+tmpX+"px;");
				if (partToDraw == playerShip || partToDraw == encounterShip) {
					partToDraw.fgDiv.appendChild(tmpDiv);
				} else {
					partToDraw.fgDiv.appendChild(tmpDiv);
				}
				tmpY = tmpY+9;
			}
		}

// portholes/crewmen
		for (j=0; j<partToDraw.maxCrew; j++) {
			tmpDiv = document.createElement("img");
//			tmpDiv.setAttribute("class", "shipImage");
			tmpDiv.setAttribute("src", "images/ships/porthole.png");
			tmpDiv.setAttribute("style", "position:absolute;top:" + (randomInteger(80)+10) + "px;left:"+(randomInteger(i*60) + (randomInteger(50)+10))+"px;");
			if (partToDraw == playerShip || partToDraw == encounterShip) {
				partToDraw.fgDiv.appendChild(tmpDiv);
			} else {
				partToDraw.fgDiv.appendChild(tmpDiv);
			}
		}

// guns!
		for (j=0; j<partToDraw.firepower; j=j+20) {
			tmpDiv = document.createElement("img");
//			tmpDiv.setAttribute("class", "shipImage");
			tmpDiv.setAttribute("src", "images/ships/gun.png");
			tmpDiv.setAttribute("style", "position:absolute;top:" + (randomInteger(80)+10) + "px;left:"+(randomInteger(i*60) + (randomInteger(50)+10))+"px;");
			if (partToDraw == playerShip || partToDraw == encounterShip) {
				partToDraw.fgDiv.appendChild(tmpDiv);
			} else {
				partToDraw.fgDiv.appendChild(tmpDiv);
			}
		}
	randomNumberSeed = tmpRandomSeed;
	
	drawShipPartHealth(partToDraw);
}

function setDivDimensions(sourceDiv, x, y, w, h)
{
	sourceDiv.style.setProperty("position", "absolute");
	if (x != null)
	{
		sourceDiv.style.setProperty("left", x + "px");
	}
	if (y != null)
	{
		sourceDiv.style.setProperty("top", y + "px");
	}
	sourceDiv.style.setProperty("width", w + "px");
	sourceDiv.style.setProperty("height", h + "px");
}


function drawShipPartHealth(partToDraw)
{
	
	var healthLeft;
	var i;
	var startX, startY;
	var healthContainerWidth;
	
	startX = partToDraw.x;
	startY = partToDraw.y;
	
	ClearAllChildren(partToDraw.healthDiv);
	ClearAllChildren(partToDraw.healthContainerDiv);

	i = Math.floor(partToDraw.attachments.length/2)+1;
//	i = 1;

	healthWidth = Math.floor(i * 50 * (GetAdjustedStatistic(partToDraw, "armor") / GetAdjustedStatistic(partToDraw, "maxArmor")));
	
	healthContainerWidth = Math.floor(i * 50);
	
	healthLeft = startX + ((((i + 1) * 60) - (healthContainerWidth)) / 2)

	partToDraw.healthDiv = document.createElement("div");
	partToDraw.healthContainerDiv = document.createElement("div");
	
	partToDraw.healthDiv.setAttribute("class", "healthBar");
	partToDraw.healthContainerDiv.setAttribute("class", "healthBarContainer");
	
	if (startY == 0) {
		partToDraw.healthDiv.setAttribute("style", "height:12px;top:"+(startY + 59)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		partToDraw.healthContainerDiv.setAttribute("style", "height:12px;top:"+(startY + 57)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	} else if (startY > 0) {
		partToDraw.healthDiv.setAttribute("style", "top:"+(startY + 122)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		partToDraw.healthContainerDiv.setAttribute("style", "top:"+(startY + 120)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	} else {
		partToDraw.healthDiv.setAttribute("style", "top:"+(startY - 4)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		partToDraw.healthContainerDiv.setAttribute("style", "top:"+(startY - 6)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	}
	
	if (healthWidth <= 0) {
		partToDraw.healthContainerDiv.style.setProperty("border", "2px solid #999999");
		partToDraw.healthContainerDiv.style.setProperty("background-color", "#333333");
//			partToDraw.healthDiv.style.setProperty("border", "2px solid #00FF00");
		partToDraw.healthDiv.style.setProperty("background-color", "#C0C0C0");
	} else if (healthWidth < (healthContainerWidth / 4))
	{
		partToDraw.healthContainerDiv.style.setProperty("border", "2px solid #FF0000");
		partToDraw.healthContainerDiv.style.setProperty("background-color", "#330000");
//			partToDraw.healthDiv.style.setProperty("border", "2px solid #FF0000");
		partToDraw.healthDiv.style.setProperty("background-color", "#FF0000");
	} else if (healthWidth < (healthContainerWidth / 2))
	{
		partToDraw.healthContainerDiv.style.setProperty("border", "2px solid #FFFF00");
		partToDraw.healthContainerDiv.style.setProperty("background-color", "#333300");
//			partToDraw.healthDiv.style.setProperty("border", "2px solid #FFFF00");
		partToDraw.healthDiv.style.setProperty("background-color", "#FFFF00");
	} else {
		partToDraw.healthContainerDiv.style.setProperty("border", "2px solid #00FF00");
		partToDraw.healthContainerDiv.style.setProperty("background-color", "#003300");
//			partToDraw.healthDiv.style.setProperty("border", "2px solid #00FF00");
		partToDraw.healthDiv.style.setProperty("background-color", "#00FF00");
	}

}

function drawShipPartHealthToDiv(partToDraw, targetDiv) 
{
	var i;
//	alert(targetDiv.children.length + ", " + partToDraw.drawDiv.children.length);
	ClearAllChildren(targetDiv);

	document.getElementById(targetDiv).appendChild(partToDraw.healthContainerDiv);
	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].healthContainerDiv);
		}
	}

	document.getElementById(targetDiv).appendChild(partToDraw.healthDiv);
	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].healthDiv);
		}
	}
}

function drawShipPartToDiv(partToDraw, targetDiv, targetHealthDiv) 
{
	var i;
//	alert(targetDiv.children.length + ", " + partToDraw.drawDiv.children.length);
	ClearAllChildren(targetDiv);

	if (document.getElementById(targetDiv) == null) {
		alert("Whoops!  targetDiv doesn't exist!" + targetDiv);
	}
	if (partToDraw.bgDiv == null) {
		alert ("D'oh!  No bgDiv!");
	}
	document.getElementById(targetDiv).appendChild(partToDraw.bgDiv);

	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].bgDiv);
		}
	}
	
	// if the shipPart is "(empty)" and the target div is NOT the ship management div, then don't draw.
	if (partToDraw.partName == "(empty)" && partToDraw != playerShip && targetDiv != "shipManagementImage") {
		return;
	}

	document.getElementById(targetDiv).appendChild(partToDraw.midDiv);
	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].midDiv);
		}
	}
	
	document.getElementById(targetDiv).appendChild(partToDraw.highlightDiv);
	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].highlightDiv);
		}
	}

	partToDraw.fgDiv.setAttribute("onClick", "selectShipPart(" + partToDraw.ID + ", '" + targetDiv + "', '" + targetHealthDiv + "');"); // "shipManagementImage"
	document.getElementById(targetDiv).appendChild(partToDraw.fgDiv);
	for (i=0; i < partToDraw.attachments.length; i++) 
	{
		if (partToDraw.attachments[i] != null)
		{
		partToDraw.attachments[i].fgDiv.setAttribute("onClick", "selectShipPart(" + partToDraw.attachments[i].ID + ", '" + targetDiv + "', '" + targetHealthDiv + "');"); // "shipManagementImage"
			document.getElementById(targetDiv).appendChild(partToDraw.attachments[i].fgDiv);
		}
	}

}


function drawShipPartOld(partToDraw, drawingDiv, startX, startY, isInitialRun)
{
	var i;
	var healthWidth;
	var healthContainerWidth;
	var healthLeft;
	var bgDiv;
	var midDiv;
	var fgDiv;
	var healthDiv;
	var healthContainerDiv;
	var highlightDiv;
	var tmpDiv;
	
	if (isInitialRun == true)
	{
		partToDraw.drawDiv = document.getElementById(drawingDiv);
		ClearAllChildren(drawingDiv);
	}

	if (partToDraw == null)
	{
		document.getElementById(drawingDiv).style.width = "0px";
		return;
	}
	
	for (i = 0; i < partToDraw.attachments.length; i++)
	{
//		alert("attachment draw! " + i + ", " + ((Math.floor(i/2) * 120) - 60));
		tmpDiv = document.createElement("div");
		if (i % 2 == 1)
		{
			drawShipPart(partToDraw.attachments[i], tmpDiv, ((Math.floor(i/2) * 60) + 30), 40, false);
		} else {
			drawShipPart(partToDraw.attachments[i], tmpDiv, ((Math.floor(i/2) * 60) + 30), -40, false);
		}
		partToDraw.attachments[i].drawDiv = tmpDiv;
		document.getElementById(drawingDiv).appendChild(tmpDiv);
	}
	
	bgDiv = document.createElement("div");
	midDiv = document.createElement("div");
	fgDiv = document.createElement("div");
	
	fgDiv.setAttribute("onClick", "selectShipPart(" + partToDraw.ID + ", " + drawingDiv + ");");
	
	healthDiv = document.createElement("div");
	healthDivContainer = document.createElement("div");
	partToDraw.x = startX;
	partToDraw.y = startY;
	partToDraw.width = ((partToDraw.bgImages.length + 1) * 60);
	partToDraw.height = 120;
	
	for (i=0; i<partToDraw.bgImages.length; i++)
	{
		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:"+partToDraw.y+"px;left:"+(partToDraw.x + (i*60))+"px;background-image:url(images/ships/a"+ (partToDraw.bgImages[i]) +".png);");
		bgDiv.appendChild(tmpDiv);

		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:"+partToDraw.y+"px;left:"+(partToDraw.x + (i*60))+"px;background-image:url(images/ships/b"+ (partToDraw.midImages[i]) +".png);");
		midDiv.appendChild(tmpDiv);

		tmpDiv = document.createElement("div");
		tmpDiv.setAttribute("class", "shipImage");
		tmpDiv.setAttribute("style", "position:absolute;top:"+partToDraw.y+"px;left:"+(partToDraw.x + (i*60))+"px;background-image:url(images/ships/c"+ (partToDraw.fgImages[i]) +".png);");
		fgDiv.appendChild(tmpDiv);

	}
	
	
	healthWidth = Math.floor(i * 50 * (GetAdjustedStatistic(partToDraw, "armor") / GetAdjustedStatistic(partToDraw, "maxArmor")));
	
	healthContainerWidth = Math.floor(i * 50);
	
	healthLeft = startX + ((((i + 1) * 60) - (healthContainerWidth)) / 2)

	healthDiv = document.createElement("div");
	healthContainerDiv = document.createElement("div");
	
	healthDiv.setAttribute("class", "healthBar");
	healthContainerDiv.setAttribute("class", "healthBarContainer");
	
	if (startY == 0) {
		healthDiv.setAttribute("style", "height:12px;top:"+(startY + 59)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		healthContainerDiv.setAttribute("style", "height:12px;top:"+(startY + 57)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	} else if (startY > 0) {
		healthDiv.setAttribute("style", "top:"+(startY + 117)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		healthContainerDiv.setAttribute("style", "top:"+(startY + 115)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	} else {
		healthDiv.setAttribute("style", "top:"+(startY + 1)+"px;left:"+(healthLeft+2)+"px;width:"+healthWidth+"px;");
		healthContainerDiv.setAttribute("style", "top:"+(startY - 1)+"px;left:"+healthLeft+"px;width:"+healthContainerWidth+"px;");
	}
	
	

	if(document.getElementById(drawingDiv) != null) {
		document.getElementById(drawingDiv).appendChild(bgDiv);
		document.getElementById(drawingDiv).appendChild(midDiv);
		document.getElementById(drawingDiv).appendChild(fgDiv);
	} else {
		drawingDiv.appendChild(bgDiv);
		drawingDiv.appendChild(midDiv);
		drawingDiv.appendChild(fgDiv);
	}
	
	if (healthWidth < healthContainerWidth)
	{
		if (healthWidth < (healthContainerWidth / 4))
		{
			healthContainerDiv.style.setProperty("border", "2px solid #FF0000");
			healthContainerDiv.style.setProperty("background-color", "#330000");
//			healthDiv.style.setProperty("border", "2px solid #FF0000");
			healthDiv.style.setProperty("background-color", "#FF0000");
		} else if (healthWidth < (healthContainerWidth / 2))
		{
			healthContainerDiv.style.setProperty("border", "2px solid #FFFF00");
			healthContainerDiv.style.setProperty("background-color", "#333300");
//			healthDiv.style.setProperty("border", "2px solid #FFFF00");
			healthDiv.style.setProperty("background-color", "#FFFF00");
		} else {
			healthContainerDiv.style.setProperty("border", "2px solid #00FF00");
			healthContainerDiv.style.setProperty("background-color", "#003300");
//			healthDiv.style.setProperty("border", "2px solid #00FF00");
			healthDiv.style.setProperty("background-color", "#00FF00");
		}

		if(document.getElementById(drawingDiv) != null) {
		document.getElementById(drawingDiv).appendChild(healthContainerDiv);
		document.getElementById(drawingDiv).appendChild(healthDiv);
		} else {
			drawingDiv.appendChild(healthDiv);
			drawingDiv.appendChild(healthContainerDiv);
		}
	}
	if(document.getElementById(drawingDiv) != null) {
		document.getElementById(drawingDiv).style.width = ((i + 1) * 60) + "px";
	} else {
		drawingDiv.style.width = ((i + 1) * 60) + "px";
	}

}


jsFilesLoaded = jsFilesLoaded + 1;
