/* Static class for the rotating header images */
RotatingHeaderImage = {

	/* Container element */
	_nodeContainer: null,

	/* Header element */
	_nodeHeader: null,

	/* Holds the index for the active header image */
	_currentImage: 0,

	/* Array of header images */
	_images: new Array(),

	/* Interval in seconds for loading the next header image */
	_interval: 3,

	/* Duration in seconds for the fade effect */
	_fadeDuration: 1,

	/**
	 * Sets the interval for loading the next header image.
	 *
	 * @param float interval (in seconds)
	 */
	addImage: function(interval)
	{
		RotatingHeaderImage._interval = interval;
	}, // function addImage

	/**
	 * Sets the duration of the fade effect.
	 *
	 * @param float duration (in seconds)
	 */
	addImage: function(duration)
	{
		RotatingHeaderImage._fadeDuration = duration;
	}, // function addImage

	/**
	 * Adds a new image to the array.
	 *
	 * @param string src
	 */
	addImage: function(src)
	{
		RotatingHeaderImage._images.push(src);
	}, // function addImage

	/**
	 * Starts the header image rotation.
	 *
	 * Should be called on document load.
	 */
	start: function()
	{
		/* Save references to required nodes */
		RotatingHeaderImage._nodeContainer = $("main");
		RotatingHeaderImage._nodeHeader = $("teaser");

		/* Periodically load a new header image */
		new PeriodicalExecuter(RotatingHeaderImage._showNextImage, RotatingHeaderImage._interval);
	}, // function start

	/**
	 * Shows the next image in line.
	 */
	_showNextImage: function()
	{
		/* Move the previous header image to the background */
		RotatingHeaderImage._moveImageToBackground(RotatingHeaderImage._currentImage);

		/* Get the index for the next image in line */
		RotatingHeaderImage._currentImage = RotatingHeaderImage._getNextImageIndex();

		/* Add the next header image to the DOM, if not yet added */
		if (!$("rotate-header-image-" + RotatingHeaderImage._currentImage))
		{
			RotatingHeaderImage._insertImageIntoDOM(RotatingHeaderImage._currentImage);
		}

		/* Fade in the image */
		Effect.Appear("rotate-header-image-" + RotatingHeaderImage._currentImage, {"duration": RotatingHeaderImage._fadeDuration});
	}, // function _showNextImage

	/**
	 * Returns the next image index.
	 *
	 * @return int
	 */
	_getNextImageIndex: function()
	{
		return (RotatingHeaderImage._currentImage < (RotatingHeaderImage._images.length - 1)) ? RotatingHeaderImage._currentImage + 1 : 0;
	}, // function _getNextImageIndex

	/**
	 * Moves an image to the background, hiding the original.
	 */
	_moveImageToBackground: function(i)
	{
		/* Set image as background image */
		RotatingHeaderImage._nodeContainer.style.backgroundImage = "url('" + RotatingHeaderImage._images[i] + "')";

		/* If the image is available in the DOM, hide it */
		if ($("rotate-header-image-" + i))
		{
			$("rotate-header-image-" + i).style.display = "none";
		}
	}, // function _moveImageToBackground

	/**
	 * Inserts an image into the DOM.
	 */
	_insertImageIntoDOM: function(i)
	{
		var image = Builder.node("div", {"id": "rotate-header-image-" + i, "class": "rotate-header-image", "style": "display: none"}, [Builder.node("img", {"src": RotatingHeaderImage._images[i]})]);
		RotatingHeaderImage._nodeContainer.insertBefore(image, RotatingHeaderImage._nodeHeader);
	} // function _insertImageIntoDOM

}; // class RotatingHeaderImage
