var imageId = 'jsGallery';
var image = $(imageId);
var xmlHttp;
var index = 0;
var images;
var imagePath = "graphics/jsGalleryImages/";

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function initGallery() { 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
		  alert ("Your browser does not support AJAX!");
		  return;
	  }
	var url="cms/modules/jsGallery.php";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() {
	if(xmlHttp.readyState==4)
	{
		images = xmlHttp.responseText.split(",");
		image = $(imageId);
		image.src = imagePath + images[0];
		
		/* Preload other images */
		for(var i=0; i < images.length; i++) {
			MM_preloadImages(imagePath + images[i]);
		}
		
		window.setInterval(function() { slideshow(); }, 10000);

	}
}

function slideshow() {
	$(imageId).fade("out");
	window.setTimeout("nextImage()", 450); // Wait for fade to end then load next image
}
 
function nextImage() {
	$(imageId).fade("in");
	if(index < images.length - 1) {
		image.src = imagePath + images[index + 1];
		index++;
	} else {
		image.src = imagePath + images[0];
		index = 0;
	}
}
 
function previousImage() {
	if(index > 0) {
		image.src = imagePath + images[index - 1];
		index--;
	} else {
		image.src = imagePath + images[images.length - 1];
		index = images.length - 1;
	}
}