// JavaScript Document
// 12-10-2006 Aggiunta gestione fade
	var aBnrManagers = new Array(); // Array con i gestori dei banner.
	var bnrMan = null;				

// Nota: Non uso le collezioni di immagini e link per ritrovare gli elementi nella pagina 
// a causa di una implementazione errata in Internet Explorer della collezione links: essa
// sembra non supportare la scelta di un elemento con le parentesi quadre.	
	function setOpacity(obj,opacity){
		opacity=(opacity==100) ? 99.999 : opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")";  // IE/Win
		obj.style.KHTMLOpacity = opacity/100; // Safari<1.2, Konqueror
		obj.style.MozOpacity = opacity/100;   // Older Mozilla and Firefox
		obj.style.opacity = opacity/100;      // Safari 1.2, newer Firefox and Mozilla, CSS3
	}

	function getBannerElement(id) {
	// Tenta con getElementByID
		if(document.getElementById) 
			return document.getElementById(id);	
	// e poi con document.all	
		else if(document.all) 
			return document.all[id];	
		return null;
	}

// Oggetto Banner
	function Banner(mn, fn, w, h, tit, lnk, trg, tt, id) {
		this.manager = mn;
		this.bnrImage = new Image(w, h);  // Pre-caching dell'immagine.
		this.bnrImage.src = fn;
		this.title = tit;
		this.url = lnk;
		this.target = trg;
		this.transTime = tt;
		this.ShowBanner = ShowBanner;
		this.id = id;
	}
	
	function ShowBanner() {
		var img = getBannerElement('pic' + this.manager.placeCode);
		var lnk = getBannerElement('link' + this.manager.placeCode);		
	// Sostituisce l'immagine solo se il caricamento è completato.
		if (this.bnrImage && this.bnrImage.complete && img && lnk) {   
			with (img) {
				src = this.bnrImage.src;
				if (width != this.bnrImage.width) // Cerca di evitare lampeggiamenti inutili
					width = this.bnrImage.width;
				if (height !=  this.bnrImage.height) 
					height = this.bnrImage.height;
				alt = this.title;
			}
			with (lnk) {
				href = this.url;
				target = this.target;
				title = this.title;			
			}
		}
	}
		
// Oggetto BnrManager
	function BnrManager(placeCode) {
		this.placeCode = placeCode;
		this.actPos = 0;	
		this.halted = false;
		this.aBanners = new Array();
		this.AddBanner = AddBanner;
		this.StartSlideshow = StartSlideshow;
		this.ShowNextBanner = ShowNextBanner;
		this.fadeOn = fadeOn;
		this.fadeOff = fadeOff;
	}
	
	function AddBanner(fn, w, h, tit, lnk, trg, tt, id) {
		this.aBanners[this.aBanners.length] = new Banner(this, fn, w, h, tit, lnk, trg, tt, id);
	}
			
	function fadeOn(objId,opacity) 
	{
		if(!document.getElementById) return;
		obj = document.getElementById(objId);
		if(opacity <= 100){
	 	  if(!this.halted) {
		 	setOpacity(obj,opacity);
		  } else
		  	opacity = 100;
		  opacity += 10;
		  var methodName = "aBnrManagers['" + this.placeCode + "'].fadeOn('" + objId + "'," + opacity + ");";
		  setTimeout(methodName, 30);
		  
		} else {
			var methodName = "aBnrManagers['" + this.placeCode + "'].fadeOff('" + objId + "',100);";
			var tt = this.aBanners[this.actPos].transTime * 1000; // Valore in ms.
			setTimeout(methodName,tt);
		}
	}
	
	function fadeOff(objId,opacity) {
		if(!document.getElementById) return;
		obj = document.getElementById(objId);
		if(opacity >= 0){
   		  if(!this.halted) {
			setOpacity(obj,opacity);
		  } else
		  	opacity = 100;		  	
		  opacity -= 10;
		  var methodName = "aBnrManagers['" + this.placeCode + "'].fadeOff('" + objId + "'," + opacity + ");";
		  setTimeout(methodName, 60);
		} else {
			var methodName = "aBnrManagers['" + this.placeCode + "'].ShowNextBanner(true);";
			setTimeout(methodName,0);
		}
	}
	
	function ShowNextBanner(fade) {	
		if (this.aBanners.length < 2) return; // Nessuna slideshow se non ci sono almeno due banner.
		var tt = this.aBanners[this.actPos].transTime * 1000; // Valore in ms.
		if (!this.halted) 
		{
			this.actPos++;	
			if (this.actPos >= this.aBanners.length) this.actPos = 0;
			this.aBanners[this.actPos].ShowBanner();
		}
		if(fade) {
			this.fadeOn(this.aBanners[this.actPos].id,0);
		} else {
			var tt = this.aBanners[this.actPos].transTime * 1000; // Valore in ms.
			var methodName = 'aBnrManagers[\'' + this.placeCode + '\'].ShowNextBanner();';
			setTimeout(methodName, tt);
		}
	}
	

	function StartSlideshow(fade) {
		if (this.aBanners.length < 2) return; // Nessuna slideshow se non ci sono almeno due banner.
		if(fade) {
			// fade
			this.fadeOn(this.aBanners[this.actPos].id,0);	
		} else  {
			var tt = this.aBanners[0].transTime * 1000; // Valore in ms.
			var methodName = 'aBnrManagers[\'' + this.placeCode + '\'].ShowNextBanner();';
			setTimeout(methodName, tt);
		}
	}
	
// Funzione accessoria per creare i BnrManager necessari.
	function getBnrManager(placeCode) {
		if (aBnrManagers[placeCode])
			return aBnrManagers[placeCode];
		var res = new BnrManager(placeCode);
		aBnrManagers[placeCode] = res;
		return res;
	}

	function pauseSlideshow(placeCode, ssHalt) {
		if (aBnrManagers[placeCode])
			aBnrManagers[placeCode].halted = ssHalt;
		if(ssHalt) {
			// riempie l'immagine
			if(document.getElementById) {
				obj = document.getElementById(aBnrManagers[placeCode].aBanners[aBnrManagers[placeCode].actPos].id);
				setOpacity(obj,100);
			}
		}

	}
