/**
 * serve id ContainerGallery_[nomegallery]
 */
Gallery = Class.create({
	initialize:function(name, tipo){
		this.name = name;
		this.el = $('containerGallery_'+this.name);

		switch (tipo){
			case 'mono':
				this.tipo = 'MONO';
			break;
			case 'thumbs':
				this.tipo = 'THUMBS';
			break;
		}

		this.fotos = new Hash();
		this.corrente = 0;
	},
	addFoto:function(src){
		if (!this.el) return false;

		var cnt = this.fotos.toArray().length;
		this.fotos.set('f'+cnt, src);

		this.makeThumb();
		if (this.tipo=='MONO'){
			this.addButtom('navigation');
			this.addButtom('zoom');
		} else if (this.tipo=='THUMBS'){
			this.makeThumbs();
		}
	},
	makeThumb:function(){
		var cnt = this.fotos.toArray().length-1;
		var src = this.fotos.get('f'+cnt);
		if (!cnt){
			var thumb = new Element('img', {
				src: ((this.tipo=='MONO')?src.src:src.big),
				id: 'thumb_'+this.name
			});
			$(this.el).appendChild(thumb);
		}
	},
	makeThumbs:function(){
		var cnt = this.fotos.toArray().length-1;
		var src = this.fotos.get('f'+cnt);
		var thumb = new Element('img', {
			src: src.src
		});
		Event.observe(thumb, 'click', function (){
			this.getFoto(cnt);
		}.bind(this));

		var div = this.el.select('.thumbs');
		if (!div.length){
			var div = new Element('div', {
				'class': 'thumbs'
			});
			$(this.el).appendChild(div);
		} else {
			div = div[0];
		}
		div.appendChild(thumb);
	},
	addButtom:function(cosa){
		var cnt = this.fotos.toArray().length-1;
		switch (cosa){
			case 'zoom':
				var src = this.fotos.get('f'+cnt);
				var aZoom = new Element('a', {
					href: src.big,
					title: src.alt,
					'class': 'overlay zoomLink',
					rel: 'lightbox['+this.name+']'
				}).update('&#160;');
				if (cnt){
					aZoom.hide();
				}
				aZoom.setAttribute('id', 'zoom_'+this.name+'_'+cnt);
				this.el.appendChild(aZoom);
			break;
			case 'navigation':
				if (!cnt){
					var a = new Element('a', {
						href: 'javascript://',
						'class': 'overlay'
					}).update('&#160;');

					var aPrev = a.clone();
					aPrev.addClassName('prevLink');
					Event.observe(aPrev, 'click', function (){
						this.prevFoto();
					}.bind(this));
					$(this.el).appendChild(aPrev);
	
					var aNext = a.clone();
					aNext.addClassName('nextLink');
					Event.observe(aNext, 'click', function (){
						this.nextFoto();
					}.bind(this));
					$(this.el).appendChild(aNext);
				}
			break;
		}
	},
	openFoto:function(){
		var foto = this.fotos.get('f'+this.corrente);
		finestra = window.open(foto.big, '', 'status=0, toolbar=0, location=0, menubar=0');
		finestra.onload = function (){
			var img = new Image();
			img.onload = function (){
				finestra.resizeTo(img.width, img.height+70);
			};
			img.src = foto.big;
		};
	},
	getFoto:function(ncnt){
		this.seleziona(ncnt);
		return false;
	},
	prevFoto:function(){
		ncnt = Math.max(this.corrente-1, 0);
		this.seleziona(ncnt);
		return false;
	},
	nextFoto:function(){
		ncnt = Math.min(this.fotos.toArray().length-1, this.corrente+1);
		this.seleziona(ncnt);
		return false;
	},
	seleziona:function(ncnt){
		if (this.tipo=='MONO'){
			$('zoom_'+this.name+'_'+this.corrente).hide();
		}
		this.corrente = ncnt;
		var foto = this.fotos.get('f'+this.corrente);
		if (!foto) return false;

		var tot = this.fotos.toArray().length;

		el = $('thumb_'+this.name);

		oldeffect = new Effect.Opacity(el, { from: 1, to: 0, duration: 0.5 });

		var img = new Image();

		img.onload=function(){
			oldeffect.cancel();
			el.src = img.src;

			new Effect.Opacity(el, { from: el.getStyle('opacity'), to: 1, duration: 0.5 });
	    	dx = Math.min(this.corrente+2,tot-1);
	    	sx1 = Math.max(this.corrente-2, 0);
	    	sx2 = Math.max(dx-4, 0);
	    	sx = Math.min(sx1, sx2);
	    	if (this.tipo=='MONO'){
				$('zoom_'+this.name+'_'+this.corrente).show();
			}
		}.bind(this);
		img.src = ((this.tipo=='MONO')?foto.src:foto.big);
	}
});
