// JavaScript Document

function createNewsPosts()
{
	var newsEvent = xmlDoc.getElementsByTagName("EVENT");
	var newsEventLength = newsEvent.length;
	var i, j;    // Loop counters

	//var newsPosts = "<div id='events'>";
	var newsPosts = "";
	
	for (i=0; i<newsEventLength; i++) {
		newsPosts += "<div class='post";
		if (i==0) {
			newsPosts += " first";	
		}
		newsPosts += "' id='news"+(i+1)+"'>";
		
		newsPosts += "<h2 class='date'>";
		newsPosts += xmlDoc.getElementsByTagName("MONTH")[i].firstChild.data+"&nbsp;";
		if (xmlDoc.getElementsByTagName("DAY")[i].hasChildNodes()==1) {
			newsPosts += xmlDoc.getElementsByTagName("DAY")[i].firstChild.data+",&nbsp;";
		}
		newsPosts += xmlDoc.getElementsByTagName("YEAR")[i].firstChild.data;
		newsPosts += "</h2>";
		
		newsPosts += "<h3 class='headline'>";
		if (xmlDoc.getElementsByTagName("HEADLINE")[i].hasChildNodes()==1) {
			newsPosts += xmlDoc.getElementsByTagName("HEADLINE")[i].firstChild.data;
		}
		/*if (xmlDoc.getElementsByTagName("CITY")[i].hasChildNodes()==1){
			document.write(xmlDoc.getElementsByTagName("CITY")[i].firstChild.data+",&nbsp;");
		}
		if (xmlDoc.getElementsByTagName("STATE")[i].hasChildNodes()==1){
			document.write(xmlDoc.getElementsByTagName("STATE")[i].firstChild.data+" - ");
		}*/
		newsPosts += "</h3>";
		
		if (xmlDoc.getElementsByTagName("HEROPHOTO")[i].hasChildNodes()==1) {
			newsPosts += "<p class='herophoto'>";
			newsPosts += xmlDoc.getElementsByTagName("HEROPHOTO")[i].firstChild.data;
			newsPosts += "</p>";
		}
		
		newsPosts += "<p class='copy'>";
		newsPosts += xmlDoc.getElementsByTagName("COPY")[i].firstChild.data;
		newsPosts += "</p>";
						
		if (xmlDoc.getElementsByTagName("RESOURCES")[i].hasChildNodes()==1) {
			newsPosts += "<br/>";
			var resources = xmlDoc.getElementsByTagName("RESOURCES")[i];
			var links = resources.getElementsByTagName("LINK");
			var linksLength = links.length;
			
			for (j=0; j<linksLength; j++) {
				newsPosts += "<p class='links'>";
				newsPosts += "<a href='"+resources.getElementsByTagName("URL")[j].firstChild.data+"' ";
				if (resources.getElementsByTagName("CLASS")[j].hasChildNodes()==1) {
					newsPosts += "class='"+resources.getElementsByTagName("CLASS")[j].firstChild.data+"' ";
				}
				newsPosts += "target='_blank'>";
				newsPosts += resources.getElementsByTagName("TITLE")[j].firstChild.data;
				newsPosts += "</a>";
				newsPosts += "</p>";
			}
		}
		newsPosts += "</div>";
	}
	//newsPosts += "</div>";
	
	document.write(newsPosts);
}

function setTitles() {
	var selectedYear = xmlDoc.getElementsByTagName("YEAR")[0].firstChild.data;
	// Update document titles according to year selected
	document.title = document.title+": "+selectedYear;
	document.getElementById("YEAR").innerHTML = ": "+selectedYear;
}

function paginationUpdate(currYear) {

	document.getElementById(currYear).className="currentyear";
}

function newsIndex_home()
{
	importXML("/news/local/xml/newsCurrent.xml");
	var newsPosts = "";
	
	// Main List
	for (var i=0; i<3; i++) {
		newsPosts += "<div class='news'>";
		newsPosts += "<h3 class='newsTitle'>";
		newsPosts += "<a href='/news/#news"+(i+1)+"'>";
		newsPosts += xmlDoc.getElementsByTagName("HEADLINE")[i].firstChild.data+"</a>";
		newsPosts += "</h3>";
		
		newsPosts += "<p class='newsDate'>";
		newsPosts += xmlDoc.getElementsByTagName("MONTH")[i].firstChild.data+" ";
		newsPosts += xmlDoc.getElementsByTagName("YEAR")[i].firstChild.data;
		newsPosts += "</p>";
		
		newsPosts += "<p class='newsCopy'>";
		var newsCopy = xmlDoc.getElementsByTagName("COPY")[i].firstChild.data;
		
		//  Stop copy at a specific char, making sure to be outside of any HTML tags 
		//  and at the end of a complete word.
		
		var endChar = 179;	// One less than desired length since count starts at zero
		var offsetEndChar = 0;
		var checkChar = null;
		var j; // Counter
		
		// Check for HTML Tags chars and add offset amount
		for (j=0; j<endChar; j++) {
			checkChar = newsCopy.charAt(j);
			if (checkChar == '<') {
				offsetEndChar += 1;
				checkChar = null;
				//var chars = "";
				while (checkChar != '>') {
					offsetEndChar++;
					j++;
					checkChar = newsCopy.charAt(j);
				}
			}
		}
		endChar += offsetEndChar;
		offsetEndChar = 0;
		
		// Sanity Check for stopping inside an HTML Tag
		var insideTag = 0;
		for (j=0; j<endChar; j++) {
			checkChar = newsCopy.charAt(j);
			//alert(checkChar);
			if (checkChar == '>') {			// End of opening tag
				insideTag = 1-insideTag;	// Toggles when hitting the end of each tag
			}
		}
		if (insideTag === 1) {
			while (checkChar != '>') {		// End of closing tag
				offsetEndChar++;
				j++;
				checkChar = newsCopy.charAt(j);
			}
		}
		endChar += offsetEndChar;
		// Code to prevent rare bug where the endChar happens to be a space
		// before an HTML tag which makes the code start at the '<' char in 
		// the HTML tag and proceed to find a space character at which to 
		// stop -- which leaves it inside the HTML tag
		endChar--; 

		// Cut the story at the end of a word
		checkChar = null;
		while (checkChar != ' ') {
			endChar++;
			checkChar = newsCopy.charAt(endChar);
			//alert(checkChar);
		}
		newsPosts += newsCopy.substring(0,endChar);
		
		// Read more link
		newsPosts += "&hellip; </p>";
		newsPosts += "<p class='newsMore'><a href='/news/#news"+(i+1)+"'>Read more</a></p>";
		newsPosts += "</div>";
	}
	document.write(newsPosts);
}
	
function newslist() {
	var whichXML = "/news/local/xml/newsCurrent.xml";
	importXML(whichXML);
	createNewsPosts();
}

function newsarchive() {
	// Parse URL for XML title 
	var yearURL = window.location.search;
	if (yearURL != '') {
		yearURL = yearURL.substring(1);
	} else {
		yearURL = "2004";
	}
	var whichXML = 'news'+yearURL+'.xml';

	importXML(whichXML);
	setTitles();
	paginationUpdate("y"+yearURL);
	createIndex();
}
