/************************************************ 
 * Copyright © 2009 FL-2. All Rights Reserved.  *
 * Updated by: Matt Wiggins, 25-Mar-2009        *
 *                                              *
 * What about the image gallery times?          *
 *                                              *
 * NOTE: Requires jQuery.                       *
 ************************************************
/                                              */
ImageGallery = {
	/************************************************ 
	 * class members                                *
	 ************************************************
	/                                              */
	// gallery stuff
	_ready:false,
	_locked:false,
	_loaded:false,
	_images:new Array(),
	_index:0,
	_totalImages:0,
	
	// slideshow stuff
	_slideshow:false, // set to true for autoplay
	_ssTimeout:0, // timeout id
	_ssTime:5000, // slideshow time in milliseconds
	
	/************************************************ 
	 * init:void                                    *
	 *                                              *
	 * NOTE: a call to this method is required and  *
	 * must be made after the document is ready.    *
	 *                                              *
	 * container:String - desired containing div    *
	 ************************************************
	/                                              */
	init:function(container) {
		// if jQuery hasn't joined the party ...
		if(typeof($) == 'undefined')
		{
			throw("Error: jQuery is required for this page to function properly.");
			return;
		}
		
		// build gallery loading divs
		var divImgGallery = document.createElement('div');
		
		divImgGallery.id = 'fl2-image-gallery';
		divImgGallery.style.position = "relative";
		divImgGallery.style.width = '100%';
		divImgGallery.style.height = '100%';
		
		divImgGallery.innerHTML = '<img id="container-img" src="" width="0" height="0" alt="" />';
		
		var divLoader = document.createElement('div');
		
		divLoader.id = 'loading';
		divLoader.style.position = 'absolute';
		divLoader.style.top = '15px';
		divLoader.style.left = '15px';
		divLoader.style.width = '100px';
		divLoader.style.fontFamily = 'Arial, Helvetica, Sans-Serif';
		divLoader.style.fontSize = '14px';
		divLoader.style.color = '#999';
		divLoader.style.fontWeight = 'bold';
		
		divLoader.innerHTML = '<div id="loading-icon" style="width:16px; float:left;"><img src="images/preloader.gif" width="16" height="16" alt="" /></div><div id="loading-label" style="display:block; width:79px; float:right; padding-left:5px;"><p style="margin:0px; padding:2px 0px 0px 0px; line-height:1em;">Loading ...</p></div><div style="padding:0; margin:0; width:0; height:0; clear:both;"></div>';
		
		divImgGallery.appendChild(divLoader);
		document.getElementById(container).appendChild(divImgGallery);
		
		// initially hide the container image and
		// register a load handler
		$('#container-img').attr('style', 'display:none');
		$('#container-img').load(ImageGallery._onImageLoad);
		
		ImageGallery._ready = true;
	},
	
	/************************************************ 
	 * add:void                                     *
	 *                                              *
	 * Add an image to the gallery.                 *
	 *                                              *
	 * path   :String - path to the image           *
	 * alt    :String - alternate text              *
	 * width  :Number - image width                 *
	 * height :Number - image height                *
	 ************************************************
	/                                              */
	add:function(path, alt, width, height) {
		if(!ImageGallery._ready) return;
		
		// add image, load it if it's the first one
		ImageGallery._images[ImageGallery._totalImages] = {path:path, alt:alt, width:width, height:height};
		if(ImageGallery._totalImages == 0) ImageGallery._loadImage(ImageGallery._images[0]);
		ImageGallery._totalImages++;
	},
	
	/************************************************ 
	 * remove:void                                  *
	 *                                              *
	 * Remove an image to the gallery.              *
	 *                                              *
	 * id :Number - index to remove                 *
	 ************************************************
	/                                              */
	remove:function(id) {
		if(!ImageGallery._ready) return;
		
		// coming soon to a theater near you!
	},
	
	/************************************************ 
	 * previous:void                                *
	 *                                              *
	 * Move to the previous image if this is not    *
	 * the first image in the gallery.              *
	 ************************************************
	/                                              */
	previous:function() {
		if(!ImageGallery._ready || ImageGallery._locked) return;
		
		ImageGallery._locked = true;
		
		if(ImageGallery._index == 0) ImageGallery._index = ImageGallery._images.length - 1;
		else ImageGallery._index--;
		
		ImageGallery._loadImage(ImageGallery._images[ImageGallery._index]);
	},
	
	/************************************************ 
	 * next:void                                    *
	 *                                              *
	 * Move to the next image if this is not        *
	 * the last image in the gallery.               *
	 ************************************************
	/                                              */
	next:function() {
		if(!ImageGallery._ready || ImageGallery._locked) return;
		
		ImageGallery._locked = true;
		
		if(ImageGallery._index == ImageGallery._images.length - 1) ImageGallery._index = 0;
		else ImageGallery._index++;
		ImageGallery._loadImage(ImageGallery._images[ImageGallery._index]);
	},
	
	/************************************************ 
	 * move:void                                    *
	 *                                              *
	 * Move to the specified index if it exists.    *
	 *                                              *
	 * id :Number - index to move to                *
	 ************************************************
	/                                              */
	move:function(id) {
		if(!ImageGallery._ready || ImageGallery._locked) return;
		
		ImageGallery._locked = true;
		
		if(id < 0 || id >= ImageGallery._images.length - 1)
		{
			throw("Error: Index out of range.");
			return;	
		}
		else ImageGallery._index = id;
		ImageGallery._loadImage(ImageGallery._images[ImageGallery._index]);
	},
	
	/************************************************ 
	 * slideshowOn:void                             *
	 *                                              *
	 * Toggle slideshow on.                         *
	 ************************************************
	/                                              */
	slideshowOn:function(ssTime) {
		
		if(!ImageGallery._ready) return;		
		if(typeof(ssTime) == 'undefined') ssTime = 5000;

		ImageGallery._slideshow = true;
		ImageGallery._ssTime = ssTime;
		ImageGallery._ssTimeout = setTimeout(ImageGallery.next, ImageGallery._ssTime);
	},
	
	/************************************************ 
	 * slideshowOff:void                            *
	 *                                              *
	 * Toggle slideshow off.                        *
	 ************************************************
	/                                              */
	slideshowOff:function() {
		if(!ImageGallery._ready) return;
		
		clearTimeout(ImageGallery._ssTimeout);
		ImageGallery._slideshow = false;
	},
	
	/************************************************ 
	 * private functions                            *
	 ************************************************
	/                                              */
	
	/************************************************ 
	 * loadImage:void (private)                     *
	 *                                              *
	 * Load a new image.                            *
	 *                                              *
	 * img :Object - image to load                  *
	 ************************************************
	/                                              */
	_loadImage:function(img) {
		if(ImageGallery._loaded)
		{
			$('#container-img').fadeOut('slow', function() {
				$('#loading').fadeIn('fast');
				$('#container-img').attr('src', img.path);
				$('#container-img').attr('alt', img.alt);
				$('#container-img').attr('width', img.width);
				$('#container-img').attr('height', img.height);
			});
		} else {
			$('#container-img').attr('src', img.path);
			$('#container-img').attr('alt', img.alt);
			$('#container-img').attr('width', img.width);
			$('#container-img').attr('height', img.height);
			ImageGallery._loaded = true;
		}
	},
	
	/************************************************ 
	 * onImageLoad:void (private)                   *
	 *                                              *
	 * Handle image loading complete.               *
	 ************************************************
	/                                              */
	_onImageLoad:function() {
		$('#loading').fadeOut('fast', function() {
			$('#container-img').fadeIn('slow', function() {
				ImageGallery._locked = false;
				if(ImageGallery._slideshow) ImageGallery._ssTimeout = setTimeout(ImageGallery.next, ImageGallery._ssTime);
			});
		});
	},
	
	/************************************************ 
	 * getter/setter                                *
	 ************************************************
	/                                              */
	
	/************************************************ 
	 * gReady:Boolean (getter)                      *
	 *                                              *
	 * Read-only ready                              *
	 ************************************************
	/                                              */
	gReady:function() {
		return ImageGallery._ready;	
	},
	
	/************************************************ 
	 * gIndex:Number (getter)                       *
	 *                                              *
	 * Current displayed index. Set with public     *
	 * show() method.                               *
	 ************************************************
	/                                              */
	gIndex:function() {
		return ImageGallery._index;	
	}
}
