// JavaScript Document
/**
 * WidgetCarousel
 * Changes the source of the given iframe with next and previous
 * buttons. (Primarily used within the Video Widget Carousel menu)
 *
 * @uses jquery-1.3.2
 */
function WidgetCarousel(iframe, ul)
{
	/**
	 * iFrame HTML element
	 *
	 * @var HTML_Element
	 */
	this.iframe = $(iframe);
	
	/**
	 * UL HTML element containing a list of all available
	 * items to cycle through
	 *
	 * @var HTML_Element
	 */
	this.ul = $(ul);
	
	/**
	 * Generated DIV HTML_Element to contain the control buttons
	 *
	 * @var HTML_Element
	 */
	this.js;
	
	/**
	 * _init
	 *
	 * Initialisation method. Generates the js DIV wrapper around the iframe
	 * and compiles the next and previous buttons with appropriate event handlers
	 *
	 * @return void
	 */
	this._init	= function()
	{
		var t	= this;
		
		$(this.ul).hide();
		$(this.iframe).wrap("<div class='js'></div>");
		$(this.iframe).before("<div class='prev'></div>");
		$(this.iframe).after("<div class='next'></div>");
		
		this.js	= $(this.iframe).parent('.js');
		
		$(this.js).find(".prev").html(this.prev());
		$(this.js).find(".next").html(this.next());
		
		$(this.js).find("a").click(function(){return t.butt(this);});
	}
	
	/**
	 * _getItemId
	 * Fetches the id of the current playing widget
	 *
	 * @return int
	 */
	this._getItemId	= function()
	{
		var itemId = parseInt($(this.iframe).attr('id').replace('widgetItem_', ''));
		if( !itemId ) itemId = 0;
		return itemId;
	}
	
	/**
	 * butt
	 * Event handler for when the next and previous buttons are clicked
	 * Changes the source of the iframe
	 *
	 * @return void
	 */
	this.butt	= function(a)
	{
		var t = this, itemId = this._getItemId();
	
		switch ($(a).attr('title')) {
			case 'Next':
				$(this.iframe).attr('id', 'widgetItem_' + (itemId + 1));
			break;
			case 'Previous':
				$(this.iframe).attr('id', 'widgetItem_' + (itemId - 1));
			break;
		}
		
		$(this.js).find(".prev").html(this.prev());
		$(this.js).find(".next").html(this.next());
		$(this.js).find("a").click(function(){return t.butt(this);});
	}
	
	/**
	 * next
	 * Generates the HTML for the next button
	 *
	 * @return string
	 */
	this.next	= function()
	{
		var url, itemId = this._getItemId();
		
		url	= $(this.ul).find("li a.widgetItem_" + (itemId+1)).attr('href');
		
		if (url) {
			return '<a href="' + url + '" title="Next" tabindex="0" target="' + $(this.iframe).attr('name') + '"><img src="/images/frontend/movieArrow_right.png" alt="Next"/></a>';
		} else {
			return '<img src="/images/frontend/movieArrow_right_grey.png" alt="Next"/>';
		}
	}
	
	/**
	 * prev
	 * Generates the HTML for the previous button
	 *
	 * @return string
	 */
	this.prev	= function()
	{
		var url, itemId = this._getItemId();
		
		url	= $(this.ul).find("li a.widgetItem_" + (itemId-1)).attr('href');
		
		if (url) {
			return '<a href="' + url + '" title="Previous" tabindex="0" target="' + $(this.iframe).attr('name') + '"><img src="/images/frontend/movieArrow_left.png" alt="Previous"/></a>';
		} else {
			return '<img src="/images/frontend/movieArrow_left_grey.png" alt="Previous"/>';
		}
	}

	
	/**
	 * Initialise the _init method
	 */
	this._init();
}