
	/**
	 * This class is responsible for showing images in a slideshow
	 */

	/**
	 * Create a new instance of this photo gallery
	 */
	photogallery = function(listItems, options)
	{
		MochiKit.Base.bindMethods(this);

		//copy the list
		this.list = listItems;

		//set the parent node
		this.parentNode = options.parentNode;
		this.useThumbnailFirst = options.useThumbnailFirst;

		this.position = 0;

		if (this.list && this.list.length > 0)
		{
			//init the image
			this.setImageFromList(this.position);
		}
	}

	/**
	 * Allows us to set the list after initialisation
	 * @param array listItems The list
	 */
	photogallery.prototype.setList = function(listItems)
	{
		this.position = 0;

		this.list = boa.objectToArray(listItems);

		if (this.list.length > 0)
		{
			this.setImageFromList(this.position);
		}
	}

	/**
	 * Show the next image in the list
	 */
	photogallery.prototype.next = function()
	{
		//make sure we are not at the end of the list
		if (!this.list || (this.list.length == 0))
		{
			return false;
		}
		if ((this.position == this.list.length-1))
		{
			this.position = -1;
		}
		this.position++;
		this.setImageFromList(this.position);
		return true;
	}

	/**
	 * Show the previous image in the list
	 */
	photogallery.prototype.previous = function()
	{
		//make sure we are not at the end of the list
		if (!this.list || (this.list.length == 0))
		{
			return false;
		}
		if(this.position == 0)
		{
			this.position = this.list.length;
		}

		this.position--;
		this.setImageFromList(this.position);
		return true;
	}

	/**
	 * Skip to the specified image in the list
	 * @param int index The index to skip to
	 */
	photogallery.prototype.skipTo = function(position)
	{
		if (this.list && position > this.list.length)
		{
			return false;
		}

		this.position = position;
		this.setImageFromList(this.position);
		return true;
	}

	/**
	 * If we have subscribed to any outside events, then we need to be notified
	 * of those.
	 */
	photogallery.prototype.notified = function(notice)
	{
		log('notification successful');

		log(typeof notice);

		if (typeof notice == 'number')
		{
			log('skipping to : ' + notice);
			this.skipTo(notice);
		}
		else
		{
			log('reset list');
			this.setList(notice);
		}
	}

	photogallery.prototype.preSetImageFromList = function(index) {}
	photogallery.prototype.postSetImageFromList = function(index) {}

	/**
	 * Set the src/title/alt of the selected image, when
	 * given the index in the list for that image object
	 */
	photogallery.prototype.setImageFromList = function(index)
	{
		if(!this.list)
		{
			log(".photogallery cannot set the image (setImageFromList)");
			return;
		}

		if(index == null)
		{
			index = 0;
		}

		this.preSetImageFromList(index);
		var img = this.list[index].src;

		if(this.useThumbnailFirst && this.list[index].tmb)
		{
			img = this.list[index].tmb;
		}
		
		var picID = index + 1;
		var picLink = document.getElementById("picLink");

		picLink.href = "javascript:HostelPictureWindow('/hostelpictures.php?PicNO="+picID+"')";
		MochiKit.DOM.$(this.parentNode).src = "http://images.hostelworld.com"+img;
		MochiKit.DOM.$(this.parentNode).alt = this.list[index].comment ? this.list[index].comment : "";
		MochiKit.DOM.$(this.parentNode).title = this.list[index].comment ? this.list[index].comment : "";
		this.postSetImageFromList(index);

		return true;
	}

	/**
	 * Opens the large-version of the currently displayed image
	 */
	photogallery.prototype.getCurrentImage = function()
	{
		if(!this.list)
		{
			return null;
		}
		return this.list[this.position];
	}

