//a features class that can switch between images and text

function Features(path, start, seconds) {
	this.path = path;
	this.currentFeature = start;
	this.delaySeconds = seconds;
	this.lastSwitchTime = new Date();
	
	this.initialize = function() {
		this.imageElement = document.getElementById('featureImage');
		this.imageCaption = document.getElementById('featureText');
		this.ulElement = document.getElementById('featureList');
		this.liElements = this.ulElement.childNodes;
		this.preloadNext();
	}

	this.loadURL = function(URL) {
		var response = null;
		var xmlhttp=new XMLHttpRequest();
		xmlhttp.open("GET",URL,false);
		xmlhttp.send(null);

		//Internet explorer 7 will choke on some (but not all) images and die when accessing responseText
		try {
			response = xmlhttp.responseText;
		} catch(error) {
			response = error.description;
		}
		return response;
	}

	this.constructFeature = function(featureIndex) {
		var feature = new Object();
		feature.listElement = this.liElements[featureIndex-1];
		var featureName = feature.listElement.getElementsByTagName('a')[0].title;
		feature.name = featureName.split('.')[0];
		
		feature.imageSRC = this.path + feature.name + '.jpg';
		feature.captionSRC = this.path + feature.name + '.html';

		return feature;
	}

	this.switchTo = function(featureIndex) {
		var feature = this.constructFeature(featureIndex);

		var captionText = this.loadURL(feature.captionSRC);

		this.imageElement.src=feature.imageSRC;
		this.imageCaption.innerHTML = captionText;
		
		for (var liElement=0;liElement<this.liElements.length;liElement++) {
			//modified to handle classes other than 'open', which need stay in place August 24 2009 -- jerry
			//this.liElements[liElement].className="";
			listItem = this.liElements[liElement];
			
			if (listItem.className) {
				//this should be a better regular expression
				listItem.className = listItem.className.replace(/ ?open ?/g, "");
			} else {
				listItem.className = "";
			}
		}
		if (feature.listElement.className) {
			feature.listElement.className += " open";
		} else {
			feature.listElement.className = "open";
		}

		this.currentFeature = featureIndex;
		this.lastSwitchTime = new Date();

		return false;
	}
	
	this.nextFeature = function() {
		var nextFeature = this.currentFeature + 1;
		if (nextFeature > this.liElements.length) {
			nextFeature = 1;
		}
		return nextFeature;
	}
	
	this.preloadNext = function() {
		//load next image for speed
		var nextFeature = this.nextFeature();
		var feature = this.constructFeature(nextFeature);
		this.loadURL(feature.imageSRC);
	}

	this.advance = function() {
		//don't switch for ten seconds after a manual switch
		var now = new Date();
		if (now.valueOf() > this.lastSwitchTime.valueOf() + 10000) {
			var nextFeature = this.nextFeature();
			this.switchTo(nextFeature);
			this.preloadNext();
		}
	}

	this.delay = function() {
		return this.delaySeconds*1000;
	}
}

function advanceFeature() {
	features.advance();
	window.setTimeout(advanceFeature, features.delay());
}

function initializeFeatures() {
	features.initialize();
	window.setTimeout(advanceFeature, features.delay());
}

//create a gallery and start the slide show in motion
function beginFeatures(start, path, seconds) {
	features = new Features(path, start, seconds);
	window.onload = initializeFeatures;
}
