/**
 *  Plugin which embed the last video from a channel
 *  @author:  andre
 *  @version: 1.0.0 (Nov/27/2009)
 */

 var __mainDiv;
 var __preLoaderHTML;
 var __opts;
 
function __jQueryYouTubeChannelReceiveData(data) {
	$.each(data.feed.entry, function(i,e) {
		$(e.content.$t).find('a[href^=http\:\/\/www\.youtube\.com\/watch?v=]').each(function(){
			YoutubeId = $(this).attr('href').split('?v=')[1];
			YoutubeId = YoutubeId.split('&')[0];
		});
		__mainDiv.flash({height: 204, width: 254 },{ version: 8 },
			function(htmlOptions){
				$this = $(this);
				htmlOptions.src = 'http://www.youtube.com/v/'+YoutubeId;
				$this.before($.fn.flash.transform(htmlOptions));						
			}
		);
	});
	$(__preLoaderHTML).remove();
	__mainDiv.show();
}
				
(function($) {
$.fn.youTubeChannel = function(options) {
	var videoDiv = $(this);

	$.fn.youTubeChannel.defaults = {
		userName: null,
		loadingText: "Loading..."
	}
			
    __opts = $.extend({}, $.fn.youTubeChannel.defaults, options);
	
	return this.each(function() {
		if (__opts.userName != null) {			
			videoDiv.append("<div id=\"channel_div\"></div>");
			__mainDiv = $("#channel_div");
			__mainDiv.hide();
			__preLoaderHTML = $("<p class=\"loader\">" + __opts.loadingText + "</p>");
			videoDiv.append(__preLoaderHTML);
			$.getScript("http://gdata.youtube.com/feeds/base/users/" + __opts.userName + "/uploads?alt=json-in-script&orderby=updated&max-results=1&format=5&callback=__jQueryYouTubeChannelReceiveData");
		}
	});
};
})(jQuery);
