/**
 * @author huw.edwards
 */

function slideShowFromXML( xmlPathAndFileName, slidePath, width, height, slideDelay )
{
	var imageArray = loadXML( xmlPathAndFileName, slidePath);
	//Select a slide at random to start the page and to give it an img object to work with
	var numSlides = imageArray.length;
	var randomSlideNum = randomFromTo( 0, (numSlides - 1));
	var randomSlideName = imageArray[ randomSlideNum ]; //the loadXML function adds the full URL path
	document.writeln('<img width=' + width + 'height=' + height + ' id="myImage" name="myImage" src="' + randomSlideName + '"></IMG>');
	// Wait before showing the rest of the sequence
	setTimeout(function()
	{
		showSlides(imageArray, slideDelay)
	}, slideDelay); // 1000 = 1 second		
}


function showSlides(slideArray, slideDelay)
{
	var slideNum;
	var nextSlide;
	
	nextSlide = new Image();
	slideNum = randomFromTo(0, (slideArray.length - 1));
	//document.write('Random slide number:' + slideNum + '<br>');
	//document.write('Slide title is: ' + slideArray[slideNum]);
	nextSlide.src = slideArray[slideNum];
	showImage(nextSlide);
	//Now call function again after a delay
  	setTimeout(function()
  	{
  		showSlides( slideArray, slideDelay ); slideArray=null; slideDelay=null
  	}, slideDelay); // example uses 5000		
}


/*
function showSlides( slideArray, slideDelay )
{
	showImage( image );
  	swapPicture( slideArray, slideDelay )

}
*/

 function showImage( slide )
 {
  //document.write('In showImage. slide = ' + slide ); 

  if (document.images)
  {
  	//document.write('document.images is true' ); 
 	if (slide && slide.complete) // it exists as an object and has finished loading
    {
  	  //document.write('slide.complete = ' + slide.complete );
	  //document.write('document.images.myImage = ' + document.images.myImage)
      var target=0;
      if (document.images.myImage) 
	  {
		//document.write('document.images.myImage is true'); 
	  	target = document.images.myImage;  // this is a reference to the ID in the example HTML <IMG WIDTH=250 HEIGHT=250 ID="myImage" NAME="myImage"  SRC="images/image0.gif"></IMG>
	  }
      if (document.all && document.getElementById("myImage")) 
	  {
		//document.write('Setting target'); 
	  	target = document.getElementById("myImage");
	  }
      // make sure target is valid.  It might not be valid if the page has not finished loading
      if (target)
      {
		target.src= slide.src;
      }	  
    }
    else    
	{
//		document.write('Couldnt find document.images'); 
		setTimeout(function()
		{
	  	showImage( slide ); slide=null
		}, 500); // try again in half a second
    }
   
  }
}
 
function loadXML( xmlURL, imagePath )
{
	///Go and get image list from XML
	if (window.XMLHttpRequest) 
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else 
	{// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", xmlURL, false);
	xmlhttp.send();
	xmlDoc = xmlhttp.responseXML;
	
	var i, n_imagelist;
//	var imagelist = xmlDoc.getElementsByTagName("image");
	var imagelist = xmlDoc.getElementsByTagName("img");
	n_imagelist = imagelist.length;
//	document.write('Number of images:' + n_imagelist + "<br>");
	var myArray = new Array(n_imagelist);
	
	var strObject = '';
	for (i = 0; i < n_imagelist; i++) 
	{
//		document.writeln(imagelist[i].attributes.getNamedItem("src").value );
//		strObject = 'http://www.camillindenny.com/gallery/home/lg/' + imagelist[i].firstChild.nodeValue;
		strObject = imagePath + imagelist[i].attributes.getNamedItem("src").value;
//		document.writeln( strObject + "<br/>");
		myArray[i] = strObject;
	}
	
	return myArray;	
}
 
 /*
  * UTILITIES
  */
 
 function randomFromTo(from, to)
 {
       return Math.floor(Math.random() * (to - from + 1) + from);
 }
