/**
*
* Module d'affichage pour les galeries Zoom sur Vassiviere.com
*
* @author Marc Plessis <marc.plessis@proximit.fr>
* @link http://www.proximit.fr
* @since 28/02/2008
* @version 1.0.0
* @copyright Copyright @ 2008 Proximit
*/
var loadedImages = [], gallery;
var slide = 0;//tracks which slide is showing
var padding=4;
slidePause = 1000; //length of time to keep a slide at 1 opacity
 
window.addEvent('domready', function(){
			var progress = $('progress'), bar = $E('#progress .bar'); 
			gallery = $('zoom_vas');
			gallery.setStyles({'opacity': 0, 'display': 'none'});
			height_pad=parseInt(height_g) + (padding*2) + 'px';
			width_pad=parseInt(width_g) + (padding*2) + 'px';
			
			gallery.setStyles({
				'height': height_pad,
				'width': width_pad
			});
			//progress.setStyle('visibility', 'hidden');
			progress.setStyle('display', 'none');
 
			loadImages();
			function loadImages(){
				var galleryImgs = $$('#gallery img');
				if (galleryImgs.length > 0) galleryImgs.each(function(image) {	image.remove();	});
 
				//progress.setStyle('visibility', 'visible');
				gallery.setStyle('display', 'block');
				new Asset.images(images, {
					onProgress: function(i) {
						if(taille_img==1){
							this.setStyles({
									'height': height_g,
									'width': width_g
								});
						}
						
						this.setStyles({
							'position': 'absolute',
							'opacity': 0,
							'left':padding,
							'top':padding
							/*'left': (gallery.getCoordinates().width / 2) - (this.width / 2)-3,
							'top': (gallery.getCoordinates().height / 2) - (this.height / 2)-3*/
						});
						loadedImages[i] = this;
						var percent = ((i + 1) * progress.getStyle('width').toInt()) / images.length;
						bar.setStyle('width', percent).setHTML(i + 1 + ' / ' + images.length);
					},
					onComplete: function() {
						var fx = $('zoom_vas').effect('opacity').start(1).chain(function(){
							progress.setStyle('display', 'none');
							nextImg(-1);
						});
					} 
				});
			} 
});
 
function nextImg(){
	slide = (slide == loadedImages.length-1)?0: slide+1;
	//slide = $random(0,loadedImages.length-1); //show pictures randomly
	if($E('img', gallery)){
		$E('img',gallery).effect('opacity', {duration: 3000, onComplete: function(){
			$E('img',gallery).remove();
			//showImg(loadedImages[slide]);// fade out completely before fading in next image
			}
		}).start(0);
		showImg(loadedImages[slide]);// fade out WHILE fading in next image
	}
	else
		showImg(loadedImages[slide]);
}
 
function showImg(img){
	if (taille_img == 0) {
		gallery.setStyles({
			'height': img.height + (padding*2),
			'width': img.width + (padding*2)
		});
	}
	img.inject(gallery);
	img.effect('opacity', {duration: 3000, onComplete: function(){
			setTimeout('nextImg()',slidePause);
		}
	}).start(1);
}