// JavaScript Document

//
//	This script requires the AJAX.js file to make the requests
//

var url = "service-slideshow/service-slideshow.xml";
var slideShowImgs = new Array();
var currImage = 0;
var timer;
var imageID = "displayedImage";	// This is the ID you wanna give to your <img> tag
var imageHolder = "slideshow";		// This is the ID of the element to which we'll append your <img> tag
var imageCache = new Array();	// Array to cache images, this is not used for anything else

//onload = getData(url, initSlide); // Add this event to the body of the html for it to work on IE

function initSlide() {
	if(ajaxRequest.readyState == 4) {
		xmldata = ajaxRequest.responseXML;
		buildSlideShow();
	}
}	


function buildSlideShow() {		
		
		// Store all images from XML document in an array
		var slideShow = xmldata.getElementsByTagName("img");
		
		var gallery = xmldata.getElementsByTagName("gallery")[0];
		path = gallery.getAttribute("path");
		
		// Image sources to an predefined array
		for(i=0; i<slideShow.length; i++) {
			var _Src = slideShow[i].getAttribute("src");
			slideShowImgs[i] = path + _Src;
			}
		
		// Cache the images
		for(j=0; j<slideShowImgs.length; j++) {
			imageCache[j]= new Image();
			imageCache[j].src = slideShowImgs[j];
		}
		
		// Create the IMG element and gives it an ID
		var imgBottom = document.createElement("img");
		imgBottom.setAttribute("id", imageID);
		
		// Set initial opacity for the images
		imgBottom.style.opacity = 0;	// Safari and modern browsers
		imgBottom.style.MozOpacity = 0; // Firefox 1.5 and earlier
			if(document.all) {
				imgBottom.style.filter="Alpha(opacity=0)";	// Explorer
			};	

		document.getElementById(imageHolder).appendChild(imgBottom);
	
		transition();
}


var op = 0; // Opacity value
var fading = false;

function transition() {

	// Start the timer
	if(!timer) {timer = setInterval("transition()", 100);}
	
	// Put image into a variable and if it doesn't have its source set yet
	// call the function switchImages to make the first image appear
	var _img = document.getElementById(imageID);
	//if(!_img.src) {switchImages();}		
	

	if(op<1 && fading==false) {
		op = op + .1;
		_img.style.opacity = (_img.style.opacity * 1) + .1;
		_img.style.MozOpacity = (_img.style.MozOpacity * 1) + .1;							 // Firefox 1.5 and earlier
		if(document.all) {
			_img.filters.alpha['opacity'] =(_img.filters.alpha['opacity'] * 1) + 10;
		};
			
			if(op>=1) {
				fading = true;
				op = 1;			
				if(timer) {
					clearInterval(timer);
					timer = null;}
				setTimeout("transition()", 3000);
				}
	}
	
	if(op>0 && fading==true) {
		op = op - .1;
		_img.style.opacity = (_img.style.opacity * 1) - .1;
		_img.style.MozOpacity = (_img.style.MozOpacity * 1) - .1;							 // Firefox 1.5 and earlier
		if(document.all) {
			_img.filters.alpha['opacity'] =(_img.filters.alpha['opacity'] * 1) - 10;
		};
			if(op<=0) { op = 0;}
	}
	
	if(op==0 && fading==true) {
		fading = false;
		switchImages();
	}
	
}

function switchImages() {

	var _img = document.getElementById(imageID);

	_img.src = slideShowImgs[currImage];
	currImage = currImage + 1;
	if(currImage >= slideShowImgs.length) {currImage = 0;}

}