var i = 0;

//preload menu hovers
menuleft = new Image(149,36);
menuleft.src="images/menu-left-hover.png";
menumid = new Image(171,36);
menumid.src="images/menu-middle-hover.png";
menuright = new Image(144,36);
menuright.src="images/menu-right-hover.png";

window.onload = function() {
	//update every 10 seconds
	setInterval('updateFeature()', 10000);
}


//AJAX request
function getXMLHttpRequest() {
	var xmlHttp;
    
	try {
    		xmlHttp = new XMLHttpRequest();
	} catch(e) {
    		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				//redirect to static version of page
				alert("Sorry, your browser does not support dynamic page updating.");
				return false;
			}
		}
	}

	return xmlHttp;
}



//Artwork class

function Artwork(title, path) {
	this.title = title;
	this.path = path;
}

Artwork.prototype.title;
Artwork.prototype.path;

Artwork.prototype.getTitle = function() {
	return this.title;
}

Artwork.prototype.getPath = function() {
	return this.path;
}


// List of Artworks

var artworks = [];
artworks[0] = new Artwork("Chasm", "pastel_oil/Chasm_275.jpg");
artworks[1] = new Artwork("Cold Depths", "pastel_oil/Cold_Depths_275.jpg");
artworks[2] = new Artwork("Discovery", "pastel_oil/Discovery_275.jpg");
artworks[3] = new Artwork("Hypolimnion", "pastel_oil/Hypolimnion_275.jpg");
artworks[4] = new Artwork("Purification", "pastel_oil/Purification_275.jpg");
artworks[5] = new Artwork("Salinity", "pastel_oil/Salinity_275.jpg");
artworks[6] = new Artwork("Seep", "pastel_oil/Seep_275.jpg");
artworks[7] = new Artwork("Still Life", "pastel_oil/Still_Life_275.jpg");




function updateFeature() {
	i++;
	if(i > 7) i = 0;

	//load the next image

	var artwork = artworks[i];

	var xmlHttp = getXMLHttpRequest();
	xmlHttp.open("GET", "images/gallery/" + artwork.getPath(), true );
	//stop IE caching request
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xmlHttp.setRequestHeader("Pragma", "no-cache");
	xmlHttp.send(null);
    
	//when image has loaded update the feature child nodes

	xmlHttp.onreadystatechange = function() {
    		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			
			var featureNode = document.getElementById("feature");
			var imageNode = featureNode.getElementsByTagName("IMG")[0];
			var captionNode = featureNode.getElementsByTagName("P")[0];

			imageNode.setAttribute("src", "images/gallery/" + artwork.getPath() );
			imageNode.setAttribute("alt", artwork.getTitle() );

			captionNode.removeChild( captionNode.firstChild );
			captionNode.appendChild( document.createTextNode(artwork.getTitle()) );
		}
	}

	i++;
}


