

/*
	Greg Woodfill
	
	logos.js
	
	These are scripts to support the logos journal a-z index. 
****************************************************************************************************
 			CLASS
			Article
			Structure to hold Articles.
			
			sortByAuthor and sortByTitle are functions to sort an array of Articles
****************************************************************************************************/
			function Article(l_name, f_name, title, volume_link, pages )
			{
				this.l_name = l_name;
				this.f_name = f_name;
				this.title = title;
				this.volume_link = volume_link;
				this.pages = pages;
			}
/*******************************************************************************************************
	sortByTitle(a,b) AND sortByAuthor(a,b)
	Sort method for an array of Articles
*******************************************************************************************************/
			
		function sortByTitle(a,b)
		{
			var tempTitle1 = a.title;
			var tempTitle2 = b.title;
			
			if (tempTitle1.charAt(0) == "\"")
			{
				tempTitle1 = tempTitle1.substring(1);
			}
			
			if (tempTitle2.charAt(0) == "\"")
			{
				tempTitle2 = tempTitle2.substring(1);
			}
			
			if (tempTitle1 < tempTitle2)
				return -1;
			if (tempTitle1 > tempTitle2)
				return 1;
			else
				return 0;
			}
/* **********************************************************************************
				Sort by last name, if last name the same, sort by first name 
				if first name the same sort by title
***************************************************************************************/
		function sortByAuthor(a,b)
		{
			
			
			var lName = ""; //used to strip out quotations
			
			if (a.l_name < b.l_name)
				return -1;
			if (a.l_name > b.l_name)
				return 1;
			if (a.l_name == b.l_name)
				{
					if (a.f_name < b.f_name)
						return -1;
					if (a.f_name > b.f_name)
						return 1;
					if (a.f_name == b.f_name)
					{
						if (a.title < b.title)
								return -1;
						if (a.title > b.title)
							return 1;
						else
							return 0;
					}
				}
		}
		
/***************************************************************************************************************
**************************************************************************************************************
	
		findChildElements(nodeListArray,nodeNameAsString)
		
		Param: nodeListArray - an Array of nodes that you want to search over, usually the childNodes of another node: myNode.childNodes 
			Param: nodeNameAsString - the class or id that you are looking for
			
			
			returns: a node if one matches
					null otherwise
		*/
		
		function findChildElement(nodeListArray, nodeNameAsString)
		{
		
			var i=0;
			
			for (i=0; i<(nodeListArray.length); i++)
			{
				if ( (nodeListArray[i].className == nodeNameAsString) || (nodeListArray[i].id == nodeNameAsString))
				{
					/* return the nodeList and end this function */
					return nodeListArray[i];
				}
			} 
			/* if no elements found then return null */
			return null;
		}
		
		
		/***************************************************************************************************************
		***************************************************************************************************************/
	
	
	
	/******************************************************************************************
		
		Display page displays the page with a string argument for what to sort by.
	
	********************************************************************************************/
		
function displayPage(sortOrderStr)
		{
				
				var i=0;
				var k=0;
				var letterIndex = new Array(); // array of all letters used in the top section
				var color = new Array("#EAE4CC","#e2dcba"); // background color array
				var borderTop = ""; //css border top code
				var compareLetter; // compares the letter for the loop to display letter tables
				var html = "";
				
				
				document.getElementById("articles").innerHTML = "";
				if (sortOrderStr == "author")
					{
						articleList.sort(sortByAuthor);
					}
				else if	(sortOrderStr == "title")
					{	
						articleList.sort(sortByTitle);
					}
				/************************************************************************
				Print out the top portion with letter links
				a b c d, etc
				*************************************************************************/
				
				
					html = html + ("<div id='LogosHeading'>");
					html = html + ("<span class='headingText'>LOGOS Archive&nbsp;&nbsp;</span>");
					html = html + ("<span class=\"headingLinks\"><ul><li><a href=\"../archives/archives.html\">Index of Issues</a></li><li><a href=\"../articleindex/AtoZ.html\">Index of Articles</a></li><li><a href=\"../currentIssueRedir.html\">Current Issue</a></li><li><a href=\"http://muse.jhu.edu/journals/logos/\">Sample Issue</a></li></ul></span>");
					html = html + ("</div>"); // close LogosHeading
					html = html + ("<div class='headingspacer'></div>");
					
					html = html + ("<div id='letterIndex'>");
					
					currLetter ="";
					for (i=0;i<articleList.length;i++)
					{
						k=0;
						
						if (sortOrderStr == "author")
						{
							letterIndex[i] = articleList[0].l_name.charAt(0);
							compareLetter = articleList[i].l_name.charAt(0);
						}
						else
						{
							if(articleList[i].title.charAt(0) == "\"")
							{
								k=k+1;
								
							}
							letterIndex[i] = articleList[i].title.charAt(k);
							compareLetter = articleList[i].title.charAt(k);
						}
						
						
				
						if (currLetter != compareLetter)
						{
							if (sortOrderStr == "author" )
							{
								currLetter = articleList[i].l_name.charAt(0);
							}
							else
							{
								currLetter = articleList[i].title.charAt(0);
							}
							
							if (!(currLetter == "\""))
								html = html + ("<a href='#" + currLetter.toUpperCase() + "'>" + currLetter.toUpperCase() + "</a>  ");
							
						}
				
					}
					html = html + ("</div>");
				
				// print out the table headings
				html = html + ("<table class='letterTable' id='heading' style='height:0; margin:0;' cellspacing='0' cellpadding='0'>");
				html = html + ("<tr style='height:16px;'>");
				html = html + ("<th class='letterSpace' style='border-top:none;visibility:hidden;'>A</th> <th class='lastName'><a href='#' onclick='displayPageByAuthor();'>Last Name</a></th> <th class='firstName'>First Name</th> <th class='articleTitle'><a href='#' onclick='displayPageByTitle();' >Title</a></th> <th class='volume'><a href='/cathstudies/logos/archives/archives.html'>Volume</a></th> <th class='pages'>Pages</th>")
				html = html + ("</tr>");
				html = html + ("</table>");
				
				for (i=0;i<articleList.length;i++)
				{
					// logic for letter changing, choose new letter close previous table and open new one
					if (sortOrderStr == "author")
					{
						compareLetter = articleList[i].l_name.charAt(0);
					}
					else
					{
						compareLetter = articleList[i].title.charAt(0);
						if (compareLetter == "\"")
							compareLetter = articleList[i].title.charAt(1);
					}
					if (currLetter != compareLetter )
					{
						currLetter = compareLetter;
						borderTop = "borderTop"; // CSS class name
						
						html = html + ("</table>"); // close the letterTable
							
						html = html + ("<table class='letterTable' cellspacing='0' cellpadding='0'>");
						html = html + ("<tr style='background-color:" + color[i%color.length] + "'>");
						html = html + ("<td id='" + currLetter.toUpperCase() + 
												"' class='letterSpace' style='padding:0' ><span class='letter'>" 
												+ currLetter + 
												"</span><br><a class='triangle' href='#pageContainer'> <img  src='../../../images/logos/cs_atoz_triangle.gif'  border='0'></a></td>");
			
					}
					else
					{
						borderTop = ""; // CSS class name
						html = html + ("<tr style='background-color:" + color[i%color.length] + "'>");
						html = html + ("<td class='letterSpace'>&nbsp;</td>");
					}
					// write out the table row.  note that the color is chosen with the modulo operator
					
					html = html + ("<td class='lastName " + borderTop + "'>" + articleList[i].l_name + "</td>");
					html = html + ("<td class='firstName " + borderTop +"'>" + articleList[i].f_name +  "</td>");
					html = html + ("<td class='articleTitle " + borderTop +"'>"  + articleList[i].title + "</td>");
					html = html + ("<td class='volume " + borderTop + "'>" + articleList[i].volume_link + "</td>");
					html = html + ("<td class='pages " + borderTop + "'>" + articleList[i].pages + "</td>");
					
					html = html + ("</tr>");
				}
				html = html + ("</table>");
				return html;
			}

function displayPageByAuthor()
{
	document.getElementById("articles").innerHTML = displayPage("author");	
}

function displayPageByTitle()
{
	document.getElementById("articles").innerHTML = displayPage("title");	
}
