/**********************************************************************
* Enrico : Oggetto rullo per la sostituzione indipendente dei Banner .
***********************************************************************/

/**********************************************************************
* Costruttore
*  @param sIdImgSwap : stringa id della immagine che si vuole sostituire
*  @param arrElemBanner : array di stringhe contenente i banner da ruotare
*  puo' avere questi valori: id,durata,tipo,width,heigth,descrizione
*  es.var arrElemBanner = new Array();
*  arrElemBanner[0]= new Array ('UrlImmagine','durata' );
*/
function bannerRotante(sIdImgSwap, arrElemBanner)
{
	this.i = 0;
	this.idInterval = 0 ;
	this.swapDurata(arrElemBanner);
	this.getSwap(this, this.getElement(), sIdImgSwap);
};
/***********************************************************************
* Rullo - estrae elementi dall'array circolare, gestisce i timer
* @param idImgSwap : id dell'immagine da sostituire
*/
bannerRotante.prototype.rullo = function(idImgSwap)
{
	// Estraggo le img dall'array circolare
	var arrEl = this.getElement();
	var sDurata = arrEl[1];

	// Pulizia dell'intervallo in caso sia utilizzato
	var objRullo = this;
	var func = function(){ objRullo.getSwap(objRullo, arrEl, idImgSwap) };
	clearTimeout( this.idInterval );
	// Controlla che la durata sia maggiore di 0, altrimenti la rotazione non deve andare avanti
	if( sDurata > 0 )
		this.idInterval = setTimeout(func, sDurata);
};
/***********************************************************************
* Esegue lo swap dell'immagine
*  @param objRullo : da usare per la ricorsione
*  @param sPathImage : percorso della nuova immagine
*  @param idImgSwap : id dell'immagine che devo sotituire con la nuova
*  @param widthSwf : largezza dell'immagine
*  @param heightSwf : altezza dell'immagine
*  @param descrImg : descrizione dell'immagine che devo sotituire con la nuova
*  @param visImg : indica se devo visualizzare la descrizione dell'immagine
*  @param linkImg : link dell'immagine
*  @param targetImg : target dell'immagine
*  @param classImg : classe dell'immagine
*/
bannerRotante.prototype.swapImages = function (objRullo, sPathImage, idImgSwap, widthImg, heightImg, descrImg, visImg, linkImg, targetImg, classImg)
{
	if( descrImg == null || descrImg == void(0) )
		descrImg = '';
	if( widthImg == null || widthImg == void(0) || widthImg == '' )
		widthImg = '100';
	if( heightImg == null || heightImg == void(0) || heightImg == '' )
		heightImg = '100';
	if( targetImg != null && targetImg != void(0) && targetImg != '' )
		targetImg = "target=\"" + targetImg + "\"";
	else
		targetImg = "";

	// Se non c'e' il link non metto il tag <a href="" />
	if( linkImg == null || linkImg == '' || linkImg == void(0) )
		document.getElementById(idImgSwap).innerHTML = '<img id="' + idImgSwap + '" src="' + sPathImage + '" width="' + widthImg + '" height="' + heightImg + '" border="0" alt="' + descrImg + '" title="' + descrImg + '" />';
	else
		document.getElementById(idImgSwap).innerHTML = '<a href="' + linkImg + '" ' + targetImg + '><img id="' + idImgSwap + '" src="' + sPathImage + '" width="' + widthImg + '" height="' + heightImg + '" border="0" alt="' + descrImg + '" title="' + descrImg + '" /></a>';

	if( classImg == null || classImg == void(0) || classImg == '' )
		jQuery('#'+idImgSwap).attr('class', 'banner');
	else
		jQuery('#'+idImgSwap).attr('class', classImg);

	// Imposto la descrizione sotto il banner, che abbia valore '' o altro non importa.
	// Controllo anche che la descrizione non debba essere visualizzata con visImg == 1
	if( document.getElementById(idImgSwap+'_desc') && visImg == 1 )
		document.getElementById(idImgSwap+'_desc').innerHTML = descrImg;
	else if( document.getElementById(idImgSwap+'_desc') )
		document.getElementById(idImgSwap+'_desc').innerHTML = '';

	objRullo.rullo(idImgSwap);
};
/***********************************************************************
* Esegue lo swap del html
*  @param objRullo : da usare per la ricorsione
*  @param sPathHtml : percorso del html
*  @param idHtmlSwap : id del div che contiene html che devo sotituire con il nuovo
*  @param classHtml : classe del html
*/
bannerRotante.prototype.swapHtml = function (objRullo, sPathHtml, idHtmlSwap, classHtml)
{
	document.getElementById(idHtmlSwap).innerHTML = sPathHtml;
	if( classHtml == null || classHtml == void(0) || classHtml == '' )
		jQuery('#'+idHtmlSwap).attr('class', 'banner_script');
	else
		jQuery('#'+idHtmlSwap).attr('class', classHtml);

	// Siccome html non ha descrizione, viene messa sempre a vuoto.
	if( document.getElementById(idHtmlSwap+'_desc') )
		document.getElementById(idHtmlSwap+'_desc').innerHTML = '';

	objRullo.rullo(idHtmlSwap);
};
/***********************************************************************
* Esegue lo swap del flash
*  @param objRullo : da usare per la ricorsione
*  @param sPathSwf : percorso del nuovo flash
*  @param idSwfSwap : id del flash che devo sotituire con il nuovo
*  @param widthSwf : largezza del swf
*  @param heightSwf : altezza del swf
*  @param classSwf : classe del swf
*/
bannerRotante.prototype.swapSwf = function (objRullo, sPathSwf, idSwfSwap, widthSwf, heightSwf, classSwf)
{
	if( widthSwf == null || widthSwf == void(0) || widthSwf == '' )
		widthSwf = '100';
	if( heightSwf == null || heightSwf == void(0) || heightSwf == '' )
		heightSwf = '100';

	document.getElementById(idSwfSwap).innerHTML = '<object type="application/x-shockwave-flash" data="' + sPathSwf + '" width="' + widthSwf + '" height="' + heightSwf + '">' +
		'<param name="allowScriptAccess" value="sameDomain" />' +
		'<param name="movie" value="' + sPathSwf + '" />' +
		'<param name="quality" value="high" />' +
		'</object>';

	if( classSwf == null || classSwf == void(0) || classSwf == '' )
		jQuery('#'+idSwfSwap).attr('class', 'banner');
	else
		jQuery('#'+idSwfSwap).attr('class', classSwf);

	// Siccome swf non ha descrizione, viene messa sempre a vuoto.
	if( document.getElementById(idSwfSwap+'_desc') )
		document.getElementById(idSwfSwap+'_desc').innerHTML = '';

	objRullo.rullo(idSwfSwap);
};
/***********************************************************************
* Simulo estrazione da array circolare.
*/
bannerRotante.prototype.getElement = function()
{
	if( this.i >= this.banner.length )
		this.i = 0;
	this.arr = this.banner[this.i];
	this.i++;
	return this.arr;
};
/***********************************************************************
* Sistema le durate dei vari banner inserendo ad ogni banner la durata dell'elemento successivo.
* Nel caso del primo elemento prendo la propria durata e nel caso dell'ultimo la durata del primo.
* @param arrElemBanner : array di stringhe da modificare
* es. prima: a1 b4 c10  dopo: a10 b1 c4
* arrFirstEl[0] = sPathImage
* arrFirstEl[1] = durata
* arrFirstEl[2] = tipoSwap
* arrFirstEl[3] = widthImg
* arrFirstEl[4] = heightImg
* arrFirstEl[5] = descrImg
* arrFirstEl[6] = visImg
* arrFirstEl[7] = linkImg
* arrFirstEl[8] = targetImg
* arrFirstEl[9] = classImg
*/
bannerRotante.prototype.swapDurata = function(arrElemBanner)
{
	var arrTempElemBanner = new Array();
	// Estraggo le img dall'array circolare
	var arrFirstEl = arrElemBanner[0];
	var arrLastEl = arrElemBanner[arrElemBanner.length-1];
	arrTempElemBanner[0] = new Array(arrFirstEl[0], arrLastEl[1], arrFirstEl[2], arrFirstEl[3], arrFirstEl[4], arrFirstEl[5], arrFirstEl[6], arrFirstEl[7], arrFirstEl[8], arrFirstEl[9]);
	for( y=1; y<arrElemBanner.length; y++)
	{
		var arrCorrEl = arrElemBanner[y];
		var arrPrecEl = arrElemBanner[y-1];
		arrTempElemBanner[y] = new Array(arrCorrEl[0], arrPrecEl[1], arrCorrEl[2], arrCorrEl[3], arrCorrEl[4], arrCorrEl[5], arrCorrEl[6], arrCorrEl[7], arrCorrEl[8], arrCorrEl[9]);
	}
	this.banner = arrTempElemBanner;
};
/***********************************************************************
* In base al tipo di swap passato scelgo una delle tre funzioni.
* tipoSwap : specifica il tipo di swap da utilizzare puo' assumere questi valori: html,swf,img
* @param objRullo : da usare per la ricorsione
* @param arrElemBanner : array di stringhe contenente i banner da ruotare
* @param idSwap : id dell'oggetto che devo sotituire
*/
bannerRotante.prototype.getSwap = function(objRullo, arrElemBanner, idSwap)
{
	var tipoSwap =  arrElemBanner[2];
	if( tipoSwap == 'img' )
		this.swapImages(objRullo, arrElemBanner[0], idSwap, arrElemBanner[3], arrElemBanner[4], arrElemBanner[5], arrElemBanner[6], arrElemBanner[7], arrElemBanner[8], arrElemBanner[9]);
	else if( tipoSwap == 'html' )
		this.swapHtml(objRullo, arrElemBanner[0], idSwap, arrElemBanner[9]);
	else if( tipoSwap == 'swf' )
		this.swapSwf(objRullo, arrElemBanner[0], idSwap, arrElemBanner[3], arrElemBanner[4], arrElemBanner[9]);
	else
		alert('Tipo swap non supportato: ' + tipoSwap);
};
