/* - ***** BEGIN LICENSE BLOCK *****
   -
   - xml.js Version: 0.1
   -
   - Copyright (c)  2004-2007  K.M. Cruikshank (CruikshankK@pdx.edu). All Rights Reserved.
   -
   - Permission is granted to copy, distribute and/or modify this document
   - under the terms of the GNU Free Documentation License, Version 1.2
   - or any later version published by the Free Software Foundation
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - ***** END LICENSE BLOCK ***** 
*/

// Global variables ...
// This allows us to quickly do multiple transformations on the documents without reloading them
var xsltDoc;
var xmlDoc;
// ======================================================================================================
// Function to load xmlFiles into document model objects (xsltDoc and xmlDoc)
//		 xmlFile: Source XML file -- path and name
//		xsltFile: Source XSLT file -- path and name
function loadFiles(xmlFile, xsltFile) {
	if (document.implementation && document.implementation.createDocument) {	// This works with Mozilla and Netscape
		var myXMLHTTPRequest = new XMLHttpRequest();
		// load the XML data document
		myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", xmlFile, false);
		myXMLHTTPRequest.send(null);
		xmlDoc = myXMLHTTPRequest.responseXML;
		// load the XSLT document
		myXMLHTTPRequest.open("GET", xsltFile, false);
		myXMLHTTPRequest.send(null);
		xsltDoc = myXMLHTTPRequest.responseXML;
	} else if (window.ActiveXObject) {		// This works IE (or browsers with ActiveX)
		// Load the XML document
		xmlDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
		xmlDoc.async = false;
		xmlDoc.load(xmlFile); 
		// Load the XSLT document
		xsltDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
		xsltDoc.async = false;
		xsltDoc.load(xsltFile);
	} else {
		alert('Your browser can\'t handle this script');
	}
}
// ======================================================================================================
// Function to load xmlFile RSS feed into document model objects (xmlDoc)
// RSS provided by USGS
//		 xmlFile: Source XML file -- path and name
function loadFeed(xmlFile) {
	if (document.implementation && document.implementation.createDocument) {	// This works with Mozilla and Netscape
		var myXMLHTTPRequest = new XMLHttpRequest();
		// load the XML data document
		myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", "http://earthquakes.usgs.gov/eqcenter/catalogs/eqs1day-M2.5.xml", false);
		myXMLHTTPRequest.send(null);
		xmlDoc = myXMLHTTPRequest.responseXML;
	} else if (window.ActiveXObject) {						// This works IE (or browsers with ActiveX)
		// Load the XML document

        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
        xmlhttp.open("GET", "http://earthquakes.usgs.gov/eqcenter/catalogs/eqs1day-M2.5.xml", false);
        xmlhttp.send(null);
        xmlDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
        xmlDoc = xmlhttp.responseXML;
        if (xmlDoc.parseError.errorCode != 0) {
           var myErr = xmlDoc.parseError;
           alert("You have error " + myErr.reason);
        } 

//		xmlDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
//		xmlDoc.async = false;
//		xmlDoc.load(xmlFile);
	} else {
		alert('Your browser can\'t handle this script');
	}
}
// ======================================================================================================
// Function to put most recent earthquake into web page
//		 Assumes xmlDoc has been generated
function mostRecentEQ(szTarget) {
	if (document.implementation && document.implementation.createDocument) {	// This works with Mozilla and Netscape
		// Get a Node lists by tag names (XMLDOMNodeList objects)
		var objImageNodeList = xmlDoc.getElementsByTagName('pubDate');
		var objCaptionNodeList = xmlDoc.getElementsByTagName('title');
		var nImageNumber = 0;
		document.getElementById(szTarget).innerHTML = objCaptionNodeList.item(nImageNumber).childNodes.item(0).data;
	} else if (window.ActiveXObject) {						// This works IE (or browsers with ActiveX)
		// Get a Node lists by tag names (XMLDOMNodeList objects)
		var objPubDateList = xmlDoc.getElementsByTagName("pubDate");
		var objTitleList = xmlDoc.getElementsByTagName('title');
		var nImageNumber = 0;
		//document.getElementById(szTarget).innerHTML = objPubDateList.item(1).text;
	} else {
		alert('Your browser can\'t handle this script');
	}
}
// ======================================================================================================
// Function to find all keywords present in XML file, and populate a list box with the results
// Works with IE 6.x and Firefox 1.0
//		divTarget: Division within the target HTML file
function getKeywordList(divTarget){
	var keywordNodeList = xmlDoc.getElementsByTagName('keyword');
	var keywords = new Array(keywordNodeList.length + 1);
	for(i = 0; i < keywordNodeList.length; i++) {
		if (keywordNodeList.item(i).firstChild != null )
			keywords[i] = keywordNodeList.item(i).firstChild.nodeValue;
	}
	keywords.sort();
	j = 1;
	var newOption = new Option(keywords[0], keywords[0], false, false);
	document.getElementById(divTarget).options[j] = newOption;
	for(i = 1; i < keywordNodeList.length; i++) {
		if (keywords[i] != keywords[i-1]) {
			j++;
			document.getElementById(divTarget).options[j] = new Option(keywords[i], keywords[i], false, false);
		}
	}
}
// ======================================================================================================
// Function to perform XSLT transformation
//		divTarget: Division within the target HTML file
//		szKeyword: Passed as parameter to XSLT
function doTransformation(divTarget, szKeyword) {
	if (document.implementation && document.implementation.createDocument) {
		// This works with Mozilla and Netscape
		var xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xsltDoc);
		xsltProcessor.setParameter("", "searchFor", szKeyword);
		var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
		document.getElementById(divTarget).innerHTML = "";
		document.getElementById(divTarget).appendChild(fragment);
		
	} else if (window.ActiveXObject) { 	
		// This works IE (or browsers with ActiveX)
		var myTemplate = new ActiveXObject("Msxml2.XSLTemplate");
		myTemplate.stylesheet = xsltDoc;
		var xsltProcessor = myTemplate.createProcessor();
		xsltProcessor.addParameter("searchFor", szKeyword);
		xsltProcessor.input = xmlDoc;
		xsltProcessor.transform;
		document.getElementById(divTarget).innerHTML = xsltProcessor.output;
	}
}

// Function reads XML file containing images filenames and captions. It then picks one of the images
// at random and display that in the current page	
function SelectImage(xmlFile, szImageContainer, szCaptionContainer){
	// =====================================================================
	// Load XML document 
	if (document.implementation && document.implementation.createDocument) {
		// This should work with Mozilla and Netscape
		var myXMLHTTPRequest = new XMLHttpRequest();
		// load the xml file
		var xmlDoc;
		myXMLHTTPRequest = new XMLHttpRequest();
		myXMLHTTPRequest.open("GET", xmlFile, false);
		myXMLHTTPRequest.send(null);
		xmlDoc = myXMLHTTPRequest.responseXML;
		// Get an list of child nodes under IMAGES tag (XMLDOMNodeList object)
		var objChildNodeList = xmlDoc.documentElement.childNodes;
		// Get a Node lists by tag names (XMLDOMNodeList objects)
		var objImageNodeList = xmlDoc.getElementsByTagName("filename");
		var objCaptionNodeList = xmlDoc.getElementsByTagName("description");
		var objPhotographPath = xmlDoc.getElementsByTagName("home");
		var nImageNumber;
		nImageNumber = Math.ceil(Math.random() * (objChildNodeList.length / 2)) - 1;
		// Get the filename from the data structure -- write to framePicture
		var path = document.getElementById(szCaptionContainer).innerHTML = objPhotographPath.item(0).childNodes.item(0).data
		document.getElementById(szImageContainer).src = path + objImageNodeList.item(nImageNumber).childNodes.item(0).data;

		// Get the caption from XML data structure -- write to frameCaption
		document.getElementById(szCaptionContainer).innerHTML = objCaptionNodeList.item(nImageNumber).childNodes.item(0).data;
	} else if (window.ActiveXObject) { 
		// This should work with IE (or browsers with ActiveX)
		// Load the XML document
		var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
		xmlDoc.async=false;
		xmlDoc.load(xmlFile); 
		// Get an list of child nodes under IMAGES tag (XMLDOMNodeList object)
		var objChildNodeList = xmlDoc.documentElement.childNodes;
					
		// Get a Node lists by tag names (XMLDOMNodeList objects)
		var objImageNodeList = xmlDoc.getElementsByTagName("filename");
		var objCaptionNodeList = xmlDoc.getElementsByTagName("description");
		var objPhotographPath = xmlDoc.getElementsByTagName("home");
		var nImageNumber;
		nImageNumber = Math.ceil( Math.random() * (objChildNodeList.length-1) ) - 1;
		
		// Get the filename from the data structure -- write to framePicture
		document.getElementById(szImageContainer).src = objPhotographPath.item(0).text + objImageNodeList.item(nImageNumber).text;

		// Get the caption from XML data structure -- write to frameCaption
		document.getElementById(szCaptionContainer).innerHTML = objCaptionNodeList.item(nImageNumber).text;
	} else {
		alert('Your browser can\'t handle this script');
	}
}

