/*
 * tmtfactory website
 * ------------------
 * $Id: newsticker.js,v 1.3 2009-10-28 16:16:38 aruzafa Exp $
 */

/**
 *  @file
 *  @brief JavaScript file for the NewsTicker widget.
 */

/**
 *  How many pixeles the newsitem are scrolled each iteration by default.
 */
var NEWS_TICKER_DEFAULT_SCROLL_VELOCITY = 3;

/**
 *  How often the newsitems are scrolled by default.
 */
var NEWS_TICKER_DEFAULT_SCROLL_FREQUENCY = 0.1;

/**
 *  Separation in pixeles between newsitems by default.
 */
var NEWS_TICKER_DEFAULT_ITEMS_SEPARATION = 20;

/**
 *  @fn
 *  @brief Create a new NewsTicker object.
 */
var NewsTicker = Class.create
({
/* --[ Properties ]-------------------------------------------------- */
	
	/**
	 *  @var
	 *  @brief The element tag binded to this NewsTicker.
	 */
	ticker: null,
	
	/**
	 *  @var
	 *  @brief The element tag that serves as viewport in the document.
	 */
	viewport: null,
	
	/**
	 *  @var
	 *  @brief Boolean to indicate if the newsticker is currently scrolling.
	 */
	is_playing: false,
	
	/**
	 *  @var
	 *  @brief How many pixeles the newsitem are scrolled each iteration.
	 */
	scroll_velocity: NEWS_TICKER_DEFAULT_SCROLL_VELOCITY,
	
	/**
	 *  @var
	 *  @brief How often the newsitems are scrolled.
	 */
	scroll_frequency: NEWS_TICKER_DEFAULT_SCROLL_FREQUENCY,
	
	/**
	 *  @var
	 *  @brief Separation in pixeles between newsitems by default.
	 */
	items_separation: NEWS_TICKER_DEFAULT_ITEMS_SEPARATION,
	
	/**
	 *  @fn
	 *  @brief Initialize a new instance of a NewsTicker object.
	 *  @param ticker The element tag binded to this NewsTicker.
	 */
	initialize: function (ticker)
	{
		this.ticker = ticker;
		
		/*
		 *  Bind back this object to the element tag.
		 */
		this.ticker.newsticker = this;
		
		this.viewport = Selector.findChildElements(ticker, [".viewport"]);
		this.viewport = (this.viewport.length > 0) ? this.viewport[0] : null;
		
		var items = Selector.findChildElements(this.viewport, [".item"]);

	},
	
	/**
	 *  @fn
	 *  @brief Arrange the child newsitems horizontally.
	 */
	positionate: function ()
	{
		var i;
		var x = this.viewport.getWidth();
		var items = Selector.findChildElements(this.viewport, [".item"]);
		for (i = 0; i < items.length; i++)
		{
			items[i].style.marginLeft = x + "px";
			x += items[i].getWidth() + this.items_separation;
			
			if (i % 2 == 0)
			{
				items[i].removeClassName("alt");
			}
			else
			{
				items[i].addClassName("alt");
			}
		}
	},
	
	/**
	 *  @fn
	 *  @brief Perform a single step scroll for all the newsitems.
	 * 
	 *  If a newsitem reach the end of the viewport and gets hidden then
	 *  is rearranged to the end of the last newsitem.
	 */
	step_scroll: function ()
	{
		var items = Selector.findChildElements(this.viewport, [".item"]);
		var x = 0;
		
		if (items.length > 0)
		{
			/*
			 * Scroll the news.
			 */
			for (i = 0; i < items.length; i++)
			{
				var x = parseInt(items[i].style.marginLeft);
				items[i].style.marginLeft = (x - this.scroll_velocity) + "px";
				
			}
			if (parseInt(items[0].style.marginLeft) + items[0].getWidth() < 0)
			{
				var item = items[0];
				var last_item = items[items.length - 1];
				var last_x = parseInt(last_item.style.marginLeft) + last_item.getWidth();
				this.viewport.removeChild(item);
				this.viewport.appendChild(item);
				item.style.marginLeft = Math.max(this.viewport.getWidth(), last_x) +
				    this.items_separation + "px";
				
				/*
				 * Alterning the .alt classname if there are a odd number
				 * of newsitems > 1
				 */ 
				if (items.length > 1 && items.length % 2 == 1)
				{
					if (item.hasClassName("alt"))
					{
						item.removeClassName("alt");
					}
					else
					{
						item.addClassName("alt");
					}
				}
			}
		}
	},
	
	/**
	 *  @fn
	 *  @brief Start the newsitem scrolling.
	 */
	play: function ()
	{
		this.is_playing = true;
		new PeriodicalExecuterUserData
		(
			function (pe)
			{
				pe.userdata.step_scroll();
				if (!pe.userdata.is_playing)
				{
					pe.stop();
				}
			},
			this.scroll_frequency,
			this
		);
	},
	
	/**
	 *  @fn
	 *  @brief Stop the newsitems scrolling.
	 */
	stop: function ()
	{
		this.is_playing = false;
	}
});

/**
 *  @fn
 *  @brief "Static" function that listen events to play the newsticker.
 *  @param e Javascript events
 */
newsticker_play = function (e)
{
	var element = e.findElement();
	if (element != null && element.newsticker != null)
	{
		element.newsticker.play();
	}
}

/**
 *  @fn
 *  @brief "Static" function that listen events to stop the newsticker.
 *  @param e Javascript events
 */
newsticker_stop = function (e)
{
	var element = e.findElement();
	if (element != null && element.newsticker != null)
	{
		element.newsticker.stop();
	}
}

/**
 *  @fn
 *  @brief "Static" function that listen events to create all the newstickers.
 *  @param e Javascript events
 */
newsticker_loader = function (e)
{
	var newstickers = $$(".widget.newsticker");
	var i;
	for (i = 0; i < newstickers.length; i++)
	{
		var newsticker = new NewsTicker(newstickers[i]);
//		Event.observe(newstickers[i], "mouseout", newsticker_play);
//		Event.observe(newstickers[i], "mouseover", newsticker_stop);
		newsticker.positionate();
		newsticker.play();
	}
}

/*
 * Trigger the creation and playing of all newstickers.
 */
Event.observe(window, "load", newsticker_loader);

