//----------------------------------------------------------------------------
// Indigo Image
//
// A  JavaScript services for an individual image obejct, for use with image
// galleries (via ImageStack)
//----------------------------------------------------------------------------

var IndigoImage = Class.create();

IndigoImage.prototype = {

	initialize: function(id,contentName,fullURL,thumbURL,galleryURL,caption) {
		this.id				= id;
		this.contentName	= contentName;
		this.fullURL		= fullURL;
		this.thumbURL		= thumbURL;
		this.galleryURL		= galleryURL;
		this.caption		= caption;
	}
}


//----------------------------------------------------------------------------
// Image Stack
//
// A new JavaScript library to help manage the images associated with an image
// gallery. Individual image details can be pushed onto the stack. The stack 
// then provides services to allow images to be traversed.
//----------------------------------------------------------------------------

var ImageStack = Class.create();

ImageStack.prototype = {

	initialize: function(params) {

		this.imageList = [];		// The list of image objects
		this.currentImage = null;	// Make sure there are no current images
	},
		
	imageCount: function( ) {
		return this.imageList.length;
	},
	
	// Create a new image object and add it to the list
	registerImage: function(id,contentName,fullURL,thumbURL,galleryURL,caption) {
		var image = new IndigoImage(id,contentName,fullURL,thumbURL,galleryURL,caption);
		this.imageList.push(image);
		i = 1;
	},
	
	// Gets called to set the last image as current
	setCurrentImage: function () {
		this.currentImage = this.imageList.length;
	},
	
	// Load the next/previous image into the specified DOM items (all by ID).
	//	size				-- The size of the imahe to be reutrned (gallery | full).
	// 	target_image_id		-- The ID of the <img> to recieve the image content.
	// 	full_image_link_id	-- The ID of the <a> to receive the full-image link.
	// 	image_caption_id	-- The ID of the <div> to receive the caption.
	//	image_counter_id	-- The ID of the <span> holding the image counter.
	
	loadNextToIMG: function (size,target_image_id,full_image_link_id,image_caption_id,image_counter_id) {
		if (this.currentImage) {
			if (this.currentImage < this.imageList.length) {
				var image_index = this.currentImage - 1;	// Adjust for 0 offset array	
				var next_image = image_index + 1;
				if (size == 'gallery') {
					$(target_image_id).src = this.imageList[next_image].galleryURL;
				} else {
					$(target_image_id).src = this.imageList[next_image].fullURL;
				}
				$(full_image_link_id).href = this.imageList[next_image].fullURL;
				
				var myCaptionDivName = 'image_caption_' + this.imageList[next_image].id;				
				$(image_caption_id).innerHTML = $(myCaptionDivName).innerHTML;
				
				this.currentImage += 1;
				$(image_counter_id).innerHTML = this.currentImage + " of " + this.imageList.length;
			}
		}
	},
	loadPreviousToIMG: function (size,target_image_id,full_image_link_id,image_caption_id,image_counter_id) {
		if (this.currentImage) {
			if (this.currentImage > 1) {
				var image_index = this.currentImage - 1;	// Adjust for 0 offset array	
				var prev_image = image_index - 1;
				if (size == 'gallery') {
					$(target_image_id).src = this.imageList[prev_image].galleryURL;
				} else {
					$(target_image_id).src = this.imageList[prev_image].fullURL;
				}
				$(full_image_link_id).href = this.imageList[prev_image].fullURL;
				
				var myCaptionDivName = 'image_caption_' + this.imageList[prev_image].id;				
				$(image_caption_id).innerHTML = $(myCaptionDivName).innerHTML;

				this.currentImage -= 1;
				$(image_counter_id).innerHTML = this.currentImage + " of " + this.imageList.length;
			}
		}
	}
	
}