/**
 * This script contains the functions to rotate intro image on a page (like a slideshow)
 */

var slideshows = new Object();
var slideshowinterval;

function slideshowShow( slide ) {
	slideGo( document.getElementById('slideshowContent') , slide-1 , 3000 );
}

function runHeader() {
	if ( document.getElementById('slideshowContent') ) startSlideshow( 'slideshowContent' , 0 );
}

// Returns the height of the intro
function startSlideshow( elid , firstslide ) {

	// Get element
	el = document.getElementById( elid );

	// Get all IMG headers in element
	var headers = el.getElementsByTagName( 'img' );

	// Save slideshow to global array
	slideshows.elid = firstslide;

	// hide all elements to start with
	hideElements ( headers );

	// Show first slide
	slideGo( el , slideshows.elid , 0 );

	// Interval to show next
	if( headers.length > 1 ) {
		clearInterval(slideshowinterval);
		slideshowinterval = setInterval( "slideNext('" + elid + "')" , 5000 );
	}
}

// Loop throug all elements and set them to display none
function hideElements ( el ) {
	for ( var i=0; i < el.length; i++ ) el[i].style.display = 'none';
}

// Function to go to any element of slideshow
function slideGo( el , i , timeout ) {
	var headers = el.getElementsByTagName( 'img' );
	var total = headers.length;
	slideshows.elid = i % total;
	hideElements( headers );
	headers[slideshows.elid].style.display = 'block';
	if ( timeout > 0 ) {
		clearInterval(slideshowinterval);
		slideshowinterval = setInterval( "startSlideshow('slideshowContent','" + slideshows.elid + "')" , timeout );
	}
}

// Function to go to next element of slideshow
function slideNext( elid ) {
	slideGo( document.getElementById(elid) , slideshows.elid+1 , 0 );
}

