function Bird(tag, name, number, sex, age, location, colorTag)
{
	this.tag = tag;
	this.name = name;
	this.number = number;
	this.sex = sex;
	this.age = age;
	this.location = location;
	this.colorTag = colorTag;
}

Bird.prototype.doMatch = function(toMatch)
{
	var returnVal = 0;
	if (this.tag == toMatch)
	{
		returnVal = 3;
	}
	else
	{
		for (var i = 0; i < toMatch.length; i++)
		{
			mChar = toMatch.substring(i, i+1);
			bChar = this.tag.substring(i, i+1);
			if (mChar == 'I')
			{
				mChar = "1";
			}
			if (bChar == 'I')
			{
				bChar = "1";
			}	
			if (mChar == "O")
			{
				mChar = "0";
			}
			if (bChar == "O")
			{
				bChar = "0";
			}	
			if (mChar == bChar && mChar != '_')
			{
				returnVal++;
			}
			else if (mChar != '_' && mChar != bChar)
			{
				returnVal = 0;
				break;
			}
		}
	}	
	// alert(toMatch + " :: " + this.tag + " :: " + returnVal);
	return returnVal;
}	

Bird.prototype.getBarNumber = function()
{
	return this.tag.substring(0,1);
}

Bird.prototype.getTagColorCombo = function()
{
	var colorCombo = 5;	// default is black with white
	var thisColor = parseInt(this.number.toString().substring(0,1));
	if (this.colorTag || thisColor == metaColorTag)
	{
		colorCombo = thisColor;
	}
	return colorCombo;
}

Bird.prototype.getTagNumbers = function()
{
	var modString = this.tag.substring(1,3);
	if (modString.indexOf('_') == 0)
	{
		modString = "&nbsp;" + this.tag.substring(2,3);
	}
	return modString;
}	

Bird.prototype.getDoColor = function()
{
	var thisColor = parseInt(this.number.toString().substring(0,1));
	return (this.colorTag || thisColor == metaColorTag);
}	
			

var birds = new Array();
var metaColorTag = -1;

birds["161"] = new Bird("161", "Matriarch", 161, "female", "May 10, 1997", "Big Sur", false);
birds["167"] = new Bird("167", "Kingpin", 167, "male", "May 6, 1997", "Big Sur", false);
birds["168"] = new Bird("168", "Beak Boy", 168, "male", "May 9, 1997", "Big Sur", true);
birds["171"] = new Bird("171", "Traveler", 171, "female", "May 26, 1997", "Big Sur", false);
birds["190"] = new Bird("190", "Slope Slug", 190, "female", "May 10, 1998", "Big Sur", true);
birds["192"] = new Bird("192", "Purist", 192, "female", "May 27, 1998", "Big Sur", false);
birds["194"] = new Bird("194", "Whalewatcher", 194, "male", "June 29, 1998", "Big Sur", false);
birds["199"] = new Bird("199", "Go 99", 199, "male", "April 8, 1999", "Big Sur", false);
birds["2_4"] = new Bird("2_4", "Amigo", 204, "male", "April 28, 1999", "Big Sur", false);
birds["2_8"] = new Bird("2_8", "Solo", 208, "female", "May 14, 1999", "Big Sur", true);
birds["2_9"] = new Bird("2_9", "Shadow", 209, "male", "May 15, 1999", "Big Sur", true);
birds["219"] = new Bird("219", "Rock", 219, "male", "April 8, 2000", "Big Sur", false);
birds["222"] = new Bird("222", "Cosmo", 222, "female", "April 17, 2000", "Big Sur", false);
birds["231"] = new Bird("231", "Wild1", 231, "female", "April 30, 2000", "Big Sur", false);
birds["236"] = new Bird("236", "Tiny", 236, "female", "March 22, 2001", "Big Sur", true);
birds["242"] = new Bird("242", "In the Red", 242, "male", "April 20, 2001", "Big Sur", false);
birds["251"] = new Bird("251", "Crush", 251, "male", "May 10, 2001", "Big Sur", true);
birds["286"] = new Bird("286", "Pinns", 286, "male", "May 16, 2002", "Big Sur", false);
birds["294"] = new Bird("294", "Late Bloomer", 294, "female", "April 7, 2003", "Big Sur", true);
birds["298"] = new Bird("298", "Plain Jane", 298, "female", "April 11, 2003", "Big Sur", false);
birds["3_3"] = new Bird("3_3", "Lucia", 303, "female", "April 22, 2003", "Big Sur", false);
birds["311"] = new Bird("311", "Loner", 311, "female", "May 12, 2003", "Big Sur", true);
birds["318"] = new Bird("318", "Benito", 318, "male", "June 9, 2003", "Big Sur", false);
birds["375"] = new Bird("375", "Survivor", 375, "female", "May 2, 2005", "Big Sur", false);
birds["044"] = new Bird("044", "Ventana", 444, "female", "May 5, 2007", "Big Sur", false);
birds["570"] = new Bird("570", "Fuego", 470, "male", "April 12, 2008", "Big Sur", false);
birds["577"] = new Bird("577", "Phoenix", 477, "unknown", "April 22, 2008", "Big Sur", false);
birds["599"] = new Bird("599", null, 499, "unknown", "2009", "Big Sur", false);
birds["6_1"] = new Bird("6_1", null, 501, "unknown", "2009", "Big Sur", false);
birds["638"] = new Bird("638", null, 538, "unknown", "2009", "Big Sur", false);

birds["006"] = new Bird("006", null, 306, "female", "May 4, 2003", "Pinnacles", false);
birds["112"] = new Bird("112", null, 112, "female", "May 15, 1994", "Pinnacles", false); // has a tag with a 12 above one dot
birds["310"] = new Bird("310", null, 310, "female", "May 11, 2003", "Pinnacles", false);
birds["312"] = new Bird("312", null, 312, "female", "May 14, 2003", "Pinnacles", false);
birds["313"] = new Bird("313", null, 313, "male", "May 15, 2003", "Pinnacles", false);
birds["317"] = new Bird("317", null, 317, "female", "May 26, 2003", "Pinnacles", false);
birds["330"] = new Bird("330", null, 330, "male", "April 19, 2004", "Pinnacles", false);
birds["332"] = new Bird("332", null, 332, "male", "April 22, 2004", "Pinnacles", false);
birds["335"] = new Bird("335", null, 335, "male", "April 23, 2004", "Pinnacles", false);
birds["336"] = new Bird("336", null, 336, "female", "April 28, 2004", "Pinnacles", false);
birds["340"] = new Bird("340", null, 340, "male", "May 9, 2008", "Pinnacles", false);
birds["345"] = new Bird("345", null, 345, "male", "May 16, 2004", "Pinnacles", false);
birds["351"] = new Bird("351", null, 351, "male", "May 28. 2004", "Pinnacles", false);
birds["4_0"] = new Bird("4_0", null, 400, "female", "April 11, 2006", "Pinnacles", false);
birds["401"] = new Bird("401", null, 401, "male", "April 13, 2006", "Pinnacles", false);
birds["4_5"] = new Bird("4_5", null, 405, "male", "April 18. 2006", "Pinnacles", false);
birds["411"] = new Bird("411", null, 411, "male", "April 26, 2006", "Pinnacles", false);
birds["018"] = new Bird("018", null, 418, "female", "May 10, 2006", "Pinnacles", false); // has a tag with an 18 above one long bar - bar not seen in real life - moved to a 0
birds["421"] = new Bird("421", null, 421, "male", "May 13, 2009", "Pinnacles", false); // has a tag with a 21 above one long bar
birds["431"] = new Bird("431", null, 431, "male", "April 12, 2007", "Pinnacles", false);
birds["438"] = new Bird("438", null, 438, "female", "April 21, 2007", "Pinnacles", false);
birds["448"] = new Bird("448", null, 448, "male", "May 8 ,2007", "Pinnacles", false);
birds["460"] = new Bird("460", null, 460, "female", "March 24, 2008", "Pinnacles", false); // has a tag with a 60 above one long bar
birds["463"] = new Bird("463", null, 463, "male", "April 2, 2008", "Pinnacles", false); // has a tag with a 63 above one long bar
birds["478"] = new Bird("478", null, 478, "male", "April 23, 2008", "Pinnacles", false); // has a tag with a 78 above one long bar
birds["481"] = new Bird("481", null, 481, "male", "April 27, 2008", "Pinnacles", false); // has a tag with a 81 above one long bar 
birds["4VI"] = new Bird("4VI", null, 451, "male", "May 15, 2007", "Pinnacles", false);
birds["0A4"] = new Bird("0A4", null, 514, "unknown", "2009", "Pinnacles", false);

// preload images
var b1 = new Image();
b1.src = "/image/condorwing_bar1.png";
var b2 = new Image();
b2.src = "/image/condorwing_bar2.png";
var b3 = new Image();
b3.src = "/image/condorwing_bar3.png";
var b4 = new Image();
b4.src = "/image/condorwing_bar4.png";
var b5 = new Image();
b5.src = "/image/condorwing_bar5.png";

function getNewTagString(getAlt)
{
	var toMatchBar = document.getElementById("toMatchBar");
	var tmb = toMatchBar.options[toMatchBar.selectedIndex].value;
	if (tmb.length == 2)
	{
		metaColorTag = parseInt(tmb.substring(0,1));
	}
	else
	{
		metaColorTag = -1;
	}	
	var toMatchDigits = document.getElementById("toMatchDigits");
	var digits = toMatchDigits.value.toUpperCase();
	if (digits.length == 0)
	{
		digits = "__";
	}
	else if (digits.length == 1)
	{
		if (getAlt)
		{
			digits = digits + "_";
		}
		else
		{
			digits = "_" + digits;
		}	
	}
	return (tmb.substring(0,1) + digits);
}


function getTagString()
{
	var toMatchBar = document.getElementById("toMatchBar");
	var toMatchDigits = document.getElementById("toMatchDigits");
	var digits = toMatchDigits.value;
	if (digits.length == 0)
	{
		digits = "00";
	}
	else if (digits.length == 1)
	{
		digits = "0" + digits;
	}
	return (toMatchBar.options[toMatchBar.selectedIndex].value + digits);
}

function doMatch(showLearnMore)
{
	var tagToMatch = getNewTagString(false);
	var altTagToMatch = getNewTagString(true);
	var hasAlt = altTagToMatch != tagToMatch;
	
	var exact = new Array();
	var close = new Array();
	var weak = new Array();
	var matchCount;
	for (var i in birds)
	{
		matchCount = birds[i].doMatch(tagToMatch);
		if (matchCount == 0 && hasAlt)
		{
			matchCount = birds[i].doMatch(altTagToMatch);
		}	
		if (matchCount == 3)
		{
			exact[exact.length] = birds[i];
		}	
		else if (matchCount == 2)
		{
			close[close.length] = birds[i];
		}
		else if (matchCount == 1)
		{
			weak[weak.length] = birds[i];
		}
	}
	var matches = document.getElementById("matches");
	removeChildren(matches);

	if (exact.length == 1)
	{
		showMatches("Bird Information: Exact Match", exact, matches, false, showLearnMore);
		fillTag(exact[0]);
	}
	else if (exact.length == 0 && close.length == 1)
	{
		showMatches("Bird Information: Close Match", close, matches, false, showLearnMore);
		fillTag(close[0]);
	}
	else if (exact.length == 0 && close.length == 0 && weak.length == 1)
	{
		showMatches("Bird Information: Potential Match", weak, matches, false, showLearnMore);
		fillTag(weak[0]);
	}
	else if (exact.length > 0 || close.length > 0 || weak.length > 0)
	{
		clearTag();
		showMatches("Exact Matches", exact, matches, true, showLearnMore);
		showMatches("Close Matches", close, matches, true, showLearnMore);
		showMatches("Potential Matches", weak, matches, true, showLearnMore);
	}
	else
	{
		clearTag();
		matches.appendChild(document.createTextNode("No Match Found"));
	}
}

function showMatches(headerString, matches, addTo, showShowTag, showLearnMore)
{
	var headerDiv;
	var matchDiv;
	if (matches.length > 0)
	{
		headerDiv = document.createElement("div");
		headerDiv.appendChild(document.createTextNode(headerString));
		addTo.appendChild(headerDiv);
		for (var i in matches)
		{
			matchDiv = document.createElement("div");
			matchDiv.className = "matchDiv";
			var stringTag= "";
			if (showShowTag)
			{
				stringTag = '<div class="matchDivTag"><a href="javascript:fillTag(birds[\'' + matches[i].tag + '\'])">show tag</a></div>';
			}
			else
			{
				stringTag = "";
			}	
			stringTag += matches[i].number + "";
			if (matches[i].name != null)
			{
				stringTag += ' - "' + matches[i].name + '"';
			}
			stringTag += "<br/>Location: " + matches[i].location;
			stringTag += "<br/>Sex: " + matches[i].sex + "&nbsp;&nbsp;&nbsp;Hatched: " + matches[i].age;
			if (showLearnMore)
			{
				var hrefString = "http://mycondor.org/condorprofiles/condor" + matches[i].number + ".html";
				if (matches[i].location == 'Pinnacles')
				{
					hrefString = "http://www.nps.gov/pinn/naturescience/profiles.htm";
				}	
				stringTag += "<br/><a href=\"" + hrefString + "\" target=\"_blank\">Learn More...</a>";
			}	
			matchDiv.innerHTML = stringTag;
			addTo.appendChild(matchDiv);
		}
	}
}

function fillTag(bird)
{
	var tagNumber = document.getElementById("tagNumber");
	var tagBarImg = document.getElementById("tagBarImg");
	var imgNumber = bird.getBarNumber();
	var doColor = bird.getDoColor();
	if (doColor)
	{
		imgNumber = 0;
	}	
	var modString = bird.getTagNumbers();
	tagBarImg.src = "/image/condorwing_bar" + imgNumber + ".png";
	tagNumber.innerHTML = modString;
	var tagCaption = document.getElementById("tagCaption");
	if (tagCaption)
	{
		var stringTag = "Condor: " + bird.number;
		if (bird.name != null)
		{
			stringTag += ' - "' + bird.name + '"';
		}
		tagCaption.innerHTML = stringTag;
	}
	else
	{
		// assume iPhone
		// window.pageYOffset.value = 0;
		scroll(0,0);
	}	
	var color = bird.getTagColorCombo();
	var bkColor = '#1d1d1d';
	var fgColor = '#cccccc';
	switch(color)
	{
		case 1:
			bkColor = '#dc1d1d';
			fgColor = '#1d1d1d';
			break;
		case 2:
			bkColor = '#dcc41d';
			fgColor = '#1d1d1d';
			break;
		case 3:
			bkColor = '#a1c6f3';
			fgColor = '#1d1d1d';
			break;
		case 4:
			bkColor = '#cccccc';
			fgColor = '#1d1d1d';
			break;
	}		
	var mainDiv = document.getElementById("mainDiv");
	tagNumber.style.color = fgColor;
	mainDiv.style.backgroundColor = bkColor;
		
}

function clearTag()
{
	var tagNumber = document.getElementById("tagNumber");
	var tagBarImg = document.getElementById("tagBarImg");
	tagNumber.innerHTML = "";
	tagBarImg.src = "/image/condorwing_bar0.png";
	var tagCaption = document.getElementById("tagCaption");
	if (tagCaption)
	{
		tagCaption.innerHTML = "";
	}	
	var mainDiv = document.getElementById("mainDiv");
	mainDiv.style.backgroundColor = '#1d1d1d';
}

function removeChildren(ele)
{
	if(!ele)
	{
		return false;
	}
	if(ele.hasChildNodes())
	{
		while(ele.childNodes.length >= 1)
		{
			ele.removeChild(ele.firstChild);
		}
	}
	return ele;
}

function hide()
{
	var s = document.getElementById("show");
	var f = document.getElementById("faq");
	s.style.display = "block";
	f.style.display = "none";
}

function show()
{
	var s = document.getElementById("show");
	var f = document.getElementById("faq");
	s.style.display = "none";
	f.style.display = "block";
}
