/**
 *  Plugin which embed the last video from a channel
 *  @author:  andre
 *  @version: 1.0.0 (Nov/27/2009)
 */

 var __mainDiv;
 var __preLoaderHTML;
 var __opts;
 var __width;
 var __height;
 
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];
		});
		if(__lastVideo){
			__mainDiv.flash({
				swf:'http://www.youtube.com/v/'+YoutubeId+'&hl=pt_BR&fs=1',
				height: __height, width: __width,
				allowFullScreen: true,
				allowScriptAccess: 'always'
			});	
		}
		else{
			__mainDiv.append(e.content.$t);
		}
		
	});
	
	// Mark the first div in the list
	$(__mainDiv).find("div:first").addClass("firstVideo");
	
	// Now hide objects depending on the settings and fix the img style
	$(__mainDiv).find("div").each(function () {
		// Do we need to remove borders from image?
		if (__opts.removeBordersFromImage) {
			jQuery(this).find("table > tbody > tr:first > td:first > div").css("border", "0px");
			jQuery(this).find("table > tbody > tr:first > td:first > div > a > img").css("border", "0px");
		}
		
		// Open in new tab?
		if (__opts.linksInNewWindow) {
			jQuery(this).find("table > tbody > tr:first > td:first > div > a").attr("target", "_blank");
			jQuery(this).find("table > tbody > tr:first > td:eq(1)  > div > a").attr("target", "_blank");
		}
		_elVideoLength = jQuery(this).find("table > tbody > tr:last > td:first")
		_elVideoLength.text(_elVideoLength.text().replace('Time:','Duração:'));
		// Hide the video length
		if (__opts.hideVideoLength) {
			jQuery(_elVideoLength).hide();
		}
		
		var additionalInfo = jQuery(this).find("table > tbody > tr:first > td:last");
		
		_elFrom = $(additionalInfo).find("div:eq(0)");
		if (__opts.hideFrom){
			$(_elFrom).hide();
		}
		$(_elFrom).find('span:eq(0)').text($(_elFrom).find('span:eq(0)').text().replace("From:","Enviado por:"));
		
		_elViews = $(additionalInfo).find("div:eq(1)");
		if (__opts.hideViews){
			$(_elViews).hide();
		}
		$(_elViews).find('span:eq(0)').text($(_elViews).find('span:eq(0)').text().replace("Views:","Visualizações:"));
		
		_elRating = $(additionalInfo).find("div:eq(2)");
		if (__opts.hideRating) {
			$(_elRating).hide();
		}
		_elNumberOfRating = $(additionalInfo).find("div:eq(3)");
		if (__opts.hideNumberOfRatings) {
			$(additionalInfo).find("div:eq(3)").hide();
		}
		$(_elNumberOfRating).find('span:eq(0)').text($(_elNumberOfRating).find('span:eq(0)').text().replace("ratings","Avaliações:"));			

			
		// Always hide the additional categories
		jQuery(this).find("table > tbody > tr:last  > td:last").hide();
	});
	
	$(__preLoaderHTML).remove();
	__mainDiv.show();
}
				
(function($) {
$.fn.youTubeChannel = function(options) {
	var videoDiv = $(this);

	$.fn.youTubeChannel.defaults = {
		userName				: null,
		lastVideo				: false,
		width					: 200,
		height					: 200,
		maxResults				: 8,
		loadingText	    		: "Loading...",
		linksInNewWindow		: true,
		hideVideoLength			: false,
		hideFrom				: true,
		hideViews				: true,
		hideRating				: true,
		hideNumberOfRatings		: true,
		removeBordersFromImage	: true
	}
			
    __opts = $.extend({}, $.fn.youTubeChannel.defaults, options);
    
	__width 	= __opts.width;
    __height	= __opts.height;
    __lastVideo	= __opts.lastVideo;
    if(__opts.lastVideo){
    	__opts.maxResults = 1;
    }	
	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="+__opts.maxResults+"&format=5&callback=__jQueryYouTubeChannelReceiveData");
		}
	});
};
})(jQuery);
