window.addEvent('domready', function(){
	var busy = false, timer, loadedImages = [], gallery = $('gallery'), progress = $('progress'), bar = $E('#progress .bar'); 
	var path = 'images/';
	var images = [
		path + 'home1.jpg',
		path + 'home2.jpg',
		path + 'home3.jpg',
		path + 'home4.jpg',
		path + 'home5.jpg',
		path + 'home6.jpg'
	];
	//gallery.setStyles({'opacity': 0, 'display': 'none'});
	//progress.setStyle('visibility', 'hidden');
	if (!busy) {		
		var galleryImgs = $$('#gallery img');
		if (galleryImgs.length > 0) galleryImgs.each(function(image) {	image.remove();	});
		busy = true;
		progress.setStyle('visibility', 'visible');
		gallery.setStyle('display', 'block');
		new Asset.images(images, {
			onProgress: function(i) {
				this.setStyles({
					'position': 'absolute',
					'opacity': 0,
					'left': (gallery.getCoordinates().width / 2) - (this.width / 2),
					'top': (gallery.getCoordinates().height / 2) - (this.height / 2)
				});
				loadedImages[i] = this;
				var percent = ((i + 1) * progress.getStyle('width').toInt()) / images.length;
				bar.setStyle('width', percent).setHTML(i + 1 + ' / ' + images.length);
			},
			onComplete: function() {
				progress.setStyle('visibility', 'hidden');
				var fx = $('gallery').effect('opacity').start(1);
				timer = 0;
				loadedImages.each(function(image, i) {
					if (i>0) timer += 1000;
					if (i==5) {
						var aw = new Element('a',{'href':'leadership.html','border':'0'});
						image.inject(aw);
						aw.inject(gallery);
					}else{
						image.inject(gallery);
					}
					fx = function() {
						var imgEffect = image.effect('opacity', {duration: 2000});
						imgEffect.start(1).chain(function() {
							if (i < loadedImages.length - 1) {
								this.start(0).chain(function() {
									image.remove();
								});
							} else {
								busy = false;
							}
						});
						
					}.delay(timer * 3);
				});
			}
		});
	}
});
