
/**
 * Rotate sequence of elements at specific speed 
 * 
 * @Since May 14 2011
 * @Author Dany Moussa  danymoussa(at)gmail.com(dot)com
 */
var marquee = function()
{
	/**
	 * Rotation speed in milliseconds
	 *
	 * @access public
	 * @default 5 seconds (5000 ms)
	 * @type int
	 */
	this.speed = 5000;
	
	
	/**
	 * Tag name of the elements to rotate eg: 'div', 'span'...
	 *
	 * @access public
	 * @default <li>
	 * @type String
	 */
	this.tag_name = 'li';
	
	
	/**
	 * Enable repetition after the rotation is over
	 *
	 * @access public
	 * @default true
	 * @type boolean
	 */
	this.repeat = true;

	
	/**
	 * Children objects of container element
	 *
	 * @access private
	 * @type Array of Objects
	 */
	var children;
	
	
	/**
	 * Masking this for internal usage since this cannot be used in certain cases
	 *
	 * @access private
	 * @type Object
	 */
	var self = this;
	
	
	/**
	 * Play/Start animation of a single list based on container element ID
	 *
	 * @access public
	 * @return void
	 * @param element_id String|int is the container element ID
	 */
	this.play = function (element_id)
	{
		children = document.getElementById(element_id).getElementsByTagName(this.tag_name);
		
		var repeat_delay = children.length*this.speed;
		
		// Display each line
		execute();
		setInterval( function(){ execute() },repeat_delay);
	}

	/**
	 * Display / Hide lines, this method is used by play method
	 *
	 * @access public
	 * @return void
	 */
	var execute = function()
	{
		for( i=0; i < children.length; i++ )
		{
			var delay = self.speed*i;
				
			(function(i){
				setTimeout(
					function(){
						hideAll();
						display(children[i]); 
					}, delay);
			})(i);
		}
	}

	
	/**
	 * Display single node by changing its style display to block
	 * This method is used by Play method
	 *
	 * @access private
	 * @return void
	 * @param node Object
	 */
	var display = function(node)
	{
		node.style.display = 'block';
	}
	
	/**
	 * Hide all children of specific container
	 * This method is used by Play method
	 *
	 * @access private
	 * @return void
	 */
	var hideAll = function()
	{
		for( var i=0; i<children.length; i++ )
		{
			children[i].style.display='none';
		}
	}
}

