// JavaScript Document
// Dial slideshow home page
// (c) copyright 2010 mauro gallo | web creations
// all rights reserved

var slides, xmlhttp;
var intervallo = 10000;

function initSlides() {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else {
		var msXHR = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
		for (var i = 0; i < msXHR.length; i++) {
			try {
				xmlhttp = new ActiveXObject(msXHR[i]);
				break;
			} catch (e) {}
		}
	}
	if (xmlhttp) {
		var milliDate = new Date();
		xmlhttp.open("GET", "ajax/slidesArray.php?fie=" + milliDate.getMilliseconds(), true);
		xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					slides = eval("(" + xmlhttp.responseText + ")");
					startShow(getRandomIndex(slides));
				}
		};
		xmlhttp.send(null); 
	}
}

function startShow(pointer) {
	if (slides) {
		var banner = document.getElementById("banner2");
		var anchorPix = banner.getElementsByTagName("a");
		var pix = anchorPix[0].getElementsByTagName("img");
		if (pointer >= slides.length)
			pointer = 0;
		var nextImg = new Image();
		nextImg.onload = function() {
			pix[0].setAttribute("src", slides[pointer].pix);
			pix[0].setAttribute("alt", slides[pointer].title);
			anchorPix[0].setAttribute("href", slides[pointer].url);
			anchorPix[0].setAttribute("title", slides[pointer].title);
			setTimeout(function () {startShow(++pointer);}, intervallo);
		}
		nextImg.src = slides[pointer].pix;
	}
}

function getRandomIndex(array) {
	while (true) {
		var index = Math.random() * 10;
		index = Math.floor(index);
		if (index < array.length) 
			return index;
	}
}
