/**
 *  Plugin which renders the YouTube channel videos list to the page
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 *  @version: 1.0.0 (Nov/27/2009)
 *	http://yankov.us
 */

 var __preLoaderHTML;
 var __opts;
 var __LeDiv;
 
 function __jQueryYouTubeChannelReceiveData(data) {
  var __mainDiv = jQuery("<div id='ccccc'></div>");
	jQuery.each(data.feed.entry, function(i,e) {
		__mainDiv.append(e.content.$t);
	});
	
	__LeDiv.append("<ul></ul>");
	__lul = __LeDiv.find("ul");
	jQuery(__mainDiv).find("div").each(function () {
	  var anch = jQuery(this).find("table > tbody > tr:first > td:eq(1) > div > a");
	  anch.removeAttr("style");
    if (anch.length) __lul.append(jQuery("<li></li>").append(anch));
	});
	// Remove the preloader and show the content
	jQuery(__preLoaderHTML).remove();
	__LeDiv.show();
	__LeDiv.find("ul").ytplaylist();
}
				
(function(jQuery) {
jQuery.fn.youTubeChannel = function(options) {
	var videoDiv = jQuery(this);

	jQuery.fn.youTubeChannel.defaults = {
		userName: null,
		loadingText: "Chargement...",
		linksInNewWindow: true,
		hideVideoLength: true,
		hideFrom: true,
		hideViews: true,
		hideRating: true,
		hideNumberOfRatings: true,
		removeBordersFromImage: true
	}
			
  __opts = jQuery.extend({}, jQuery.fn.youTubeChannel.defaults, options);
	
	return this.each(function() {
		if (__opts.userName != null) {
		  videoDiv.append("<div id=\"ytvideo\" style='float:left'></div>");			
			videoDiv.append("<div id=\"channel_div\"></div>");
			__LeDiv = jQuery("#channel_div");
			__LeDiv.hide();
			
			__preLoaderHTML = jQuery("<p class=\"loader\">" + __opts.loadingText + "</p>");
			videoDiv.append(__preLoaderHTML);
			
			// TODO: Error handling!
			jQuery.getScript("http://gdata.youtube.com/feeds/base/users/" + __opts.userName + "/uploads?alt=json-in-script&callback=__jQueryYouTubeChannelReceiveData");
		}
	});
};
})(jQuery);
