/* 
 * Kill9 Studios YouTube Comments Plugin
 * 
 * @author: Rocco Augusto
 * @url: kill9studios.com
 * @version: 0.1
 * 
 * @usage:
 * 	$(commentID).comments({ 
 *		total: '5',	//Number of comments to pull in
 *		class: 'spt',	//Indicator of which site colors to use (e.g. spt, ppct, zt, at, dht)
 *		url: '.tools li:eq(1) a',	//HTML element that contains page to parse for comments
 *		parseUrl: 'parse.php?u='	//location of the server side file used to parse
 *	});
*/

(function($) {
	$.fn.comments = function(options) { 
		var opts = $.extend({}, $.fn.comments.defaults, options),
			debug = function(e) { console.log(e) };
		
		return this.each(function() { 
			var obj = $(this);			
			
			//load commetns from youtube
			$.ajax({
				type: 'GET',
				url: opts.parseUrl + 'http://gdata.youtube.com/feeds/api/videos/' + opts.vid + '/comments',
				dataType: 'xml',
				success: function(xml) 
				{
					var xml = $(xml).find('feed').find('entry'),
						time = new Date(),
						comment = '';
						
					xml.each(function(i) {
										
							var name = $(this).find('author').find('name').text(),
								title = $(this).find('title').text(),
								date = $(this).find('published').text();
								content = $(this).find('content').text();
							
							comment += '<div class="comment">';
							comment += '  <div class="avatar">';
							comment += '    <p>' + name + '</p>';
							comment += '    <img src="/images/avatar.png" alt="" />';
							comment += '  </div>';
							comment += '  <div class="comment-body">';
							comment += '    <small>' + time.toLocaleString(date) + '</small>';
							comment += '    <p>' + content + '</p>';
							comment += '  </div>';
							comment += '</div>';
							
							if(i == 25 || i == (xml.length-1))
							{
							  var more = 'http://www.youtube.com/?v=' + opts.vid;
							  $('#video-comments .post a').attr('href', more).attr('target', '_blank');
							}	
							
							if(i > 25) return false;
					});
					
					//alert(comment);
					obj.html(comment)
					$('.post').css('margin-top', '20px');
					$('.comment:even').css('background-color', '#696660');
				}
			});
		});
	};

	$.fn.comments.defaults = { 
		vid: '',
		parseUrl: '/parse.php?u='
	};
})(jQuery);