// Set the slideshow speed (in milliseconds)
var SlideShowSpeed = 6000;

// Set the duration of crossfade (in seconds)
var CrossFadeDuration = 3;

var cookiename = "defregger";

var Picture = new Array();

Picture[0] = 'images/top/DSC_0109_2.jpg';
Picture[1] = 'images/top/ClipPuPu Springs auf Kopf_1.jpg';
Picture[2] = 'images/top/DSC_0122_2.jpg';
Picture[3] = 'images/top/DSC_1125_1.jpg';
Picture[4] = 'images/top/DSC_0117_2.jpg';
Picture[5] = 'images/top/DSC_0129_2.jpg';
Picture[6] = 'images/top/DSC_0859_Cliffhouse_Bay_3.jpg';
Picture[7] = 'images/top/DSC_0115_2.jpg';
Picture[8] = 'images/top/DSC_1129_1.jpg';
Picture[9] = 'images/top/DSC_1123_1.jpg';
Picture[10] = 'images/top/DSC_1452_Fishing_Blue_3.jpg';
Picture[11] = 'images/top/DSC_0854_Cloudy_Bay_3.jpg';
Picture[12] = 'images/top/DSC_0887_Ignition_3.jpg';
Picture[13] = 'images/top/IMG_0052_1.jpg';
Picture[14] = 'images/top/IMG_0013_1.jpg';
Picture[15] = 'images/top/IMG_0312_1.jpg';
Picture[16] = 'images/top/IMG_0051_1.jpg';
Picture[17] = 'images/top/DSC_0064_Autumn_3.jpg';
Picture[18] = 'images/top/IMG_0061_1.jpg';
Picture[19] = 'images/top/IMG_0065_1.jpg';
Picture[20] = 'images/top/IMG_0317_1.jpg';
Picture[21] = 'images/top/IMG_0333_1.jpg';
Picture[22] = 'images/top/IMG_0344_1.jpg';                                                            
Picture[23] = 'images/top/IMG_0040_ICEING_3.jpg';
Picture[24] = 'images/top/IMG_0155_1.jpg';
Picture[25] = 'images/top/IMG_0254_1.jpg';
Picture[26] = 'images/top/IMG_0250_1.jpg';
Picture[27] = 'images/top/IMG_0022_1.jpg';
Picture[28] = 'images/top/IMG_0016_1.jpg';
Picture[29] = 'images/top/IMG_0024_1.jpg';
Picture[30] = 'images/top/IMG_0334_1.jpg';
Picture[31] = 'images/top/IMG_0050_Universe_4.jpg';
	
var tss;
var cookie = readCookie(cookiename);
var preLoadStart = 0; 
if(cookie != null)
	preLoadStart = parseInt(cookie)+1;
var preLoadEnd = Picture.length;
if (preLoadStart < 0 || preLoadStart >= preLoadEnd)
	preLoadStart = 0;
var showImage = preLoadStart;
var preLoadIndex = preLoadStart;
var preLoadImage = preLoadIndex;
var preLoadCount = 0;
var preLoadInitiated = false;
var pictureBox;
var documentAll;

// preLoad the first picture
var preLoad = new Array();
preLoad[preLoadIndex] = new Image();
preLoadIndex = preLoadIndex + 1;
preLoadCount = preLoadCount + 1;
if (preLoadIndex >= preLoadEnd)
	preLoadIndex = 0;
preLoad[preLoadImage].src = Picture[preLoadImage];

var time;
var startTime;
var endTime;
var bandwidth = 1500;

function preLoadNextImage() {
	// alert("preLoadNextImage: preLoadIndex="+preLoadIndex+" bandwidth="+bandwidth);
	time = new Date();
	startTime = time.getTime();
    preLoad[preLoadIndex] = new Image();
	preLoad[preLoadImage].onload = null;
	preLoadImage = preLoadIndex;
	preLoadIndex = preLoadIndex + 1;
	preLoadCount = preLoadCount + 1;
	if (preLoadIndex >= preLoadEnd)
		preLoadIndex = 0;
	if (preLoadCount < preLoadEnd && bandwidth > 500) 
		preLoad[preLoadImage].onload = function () {setTimeout('preLoadNextImage()',1);};
	else
		preLoad[preLoadImage].onload = null;
	preLoad[preLoadImage].src = Picture[preLoadImage];	
	time = new Date();
	endTime = time.getTime();
	//// calculate download time
	if (startTime == endTime) {
		bandwidth = 1500; // if it's to fast to measure: dummy bandwidth 1500 kbps
	}
	else { 
		bandwidth = 800/((endTime - startTime)/10000); //bandwidth in kbps
	}
}

function runSlideShow(){
	if(!preLoadInitiated) {
		preLoad[preLoadIndex] = new Image();
		preLoadImage = preLoadIndex;
		preLoadIndex = preLoadIndex + 1;
		preLoadCount = preLoadCount + 1;
		if (preLoadIndex >= preLoadEnd)
			preLoadIndex = 0;
		preLoad[preLoadImage].onload = function () {setTimeout('preLoadNextImage()',1);};
		preLoad[preLoadImage].src = Picture[preLoadImage];
		preLoadInitiated = true;
		pictureBox = document.images.PictureBox;
		if(document.all) {
			documentAll = true;
			pictureBox.style.filter="blendTrans(duration=2)";
			pictureBox.style.filter="blendTrans(duration=CrossFadeDuration)";
			if (pictureBox.filters)
				pictureBox.filters.blendTrans.Apply();
			pictureBox.width = 800;
			pictureBox.onload = null;
		}
		else
			documentAll = false;
	}
	pictureBox = document.images.PictureBox;
	if(document.all) {
		pictureBox.style.filter="blendTrans(duration=2)";
		pictureBox.style.filter="blendTrans(duration=CrossFadeDuration)";
		if (pictureBox.filters)
			pictureBox.filters.blendTrans.Apply();
	}
	pictureBox.src = preLoad[showImage].src;
	pictureBox.width = 800;
	if(document.all) {
		if (pictureBox.filters)
			pictureBox.filters.blendTrans.Play();
	}
	createCookie(cookiename, showImage, 1);
	showImage = showImage + 1;
	if (showImage >= preLoadEnd) 
		showImage = 0;
	tss = setTimeout('runSlideShow()', SlideShowSpeed);
}

function currentImage()
{
	document.writeln("<img name=\"PictureBox\" src=\"" + Picture[showImage] + 		
		  "\"width=\"800px\"/>");
}

function currentImageID()
{
	document.write("image="+showImage);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
