/*
 * gallery.js - gallery in popup o in finestra principale, con preload delle immagini
 *
 * richiede php:
 * photo.php o implementazione equivalente, e sue dipendenze
 *
 * richiede Javascript esterni:
 * dommanip.js
 *
 * richiede CSS:
 * gallery.css
 * gallery_ie.css
 *
 * UTILIZZO (dove gal e' il nome dell'oggetto):
 * gal = new Gallerystatus(<index_inizio>, <index_massimo>, <urlbaseimg>, <link_root>);
 * gal.galleryData = <array con i dati, per es. json_encode($records) da PHP>
 * gal.setupGallery(<id_prev>, <id_next>, <id_close>);
 *
 * dove
 * <index_inizio> e' l'indice dell'immagine mostrata inizialmente (0=prima)
 * <index_massimo> e' l'indice dell'ultima immagine (= numero di immagini - 1)
 * <urlbaseimg> e' la parte comune all'url di tutte le immagini
 * <link_root> facoltativo, e' la parte dell'url cui 'appendere' l'id immagine
 * per ricaricare la gallery a partire da tale immagine; cfr. photo.php
 * (se <link_root> e' omesso, la gallery non funzionera' senza javascript)
 * <id_prev>, <id_next> sono gli id dei link 'precedente' e 'successiva',
 * <id_close> facoltativo, e' l'id del link 'chiudi finestra'
 *
 * formato di un elemento dell'array:
 * {id: id immagine come da data base,
 *  img_file: nome del file immagine,
 *  didascalia: testo associato all'immagine}
 * 
 * IMPORTANTE: nell'evento window.onunload includere la chiamata
 * gal.unloadme();
 * in caso contrario si verificheranno memory leak, particolarmente in IE6
 *
 * pigi - rev. 20071121
 */

function doFalse()
{
	return false;
}

/* oggetto gallery */

function Gallerystatus(ci, mx, url, lr)
{
	this.currentIndex = ci;
	this.minIndex = 0;
	this.maxIndex = mx;
	this.urlbaseImg = url;
	this.linkRoot = lr;
	this.galleryData = null;
	this.workName = null;
	this.bgbak = null;
	this.preloaded = [];
	this.sPrefix = 'gallery';

	this.prevLink = null;
	this.nextLink = null;
	this.closeLink = null;
	return this;
}

Gallerystatus.prototype.preloadImg = function(idx)
{
	if (!this.preloaded[idx]) {
		this.preloaded[idx] = new Image();
		this.preloaded[idx].src = (this.urlbaseImg + this.galleryData[idx].img_file);
	}
};

Gallerystatus.prototype.updatePhoto = function(idPhoto)
{
	if ((idPhoto < this.minIndex) || (idPhoto > this.maxIndex)) { return; }

	var prefix = this.sPrefix;
	var divlinks = document.getElementById(prefix + "_links");
	var img = document.getElementById(prefix + "_image");
	var mydiv = document.getElementById(prefix + "_dida");
	var prevDiv = document.getElementById(prefix + "_prev");
	var nextDiv = document.getElementById(prefix + "_next");
	var idxSpan = document.getElementById(prefix + "_idx");

	/* aggiorna index */
	this.currentIndex = idPhoto;

	/* aggiorna immagine */
	img.src = (this.urlbaseImg + this.galleryData[idPhoto].img_file);

	/* aggiorna didascalia */
	emptyElement(mydiv);
	appendText(mydiv, this.galleryData[idPhoto].didascalia);
	emptyElement(idxSpan);
	appendText(idxSpan, parseInt(idPhoto + 1, 10));

	/* aggiorna link precedente */
	if (idPhoto == this.minIndex) {
		prevDiv.className = "hide";
	} else {
		prevDiv.className = "show";
		if (this.prevLink && this.linkRoot) {
			this.prevLink.href = (this.linkRoot + this.galleryData[this.currentIndex - 1].id);
		}
	}

	/* aggiorna link successivo */
	if (idPhoto == this.maxIndex) {
		nextDiv.className = "hide";
	} else {
		nextDiv.className = "show";
		if (this.nextLink && this.linkRoot) {
			this.nextLink.href = (this.linkRoot + this.galleryData[this.currentIndex + 1].id);
		}
	}

	divlinks.style.position = 'relative';
	divlinks.style.position = 'absolute';
};

Gallerystatus.prototype.prevPhoto = function()
{
	this.updatePhoto(this.currentIndex - 1); // modifica currentIndex
	if (this.currentIndex > this.minIndex) {
		this.preloadImg(this.currentIndex - 1);
	}
};

Gallerystatus.prototype.nextPhoto = function()
{
	this.updatePhoto(this.currentIndex + 1); // modifica currentIndex
	if (this.currentIndex < this.maxIndex) {
		this.preloadImg(this.currentIndex + 1);
	}
};

Gallerystatus.prototype.setupGallery = function(prevId, nextId, closeId)
{
/*
 * closeId si intende facoltativo; se omesso, non sara' gestito il link Chiudi
 * (cio' e' utile anche per l'integrazione in eGallery)
 */

	var me = this;
	if (document.getElementById) {
		this.prevLink = document.getElementById(prevId);
		this.nextLink = document.getElementById(nextId);

		if (closeId) {
			this.closeLink = document.getElementById(closeId);
			if (this.closeLink) {
				this.closeLink.onclick = function() {
					me.unloadme();
					window.close();
				};
			}
		} 

		if (this.prevLink && this.nextLink) {
			// usa onmouseup per le azioni vere e proprie (per IE), NON aggiungere return false!
			this.prevLink.onclick = doFalse;
			this.nextLink.onclick = doFalse;
			this.prevLink.onmouseup = function() { me.prevPhoto(); };
			this.nextLink.onmouseup = function() { me.nextPhoto(); };
			this.preloadImg(this.currentIndex);
			
			if (this.currentIndex > this.minIndex) {
				this.preloadImg(this.currentIndex - 1);
			}
			if (this.currentIndex < this.maxIndex) {
				this.preloadImg(this.currentIndex + 1);
			}
		}
	}

};

Gallerystatus.prototype.unloadme = function()
{
	this.prevLink.onclick = null;
	this.nextLink.onclick = null;
	this.prevLink.onmouseup = null;
	this.nextLink.onmouseup = null;
	if (this.closeLink) {
		this.closeLink.onclick = null;
	}
};
