/* XML functions */
function verify() {
	// 0 Object is not initialized
	// 1 Loading object is loading data
	// 2 Loaded object has loaded data
	// 3 Data from object can be worked with
	// 4 Object completely initialized
	if (xmlDoc.readyState != 4) {
		return false;
	};
};
function stripWhitespaceDoublePass (theNode) {
//alert("stripWhitespaceDoublePass invoked. theNode="+theNode);
	// Loop through all children of theNode
	for ( var i=0; i<theNode.childNodes.length; i++) {
		// if the current node is a text node...
		if (theNode.childNodes[i].nodeType ==3) {
			// Check for any useful chars in the code
			var j=0;
			var emptyNode = true;
			for (j=0; j < theNode.childNodes[i].nodeValue.length; j++ ){
				if (theNode.childNodes[i].nodeValue.charCodeAt(j) > 32) {
					emptyNode = false;
					break;
				}
			};
			// If no useful chars are found, delete the node
			if (emptyNode) theNode.removeChild(theNode.childNodes[i]);
		};
	};
	// Now that all whitespace nodes have been removed,
	// call this function recursively on remaining children
	for (var k=0; k < theNode.childNodes.length; k++) stripWhitespaceDoublePass(theNode.childNodes[k]);
};
function importXML( filename )
{
	var xmlFile = xmlFolder + filename + '.xml';
	try
	{
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open( "GET", xmlFile, false);
	}
	catch (Exception)
	{
		var ie = ( typeof window.ActiveXObject != 'undefined' );
		if( ie )
		{
			xmlObj = new ActiveXObject( "Microsoft.XMLDOM" );
			xmlObj.async = false;
			while( xmlObj.readyState != 4 ) {};
			xmlObj.load( xmlFile );
			parseXML();
			xmlloaded = true;
		}
		else
		{
			xmlObj = document.implementation.createDocument( "", "", null );
			xmlObj.onload = parseXML;
			xmlObj.load( xmlFile );
			xmlloaded = true;
		};
	};
	if (!xmlloaded)
	{
		xmlhttp.setRequestHeader( 'Content-Type', 'text/xml' );
		xmlhttp.send("");
		xmlObj = xmlhttp.responseXML;
		parseXML();
		xmlloaded = true;
	};
};
function getTagText ( searchElement, tagName ) {
	var tagArray = searchElement.getElementsByTagName( tagName );
	if ( tagArray.length > 0 ) {
		if ( tagArray[ 0 ].firstChild ){
			if ( tagArray[ 0 ].firstChild.nodeValue.length > 0 ){ return tagArray[ 0 ].firstChild.nodeValue; }
		};
	};
	return null;
};
function parseXML () {
	// Assumptions: There is a div named imageDisplay.
//alert("imageProjectXML invoked. xmlObj.firstChild.tagName="+xmlObj.firstChild.tagName);
	
	// If XML contains projects
	images = xmlObj.getElementsByTagName("image");

	if ( images.length > 0 ) {
		// Add new div for large image display
		var tDiv = document.createElement("div");
		tDiv.id = "imageDisplay";
		imagesDisplayDiv.appendChild(tDiv);
		imageDisplayDiv = document.getElementById("imageDisplay");
		
		// Add new div for thumbnail display
		var tDiv = document.createElement("div");
		tDiv.id = "thumbnails";
		imagesDisplayDiv.appendChild(tDiv);
		thumbnailsDiv = document.getElementById("thumbnails");

		// For every image listed in the XML, add them to the div
		for (var i=0; i < images.length; i++) {
			// Get the filename of the image
			var tName = getTagText( images[i], 'filename' );
			// Create A node
			var tAnode = document.createElement("a");
			tAnode.style.borderColor = thumbNormal;
			tAnode.setAttribute( "name", i);
			tAnode.onclick  = function () {
				resetThumbBorders(thumbnailsDiv);
				this.style.borderColor = thumbActive;
				this.firstChild.style.visibility = "hidden";
				displayImage(this.name); 
				return false;
			};
			tAnode.onmouseover  = function () {
				if (this.firstChild.style.visibility!="hidden") {
					this.style.borderColor = thumbHover;
				};
			};
			tAnode.onmouseout  = function () {
				if (this.firstChild.style.visibility!="hidden") {
					this.style.borderColor = thumbNormal;
				};
			};
			// Create IMG node
			var tImg = document.createElement("img");
			tImg.src=thumbLocation+tName+thumbType;
			tAnode.appendChild(tImg);
			thumbnailsDiv.appendChild(tAnode);

			// Set correct feedback
			if (i==0) {
				tAnode.style.borderColor = thumbActive;
				tAnode.firstChild.style.visibility = "hidden";
				imageDisplayDiv.style.backgroundImage = "url("+fileLocation+tName+fileType+")";
			};
		};// endFor every image
	};// endif XML contains images
};
///// The background image for the Anode is the thumbnail!!!
function removeChildrenFromNode(nodeToClear) {
//alert("removeChildrenFromNode invoked.");
	//while(nodeToClear.firstChild) nodeToClear.removeChild(nodeToClear.firstChild);
};

function displayImage (nodeNumber) {
//alert("displayImage: nodeNumber="+nodeNumber);
	// Store selected project number
	imageNumber = nodeNumber;
	
	// Set pointer to the selected image
	var imageName = getTagText( images[ nodeNumber ], 'filename' );
	var imageFile = "url("+fileLocation+imageName+fileType+")";

	// Display default large image
	imageDisplayDiv.style.backgroundImage = imageFile;
	
	// Reset thumb borders
	resetThumbBorders( nodeNumber );
	
};
function resetThumbBorders ( nodeNumber ) {
//alert("resetThumbBorders invoked");
	// Get the array of thumbnails
	var thumbsToChange = thumbnailsDiv.getElementsByTagName("a");
	// How many thumbmails are there?
	var numThumbs = thumbsToChange.length;
	for ( var i=0; i<numThumbs; i++ ) {
		if ( i!=nodeNumber) {
			thumbsToChange[i].style.borderColor = thumbNormal;
			thumbsToChange[i].firstChild.style.visibility = "visible";
		} else {
			thumbsToChange[i].style.borderColor = thumbActive;
			thumbsToChange[i].firstChild.style.visibility = "hidden";
		};
	};
};

