
	
	// Element that will contain the slide gallery.
	var ginger_root;
	
	var win_loaded = false;
	var img_loaded = false;

	window.addEvent('domready', function() {
										 
		// Dynamically build the DOM that holds the slideshow images.
		
		ginger_root = document.id('ginger-slideGallery');
		
		// Fetch the list of images used in the gallery
		var list = [];
		
		// Path to images
		//var path = 'assets/images/homepage/slides/';
		var path = 'assets/site/homepage/makeThumb.php?pic=';
		
		var request = new Request.JSON({
			url: '/assets/site/homepage/imagelist.php',
			onSuccess: buildList.bind(this)
		}).get();
		
		function buildList() {

			// Build the DOM structure that will contain the unordered list.
	//		var root = document.id(targetEl);
		//	var caption = new Element('span.caption'). inject(root);
			var ul = new Element('ul').inject(
				new Element('div.holder').inject(
					new Element('div.gallery'). inject(
						ginger_root)));

			// Process images list, build list items.
			var list = arguments[0];
			var isfirst = true;
			var count = list.length;
			list.each(function(v) {
				new Element('img', {
					src: '/' + path + v[0]
					,alt: v[1]
					,style: (!isfirst) ? 'visibility:hidden' : ''
					,events: {
						load: function(){
							if (!--count) {
								img_loaded = true;
								load_gallery();
							}
						}
					}
				}).inject(
					new Element('li').inject(
						ul));
				isfirst = false;
			});
		}
			 
	});

	window.addEvent('load', function() {
		win_loaded = true;
		load_gallery();
	});

	function load_gallery() {
		if (img_loaded && win_loaded) {
			$(ginger_root).getElements('img').setStyle('visibility', 'visible');
			var gallery = new fadeGallery(ginger_root, {
				speed: 2000
				,pagingEvent: "mouseenter"
				,autoplay: true
				,duration: 4000
				,onStart: function() {
				}
				,onPlay: function() {
					this.fireEvent("start");
				}
			});
		}
	}
	
	
	
