// JavaScript Document for management of chocobo data
function Chocobo(server, owner, name, breed, phenotype, g1, g2, gender, mother, father) {
	this.server = server;
	this.owner = owner;
	this.name = name;
	this.breed = breed;
	this.phenotype = phenotype;
	this.g1 = g1;
	this.g2 = g2;
	this.gender = gender;
	this.mother = mother;
	this.father = father;
}

var chocobos = new Array(new Chocobo("Unicorn", "Fidelina", "DarkFire", "Courser", "Red", "r", "r", "Male", "SouthernGirl", "BlackBlack"));
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "AvianArrow", "Rousey", "Yellow", "Y", "Y", "Male", "AvianCyclone", "ShadowJustice");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "BlackGirl", "Courser", "Red", "r", "r", "Female", "LadyCinder", "BlackBlack");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "HumbleGreen", "Jennet", "Yellow", "Y", "g", "Male", "SouthernGirl", "ExcellentHope");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "JadeBaron", "Jennet", "Green", "g", "?", "Male", "PrincessRed", "JadeStallion");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "PrincessJade", "Jennet", "Green", "g", "?", "Female", "FantasticIII", "JadeBaron");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "PrincessRed", "Courser", "Red", "r", "r", "Female", "BlackGirl", "DarkGirl");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Iain", "OrJade", "Jennet", "Green", "g", "?", "Male", "AvianCyclone", "JadeBaron");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "OrAzure", "Palfrey", "Blue", "Y", "g", "Female", "AvianCyclone", "JadeStallion");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "BlackBlack", "Destrier", "Black", "k", "r", "Male", "DarkShadow", "RedFury");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "DarkGirl", "Destrier", "Black", "k", "r", "Male", "LadyCinder", "BlackBlack");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "GreenGirl", "Jennet", "Green", "g", "?", "Female", "FantasticIII", "JadeStallion");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "JadeStallion", "Jennet", "Green", "g", "?", "Male", "FantasticIII", "PeaceRhapsody");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "PaprikaGirl", "Course", "Red", "r", "r", "Female", "PrincessRed", "TranquilleGreen");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "SchwarzeBoy", "Destrier", "Black", "k", "r", "Male", "BlackGirl", "DarkGirl");
	chocobos[chocobos.length] = new Chocobo("Unicorn", "Liltie", "TranquilleGreen", "Jennet", "Green", "g", "?", "Male", "FantasticIII", "PeaceRhapsody");

var chicks = new Array(new Chocobo("Unicorn", "Fidelina", "????", "????", "????", "?", "?", "????", "GreenGirl", "JadeStallion"));
	chicks[chicks.length] = new Chocobo("Unicorn", "Iain", "????", "????", "????", "?", "?", "????", "PrincessJade", "BlackBlack");
	chicks[chicks.length] = new Chocobo("Unicorn", "Inuart", "????", "????", "????", "?", "?", "????", "GreenGirl", "BlackBlack");
	chicks[chicks.length] = new Chocobo("Unicorn", "Liltie", "????", "????", "????", "?", "?", "????", "GreenGirl", "BlackBlack");
	chicks[chicks.length] = new Chocobo("Unicorn", "Zoina", "????", "????", "????", "?", "?", "????", "PrincessRed", "BlackBlack");
	chicks[chicks.length] = new Chocobo("Unicorn", "Rivas", "????", "????", "????", "?", "?", "????", "SouthernGirl", "BlackBlack");
	
var stock = new Array(new Chocobo("Unicorn", "Coldsshadow", "DarkShadow", "Destrier", "Black", "k", "?", "Female", "????", "????"));
	stock[stock.length] = new Chocobo("Unicorn", "Coldsshadow", "RedFury", "Destrier", "Black", "k", "?", "Male", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "ColoredChocos", "AvianCyclone", "Palfrey", "Blue", "Y", "g", "Female", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "ColoredChocos", "FantasticIII", "Jennet", "Green", "g", "?", "Female", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "ColoredChocos", "LadyCinder", "Destrier", "Black", "k", "?", "Female", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "ColoredChocos", "ShadowJustice", "Palfrey", "Blue", "Y", "g", "Male", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "Dissi", "ExcellentHope", "Jennet", "Green", "g", "?", "Male", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "Dissi", "PeaceRhapsody", "Jenent", "Green", "g", "?", "Male", "????", "????");
	stock[stock.length] = new Chocobo("Unicorn", "Iain", "GaleFirecracker", "Rounsey", "Yellow", "Y", "?", "Female", "...", "quested...");
	stock[stock.length] = new Chocobo("Unicorn", "Schaeffer", "SouthernGirl", "Palfrey", "Blue", "Y", "g", "Female", "...", "ISNM...");

function getServer(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
		return birds[bird].server;
		}
	}
}

function getOwner(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].owner;
		}
	}
}

function getBreed(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].breed;
		}
	}
}

function getPhenotype(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].phenotype;
		}
	}
}

function getGenotype(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].g1 + birds[bird].g2;
		}
	}
}

function getG1(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].g1;
		}
	}
}

function getG2(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].g2;
		}
	}
}

function getMother(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].mother;
		}
	}
}

function getFather(name) {
	var birds = chocobos.concat(chicks).concat(stock);
	for(bird in birds) {
		if(birds[bird].name == name) {
			return birds[bird].father;
		}
	}
}

function getBird(name, owner) {
	if(name == "????") {
		var birds = chicks;
		for(bird in birds) {
			if(birds[bird].owner == owner) {
				return birds[bird];
			}
		}
	}
	else { 	var birds = chocobos.concat(stock);
		for(bird in birds) {
		if(birds[bird].name == name) {
				return birds[bird];
			}
		}
	}
}

function makePSquare(name, owner) {
	var bird = getBird(name, owner);
	var mg1 = getG1(bird.mother);
	var mg2 = getG2(bird.mother);
	var fg1 = getG1(bird.father);
	var fg2 = getG2(bird.father);

	var pSquare = "<div class = 'example'>";
	pSquare += "<div>" + bird.name + "</div>";
	if(bird.gender == "????") { pSquare += "<div class = 'subTitle'>" + bird.owner + "</div>"; }
	else { pSquare += "<div class = 'subTitle'>" + bird.owner + "'s " + bird.phenotype + " " + bird.gender + "</div>"; }
	pSquare += "<table align = 'center'>";
	pSquare += "<tr><td colspan = '2' rowspan = '2'>" + bird.g1 + bird.g2 + "</td><td colspan = '2'>" + bird.mother + "</td></tr>";
	pSquare += "<tr><td class = 'line-right' >" + mg1 + "</td><td>" + mg2 + "</td></tr>";
	pSquare += "<tr><td rowspan = '2'>" + bird.father + "</td><td class = 'line-bottom'>" + fg1 + "</td><td class = 'line-bottom-right' >" + fg1 + mg1 + "</td><td class = 'line-bottom' >" + fg1 + mg2 + "</td></tr>";
	pSquare += "<tr class = 'noline' ><td>" + fg2 + "</td><td class = 'line-right' >" + fg2 + mg1 + "</td><td>" + fg2 + mg2 + "</td></tr></table></div>";
	
	return pSquare;
}

function listPSquares(birds) {
	var list = "";
	for (bird in birds.sort(sortByName)) {
		list += makePSquare(birds[bird].name, birds[bird].owner);
	}
	return list;
}

function listBirds(birdArray) {
	var list = "";
	for(bird in birdArray) {
		list += "<tr><td>" + birdArray[bird].server + "</td><td>" + birdArray[bird].owner + "</td><td>" + birdArray[bird].name + "</td><td>" + birdArray[bird].breed + "</td>";

		if(birdArray[bird].phenotype == "Yellow") {
			list += "<td><img src = 'images/rounseyR.png' class = 'phenotype' alt = 'Yellow' /></td>";
		}
	
		if(birdArray[bird].phenotype == "Black") {
			list += "<td><img src = 'images/destrier.png' class = 'phenotype' alt = 'Black' /></td>";
		}
		
		if(birdArray[bird].phenotype == "Blue") {
			list += "<td><img src = 'images/palfrey.png' class = 'phenotype' alt = 'Blue' /></td>";
		}
		
		if(birdArray[bird].phenotype == "Green") {
			list += "<td><img src = 'images/jennet.png' class = 'phenotype' alt = 'Green' /></td>";
		}
		
		if(birdArray[bird].phenotype == "Red") {
			list += "<td><img src = 'images/courser.png' class = 'phenotype' alt = 'Red' /></td>";
		}
		
		if(birdArray[bird].phenotype == "????") {
			list += "<td>????</td>";
		}
		
		list += "<td>" + birdArray[bird].g1 + birdArray[bird].g2 + "</td><td>" + birdArray[bird].gender + "</td>";
		if(birdArray[bird].mother == "...") { list += "<td colspan = '2'>" + birdArray[bird].mother + birdArray[bird].father + "</td>"; }
		else { list += "<td>" + birdArray[bird].mother + "</td><td>" + birdArray[bird].father + "</td></tr>"; }
	}
	
	return list;
}

function listAll() {
	var list = makeTable("stock") + makeTable("birds") + makeTable("planned");
	return list;
}

function makeTable(howMany) {
	var list = 	"<table align = 'center'><tr><th>Server</th><th>Owner</th><th>Name</th><th>Breed</th><th>Phenotype</th><th>Genotype</th><th>Gender</th><th>Mother</th><th>Father</th></tr>";

	if(howMany == "all") { list += listAllStock() + listAllBirds() + listAllPlanned(); }
	if(howMany == "birds") { list += "<caption>Progeny</caption>" + listBirds(chocobos.sort(sortByOwner)); }
	if(howMany == "planned") { list += "<caption>Planned Crosses</caption>" + listBirds(chicks.sort(sortByOwner)); }
	if(howMany == "stock") { list += "<caption>Original Stock</caption>" + listBirds(stock.sort(sortByOwner)); }
	
	list += "</table>";
	
	return list;
}

function listCrosses() {
	var birds = chocobos.concat(chicks);
	birds.sort(sortByFather);
//	birds = birds.concat(chicks.sort(sortByFather));
	
	var list = "<div class = 'column'>";
	
	for (var i = 0; i < (birds.length / 2); i++) {
		list += birds[i].father + " (" + getGenotype(birds[i].father) + ") " + "x " + birds[i].mother + " (" + getGenotype(birds[i].mother) + ") " + " = " + birds[i].name + " (" + getGenotype(birds[i].name) + ") " + "<br />";
	}
	
	list += "</div><div class = 'column'>";
	
	for (var i = (birds.length / 2); i < birds.length; i++) {
		list += birds[i].father + " (" + getGenotype(birds[i].father) + ") " + "x " + birds[i].mother + " (" + getGenotype(birds[i].mother) + ") " + " = " + birds[i].name + " (" + getGenotype(birds[i].name) + ") " + "<br />";
	}

	list += "</div>"

	return list;
}

function sortByColor(thisObject,thatObject) {	
	if (thisObject.phenotype.toLowerCase() > thatObject.phenotype.toLowerCase())
	{
		return 1;
	}
	if (thisObject.phenotype.toLowerCase() < thatObject.phenotype.toLowerCase())
	{
		return -1;
	}
	return 0;
}

function sortByFather(thisObject,thatObject) {	
	if (thisObject.father.toLowerCase() > thatObject.father.toLowerCase())
	{
		return 1;
	}
	if (thisObject.father.toLowerCase() < thatObject.father.toLowerCase())
	{
		return -1;
	}
	return 0;
}

function sortByMother(thisObject,thatObject) {	
	if (thisObject.mother.toLowerCase() > thatObject.mother.toLowerCase())
	{
		return 1;
	}
	if (thisObject.mother.toLowerCase() < thatObject.mother.toLowerCase())
	{
		return -1;
	}
	return 0;
}

function sortByName(thisObject,thatObject) {	
	if (thisObject.name.toLowerCase() > thatObject.name.toLowerCase())
	{
		return 1;
	}
	if (thisObject.name.toLowerCase() < thatObject.name.toLowerCase())
	{
		return -1;
	}
	return 0;
}

function sortByOwner(thisObject,thatObject) {
	if (thisObject.owner.toLowerCase() > thatObject.owner.toLowerCase())
	{
		return 1;
	}
	if (thisObject.owner.toLowerCase() < thatObject.owner.toLowerCase())
	{
		return -1;
	}
	return 0;
}
