var images = new Array(
		["hn-mainpic1.jpg", "describing text for image one", "Bild1"],
		["hn-mainpic2.jpg", "describing text for image two", "Bild2"],
		["hn-mainpic3.jpg", "describing text for image three", "Bild3"],
["hn-mainpic4.jpg", "describing text for image three", "Bild4"],
["hn-mainpic5.jpg", "describing text for image three", "Bild5"],
["hn-mainpic6.jpg", "describing text for image three", "Bild6"]
		 );

var speed = 30;

var interval = 6000;

var index = oldIndex = 0;

function start() {
	createBackgroundContainer(document.getElementById('wechselbild'));
	setTimeout('startFading()', interval);
}

function createBackgroundContainer(element) {
	var image = document.createElement('img');
	image.setAttribute('id', 'wechselbild');
	image.setAttribute('src', element.src);
	image.setAttribute('alt', element.alt);
	image.style.width = element.width + 'px';
	image.style.height = element.height + 'px';
	image.style.margin = '0';
	
	var backgroundContainer = document.createElement('span');
	backgroundContainer.setAttribute('id', 'backgroundContainer');
	backgroundContainer.style.display = 'block';
	backgroundContainer.style.margin = '0 auto 0 auto';
	backgroundContainer.style.padding = '0';
	backgroundContainer.style.width = element.width + 'px';
	backgroundContainer.style.height = element.height + 'px';
	backgroundContainer.appendChild(image);
	
	var parent = element.parentNode;
	parent.replaceChild(backgroundContainer, element);
}

function startFading() {
	var image = document.getElementById('wechselbild');
	index = Math.round(Math.random() * (images.length - 1));
	while (oldIndex == index) {
		index = Math.round(Math.random() * (images.length - 1));
	}
	oldIndex = index;

	var backgroundContainer = document.getElementById('backgroundContainer');
	backgroundContainer.style.backgroundImage = "url('" + document.getElementById('wechselbild').src + "')";
	
	image.style.opacity = 0;
	image.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	image.setAttribute('src', images[index][0]);
	image.setAttribute('alt', images[index][1]);
	image.setAttribute('title', images[index][2]);
	
	for (var i = 0; i < 100; i++) {
		setTimeout('fadeIn(' + i + ')', speed*i);
	}
	setTimeout("startFading()", interval);
}

function fadeIn(i) {
	document.getElementById('wechselbild').style.opacity = i/100;
	document.getElementById('wechselbild').style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + i + ")";
}

