window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgsAll = new Array(), imgs = new Array(), zInterval = null, current=0, nIndex=0, pause=false, currTimeout=null;

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if(sPage=='index.php'){
    // HOME PAGE
    var intTimeout = 4000;
}else{
    var intTimeout = 5000;
}

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	// imgs = d.getElementById('slideshow').getElementsByTagName('img');


	imgsAll = d.getElementById('slideshow').getElementsByTagName('div');

	for(i=0;i<imgsAll.length;i++){
		if(imgsAll[i].id.indexOf('slideMe')>=0 && imgsAll[i].id.indexOf('titolo')<0){
			imgs.push(imgsAll[i]);
		}
	}

	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = 'block';

	imgs[0].xOpacity = .99;

	nIndex = imgs[current+1]?current+1:0;


    currTimeout=setTimeout(startFade,intTimeout);
}

function startFade()
{
	nIndex = imgs[current+1]?current+1:0;
	slideshowSwitchImage();
	so_xfade();
}

function startCustomFade(nextImg)
{
	clearTimeout(currTimeout);

	imgs[current].xOpacity = .99;
	setOpacity(imgs[current]);
	imgs[current].style.display = 'block';

	imgs[nIndex].xOpacity = 0;
	setOpacity(imgs[nIndex]);
	imgs[nIndex].style.display = 'none';
	document.getElementById("slideshowBtn"+nIndex).setAttribute("class", "slideshowBtn");

	nIndex=nextImg;

	slideshowSwitchImage();
	so_xfade();
}

function startCustomFadeLabel(nextImg)
{
	clearTimeout(currTimeout);

	imgs[current].xOpacity = .99;
	setOpacity(imgs[current]);
	imgs[current].style.display = 'block';

	imgs[nIndex].xOpacity = 0;
	setOpacity(imgs[nIndex]);
	imgs[nIndex].style.display = 'none';
	document.getElementById("slideshowLabel"+nIndex).setAttribute("class", "slideshowLabel");

	nIndex=nextImg;

	slideshowSwitchImage();
	so_xfade();
}

function slideshowSwitchImage()
{
	/*
	document.getElementById("slideshowBtn"+current).style.color="#000000";
	document.getElementById("slideshowBtn"+current).style.fontSize="10px";
	document.getElementById("slideshowBtn"+nIndex).style.color="#ee0000";
	document.getElementById("slideshowBtn"+nIndex).style.fontSize="13px";
	*/

    // INDEX UTENTE
    if(document.getElementById("slideshowBtn"+current))
	    document.getElementById("slideshowBtn"+current).setAttribute("class", "slideshowBtn");
	if(document.getElementById("slideshowBtn"+nIndex))
        document.getElementById("slideshowBtn"+nIndex).setAttribute("class", "slideshowBtn_sel");

    // INDEX SITO NEWS
    if(document.getElementById("slideshowLabel"+current))
	    document.getElementById("slideshowLabel"+current).setAttribute("class", "slideshowLabel");
	if(document.getElementById("slideshowLabel"+nIndex))
        document.getElementById("slideshowLabel"+nIndex).setAttribute("class", "slideshowLabel_sel");
}

function so_xfade()
{

	cOpacity = imgs[current].xOpacity;
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);
	// forzo alpha per sfondo su IE7
	//document.getElementById('slideMe'+current+'sfondo').style.filter = 'alpha(opacity=70)';

	if(cOpacity<=0 && nOpacity>=1)
	{
		imgs[current].style.display = 'none';
		current = nIndex;
		currTimeout=setTimeout(startFade,intTimeout);
	}
	else
	{
		currTimeout=setTimeout(so_xfade,50);
	}
}

function setOpacity(obj)
{
	if(obj.xOpacity>.99)
	{
		obj.xOpacity = .99;
	}
	if(obj.xOpacity<0)
	{
		obj.xOpacity = 0;
	}

	obj.style.opacity = obj.xOpacity;
	obj.style.MozOpacity = obj.xOpacity;
	obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';

}