/* This script powers the 'click next' feature on the homepage, by showing/hiding one working paper at a time */
/* Ben Hayes, ILRT, 2008 */

/* getElementsByClassName taken by Robert Nyman */
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

function nextfeature(features) {
	
	var numFeatures = features.length;

	for(var i=0; i<numFeatures; i++)	{
		/* When we find the currently visible paper: */
		if(features[i].className == 'current feature') {
			
			// Remove the 'current' class from this one and put it on the next instead: 
			features[i].className = 'feature';

			// If this is the last paper, go back to the first:
			if(i == numFeatures-1) features[0].className = 'current feature';  
			// Otherwise it's the next in line:
			else features[i+1].className = 'current feature';		
			
			//Now end:
			return;
		}
	}	

}

window.onload = function() {
	
	/* Don't run this if we're not on the right page: */
	if(!document.getElementById('featured')) { return false; }

	//Get an array of features. Each feature is a child of #featured
	//Get rid of other stuff like new lines - we only want DIVs
	var children = document.getElementById('featured').childNodes;
	var features = new Array(); var j=0;

	for(i=0; i<children.length; i++) {
		if(children[i].tagName == 'DIV') {
			features[j] = children[i];
			j++;
		}
	}

	/* Assign event handlers for each next button: */
	var featured = document.getElementById('featured');
	var nextlinks = getElementsByClassName("next", "a", featured);
	for(var i=0; i<nextlinks.length; i++) {
		nextlinks[i].onclick = function() {
		nextfeature(features);
		return false;
		}

	}
}

