//Script per la gestione di una galleria fotografica
function Gallery(img) {

	this.img = null;
	this.numberPath = null;
	this.imgsPos = new Array(
		null, //decimale posizione
		null, //unita posizione
		null, //decimale dimensione
            null //unita diemnsione
	);
	this.dimFont = null; //dimensione img
	this.FIRST = 0;
	this.errno = -1;
	this.size = -1;
	this.currentPos = this.FIRST;
	this.arrayImg = new Array();
	this.arrayText = new Array();
	
	this.putImg = function putImage(path, text)
	{
		if (typeof path != "undefined"
			&& typeof path == "string"
			&& path != "")
		{
			this.image = new Image();
			this.image.src = path;
			this.arrayImg[++this.size] = this.image;
			if (typeof text != "undefined"
			&& typeof text == "string"
			&& text != "")
			{
                            this.arrayText[this.size] = text;
			}
			else
			{
				this.arrayText[this.size] = null;
			}
		}
		else 
		{
			this.errno = 1;
			this.getErrno();
		}
	}

	/* è meglio inserire delle immagini prima di inizializzare l'oogetto */
	this.initGallery = function _initGalley() 
	{
		if (typeof img != "undefined")
		{
			this.img = img;
			if (this.size >= this.FIRST)
			{
				this.show(this.FIRST);
			}
                        else
                        {
                                this.errno = 2;
                                this.getErrno();
                        }
                }
	}
	
	this.show = function _show(pos)
	{
	
		if (typeof pos != "undefined"
			 && typeof pos == "number")
		{
			if (pos < this.FIRST) 
			{
				this.errno = 4;
				pos = this.FIRST;
			}
			if (pos >= this.size)
			{
				this.errno = 5;
				pos = this.size;
			}
				this.image = this.arrayImg[pos];
				//this.img.height = this.image.height;
				//this.img.width = this.image.width;
				this.img.src = this.image.src;
				this.currentPos = pos;
		} 
		else
		{
			this.errno = 3;
			this.getErrno();
		}
	}
	
	this.getText = function _getText()
	{
            this.tmp = this.arrayText[this.currentPos];
            if (this.tmp != null && typeof this.tmp != "undefined") 
                    return this.tmp;
            else
                    return "";
	}
	
	this.getSize = function _getSize()
	{
		return this.size + 1;
	}
	
	this.getPos = function _getPos()
	{
		return this.currentPos + 1;
	}

	this.getInitImgNumber = function _getInitImgNumber(path, imgPosDecimal, imgPosUnit, imgSizeDecimal, imgSizeUnit, dimFont)
	{
		if (typeof path != "undefined"
			 && typeof path == "string"
			 && path != ""
			 && typeof dimFont != "undefined"
			 && (typeof dimFont == "number" 
			 	  || typeof dimFont == "object") 
			 && imgPosDecimal != "undefined"
			 && imgPosUnit != "undefined"
			 && imgSizeDecimal != "undefined"
			 && imgSizeUnit != "undefined")
		{
			this.numberPath = path;
			this.imgsPos[0] = imgPosDecimal;
			this.imgsPos[1] = imgPosUnit;
			this.imgsPos[2] = imgSizeDecimal;
			this.imgsPos[3] = imgSizeUnit;
			this.dimFont = dimFont;
		}
		else
		{
			this.errno = 1;
			this.getErrno();
		}
	}
	
	this.showImgPosSize = function _showImgPosSize()
	{
		if (this.numberPath != null)
		{
			if (this.size <= 99 || this.currentPos <= 99)  
			{

                                this.checkP = false;
                                this.checkS = false;
                                this.tmpPos = 0;
                                this.tmpSize = 0;
                                this.tmpPos = 0;
                                this.tmpSize = 0;
                                this.unitP = 0;
                                this.decimP = 0;
                                this.unitS = 0;
                                this.decimS = 0;
                                if (this.size >= this.FIRST) 
                                {
                                    this.tmpPos = this.currentPos + 1;
                                    this.tmpSize = this.size + 1;
                                    this.unitP = this.tmpPos % 10;
                                    this.decimP = this.tmpPos - this.unitP;
                                    this.unitS = this.tmpSize % 10;
                                    this.decimS = this.tmpSize - this.unitS;

                                    if (this.decimP <= 9)
                                    {
                                            this.checkP = false;
                                            this.decimP = this.decimP;
                                    }
                                    else 
                                    {
                                            this.checkP = true;
                                            this.decimP = this.decimP / 10; 
                                    }
                                    if (this.decimS <= 9)
                                    {
                                            this.checkS = false;
                                            this.decimS = this.decimS;
                                    }
                                    else 
                                    {
                                            this.checkS = true;
                                            this.decimS = this.decimS / 10; 
                                    }
				}

				this.imgsPos[0].src = this.numberPath + "/" + ((this.decimP > 9 || this.checkP) ? this.decimP : "null") +".gif"
				this.imgsPos[1].src = this.numberPath + "/" + this.unitP +".gif"
				this.imgsPos[2].src = this.numberPath + "/" + ((this.decimS > 9 || this.checkS) ? this.decimS : "null") +".gif" 
				this.imgsPos[3].src = this.numberPath + "/" + this.unitS +".gif"
				if (this.dimFont != null) {
					this.imgsPos[0].height = this.dimFont;
					this.imgsPos[1].height = this.dimFont;
					this.imgsPos[2].height = this.dimFont;
					this.imgsPos[3].height = this.dimFont;
				}
			}
		} 
		else
		{
			this.errno = 1;
			this.getErrno();
		}
	}
	
	this.next = function _next() 
	{
		this.show(this.currentPos + 1);
		if (this.imgsPos[0] != null
                    && this.imgsPos[1] != null
                    && this.imgsPos[2] != null
                    && this.imgsPos[3] != null) 
                    this.showImgPosSize();
	}
	
	this.before = function _before() 
	{
		this.show(this.currentPos - 1);
		if (this.imgsPos[0] != null
                    && this.imgsPos[1] != null
                    && this.imgsPos[2] != null
                    && this.imgsPos[3] != null) 
                    this.showImgPosSize();
	}
	
	this.getErrno =function getErrorno() 
	{
		switch (this.errno) 
		{
			case 1: 
				alert("Percorso immagine errato");
				return;
			case 2: 
				alert("Non è stata definita l'immagine di riferimento");
				return;
			case 3: 
				alert("Impossibile visualizzare l'immagine");
				return;
			case 4: 
				alert("L'elemento è in prima posizione");
				return;
			case 5: 
				alert("L'elemento è in ultimna posizione");
				return;
		}
	}
}
