function Player(name, race, homeworld)
{
	this.Construct(name, race, homeworld);
	
	this.factoryCost = 10;
	this.workerCost = 20;
	this.factoriesPerWorker = 2;
	this.workerProduction = 0.5;
	this.factoryProduction = 1;		// manual says 2, but likely it's 2 - 1 maint.
	
	this.wastePerFactory = 1;
	this.wasteCleanupCost = 0.5;
	
	this.shipLevel = 0;
	this.research = 0;
	
}
Player.prototype.type = "Player";
Player.className = "Player";
Player.Classes = {"Player":Player};

Player.Races = ["Human","Mrrshan","Silicoid","Sakkra","Psilon","Alkari","Klackon","Bulrathi","Meklar","Darlok"];
Player.Names = [["Durash IV","Alexander","Strader","Johann III","Lasitus","Bladrov II"]
				,["Prrsha","Miamar","Mirana","Shandra","Jasana","Yalara"]
				,["Igneous","Crystous","Geode","Carnax","Sedimin","Granid"]
				,["Hissa","Kryssta","Sauron","Tyranid","Guanar","Saurak"]
				,["Tachaon","Quark","Meson","Kelvan","Dynalon","Zygot"]
				,["Farseer","Skylord","Ariel","Redwing","Highsoar","Sharpclaw"]
				,["Ixitixl","K'kalak","Kikitik","Xantak","Kaxal","Klaquan"]
				,["Grunk","Bullux","Krungo","Monch","Durpp","Smurch"]
				,["M5-35","TX-1138","CB-715","QX-537","INT-986","TVC-15"]
				,["Ssithra","Nazgur","Darquan","Morfane","Shador","Narzina"]];

Player.prototype.Construct = function(name, race, homeworld)
{
	this.name = name;
	this.race = race;
	this.homeworld = homeworld;
	this.Planets = [homeworld];
	this.Fleets = [];
	this.Explored = [homeworld];
	this.Treaties = [];
	this.Reports = [];
}

Player.prototype.toJSON = function()
{
	var planetCombiner = function(planets, planet){planets.push(planet.name);return planets;};
	var planetNames = Util.Reduce(planetCombiner, this.Planets, []);
	var exploredNames = Util.Reduce(planetCombiner, this.Explored, []);
	
	//var planetNames = [];
	//var exploredNames = [];
	
	
	return {name:this.name
			,race:this.race
			,index:this.index
			,type:this.type
			,homeworld:this.homeworld.name
			,shipLevel:this.shipLevel
			,research:Math.floor(this.research)
			,Planets:planetNames
			,Explored:exploredNames
			,Fleets:this.Fleets
			,Treaties:this.Treaties
			};
}

Player.prototype.HasExplored = function(planet)
{
	for (var a=0; a<this.Explored.length; a++)
	{
		//alert(this.Explored[a])
		if (this.Explored[a] == planet || this.Explored[a].name == planet)
		{
			return true;
		}
	}
	return false;
}

Player.prototype.RemovePlanet = function(planet)
{
	for (var a=0; a<this.Planets.length; a++)
	{
		if (this.Planets[a] == planet)
		{
			this.Planets.splice(a,1);
			
			// does this matter?
			//if (this.homeworld == planet)
			//{
			//	this.homeworld = null;
			//}
			return;
		}
	}
}

Player.prototype.MoveShips = function()
{
	for (var a=this.Fleets.length-1; a>=0; a--)
	{
		var fleet = this.Fleets[a];
		
		if (fleet.destination)
		{
			if (fleet.courseProgress < fleet.courseLength-1)
			{
				fleet.inTransit = true;
				fleet.courseProgress++;
			}
			else
			{
				var existingFleet = Fleet.GetByPlanetAndOwner(fleet.destination, fleet.owner);
				if (existingFleet && existingFleet!=fleet)
				{
					existingFleet.Merge(fleet);
					fleet.Delete();
				}
				else
				{
					fleet.planet = fleet.destination;
					fleet.destination = null;
					fleet.inTransit = false;
					fleet.courseProgress = 0;
					fleet.courseLength = 0;
				}
				
			}
		}
	}
}

Player.prototype.NoteExplored = function()
{
	for (var a=this.Fleets.length-1; a>=0; a--)
	{
		var fleet = this.Fleets[a];
		
		if (!this.HasExplored(fleet.planet))
		{
			this.Explored.push(fleet.planet);
		}
	}
}
Player.prototype.Colonize = function()
{
	for (var a=this.Fleets.length-1; a>=0; a--)
	{
		var fleet = this.Fleets[a];
		var planet = fleet.planet;
		
		if (fleet.inTransit)
		{
			// not there yet.  chill.
		}
		else if (fleet.transports > 0)
		{

			if (!planet.owner)
			{
				// TODO - add report about this...
				fleet.transports = 0;
				if (fleet.IsEmpty())
				{
					fleet.Delete();
				}
			}
			else if (planet.owner == fleet.owner)
			{
				// new or ours
				planet.population += fleet.transports;
				
				if (planet.owner != fleet.owner)
				{
					planet.owner = fleet.owner;
					fleet.owner.Planets.push(planet);
				}
				
				fleet.transports = 0;
				if (fleet.IsEmpty())
				{
					fleet.Delete();
				}
			}
			else
			{
				// somebody else's planet.  Planet.ResolveGroundConflicts will take care of this case.
				//alert("somebody else's planet");
			}
		}
		
		if (!fleet.inTransit && fleet.transports > 0 && (!planet.owner || planet.owner == fleet.owner))
		{
		}
		else
		{
			
		}
	}
}

Player.prototype.Plan = function()
{
	// yearly maintenance
	this.Reports = [];
	
	// base player is Human controlled.  Nothing to do here
	return;
	
	// TODO - this is where the AI will do its thing in all derived Player classes.
	
}

Player.prototype.ToOrders = function()
{
	//this.priority[Planet.Priorities.Base] = 50;
	//return this.priority;
}

