// Oggetto Gallery

/* Funzione di supporto */
function SelectorClick(selector)
{
	var id=selector.getProperty('id');
	var part=id.split('_');
	/* E' sempre dopo il secondo _ */	
	return parseInt(part[2],10);
}

/* Oggetto gallery */
function GallerySlide(element,imageArray,transArray,tempo,pausa,autostart)
{
	var i =0;
	this.temporizzazione = tempo;
	this.pausa = pausa;
	this.origImageArray = imageArray;
	this.transArray = transArray;
	this.autostart = autostart;
	this.counter = this.origImageArray.length;
	this.element = element;
	this.left = 245 - (21 * (this.counter -1));
	this.current = -1;
	
	this.freccia_sx = new Element('div',{'id':'freccia_sx','class':'freccia_sx'});
	this.freccia_dx = new Element('div',{'id':'freccia_dx','class':'freccia_dx'});
	
	this.freccia_sx.inject($(this.element));
	this.freccia_dx.inject($(this.element));
	
	var wait_slide = new Element('img',{'id':'wait_slide','src':'images/shop/spinner.gif'});
	wait_slide.inject($(this.element));

	var myImages = Asset.images(this.origImageArray,
			{
			onComplete: function()
						{
						if (Sito.OpenGallery.autostart)
								{
								Sito.OpenGallery.Loaded = true;
								Sito.OpenGallery.Mostra(0);								
								}
						}
			});
	
	this.imagediv = [];
	this.imageArray = [];
			
	for (i=0;i<this.counter;i++)
		{
		this.imagediv[i] = new Element('div',{'id':'foto_' + element + '_' + i,'class':'gallery'});
		this.imageArray[i] = new Element('img', {'id':'img_' + element + '_' + i,'src':this.origImageArray[i]});
		this.imageArray[i].inject(this.imagediv[i]);
		
		this.imagediv[i].get('morph').addEvents({
			'complete': function() {
									Sito.OpenGallery.Enable();
									}
									});
		this.imagediv[i].inject($(this.element));
		}
}

GallerySlide.prototype.SetTimer = function ()
{
	if (this.Loaded)
		{
		this.timer = this.Avanti.periodical(this.temporizzazione+this.pausa, this);
		}
};

GallerySlide.prototype.ClearTimer = function ()
{
	clearInterval(this.timer);
};

GallerySlide.prototype.Mostra = function (index)
{
	if (this.Loaded)
		{
		var imgW = this.imageArray[index].width;
		var imgH = this.imageArray[index].height;
		
		var difW = 836-imgW;
		var difH = 417-imgH;
		
		switch (this.transArray[index])
			{
				// Fermo
				case "0": currentT = {'opacity': [1,0]};
						  indexT = {'visibility':'visible'};break;
				// 1 da sx a dx
				case "1": currentT = {'opacity': [1,0]};
						  indexT = {'left':[0,difW],'visibility':'visible'};break;
				// 2 da dx a sx
				case "2": currentT = {'opacity': [1,0]};
						  indexT = {'left':[difW,0],'visibility':'visible'};break;					  
				// 3 da alto a basso
				case "3": currentT = {'opacity': [1,0]};
						  indexT = {'top':[0,difH],'visibility':'visible'};break;
				// 4 da basso a alto
				case "4": currentT = {'opacity': [1,0]};
						  indexT = {'top':[difH,0],'visibility':'visible'};break;					  
			}
		
		if (this.current !== -1)
			{
			this.imagediv[this.current].set('morph', {duration: (500)});
			this.imagediv[this.current].set('styles',{'z-index':'200'});
			}
			
		this.imagediv[index].set('morph', {duration: this.temporizzazione});
		this.imagediv[index].set('styles',{'z-index':'100'});
		this.imagediv[index].set('styles',{'opacity':'1'});
		
		if (this.current !== -1)
			{
			this.imagediv[this.current].get('morph').start(currentT);
			}
			
		this.imagediv[index].get('morph').start(indexT);
		this.current = index;
		this.SetTimer();
		this.Enable();
		}
};

GallerySlide.prototype.Avanti = function ()
{
	this.Disable();
	this.ClearTimer();
	var prossimo = this.current + 1;
	if (prossimo >= this.counter)
			{
			prossimo = 0;
			}
	this.Mostra(prossimo);
};

GallerySlide.prototype.Indietro = function ()
{
	this.Disable();
	this.ClearTimer();
	var prossimo = this.current - 1;
	if (prossimo < 0 )
			{
			prossimo = this.counter-1;
			}
	this.Mostra(prossimo);
};

GallerySlide.prototype.MostraFrecce = function ()
{
	this.freccia_sx.show();
	this.freccia_dx.show();
};

GallerySlide.prototype.NascondiFrecce = function ()
{
	this.freccia_sx.hide();
	this.freccia_dx.hide();
};

GallerySlide.prototype.Svuota = function ()
{
	this.ClearTimer();
	this.Loaded = false;
	delete this.imagediv;
	delete this.freccia_sx;
	delete this.freccia_dx;
	delete this.myImages;
};

GallerySlide.prototype.Enable = function ()
{
	this.freccia_sx.addEvents({
					click:function()
						{
						Sito.OpenGallery.Indietro();
						}
					});	
					
	this.freccia_dx.addEvents({
					click:function()
						{
						Sito.OpenGallery.Avanti();
						}
					});	
};

GallerySlide.prototype.Disable = function ()
{
	if (this.Loaded)
		{
		this.freccia_sx.removeEvents('click');
		this.freccia_dx.removeEvents('click');
		}
};
