/**
 * Image switcher class
 */
Images = {

	_links: new Array(),

	/**
	 * Initializes the image switcher.
	 */
	init: function()
	{
		if ($("fotonavi") != undefined)
		{
			Images._links = Element.getElementsBySelector($("fotonavi"), "a");
			for (var i = 0; i < Images._links.length; i++)
			{
				/* click changes big image */
				Element.observe(Images._links[i], "click", Images.handleClick.bind(Images._links[i]));
	
				/* dim images on load (if active, won't be dimmed) */
				Images.dimImage.bind(Images._links[i])();
	
				/* mouse over and mouse out change opacity */
				Element.observe(Images._links[i], "mouseover", Images.highlightImage.bind(Images._links[i]));
				Element.observe(Images._links[i], "mouseout", Images.dimImage.bind(Images._links[i]));
			}
		}
	}, // function init

	/**
	 * Handles the onclick event.
	 */
	handleClick: function(e)
	{
		/* link go to the link location */
		Event.stop(e);

		/* but show the images we linked to */
		Images.showImage(this.href);

		/* dim all image (active won't be dimmed) */
		for (var i = 0; i < Images._links.length; i++)
		{
			Images.dimImage.bind(Images._links[i])();
		}
	}, // function handleClick

	/**
	 * Shows an image.
	 *
	 * @param	string	url
	 */
	showImage: function(url)
	{
		/* set src on big image */
		var bigImage = Element.getElementsBySelector($("fotogroot"), "img");
		if (bigImage.length == 1)
		{
			bigImage[0].src = url;
		}
	}, // function showImage

	/**
	 * Highlights a thumbnail image.
	 */
	highlightImage: function(e)
	{
		var image = Element.getElementsBySelector(this, "img");
		if (image.length == 1)
		{
			image[0].setOpacity(1);
		}
	}, // function highlightImage

	/**
	 * Dims a thumbnail image.
	 */
	dimImage: function(e)
	{
		var image = Element.getElementsBySelector(this, "img");
		if (image.length == 1)
		{
			/* only dim if not the active image */
			if (!Images.isEnlarged(this.href))
			{
				image[0].setOpacity(0.5);
			}
			else
			{
				image[0].setOpacity(1);
			}
		}
	}, // function dimImage

	/**
	 * Checks if the umage with the URL is the enlarged image.
	 *
	 * @param	string	url
	 *
	 * @return	boolean
	 */
	isEnlarged: function(url)
	{
		var bigImage = Element.getElementsBySelector($("fotogroot"), "img");
		if (bigImage.length == 1)
		{
			return (bigImage[0].src == url);
		}
	} // function isEnlarged

}; // class Images

/* Initialise image switcher on load */
Element.observe(window, "load", Images.init);