// configuration
var numberImages;
var interval;

//
var imageNodes = new Array();
var images = new Array();
var imageTargets = new Array();

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/001.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/002.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/003.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/004.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/005.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/006.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/007.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/008.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/009.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/010.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/011.jpg" );
imageTargets.push( "http://www.studenci.de" );

images.push( "http://www.studenci.de/site/fileadmin/template/main/images/skrypt/012.jpg" );
imageTargets.push( "http://www.studenci.de" );

function getImageBar() {
	return document.getElementById( "bar" );
}

function initImages() {
	var bar = getImageBar();
	var firstChild = bar.firstChild;
	var aNode = (firstChild.nodeName == "A") ? firstChild : getNext( firstChild, "A" );
	
	numberImages = 0;
	
	while( aNode != null ) {
		var imgNode = aNode.firstChild;
		if( imgNode.nodeName == "IMG" ) {
			numberImages++;
			setRandomly( imgNode );
			imageNodes.push( imgNode );
		}
		aNode = getNext( aNode, "A" );
	}
	interval = window.setInterval( "setRandomImageRandomly()", 5000 );
}

function getNext( node, tag ) {
	node = node.nextSibling;
	while( node != null ) {
		if( node.nodeName == tag ) return node;
		node = node.nextSibling;
	}
}

function getRandom( range ) {
	return Math.round( Math.random() * (range-1) );
}

function setRandomly( imgNode ) {
	// if( numberImages > images.length ) return;

	var randomNumber = getRandom( images.length );
	
	while( isImageInBar( images[randomNumber] ) ) {
		randomNumber = getRandom( images.length );
	}
	imgNode.src = images[randomNumber];
	imgNode.parentNode.href = imageTargets[randomNumber];
}

function setRandomImageRandomly() {
	var randomNumber = getRandom(numberImages);
	setRandomly( imageNodes[randomNumber] );
}

function isImageInBar( imgSrc ) {
	var imageInBar = false;
	
	for( i=0; i<imageNodes.length; i++ ) {
		if( imageNodes[i].src == imgSrc ) {
			imageInBar = true;
		}
	}
	return imageInBar;
}