
var __mainDiv; 
var __preLoaderHTML; 
var __opts; 

function __jQueryYouTubeChannelReceiveData(data) { 

var cnt = 0; 

$.each(data.feed.entry, function (i, e) { 
if (cnt < __opts.numberToDisplay) { 
var parts = e.id.$t.split( '/' ); 
var videoId = parts[parts.length-1]; 
var out = '<div class="video"><a target="_blank" href="' + 
e.link[0].href + '"><img src="http://i.ytimg.com/vi/' + 
videoId + '/2.jpg" style="width:110px;float:left;margin-right:8px;"/></a><a target="_blank" href="' + 
 e.link[0].href + '">' + e.title.$t + '</a><p><br clear="all"/><br/>' ; 
if (!__opts.hideAuthor) { 
out = out + 'Author: ' + e.author[0].name.$t + '' ; 
} 
out = out + '</p></div>' ; 
__mainDiv.append( out ); 
cnt = cnt + 1; 
} 
}); 

// Open in new tab? 
if (__opts.linksInNewWindow) { 
$(__mainDiv).find( "li > a" ).attr( "target" , "_blank" ); 
} 

// Remove the preloader and show the content 
$(__preLoaderHTML).remove(); 
__mainDiv.show(); 
} 

( function ($) { 
$.fn.youTubeChannel = function (options) { 
var videoDiv = $( this ); 

$.fn.youTubeChannel.defaults = { 
 userName: null, userName: null , 
channel: "favorites" , //options are favorites or uploads 
loadingText: "Carregando..." , 
numberToDisplay: 3, 
linksInNewWindow: true , 
hideAuthor: false 
} 

__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); 

// TODO: Error handling! 
$.ajax({ 
url: "http://gdata.youtube.com/feeds/base/users/" + 
__opts.userName + "/" + __opts.channel + "?alt=json" , 
cache: true , 
dataType: 'jsonp' , 
success: __jQueryYouTubeChannelReceiveData 
}); 
} 
}); 
}; 
})(jQuery); 
