//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() {
		//load various objects once, since we shouldn't need to load them every time
		this.imageCaption = document.getElementById('featureText');
		this.linkHolder = document.getElementById('featureLink');
		this.ulElement = document.getElementById('featureList');
		this.liElements = this.ulElement.childNodes;
		if (this.delaySeconds) {
			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 {
			if (xmlhttp.status != 200) {
				response = null;
			} else {
				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';
		feature.holderSRC = this.path + feature.name + '.url';

		return feature;
	}

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

		//replace the caption text with the new caption
		var captionText = this.loadURL(feature.captionSRC);
		this.imageCaption.innerHTML = captionText;

		//check to see if this image should be linked to x.url
		if (this.linkHolder) {
			newLink = this.loadURL(feature.holderSRC);

			imageLink = this.linkHolder.getElementsByTagName('a');
			imageLink = imageLink[0];
			if (imageLink) {
				imageHTML = imageLink.innerHTML;
			} else {
				imageHTML = this.linkHolder.innerHTML;
			}

			if (newLink) {
				imageHTML = newLink + imageHTML + '</a>';
			}

			this.linkHolder.innerHTML = imageHTML;
		}

		//need to do image src here, in case the image HTML gets replaced above
		imageElement = document.getElementById('featureImage');
		imageElement.src=feature.imageSRC;

		//set the class on all of the feature icons
		for (var liElement=0;liElement<this.liElements.length;liElement++) {
			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);
	addLoadFunction(initializeFeatures);
}

/* for non-rotating features */
function staticFeature() {
	features.initialize();
}
function staticFeatures(path) {
	features = new Features(path);
	addLoadFunction(staticFeature);
}

function addLoadFunction(newLoadFunction) {
	if (!window.onload || typeof window.onload != 'function') {
		window.onload = newLoadFunction;
	} else {
		var oldLoadFunction = window.onload;
		window.onload = function() {
			oldLoadFunction();
			newLoadFunction();
		}
	}
}

