/**************************************************************

	Script		: Gallery
	Author		: Wes Hatch
	Notes		: Thanks to Valerio Proietti (http://mad4milk.net) 
				  MIT-style license

**************************************************************/


var Gallery = {
	
	start: function() {
		var fadeTime = 450;

		this.thumbs = $$('#img-nav a');

		this.imageFader = new Fx.Style ('pressImg', 'opacity', { duration: fadeTime, transition: Fx.Transitions.Circ });
		this.textFader =  new Fx.Style ('detail', 'opacity', { duration: fadeTime, transition: Fx.Transitions.Circ });

		this.thumbs.each(function(t) {
			t.addEvent('click', function(e) {
				e = new Event(e).stop();
				$('loading').setStyle('visibility', 'hidden');
				var picURL = this.href;
				var picDtl = picURL.substring(0, picURL.length-4) + '_d.jpg';
				var imgTxt = this.name;

//var imgDetail = new Asset.image(picDtl, {onload: function(){ imgDetail.injectAfter.('desc')} });

				Gallery.textFader.start(0);
				Gallery.imageFader.start(0).chain(function(){
					$('loading').setStyle('visibility', 'visible');

					var img = new Image();	// hijack the a href tag this way, instead of using new Asset.image(...)
					img.src = picURL;
					img.onload = function() {			// || img.complete for Safari ?
						var wd = img.width;
						$('loading').setStyle('visibility', 'hidden');
						$('desc').setHTML(imgTxt);
						$('pressImg').src = picURL;
						$('pressImg').width = wd; // Safari fix
						Gallery.imageFader.start(1);
						( function() { Gallery.textFader.start(1)} ).delay(450);
						Gallery.setActiveThumb();
					}
					if (!img.complete){ $('pressImg').setHTML('Error: picture not found'); }
				});				

			});
		});
	},


	setActiveThumb: function() {
		this.thumbs.each(function(t) {
			if (t.href == $('pressImg').src) { t.addClass('active'); }
			else { t.removeClass('active'); }
		});
	}
};


