/***************************************************************
* Copyright notice
*
* (c) 2006 Oliver Klee (typo3-coding@oliverklee.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * Initializes the marquee and sets the timer for the animation.
 *
 * @param	string		type of marquee (either "smooth" or "discrete")
 * @param	string		ID of the inner scrolling container
 * @param	integer		number of ms between each animation frame
 * @param	integer		number of ms before the first animation frame
 * @param	integer		the number of images to advance in one step for the discrete animation
 * @param	integer		width (in pixels) of one set of images
 *
 * @access	public
 */
function Marquee(animationType, elementId, animationDelay, startDelay, stepsize, linkWidth) {
	var scrollingContainer;
	var animationDelay;
	var scrollPosition;
	var linkWidth;
	var animationType;
	var stepsize;

	/**
	 * Advances the smooth animation by one frame.
	 *
	 * @access	public
	 */
	this.animateSmooth = function() {
		this.scrollPosition--;
		// If we have scrolled just completely out of view, reset the scroll position.
		if (this.scrollPosition == - this.linkWidth) {
			this.scrollPosition = 0;
		}
		this.scrollingContainer.style.left = this.scrollPosition + "px";
		this.setTimer(this.animationDelay);

		return;
	}

	/**
	 * Advances the discrete animation by one image.
	 *
	 * @access	public
	 */
	this.animateDiscrete = function() {
		for (i = 0; i < this.stepsize; i++) {
			var firstChild = this.getFirstChildElement(this.scrollingContainer);
			if (firstChild) {
				this.scrollingContainer.appendChild(firstChild);
			}
		}
		this.setTimer(this.animationDelay);

		return;
	}

	/**
	 * Sets the animation timer for another turn.
	 * The function that will be called on timeout depends on the marquee type.
	 *
	 * @param	integer		timeout in ms
	 *
	 * @access	private
	 */
	this.setTimer = function(delay) {
		// Safeguard against too small animation steps.
		var delayToUse = delay;
		if (delayToUse < 10) {
			delayToUse = 10;
		}

		var thisObject = this;
		switch (this.animationType) {
			case "discrete":
				setTimeout(function() {thisObject.animateDiscrete();}, delayToUse);
				break;
			case "smooth":
			default:
				setTimeout(function() {thisObject.animateSmooth();}, delayToUse);
				break;
		}
		return;
	}

	/**
	 * Gets the first child that is an element(will be null if there is no such child).
	 * This function is needed because in some cases there are
	 * empty TEXT nodes as first childs.
	 *
	 * @return	object		DOM element
	 *
	 * @access	private
	 */
	this.getFirstChildElement = function(parent) {
		var element = null;
		var childNodes = parent.childNodes;
		for (currentChild = 0; currentChild < childNodes.length; currentChild++) {
			 if (childNodes[currentChild].nodeType == 1) {
			 	element = childNodes[currentChild];
			 	break;
			 }
		}

		return element;
	}

	// Only continue if the browser supports DOM scripting
	// and the element to use exists.
	if (document.getElementById && document.getElementById(elementId)) {
		this.scrollPosition = 0;
		this.animationType = animationType;
		this.scrollingContainer = document.getElementById(elementId);
		this.animationDelay = animationDelay;
		this.linkWidth = linkWidth;
		this.stepsize = (stepsize >= 1) ? stepsize : 1;
		this.setTimer(startDelay);
	}

	return;
}
