var slideshow;

function startSlideshow() {
	slideshow = new Slideshow();
	slideshow.init();
	slideshow.run();
}

function stopSlideshow() {
	slideshow.stop();
}
		
function Slideshow() {
	var interval = 5 * 1000;
	var loop = true;
	var slides = new Array();
	var display = "block"; // default type of css display
	var current;
	var id;
	
	if (typeof Slideshow._initialized == "undefined") {
		Slideshow.prototype.add = function(oDiv) {
			this.slides.push(oDiv);
		}
		
		Slideshow.prototype.setInterval = function(iSecs) {
			this.interval = iSecs * 1000;
		}
		
		Slideshow.prototype.setLoop = function(bLoop) {
			this.loop = bLoop;
		}
		
		Slideshow.prototype.setDisplay = function(sDisplay) {
			this.display = sDisplay;
		}
		
		Slideshow.prototype.init = function() {
			this.slides = getElementsByClassName(document, "*", "slideshow");
			for (var i = 0; i < this.slides.length; i++)
				this.slides[i].style.display = "none";
				
			this.slides[0].style.display = display;
			current = 0;
		}
		
		Slideshow.prototype.run = function() {
			var oThis = this;
			function helper() {
				oThis.toggle();
			}
			
			id = window.setInterval(helper, interval);
		}
		
		Slideshow.prototype.stop = function() {
			if (id != null)
				window.clearInterval(id);
				
			this.init();
		}
		
		Slideshow.prototype.toggle = function() {
			if (current == this.slides.length - 1) {
				if (loop) {
					this.slides[current].style.display = "none";
					this.slides[0].style.display = display;
					current = 0;
				} else {
					this.stop();
				}
			} else {
				this.slides[current].style.display = "none";
				this.slides[current + 1].style.display = display;
				current++;
			}
		}
		
		Slideshow._initialized = true;
	}
}

/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if (typeof oClassNames == "object") {
		for (var i = 0; i < oClassNames.length; i++)
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
	} else
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));

	var oElement;
	var bMatchesAll;
	for (var j = 0; j < arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for (var k = 0; k < arrRegExpClassNames.length; k++){
			if (!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		
		if (bMatchesAll)
			arrReturnElements.push(oElement);
	}
	
	return arrReturnElements;
}