//
// carrousel.js - Carrusel fotográfico
//
// $Id: carrousel.js 1.9 2005/05/02 06:32:36 jordinas Exp $
//
// Joan Ordinas - <jordinas@EscolaDelTreball.ORG>
//

function _Carrousel() {
	var aArgs = location.search.split("&");	// Opera quiere "&" en lugar de /&/
	var sSection = aArgs[0].substr(1);	// cut "?"
	var sCarrousel = aArgs[1];
	var sFirst = aArgs[2];
	var tmp;
	//
	try {
		eval("tmp = g_carrousel_"+sSection+"_"+sCarrousel+"_header;");
		this.sHeader = tmp;	// make Opera happy
	} catch(ignore) {
		eval("tmp = g_carrousel_"+sSection+";");
		this.sHeader = tmp;	// make Opera happy
	}
	eval("tmp = g_carrousel_"+sSection+"_"+sCarrousel+";");
	this.aImageNames = tmp;
	this.nLength = tmp.length;
	this.aImages = new Array(this.nLength);
	for (var i = 0; i < this.nLength; ++i) {
		this.aImages[i] = null;
	}
	this.nCurrentImage = (sFirst) ? Number(sFirst) : 0; // (sFirst != undefined) falla
	this.load(this.nCurrentImage);
	var nPrev = (this.nCurrentImage > 0) ? this.nCurrentImage-1 : this.nLength-1;
	var nNext = (this.nCurrentImage === (this.nLength-1)) ? 0 : this.nCurrentImage+1;
	this.load(nPrev);
	this.load(nNext);
}

_Carrousel.prototype.load = function(nImage) {
	if (!this.aImages[nImage]) {
		this.aImages[nImage] = new Image();
		this.aImages[nImage].src = this.aImageNames[nImage];		
	}
}

_Carrousel.prototype.display = function() {
	// Establece 'display' y 'image_counter' como nombres de nodo
	document.write('<img alt="Foto" border="1" name="display" src="'
			+ this.aImageNames[this.nCurrentImage]
			+ '" width="320" heigth="240">');
	if (Browser.Explorer || Browser.Gecko) {
		document.write('<div id="image_counter">'
				+ (this.nCurrentImage+1) + '/'
				+ this.nLength + '</div>'); 
	}
}

_Carrousel.prototype.close = function() {
	history.go(-1);
}

_Carrousel.prototype.header = function() {
	document.write(this.sHeader);
}

_Carrousel.prototype.goPrev = function() {
	if (--this.nCurrentImage < 0) this.nCurrentImage = this.nLength-1;
	this._show();
}

_Carrousel.prototype.goNext = function() {
	if (++this.nCurrentImage === this.nLength) this.nCurrentImage = 0;
	this._show();
}

_Carrousel.prototype._show = function() {
	if (Browser.Explorer || Browser.Gecko) {
		var oNode = document.getElementById("image_counter");
		oNode.innerHTML = (this.nCurrentImage+1)+"/"+this.nLength;
		//DOM version:
		//	oNode.childNodes[0].nodeValue = (this.nCurrentImage+1)+"/"+this.nLength;
	}
	document.display.src = this.aImages[this.nCurrentImage].src;	
	var nPrev = (this.nCurrentImage > 0) ? this.nCurrentImage-1 : this.nLength-1;
	var nNext = (this.nCurrentImage === (this.nLength-1)) ? 0 : this.nCurrentImage+1;
	this.load(nPrev);
	this.load(nNext);
}


