window.addEvent('domready', function() {
	// Don't want to interrupt a load
	var loading = false;
	// Universal loader image
	var loader = new Asset.image('images/loading.gif', { id: 'loading' });
	// Active image element
	var active = $('carousel-1-image');
	
	// Setup the tooltips
	new Tips('.thumbnails a', { className: 'tip-box' });
	
	// Initialize the carousel actions
	$('carousel').getElements('.thumbnails a').addEvent('click', function(e) {
		e.stop();
		var image_id = this.get('id') + '-image';
		if (!loading && active != $(image_id)) {
			this.grab(loader, 'bottom');
			loader.setStyle('display', 'block');
			loading = true;
			
			if ($(image_id)) {
				// Already been loaded
				display_image($(image_id));
			} else {
				var image = new Asset.image(this.get('href'), {
					id: image_id,
					'class': 'detail',
					alt: '',
					onload: function() {
						image.fade('hide');
						display_image(image);
					}
				});
			}
		}
	});
	
	function display_image(image) {
		loader.setStyle('display', 'none');
		image.inject($('carousel'), 'bottom').get('tween', { property: 'opacity', duration: 750 }).start(1).chain(function() {
			loading = false;
			active.fade()
			active = image;
		});
	}
});
