/**
 * GESTIONE FADE DI ELEMENTI
 *
 * @author Emanuele Ravasi http://www.vipsrl.com
 * @copyright Vip Srl 2/10/2009
 */


/**
 * Cambia l'opacità a comando, con parametri.
 *
 * @param id Identificativo dell'elemento
 * @param opacStart Valore da cui comincia il fade
 * @param opacEnd Valore a cui termina il fade
 * @param millisec Durata del fade
 */
function opacity(id, opacStart, opacEnd, millisec)
{
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd)
		for(i = opacStart; i >= opacEnd; i--)
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	else if(opacStart < opacEnd)
		for(i = opacStart; i <= opacEnd; i++)
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
}

/**
 * Cambia l'opacità per browser differenti.
 *
 * @param opacity Valore di opacità
 * @param id Identificativo dell'elemento
 */
function changeOpac(opacity, id)
{
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}


/**
 * Cambia l'opacità con logica switch: se un elemento è invisibile lo rende invisibile, altrimenti fa il contrario.
 *
 * @param id Identificativo dell'elemento
 * @param millisec Durata del fade
 */
function shiftOpacity(id, millisec)
{
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0)
		opacity(id, 0, 100, millisec);
	else
		opacity(id, 100, 0, millisec);
}


/**
 * Fade da un'immagine all'altra.
 *
 * @param divid
 * @param imageid
 * @param imagefile
 * @param millisec Durata del fade
 */
function blendimage(divid, imageid, imagefile, millisec)
{
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	//make image transparent
	changeOpac(0, imageid);
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++)
	{
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}


/**
 * @param id Identificativo dell'elemento
 * @param opacEnd
 * @param millisec Durata del fade
 */
function currentOpac(id, opacEnd, millisec)
{
	//standard opacity is 100
	var currentOpac = 100;

	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100)
		currentOpac = document.getElementById(id).style.opacity * 100;

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}


/**
 * Mostra una dopo l'altra diverse immagini contenute nell'array, con fading tra le immagini.
 *
 * @param idDiv Identificativo del div contenitore
 * @param idImg Identificativo dell'immagine
 * @param aImmagini Array contenente le immagini da mostrare
 * @param indice Indice da incrementare per passare da un'immagine all'altra
 * @param milliSecBlend Durata del fade
 * @param milliSecTimeout Durata del timeout
 *
 * Codice esempio:
 * <div style="background-image: url(immagini/prima_immagine.jpg); background-repeat: no-repeat; width: 30px; height: 15px;" id="blenddiv">
 *	<img style="filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0;" id="blendimg" src="immagini/prima_immagine.jpg" alt="" />
 * </div>
 *
 * <script>
 * 	var aImmagini = new Array();
 * 	aImmagini[0] = "immagini/minilogo_vip_sintel.gif";
 * 	aImmagini[1] = "immagini/minilogo_vip_segrino.gif";
 * 	aImmagini[2] = "immagini/minilogo_vip_tentori.gif";
 * 	blendRotation('blenddiv', 'blendimg', aImmagini, 0, 200, 2000);
 * </script>
 */
function blendRotation(idDiv, idImg, aImmagini, indice, milliSecBlend, milliSecTimeout)
{
	//alert(stato + "/" + stati);
	if (indice == aImmagini.length) indice = 0;

	blendimage(idDiv, idImg, aImmagini[indice], milliSecBlend);
	indice++;
	setTimeout("blendRotation('" + idDiv + "', '" + idImg + "', aImmagini, " + indice + ", " + milliSecBlend + ", " + milliSecTimeout + ")", milliSecTimeout);
}
