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



function GetAdjustedStatistic(rootPart, statToGet, cascadeType, recalculate)
{
//	recalculate == null; // scratch this for the time being...
	if (recalculate == null)
	{
		switch(statToGet)
		{
			case "baseCost":
			{
				return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType) * (1 - (getStatisticModifier(rootPart, "haggling") / 600)));
				break;
			}
			case "maxSpeed":
			{
				if (inEncounter == 1 && rootPart.posture == "evade")
				{
					return rootPart.adjMaxSpeed * 2;
				} else {
					return rootPart.adjMaxSpeed;
				}
				break;
			}
			case "maxCargo":
			{
				return rootPart.adjMaxCargo;
				break;
			}
			case "maxFuel":
			{
				return rootPart.adjMaxFuel;
				break;
			}
			case "maxArmor":
			{
				return rootPart.adjMaxArmor;
				break;
			}
			case "firepower":
			{
				return rootPart.adjFirepower;
				break;
			}
			case "profitMargin":
			{
				return rootPart.adjProfitMargin;
				break;
			}
			case "partName":
			{
				return rootPart.partName;
				break;
			}
			case "partNumber":
			{
				return rootPart.partNumber;
				break;
			}
			case "curCrew":
			case "maxCrew":
			case "curCargo":
			{
				return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType));
				break;
			}
			case "fuel":
			{
				return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "navigation", true) / 100)));
				break;
			}
			case "armor":
			{
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "engineering", true) / 100)));
//				return rootPart.armor;
				break;
			}
			default:
			{
				alert("whoops!  Preset getStatisticModifier failed for " + statToGet);
				return;
				break;
			}
		}
	}
	
	if (fullyLoaded == 1 && rootPart == playerShip)
	{
//		alert("checking stat: " + statToGet);
	}

	if (cascadeType == null)
	{
		cascadeType = "children";
	}
	switch (statToGet)
	{
		case "fuel":
		case "maxFuel":
		case "maxSpeed":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "navigation", true) / 100)));
			break;
		case "armor":
		case "maxArmor":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "engineering", true) / 100)));
			break;
		case "firepower":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "gunnery", true) / 100)));
			break;
		case "baseCost":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 - (getStatisticModifier(rootPart, "haggling", true) / 600)));
			break;
		case "curCrew":
		case "maxCrew":
		case "curCargo":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate));
			break;
		case "profitMargin":
			return baseProfitMargin - (getStatisticModifier(rootPart, "haggling", true) / 600);
			break;
		case "maxCargo":
			return Math.floor(GetPartStatistic(rootPart, statToGet, cascadeType, recalculate) * (1 + (getStatisticModifier(rootPart, "haggling", true) / 100)));
			break;
		case "partName":
			return rootPart.partName;
			break;
		case "partNumber":
			return rootPart.partNumber;
			break;
		default:
			alert("Error: can't find part statistic '"+statToGet+"'!");
			return 0;
			break;
	}
}

function getBaseStatisticDifference(rootPart, targetPart, statToGet)
{
	var tmpDiff;
	
	tmpDiff = Math.floor(GetPartStatistic(rootPart, statToGet, "none")) - Math.floor(GetPartStatistic(targetPart, statToGet, "none"))
	if (tmpDiff < 0) {
		return "(" + tmpDiff + ")";
	} else {
		return "(+" + tmpDiff + ")";
	}
}

function GetPartStatistic(rootPart, statToGet, cascadeType)
{
	var i;
	var returnValue=0;
	var loopUntil=0;
	
	if (cascadeType == null)
	{
		cascadeType = "children";
	}
	
	if (rootPart == null)
	{
		return 0;
	}
	
	if (rootPart.attachments == null)
	{
		loopUntil = 0;
	} else {
		loopUntil = rootPart.attachments.length;
	}
	
	if (cascadeType == "none")
	{
		loopUntil = 0;
	}
	
	for (i=0; i<loopUntil; i++)
	{
		if (rootPart.attachments[i] != null)
		{
			returnValue = returnValue + GetPartStatistic(rootPart.attachments[i], statToGet);
		}
	}
	if (rootPart != null)
	{
		eval("returnValue = returnValue + rootPart." + statToGet + ";");
	}
	
	
	if (statToGet == "curCrew") {
		for (i = 0; i < rootPart.crew.length; i++) {
			if (rootPart.crew[i] != null) {
				if (rootPart.crew[i].firstName == "Auto-") {
					returnValue = returnValue - 1;
				}
			}
		}
	}

	if (rootPart.armor == 0 && (statToGet == "maxSpeed" || statToGet == "firepower" || statToGet == "maxFuel" || statToGet == "fuel")) {
		// damaged--no results!
		return 0;
	}

	
	return returnValue;
}

function getStatisticModifier(rootPart, modifierToGet, recalculate)
{
	var returnValue=0;
	var i;
	
//	recalculate = null // disable this for now...
	
	if (rootPart == null)
	{
		return returnValue;
	}
	
	if (recalculate == null)
	{
		switch(modifierToGet)
		{
			case "engineering":
			{
				return rootPart.adjEngineering;
				break;
			}
			case "haggling":
			{
				return rootPart.adjHaggling;
				break;
			}
			case "gunnery":
			{
				return rootPart.adjGunnery;
				break;
			}
			case "navigation":
			{
				return rootPart.adjNavigation;
				break;
			}
			default:
			{
				alert("whoops!  Preset getStatisticModifier failed for " + modifierToGet);
				return;
				break;
			}
		}
	
	
	}
	
	if (rootPart.attachments != null)
	{
		for (i=0; i<rootPart.attachments.length; i++)
		{
			if (rootPart.attachments[i] != null)
			{
				returnValue = returnValue + getStatisticModifier(rootPart.attachments[i], modifierToGet, recalculate);
			}
		}
	}
	
	if (rootPart.crew == null) {
		return returnValue;
	}
	
	for (i=0; i<rootPart.crew.length;i++)
	{
		if (rootPart.crew[i] != null)
		{
			eval("returnValue = returnValue + rootPart.crew[" + i + "]." + modifierToGet + ";");
		}
	}
	return returnValue;
	
}

function calculateAdjustedValues(targetShip) {

	var i, j;
	var fillTank;
	
	fillTank = false;
	
	if (targetShip == null)
	{
		return;
	}
	
	if (targetShip.attachments != null)
	{
		for (i=0; i < targetShip.attachments.length; i++)
		{
			calculateAdjustedValues(targetShip.attachments[i]);
		}
		
	}

	if (targetShip.fuel >= targetShip.maxFuel) {
		fillTank = true;
	}
	
	targetShip.adjHaggling = getStatisticModifier(targetShip, "haggling", true);
	targetShip.adjNavigation = getStatisticModifier(targetShip, "navigation", true);
	targetShip.adjGunnery = getStatisticModifier(targetShip, "gunnery", true);
	targetShip.adjEngineering = getStatisticModifier(targetShip, "engineering", true);
	
	if (fullyLoaded == 1 && targetShip.adjNavigation != 0) {
//		alert ("adjNavigation: " + adjNavigation);
	} else if (fullyLoaded == 1) {
//		alert ("adjNav? " + getStatisticModifier(playerShip, "navigation", true));
	}

	targetShip.adjMaxSpeed = GetAdjustedStatistic(targetShip, "maxSpeed", "children", true);
	targetShip.adjMaxCargo = GetAdjustedStatistic(targetShip, "maxCargo", "children", true);
	targetShip.adjMaxFuel = GetAdjustedStatistic(targetShip, "maxFuel", "children", true);
	targetShip.adjMaxArmor = GetAdjustedStatistic(targetShip, "maxArmor", "children", true);
	targetShip.adjFirepower = GetAdjustedStatistic(targetShip, "firepower", "children", true);
	targetShip.adjProfitMargin = GetAdjustedStatistic(targetShip, "profitMargin", "children", true);
	targetShip.adjBaseCost = GetAdjustedStatistic(targetShip, "baseCost", "children", true);
	
	targetShip.adjMaxCargo = GetAdjustedStatistic(targetShip, "maxCargo", "none", true);
	for (j=0; j<targetShip.attachments.length; j++) {
		if (targetShip.attachments[j] != null) {
			targetShip.adjMaxCargo = targetShip.adjMaxCargo + GetAdjustedStatistic(targetShip.attachments[j], "maxCargo", "none", true);
		}
	}
	
	if (fillTank == true) {
		targetShip.fuel = targetShip.adjMaxFuel;
	}
	
	// workaround: if the player ship's speed is less than 1, make it 1.
	if (targetShip == playerShip && playerShip.adjMaxSpeed < 1) {
		playerShip.adjMaxSpeed = 1;
	}

}

jsFilesLoaded = jsFilesLoaded + 1;
