//GENERIC ADDLOAD EVENT FUNCTION
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
//////////////////////////////////

//Images and alt text - needs to be the same number in each
var images = new Array('img/waterfall-near-hotel.jpg','img/whitby_pier.jpg','img/steam_train.jpg','img/scarborough.jpg','img/fylingdales.jpg');
var alttext = new Array('The waterfall from which the Salmon Leap takes its name','The pier at Whitby harbour entrance','Steam trains on the North Yorkshire Moors Railway','A view of Scarborough, looking down from the Castle','Beautiful North Yorkshire countryside with the Fylingdales listening post in the distance.');

//Store current image number
var currentIndex = 0;
var nextIndex = 1;
var timeOutDelay = 4000;//Delay in milliseconds
var currentOpacity = new Array();
var FADE_STEP = 2;
var FADE_INTERVAL = 20;
var pause = false;
var browsertype;
var COUNTER;

//Initialise slideshow if the objects exist for it to work
function startSlideshow() {
	if (!document.getElementsByTagName || !document.getElementById || !document.getElementById('slideshow')) {
		return false;
	}
	
	currentOpacity[0]=99;
	for(i=1; i<images.length; i++) {
		currentOpacity[i]=0;
	}

	var slideshow = document.getElementById('slideshow');
	//remove existing image
	//slideshow.removeChild(slideshow.firstChild);
	var agt = navigator.userAgent.toLowerCase();
	var is_opera = (agt.indexOf("opera") != -1);
	//Browser check
	if(document.all && !is_opera) {
		browsertype = 'ie';
		//alert('ie');
	} else if (document.all && is_opera) {
		browsertype = 'opera';
		//alert('opera');
	} else {
		browsertype = 'moz';
		//alert('moz');
	}
	
	for(i=0; i<images.length; i++) {
		
		var newimage = document.createElement('IMG');
		newimage.setAttribute('id','image' + i);
		newimage.setAttribute('src',images[i]);
		newimage.setAttribute('alt',alttext[i]);
		newimage.className = "imgfade";
		
		slideshow.appendChild(newimage);
		
		if (browsertype != 'opera') {
			setOpacity(newimage, 0);
		} else {
			newimage.style.visibility = 'hidden';
		}
	}

	if (browsertype != 'opera') {
		setOpacity(document.getElementById('image'+currentIndex), 100);
	} else {
		document.getElementById('image'+currentIndex).style.visibility = 'visible'
	}

	COUNTER = setTimeout("swapImage()",timeOutDelay);
}

function swapImage() {
	COUNTER = 0;
	if (browsertype != 'opera') {
		doFade =  setInterval("crossFade()",FADE_INTERVAL);
	} else {
		document.getElementById('image'+currentIndex).style.visibility = 'hidden';
		document.getElementById('image'+nextIndex).style.visibility = 'visible';
		currentIndex = nextIndex;
		nextIndex++;
		if(nextIndex == images.length) { 
			nextIndex=0;
		}
	}
	COUNTER = setTimeout("swapImage()",timeOutDelay);
}

function crossFade() {
	currentOpacity[currentIndex]-=FADE_STEP;
	currentOpacity[nextIndex] += FADE_STEP;

	setOpacity(document.getElementById('image'+currentIndex), currentOpacity[currentIndex]);
	setOpacity(document.getElementById('image'+nextIndex), currentOpacity[nextIndex]);

	if(currentOpacity[nextIndex]/100>=.98) {
		currentIndex = nextIndex;
		nextIndex++;
		window.clearInterval(doFade);
		clearInterval(doFade);
		if(nextIndex == images.length) { 
			nextIndex=0;
		}
	}
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter = "alpha(opacity:"+opacity+")";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
}

//Add onload event to page
addLoadEvent(startSlideshow);
