window.addEventListener
? /* standard */ window.addEventListener ("load", initScorrevoli, false)
: /* IE */ window.attachEvent ("onload", initScorrevoli);

image_element_name = "scorrevoli";
N_SCORREVOLI = 10; // number of slides
DURATA_SCORREVOLE = 6000; // speed (duration of one slide, including transition)
SCORREVOLE_PASSAGGIO = 3600; // duration of transition

var scorrevoli; // array of slides
var scorrevole_primo = 0; // index of first slide
var contatore_scorrevoli = 0; // current count of slides shown

function initScorrevoli() {
	loadScorrevoli();
	scorrevole_primo = Math.floor (Math.random() * N_SCORREVOLI);
	showNextScorrevole();
}

function loadScorrevoli() {
	if (scorrevoli == null) {
		scorrevoli = new Array (N_SCORREVOLI);
	}
	for (var i = 0; i < N_SCORREVOLI; i++) {
		scorrevoli[i] = new Image();
		scorrevoli[i].src = "/img/scorrevoli/"+(i+1)+".jpg";
	}
	shuffle (scorrevoli);
}

function showNextScorrevole() {

	slide = (scorrevole_primo + contatore_scorrevoli) % N_SCORREVOLI;
	contatore_scorrevoli++;
	
	var timeout = DURATA_SCORREVOLE;
	if (contatore_scorrevoli == 1) {
	    document.images["scorrevoli"].src = scorrevoli[slide].src;
	    timeout -= SCORREVOLE_PASSAGGIO;
	} else {
		crossfade (document.images["scorrevoli"], scorrevoli[slide].src, SCORREVOLE_PASSAGGIO, '');
	}
	
	slide_timeout_state = setTimeout ("showNextScorrevole()", timeout);
}

//--------------------------------------------------------------------

// Fisher-Yates in-place shuffle
function shuffle (anArray) {
  var j, x;
  for (var i = anArray.length - 1; i > 0; i--) {
    j = parseInt (Math.random() * (i + 1));
    x = anArray[i];
    anArray[i] = anArray[j];
    anArray[j] = x;
  }
  return anArray;
}

//--------------------------------------------------------------------

// *****************************************************
// Based on IXF1.11:: Image cross-fade
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
// global object
var xf = { 'clock' : null, 'count' : 1 };
//******************************************************

// crossfade setup function
function crossfade (imgObj, newImgSrc, fadeMillis, imgAltText)
{
	// if the timer is not already going
	if (xf.clock == null) {
		// copy the arguments
		xf.obj = imgObj;
		xf.src = newImgSrc;
		
		// change the image alt text if defined
		if (imgAltText != null
		    && typeof imgAltText != 'undefined'
		    && imgAltText != '') {
			xf.obj.alt = imgAltText;
		}
		
		// find out which form of opacity is supported
		if (typeof xf.obj.style.opacity != 'undefined') {
			xf.type = 'w3c';
		}
		else if (typeof xf.obj.style.MozOpacity != 'undefined') {
			xf.type = 'moz';
		}
		else if (typeof xf.obj.style.KhtmlOpacity != 'undefined') {
			xf.type = 'khtml';
		}
		else if (typeof xf.obj.filters == 'object'
		         && xf.obj.filters.length > 0
			     && typeof xf.obj.filters.alpha == 'object'
			     && typeof xf.obj.filters.alpha.opacity == 'number') {
			xf.type = 'ie';
		}
		else {
			xf.type = 'none';
		}
		
		// if any kind of opacity is supported
		if (xf.type != 'none') {
		
		    xf.newImgObj = xf.createImageObject();
			
			// set src to new image src
			xf.newImgObj.src = xf.src
			
			// create fade resolution argument as 20 steps per transition
			xf.resolution = Math.round (fadeMillis / 1000 * 20);
			
			// adjust fade length to be an integer multiple of the resolution
			xf.length = Math.round (fadeMillis / xf.resolution) * xf.resolution;
			
			// start the timer
			xf.clock = setInterval ('xf.crossfade()', xf.length/xf.resolution);
		}
		
		// otherwise if opacity is not supported
		else {
			// just do the image swap
			xf.obj.src = xf.src;
		}
	}
};

xf.createImageObject = function() {
    var newImg;
	if (typeof document.createElementNS != 'undefined') {
		newImg = document.getElementsByTagName ('body') [0].appendChild
			(document.createElementNS ('http://www.w3.org/1999/xhtml', 'img'));
	} else if (typeof document.createElement != 'undefined') {
		newImg = document.getElementsByTagName ('body') [0].appendChild
			(document.createElement ('img'));
	} else {
		newImg = null;
	}
	
	newImg.style.position = 'absolute';
    newImg.style.zIndex = '9999';
    newImg.style.visibility = 'hidden';

	// move image to superimpose original image
	newImg.style.left = getRealPosition (xf.obj, 'x') + 'px';
	newImg.style.top = getRealPosition (xf.obj, 'y') + 'px';
    
	return newImg;
};

// crossfade timer function
xf.crossfade = function()
{
	// decrease the counter on a linear scale
	xf.count -= (1 / xf.resolution);
	
	// if the counter has reached the bottom
	if (xf.count < (1 / xf.resolution)) {
		// clear the timer
		clearInterval (xf.clock);
		xf.clock = null;
		
		// reset the counter
		xf.count = 1;
		
		// set the original image to the src of the new image
		xf.obj.src = xf.src;
	}
	
	// set new opacity value on both elements
	// using whatever method is supported
	switch (xf.type) {
		case 'ie':
			xf.obj.filters.alpha.opacity = xf.count * 100;
			xf.newImgObj.filters.alpha.opacity = (1 - xf.count) * 100;
			break;
			
		case 'khtml':
			xf.obj.style.KhtmlOpacity = xf.count;
			xf.newImgObj.style.KhtmlOpacity = (1 - xf.count);
			break;
			
		case 'moz': 
			// restrict max opacity to prevent a visual popping effect in firefox
			xf.obj.style.MozOpacity = (xf.count == 1 ? 0.9999999 : xf.count);
			xf.newImgObj.style.MozOpacity = (1 - xf.count);
			break;
			
		default: 
			// restrict max opacity to prevent a visual popping effect in firefox
			xf.obj.style.opacity = (xf.count == 1 ? 0.9999999 : xf.count);
			xf.newImgObj.style.opacity = (1 - xf.count);
	}
	
	// keep new image in position with original image
	// in case text size changes mid transition or something
	xf.newImgObj.style.left = getRealPosition (xf.obj, 'x') + 'px';
	xf.newImgObj.style.top = getRealPosition (xf.obj, 'y') + 'px';
	
	// now that we've gone through one fade iteration 
	// we can show the image that's fading in
	xf.newImgObj.style.visibility = 'visible';
	
	// if the counter is at the top, which is just after the timer has finished
	if (xf.count == 1) {
		// remove the duplicate image
		xf.newImgObj.parentNode.removeChild (xf.newImgObj);
	}
};

// function getRealPosition
// Arguments:
// obj the object of which to get the position coordinate
// whichCoord one of 'x' or 'y'
function getRealPosition (obj, whichCoord) {
	pos = (whichCoord == 'x') ? obj.offsetLeft: obj.offsetTop;
	par = obj.offsetParent;
	while (par != null) {
	    pos += (whichCoord == 'x') ? par.offsetLeft: par.offsetTop;
	    par = par.offsetParent;
	}
	return pos;
}

