/**
 *  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) {
	$.each(data.feed.entry, function(i,e) {
		var div_item;
		
		div_item = '<div class="video-item"><a href="'
			+ e.media$group.media$player[0].url
			+ '&fs=1&autoplay=1'
			+ '" target="_blank"><img src="'
			+ e.media$group.media$thumbnail[0].url
			+ '" /><div class="title">'
			+ e.title.$t
			+ '</div></a><div class="clear"></div></div>';
			
		__mainDiv.append(div_item);
	});
	
	// Remove the preloader and show the content
	$(__preLoaderHTML).remove();
	__mainDiv.show();

 	$("#videos .video-item a").each(function(){
		$(this).fancybox({
			'titleShow'     : false,
			'transitionIn'  : 'elastic',
			'transitionOut' : 'elastic',
			'width'			: 640,
			'height'		: 480,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {'wmode':'transparent','allowfullscreen':'true'}
		});
	});	
}
				
(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");
			$.getScript("http://gdata.youtube.com/feeds/api/playlists/B893BFE3BA884398?alt=json-in-script&callback=__jQueryYouTubeChannelReceiveData");
			// http://www.youtube.com/view_play_list?p=B893BFE3BA884398
		// }
	});
};
})(jQuery);