// JavaScript Document
var lastCategory;
var output;
var currentCategoryStr;
var imageLink;
var allCategoriesStr;

function searchProceedingsFor(thisText)
{
	var d = new Date();
	var t = d.getTime();
	
	return "<div class='proceedingsDivTop'><span class='proceedingsBtnTop' onclick=\"window.open('http://alfalfa.ucdavis.edu/+symposium/proceedings/search.aspx?q=" +thisText + "','" + t + "', 'menubar=1,resizable=1,location=1,status=1,scrollbars=1,width=920, height=800')\"><b>" + (menu.length == 0 ? "Try This<br> " : "More<br> ") + "</b> <img alt=\"Search Icon\" src=\"http://alfalfa.ucdavis.edu/-images/search.gif\" /> Search Alfalfa Symposium Proceedings for \"" + thisText + "\"</span></div>";
}

function outputResults()
{	
	var results = new String();
	var imageLink;
	
	document.getElementById("searchText").value = searchTextParsed;
	document.title = searchTextParsed + " (" + menu.length + " " + resultType + ")";
	//updateHash(searchTextParsed);
	
	output = "";
	currentCategoryStr = "";
	allCategoriesStr = "";
	
	output = "<form style='margin-bottom:10px;'><table><tr><td><b>(" + menu.length + " "+ resultType +") </b></td><td><input class='searchLinkBox' onclick='this.focus;this.select();' type='text' value='" + searchTextParsed + "'/></td></tr>";
	
	output += "</table></form>";

	
	if(menu.length == 0) //amtToShow == 0
	{
		output += "<div style='padding:20px 0 20px 0; color: red; font-weight:bold'>No "+resultType+" found.</div>";
	}
	else
	{	

		//results += getViewingResults(start);
		//results += sections;
		
		var curName, curPhoto, curExtLink, curExtLink2, curTitle, curLocation, curPhone, curFax, curEmail, curSpecialties;
		
		for(var i=0; i <  menu.length /**(Number(start) + amtToShow)*/; i++)
		{
			results = "";
			results += "<div class=\"imageContainer\">";
			
			//store cur info for easier code below
			curName = menu[i].getElementsByTagName('n')[0].childNodes[0];
			curPhoto = menu[i].getElementsByTagName('img')[0].childNodes[0];
			curExtLink = menu[i].getElementsByTagName('link')[0].childNodes[0];
			curExtLink2 = menu[i].getElementsByTagName('link2')[0].childNodes[0];
			curTitle = menu[i].getElementsByTagName('et')[0].childNodes[0];
			curLocation = menu[i].getElementsByTagName('l')[0].childNodes[0];
			curPhone = menu[i].getElementsByTagName('p')[0].childNodes[0];
			curFax = menu[i].getElementsByTagName('f')[0].childNodes[0];
			curEmail = menu[i].getElementsByTagName('e')[0].childNodes[0];
			curSpecialties = menu[i].getElementsByTagName('s')[0].childNodes[0];
			
			if(curPhoto != null)
			{
				results += "<img alt=\"" + curName.nodeValue + " Photo\" class=\"imageStyle\" src=\"" + imagePath + curPhoto.nodeValue + "\">";
			}
			results += "</div><div class=\"member\">";
			results += "<ul><li class=\"name\">" + curName.nodeValue + "</li>";
			
			if(curTitle != null)
			{
				results += "<li class=\"title\">" + curTitle.nodeValue + "</li>";
			}
			results += "</ul><table>";
			
			if(curExtLink != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Website:</td><td class=\"info\"><a href=\"" + curExtLink.nodeValue + "\">"+ curExtLink.nodeValue + "</a></td></tr>";
			}
			
			if(curExtLink2 != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Website 2:</td><td class=\"info\"><a href=\"" + curExtLink2.nodeValue + "\">"+ curExtLink2.nodeValue + "</a></td></tr>";
			}
			
			if(curLocation != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Location:</td><td class=\"info\">" + curLocation.nodeValue + "</td></tr>";
			}
			
			if(curPhone != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Phone:</td><td class=\"info\">" + curPhone.nodeValue + "</td></tr>";
			}
			
			if(curFax != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Fax:</td><td class=\"info\">" + curFax.nodeValue + "</td></tr>";
			}
			
			if(curEmail != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">E-mail:</td><td class=\"info\"><a href=\"mailto:" + curEmail.nodeValue + "\">" + curEmail.nodeValue + "</a></td></tr>";
			}
			
			if(curSpecialties != null)
			{
				results += "<tr><td valign=\"top\" class=\"subtitle\">Specialties:</td><td class=\"info\">" + curSpecialties.nodeValue + "</td></tr>";
			}
			
			results += "</table></div>";
			
			addEntry(menu[i].parentNode.getElementsByTagName('ct')[0].childNodes[0].nodeValue, i, results);	
		}
		endCategory(); //need to end last category
		lastCategory = "";
		output += allCategoriesStr;
	}
	//output += searchProceedingsFor(searchTextParsed);
	
	//document.getElementById("everything").style.display = "none";
	//document.getElementById("onScreen").style.display = "";
	document.getElementById("peopleData").innerHTML += output;
	//document.getElementById("dropDown").style.display = "";
}

function endCategory()
{
	currentCategoryStr += "</div>";
			
	if(lastCategory == searchTextParsed)
		allCategoriesStr = currentCategoryStr + allCategoriesStr;	
	else
		allCategoriesStr = allCategoriesStr + currentCategoryStr;
}

function addEntry(category, i, entry)
{
	if(category == lastCategory)
		currentCategoryStr += entry; //add the entry to the same category
	else
	{
		if(currentCategoryStr != "")
			endCategory();
		
		//start the new category
		currentCategoryStr = ""
		currentCategoryStr += "<div class='groupDiv'><div class='headerTextSearch'>" + category + "</div>";
		currentCategoryStr += entry;

		lastCategory = category;
	}
}