// gallery.js
//
// Date				Change																					Who
// ---------- ----------------------------------------------- ---
// 22/10/2008 Altered animation to be repetitive							SB
//
// -----------------------------------------------------------------
// NOTE
// ====
// If you want the animation to be smooth, the last image in the 
// images array must be the same as the first.
// 
// Also, the animation effect duration must be the same as or very 
// similar to delay between transformations, otherwise you will have 
// a larger delay in the repeated first frame of the animation.
//
// -----------------------------------------------------------------

var fLoadedImages = [];
var fFirst = true;

window.addEvent('domready', function(){
	var busy = false, timer, gallery = $('gallery'); 
	var path = 'images/banner/images/';
	var images = [
		path + 'banner.jpg',
		path + 'banner2.jpg', 
		path + 'banner3.jpg',
		path + 'banner.jpg'
	];

	if (!busy) { 
		gallery.setStyle('display', 'block');
		new Asset.images(images, {
			onProgress: function(i) {
				this.setStyles({
					'position': 'absolute',
					'opacity': 0,
                            'left': (gallery.getSize().size.x - 561) / 2,
                            'top': (gallery.getCoordinates().height / 2) - (this.height / 2)
				});
				fLoadedImages[i] = this;
			},
			onComplete: function() {
				queueSequence();
			}
		});
	}
	 
});

function queueSequence() {
	var fx, count;

	if(fFirst) {
		fx = $('gallery').effect('opacity').start(1);
	}
	
	// Reset opacity for each image except the fFirst
	fLoadedImages.each(function(image, i) {
		if(i > 0) image.setStyles({'opacity': 0});
	});
	
	// Queue an animation effect for each of the images (except the fFirst)
	count = 0;
	fLoadedImages.each(function(image, i) {
	
		image.inject($('gallery'));
		
		if (fFirst || i > 0) {
			fx = function() {
				var imgEffect = image.effect('opacity', {duration: 5000,transition: Fx.Transitions.Cubic.easeOut});
				imgEffect.start(1).chain(function() {
					if(i == fLoadedImages.length - 1) {
					window.queueSequence();
					}
				});

			}.delay(5000 * count);
			
			count++;
		}
	});
	fFirst = false;
}
