// Walter's Understated Slide Show
// wuss.js
// copyright 2011 Walter Bowie
// v1.0



var wuss = function(url,id) {

	this.currentImage = -1;
	this.display_id = id;
	this.display_div = document.getElementById(id);
	this.pathList = [];
	// say is not yet implemented
	this.sayList = [];
	this.schema = {
		rowtag: "image",
		columns: [
				  	{ tagname: "@path", label: "Path" },
					{ tagname: "@say", label: "Say" }
				 ]
	};

	var xmldoc = AJAX_load(url); // uses function at bottom
	var xmlrows = xmldoc.getElementsByTagName(this.schema.rowtag);
	for(ktr=0;ktr<xmlrows.length;ktr++){
		var xmlrow = xmlrows[ktr];
        for(var c = 0; c < this.schema.columns.length; c++) {
            var sc = this.schema.columns[c];
            var tagname = (typeof sc == "string")?sc:sc.tagname;
            var celltext;
            if (tagname.charAt(0) == '@') {
                // If the tagname begins with '@', it is an attribute name
				switch (tagname) {
					case '@path':
					celltext = xmlrow.getAttribute(tagname.substring(1));
					this.pathList[ktr] = celltext;
					break;
					case '@say':
					celltext = xmlrow.getAttribute(tagname.substring(1));
					this.sayList[c] = celltext;
					break;
				}
            }
            else {
                // Find the XML element that holds the data for this column
                var xmlcell = xmlrow.getElementsByTagName(tagname)[0];
				// Check if there is a value  - walter
				// ie7 returns null , firefox undefined if there is no value
				// to test explicitly for either use === , as in if(xmlcell === undefined)
				// for both null and undefined , use if(xmlcell)
				// Use hyphen unless there is legitamite data 
				celltext = "-";
				if(xmlcell){ // this filters out any null or undefined values
					// ProductID is a lookup table, substitute name for ID number
					if(tagname == "ProductID"){
						// if there is a matching lookup name, use it
						//if(productlookup[xmlcell.firstChild.data]){celltext = productlookup[xmlcell.firstChild.data];}
					}
					// IndustryID is a lookup table, substitute name for ID number
					else if(tagname == "IndustryID"){
						// if there is a matching lookup name, use it
						//if(industrylookup[xmlcell.firstChild.data]){celltext = industrylookup[xmlcell.firstChild.data];}
					}
					else if(tagname == "ApplicationID"){
						// if there is a matching lookup name, use it
						//if(applicationlookup[xmlcell.firstChild.data]){celltext = applicationlookup[xmlcell.firstChild.data];}
					}
                    // All other fields are 
					else {celltext = xmlcell.firstChild.data;}
				} // if(xmlcell)
            } // else
            // Create the HTML element for this cell
            //var cell = document.createElement("td");
            // Put the text data into the HTML cell
            //---this.display_div.appendChild(document.createTextNode(celltext));
            // Add the cell to the row
            //row.appendChild(cell);
        } // for c

	} // for ktr

// preload images
var osd = document.createElement('div');
osd.setAttribute('id', 'off_screen_div');
osd.style.display = "none";
document.body.appendChild(osd);
for(var ktr=0;ktr < this.pathList.length;ktr++){
	var newImage = document.createElement('img');
	newImage.src = this.pathList[ktr];
	osd.appendChild(newImage);
}


this.timer;
// this function uses closure via the "with" keyword to reference itself 
this.play = function() {
	this.currentImage++;  
	if(this.currentImage == this.pathList.length){ 
		this.currentImage = 0;
	}
	this.showImage(this.currentImage);
	  //alert("hi");
      with (this) { this.timer = setTimeout( function() { play() }, 3500 );}
   }

this.showImage(0);
this.play();


} // end of wuss constructor

wuss.prototype.prevImage = function() {
	this.currentImage--;  
	if(this.currentImage < 0){ 
		this.currentImage = this.pathList.length-1;
	}
	//alert(this.joe);
	clearTimeout(this.timer);
	this.showImage(this.currentImage--);
	this.play();

}
wuss.prototype.nextImage = function() {
	//this.currentImage++;  
	//if(this.currentImage >= this.pathList.length){ 
	//	this.currentImage = 0;
	//}
	clearTimeout(this.timer);
	this.showImage(this.currentImage);
	this.play();

}

wuss.prototype.showImage = function (num) {

	var t = this.pathList[num];
	var img = document.createElement("img");
	img.className = "red";
	img.src = t;
	img.id = "joe_" + num;
	clearDiv(this.display_id);
	this.display_div.appendChild(img);
	//var imageTag = "<img scr=\"" + t + "\" />"; 
	//this.display_div.innerHTML=imageTag;
	//this.display_div.appendChild(document.createTextNode(imageTag));

}

function clearDiv(id){
    var thediv = document.getElementById(id);
	while(thediv.hasChildNodes()){
	    thediv.removeChild(thediv.lastChild);
	}
}

function AJAX_load( /* string */ path)
/*	
	Preconditions: path is valid URI;
	Postconditions: returns a loaded XMLDocument
	Purpose: loads an external XML document and returns it when it loads
		alerts error message if any errors occur
    Source: http://www.dreamincode.net/forums/topic/97149-xml-document/
	Adapted from: [url="http://www.w3schools.com/Dom/dom_parser.asp"]http://www.w3schools.com/Dom/dom_parser.asp[/url]
*/	
{
	try
	{ // MSIE
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
	{
		try
		{ // Firefox, Opera, etc.
			var xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e)
		{
			alert(e.message)
		}
	}
	try
	{
		xmlDoc.async = false; // halt code execution until xmlDoc is loaded
		xmlDoc.load( path );	// load file
	}
	catch(e)
	{ // unable to load
		try
		{	// for Safari
			xmlDoc = new XMLHttpRequest();
			xmlDoc.open("GET", path, false);
			xmlDoc.send();
			xmlDoc=xmlDoc.responseXML;
			
		}
		catch(e)
		{
			alert(e.message)
			return; // return with nothing
		}
	}
	return xmlDoc;
}

