/* XML functions */
var xmlObj;
var xmlloaded = false;

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 loadLandscapeServicesXML ( pageName ) {
//alert("loadLandscapeXML invoked.");
	var xmlToLoad = '/xml/'+pageName+'.xml';								// Name of xml data file
	importXML( xmlToLoad );													// Load the xml data
}
function importXML( xmlfile )
{
	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 );
			parseLandscapeServicesXML();
			xmlloaded = true;
		}
		else
		{
			xmlObj = document.implementation.createDocument( "", "", null );
			xmlObj.onload = parseLandscapeServicesXML;
			xmlObj.load( xmlfile );
			xmlloaded = true;
		}
	};
	if (!xmlloaded)
	{
		xmlhttp.setRequestHeader( 'Content-Type', 'text/xml' );
		xmlhttp.send("");
		xmlObj = xmlhttp.responseXML;
		parseLandscapeServicesXML();
		xmlloaded = true;
	};
};
/*function loadXML (xmlFile) {
	// Assumptions: There is an XML file with the same name as the body.className
	//   of the html calling this.
	if (document.implementation && document.implementation.createDocument){
		// Mozilla
//alert("loadXML invoked. Browser is FF");
		xmlObj = document.implementation.createDocument("", "", null);
  		xmlObj.onload = function() {
			stripWhitespaceDoublePass(this);
			parseLandscapeServicesXML();}
  		xmlObj.load(xmlFile);
		//alert(xmlObj.nodeValue);
	} else if(window.ActiveXObject) {
		// IE
//alert("loadXML invoked. Browser is IE");
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.onreadystatechange=verify;
		xmlDoc.load(xmlFile);
		xmlObj=xmlDoc.documentElement;
		parseLandscapeServicesXML();
	} else {
		// Browser unknown
		alert("Browser unknown");
	}
}*/
function getElement( node, element ) {
// Returns the value of the specified element in the specified node
// Returns null if nothing is found
	if (!document.getElementsByTagName) return null;
	
	if ( node ) {
		var nodeElements = node.getElementsByTagName( element );
		if ( nodeElements.length > 0 ) {
			return nodeElements[ 0 ].firstChild.nodeValue;
		} else { 
			return null; 
		}
	} else { 
		return null; 
	}
}
function parseLandscapeServicesXML () {
	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;
	}
//alert("parseLandscapeServicesXML invoked. xmlObj.firstChild.tagName="+xmlObj.firstChild.tagName);
	// If XML contains projects
	var images = xmlObj.getElementsByTagName( 'image' );
	//alert ("persons.length="+persons.length);

	if (images.length > 0) {
		imageArray = new Array( images.length );
		// For every image listed in the XML, add them to the array
		for (var i=0; i < images.length; i++) {
			//var fileName = "'" + getTagText ( images[ i ], 'filename' )  + imageType + "'";
			imageArray[ i ] = "'" + getTagText ( images[ i ], 'filename' )  + imageType + "'";
		}// endFor every image
		// Format data for slideshow
		var arrayInfo = '['+imageArray.toString()+']';

		// Instatiate slideshow
		myShow = new Slideshow('my_slideshow', {hu: 'images/', images: eval( arrayInfo ), type: 'fade', pan: 0, zoom: 0, navigation: 'thumbnails fast', thumbnailre: [/\./, '-s.'] });
		//myShow = new Slideshow('my_slideshow', {hu: 'images/', images: ['AthenaHighSchool.jpg','Gigas02.jpg'], navigation: 'thumbails'});
		//myShow = new Slideshow('my_slideshow', {hu: 'images/', navigation: 'thumbnails fast', thumbnailre: [/\./, '-s.'], images: ['Gionta05.jpg','HeritageJewelers.jpg']});
	}// endif XML contains images
}


var xmlDoc;																	// The XML file
var xmlObj;  																// The resulting XML object
var imageType = '.jpg';														// Image type of thumbs
var imageArray;																// Array of image names to pass to slideshow

/* if not mootools
addLoadEvent( function () {loadLandscapeServicesXML();});
*/
window.addEvent('domready', function(){	
	loadLandscapeServicesXML( document.body.className );
});
