/**
 *  Plugin which renders the YouTube channel videos list to the page
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 * 	@mod by: Sergei (support at creatiff.com.ua)
 *  @version: 1.0.0 (Nov/27/2009)
 *	http://yankov.us
 */

 var __mainDiv;
 var __preLoaderHTML;
 var __opts;
 
 function __jQueryYouTubeChannelReceiveData(data) {
	if(typeof(data.feed.entry) == 'undefined')
		return;
	
	var html = '<ul class="media-list columns">';
	
	for(i = 0; i < data.feed.entry.length; i++)
	{
		if(i > 2) break;
		
		var entry = data.feed.entry[data.feed.entry.length - i - 1];
		
		var id = entry.id.$t;
		id = id.replace('http://gdata.youtube.com/feeds/base/videos/', '');
		
		entry.content.$t = entry.content.$t.replace(/[\n\r\t]/g, ''); 
		
		var re = new RegExp('<span style="color: #000000; font-size: 11px; font-weight: bold;">(([0-9]{2}):)?([0-9]{2}):([0-9]{2})</span>');
		var m = re.exec(entry.content.$t);
		
		var len = '';
			
		if(m)
		{
			if(typeof(m[2]) != 'undefined')
			{
				len = m[2] + ':';
			}
			if(typeof(m[3]) != 'undefined')
			{
				len +=  m[3];
			}
			if(typeof(m[4]) != 'undefined')
			{
				len +=  ':' + m[4];
			}
		}
		
		//console.log(entry.content.$t);
		
		var re = new RegExp('<div><span style="color: #666666; font-size: 11px;">Views:</span>([0-9]{1,25})</div>');
		var m = re.exec(entry.content.$t);
		
		var views = ''
		
		if(m && typeof(m[1]) != 'undefined')
		{
			views = m[1];
		}
		
		html += '<li><div class="img">' +
				  '<a href="'+ entry.link[0].href +'">' +	
                  '<img src="http://i.ytimg.com/vi/' + id + '/0.jpg" width="118" alt="" border="0" />' +
                  '</a><span class="movie-time">' + len + '</span>' +
                '</div>' + 
                '<span class="info">' +
                  '<a href="'+ entry.link[0].href +'">' + entry.title.$t + '</a>' +
                  '<span class="stats">' + views + ' views</span>'
                '</span></li>';
		
	}
	
	html += '</ul>';
	
	__mainDiv.html(html);
    
    $('ul li:first', __mainDiv).addClass('first');
	
	
	// 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) {			
			__mainDiv = videoDiv;
			__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&format=5");
		}
	});
};
})(jQuery);
