/**
 *  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
 */
 
 function convertToList(element) {
    var list = $("<ul/>");

    $(element).find("tr").each(function() {
        var p = $(this).children().map(function() {
            return "<p>" + $(this).html() + "</p>";
        });

        list.append("<li>" + $.makeArray(p).join("") + "</li>");
    });

    $(element).replaceWith(list);
 }



 var __mainDiv;
 var __preLoaderHTML;
 var __opts;
 
 function __jQueryYouTubeChannelReceiveData(data) {
	$.each(data.feed.entry, function(i,e) {
		__mainDiv.append(e.content.$t);
	});
	
	// Mark the first div in the list
	//$(__mainDiv).find("div:first").addClass("firstVideo");
	
	
	//for each video div
	$(__mainDiv).find("div").each(function () {
		
		var _div = jQuery(this);
		
		//strip out everything apart from the image and title
		_div.find("table > tbody > tr:first > td:eq(1) > div:eq(1)").remove(); //2nd div in 2nd td in 1st tr
		_div.find("table > tbody > tr:first > td:eq(2)").remove(); //3rd td in 1st tr
		_div.find("table > tbody > tr:eq(1)").remove(); //2nd tr
		
		//remove all the extraneous html
		_div.find("tbody").unwrap(); //remove the table
		_div.find("tr").unwrap(); //remove the tbody
		_div.find("td").unwrap(); //remove the tr
		_div.find("div").unwrap(); //remove the tds
		_div.find("a").unwrap(); //remove the divs
		
		_div.addClass('clearfix'); //add clearfix to the div
		
		//convert link to the youtube page into link to the video itself
		_div.find("a").each(function() {
			var href = jQuery(this).attr("href");
			href = href.replace(/watch/i, "watch_popup");
			href = href.replace(/feature=youtube_gdata/i, "vq=medium");
			jQuery(this).attr("href", href);
		});
		
		_div.find("img").each(function() {
		   var src = jQuery(this).attr("src");
		   src = src.replace(/0.jpg/, "2.jpg");
		   jQuery(this).attr("src", src); 
		});
		
		_div.find("a").attr("class", "fancyyoutube"); //add fancybox class to the link
		
	});
	
	
	// 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");
		}
		
		//Hide descriptions
		if (__opts.hideDescription)
			jQuery(this).find("table > tbody > tr:first > td:eq(1) > div:eq(1)").hide();
	
		// Hide the video length
		if (__opts.hideVideoLength)
			jQuery(this).find("table > tbody > tr:last > td:first").hide();
		
		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();
		
		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();
	
	$('.fancyyoutube').fancybox({
		'type': 'iframe'
	});
	
	
}
				
(function($) {
$.fn.youTubeChannel = function(options) {
	var videoDiv = $(this);

	$.fn.youTubeChannel.defaults = {
		userName: null,
		//loadingText: "Loading...",
		//linksInNewWindow: true,
		//hideDescription: false,
		//hideVideoLength: false,
		//hideFrom: true,
		//hideViews: false,
		//hideRating: false,
		//hideNumberOfRatings: true,
		//removeBordersFromImage: true,
		maxResults: 10
	}
			
    __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&max-results=" + __opts.maxResults);
		}
	});

};
})(jQuery);
