/**
 *  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 __mainDiv;
 var __preLoaderHTML;
 var __opts;
 
 function __jQueryYouTubeChannelReceiveData(data) {
    var videoCount = 0;
 
	$.each(data.feed.entry, function(i,e) {
		__mainDiv.append(e.content.$t);
		
		videoCount = videoCount + 1;
		if (videoCount == 4)
		{
		    return false;
		}
	});
	
	// Mark the first div in the list
	$(__mainDiv).find("div:first").addClass("firstVideo");
	
	$(__mainDiv).find("> div").css("width", "275px");

	
	// Now hide objects depending on the settings and fix the img style
	$(__mainDiv).find("div").each(function () {
	
	    // TO ADD MORE
	    // Create a new if statement if it might be turned on or off, otherwise write out the query line
		/*
		if (__opts.removeDescription) {
		    
		    // A typical query line, finds through the various tags, can use :first and :nth-child etc.
		    // .hide() will hide something, .css allows for a stylesheet controls to be used
		    // Don't use "color: red", instead split them .css("color", "red") and do one per line
		    // A link to a stylesheet could be done with .attr("class", "cssClassName")
			jQuery(this).find("table > tbody > tr:first > td:nth-child(2) > div:nth-child(2)").hide();
		}
	    */
	
	
		// 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");
		}
		
		jQuery(this).find("table").addClass("noborders");
		
		// Remove description text
		if (__opts.removeDescription) {
			jQuery(this).find("table > tbody > tr:first > td:nth-child(2) > div:nth-child(2)").hide();
		}
		
		// Set title style
		if (__opts.titleStyle) {
			jQuery(this).find("table > tbody > tr:first > td:nth-child(2) > div:nth-child(1) > a").attr("class", "youtubeTitle");
			jQuery(this).find("table > tbody > tr:first > td:nth-child(2) > div:nth-child(1) > a").css("font-size", "1em");			
			jQuery(this).find("table > tbody > tr:first > td:nth-child(2) > div:nth-child(1) > a").css("font-weight", "bold");
		}
		
		if (__opts.imageSize) {
			jQuery(this).find("table > tbody > tr:first > td:first > div > a > img").css("height", "64px");
			jQuery(this).find("table > tbody > tr:first > td:first > div > a > img").css("width", "90px");
			
			// Image Cell Fix
		    jQuery(this).find("table > tbody > tr:first > td:first").css("width", "90px");
		    jQuery(this).find("table > tbody > tr:first > td:first").css("padding", "0 5px 5px 0");
		    jQuery(this).find("table > tbody > tr:first > td:first > div").css("margin", "0");
		}
		
		jQuery(this).find("table > tbody > tr:first > td:nth-child(2)").css("width", "175px");
		
		jQuery(this).find("table > tbody > tr:first > td:last").css("width", "0");
		jQuery(this).find("table > tbody > tr:first > td:last").css("padding", "0");
		
		
		// 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");
		}
	
		// Hide the video length
		if (__opts.hideVideoLength)
			jQuery(this).find("table > tbody > tr:last > td:first").hide();
			jQuery(this).find("table > tbody > tr:last > td:first > span:first").hide();
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("margin", "-21px 0px 0px -37px");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("display", "block");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("text-align", "right");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("width", "29px");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("position", "relative");
			
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("color", "#000000");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("text-shadow", "#666666 0 -1px 1px");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("padding", "1px");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("background-color", "white");
			jQuery(this).find("table > tbody > tr:last > td:first > span:last").css("opacity", "0.8");
			
			
		var additionalInfo = jQuery(this).find("table > tbody > tr:first > td:last");
		
		if (__opts.hideFrom)
			$(additionalInfo).find("div:eq(0)").hide();
		
		if (__opts.hideViews)
			$(additionalInfo).find("div:eq(1)").hide();
			$(additionalInfo).find("div:eq(1)").css("margin", "47px 0pt 0pt -179px");
			$(additionalInfo).find("div:eq(1)").css("width", "100px");
			$(additionalInfo).find("div:eq(1)").css("font-weight", "bold");
			$(additionalInfo).find("div:eq(1) > span").css("font-weight", "normal");
			$(additionalInfo).find("div:eq(1)").css("color", "#333333");
			$(additionalInfo).find("div:eq(1) > span").css("color", "#333333");
		
		if (__opts.hideRating)
			$(additionalInfo).find("div:eq(2)").hide();
			
		if (__opts.hideNumberOfRatings)
			$(additionalInfo).find("div:eq(3)").hide();
			
		// Always hide the additional categories
		jQuery(this).find("table > tbody > tr:last  > td:last").hide();
	});
	
	// Remove the preloader and show the content
	$(__preLoaderHTML).remove();
	__mainDiv.show();
}
				
(function($) {
$.fn.youTubeChannel = function(options) {
	var videoDiv = $(this);

	$.fn.youTubeChannel.defaults = {
		userName: null,
		loadingText: "Loading...",
		linksInNewWindow: true,
		hideVideoLength: false,
		hideFrom: true,
		hideViews: false,
		hideRating: false,
		hideNumberOfRatings: true,
		removeBordersFromImage: true
	}
			
    __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!
			$.getScript("http://gdata.youtube.com/feeds/base/users/" + __opts.userName + "/uploads?alt=json-in-script&callback=__jQueryYouTubeChannelReceiveData");
		}
		
	});
};
})(jQuery);
