function __jQueryGetLastUpdatedPlaylist(data) {
	 	var latestChannel;
		$.each(data.feed.entry, function(i,e) {			
			if(latestChannel==undefined){
				latestChannel=e;
			}
			else{
				var latest = isoDateReviver(latestChannel.updated.$t);
				var date = isoDateReviver(e.updated.$t);
				if(date>latest){
					latestChannel=e;
				}
			}
		});
		var url = latestChannel.gd$feedLink[0].href+"?orderby=published&v=2&alt=jsonc-in-script&callback=__jQueryGetLastVideo&max-results=1";
		// alert(url);
		//$.getScript(url);
		$.ajax({ type: "GET",
            url: url,
            dataType: "script",
            success: function(data){
               eval(data);
              },
            error: function(data){
                  alert(data.responseXML);
                 }
            });
 }
 
 function __jQueryGetLastVideo(data) {
		$.each(data.data.items, function(i,e) {
			var duration = secondsToTime(e.video.duration);
			var html = "<div id='video'><div id='videoThumbnail' style='width:120px;height:90px;background:URL(\""+e.video.thumbnail.sqDefault+"\");'><div id='videoLength'>"+duration.m+":"+duration.s+"</div></div>" +
					   "<div id='videoInfo'><div id='videoTitle'>"+e.video.title+"</div></div></div>";		 
			$(__mainDiv).html(html);
		});
	 
 }
 			
(function($) {
	$.fn.getLatestVideoForChannel = function(options) {
		__mainDiv = $(this);
	
		return this.each(function() {
			if (options.userName != null) {							
				__preLoaderHTML = $("<img alt='cargando' src='img/ajax-loader.gif'></img>");
				__mainDiv.html(__preLoaderHTML);
				// TODO: Error handling!
				//$.getScript("http://gdata.youtube.com/feeds/api/users/" + options.userName + "/playlists?alt=json-in-script&callback=__jQueryGetLastUpdatedPlaylist");
				$.ajax({ type: "GET",
                    url: 'http://gdata.youtube.com/feeds/api/users/' + options.userName + '/playlists?alt=json-in-script&callback=__jQueryGetLastUpdatedPlaylist',
                    dataType: "script",
                    cache: false,
                    success: function(data){
                       eval(data);
                      },
                    error: function(data){
                    	  alert("error: "+data.responseText);
                         }
                    });
				
			}
		});
	};
	})(jQuery);