window.addEvent('domready', function(){
	
	new Element('div', {'id': 'preload'}).injectBefore('th');
	var boxImgBig = new Element('div', {'id': 'tdBoxImgBig'}).injectInside('boxImgBig');
	var numbTh;
	
	$$('#th a').each(function(el, n){numbTh = n;});

	if(numbTh==undefined) {
		$('preload').remove();
		$('boxImgBig').addClass('bgNoImage');
	} else {

		$$('#th a').each(function(href, index){
								  
			var e = index;					  
			var i = href.getProperty('href');
			var id = 'imgBig'+e;
			new Element('img', {'id': id, 'src': i}).injectInside('preload');
			start();
			
			function alpha() {
				//imgBig = new Element('img', {'id': 'imgBig', 'src': 'i/t/th-mask-big.png', 'styles': {'background': 'url('+i+') top center no-repeat'}}).injectInside('tdBoxImgBig');
				imgBig = new Element('img', {'id': 'imgBig', 'src': ''+i+''}).injectInside('tdBoxImgBig');
			
				imgBig.effect('opacity').set(0);
				imgBig.effect('opacity', {duration: 500}).start(0,0.99);
			}
			
			function start() {
				if (numbTh==0) $('th').remove();
				if (e==0) alpha(); //carica la prima immagine dell'elenco
				if (e==numbTh) $('preload').remove(); //al caricamento dell'ultima immagine in elenco elimina il preload
				if (!e && !numbTh) $('thCont').remove();
				
				href.addEvent('click', function(event){
					imgBig.remove({onComplete: alpha()});
				});
			}
		});

	}

});

