// Declare variables
var youTube;

/**
 * Tv object
 *
 * @param string closeCallbackFunction
 */
var Tv = function( closeCallbackFunction )
{
	var _hoverContainer = $('#hoverContainer');
	var _closeCallbackFunction = closeCallbackFunction;

	/**
	 * Initialisation
	 *
	 */
	this.init = function()
	{
	}

	/**
	 * Displays the hover for the tv options
	 */
	this.show = function()
	{
		var self = this;

		_hoverContainer.load('/tv/hover/', function()
		{
			centerPanel(_hoverContainer);

			// Fade in the container for the form
			_hoverContainer.fadeIn(1200, function(){

				// Load the videos in the channel
				youTube = new YouTube('murdochdigitalmedia');
				youTube.loadChannelListing();

				// Ensure the close button closes the hover
				$("#btnCloseShare").click( function()
				{
					self.close();
				});
			});

			// Ensure the close button closes the hover
			$("#btnClose").click( function()
			{
				self.close();
			});

		});
	}

	/**
	 * Closes the Tv Options hover
	 */
	this.close = function()
	{
		$('#movieContainer').remove();

		_hoverContainer.fadeOut(800, function()
		{
			_hoverContainer.empty();

			// Issue the callback function if exists
			if( typeof _closeCallbackFunction == 'function' )
			{
				_closeCallbackFunction();
			}
		});
	}

	// Initialise
	this.init();
}


/**
 * YouTube object
 */
var YouTube = function(username)
{
	// Declare variables
	var _username = username;
	var _channelDivUl = $('div#channelContainer ul');

	/**
	 * Get channel listing
	 */
	this.loadChannelListing = function()
	{
		$.getScript('http://gdata.youtube.com/feeds/base/users/' + _username + '/uploads?v=2&alt=json-in-script&format=5&callback=youTube.displayChannelListing');
	}

	/**
	 * Display Channel Listing
	 *
	 * @param object data
	 */
	this.displayChannelListing = function(data)
	{
		// Declare variables
		var _self = this;
		var feed = data.feed;
		var entries = feed.entry || [];

		// Iterate over videos
		for( var i = 0; i < entries.length; i++ )
		{
			// Declare variables
			var entry = entries[i];
			var id = entry.id.$t;

			// Find movie Id
			id = id.substring(id.indexOf(':video:') + 7);

			// Build LI for listing
			var html = this.buildChannelListing(id, entry.title.$t);

			html.find('img').click(function(){
				_self.loadMovie($(this).parent().attr('id'));
			});

			_channelDivUl.append(html);
		}

		// Load the first movie
		this.loadMovie(_channelDivUl.find('li:eq(0)').attr('id'));

		// Make pretty scrollabrs
		$('#channelContainer div').jScrollPane({scrollbarWidth: 39, scrollbarMargin: 10, topCapHeight: 16, bottomCapHeight: 16, dragMaxHeight: 52});
	}

	/**
	 * Build display listing
	 *
	 * @param int id
	 * @param string title
	 */
	this.buildChannelListing = function(id, title)
	{
		// Declare variables
		var html = '';

		html += '<li id="' + id + '">';
		html += '<h3>' + title + '</h3>';
		html += '<img src="http://i.ytimg.com/vi/' + id + '/default.jpg" width="120" height="90" alt="' + title + '" title="' + title + '"/>';
		html += '</li>';

		return $(html);
	}

	/**
	 * Load movie
	 *
	 * @param int id
	 */
	this.loadMovie = function(id)
	{
		// Declare variables
		var html = '';

		$('div#movieContainer .movie').fadeOut('5000', function(){

			html += '<object type="application/x-shockwave-flash" width="550" height="334" data="http://www.youtube.com/v/' + id + '">';
			html += '<param name="movie" value="http://www.youtube.com/v/' + id + '"></param>';
			html += '</object>';
			html += '<h2>' + $('li#' + id + ' h3').html() + '</h2>';

			$('div#movieContainer .movie').html(html);

			$('div#movieContainer .movie').fadeIn('5000');
		});
	}
}
