var voteFeedPage = 1;
var commentFeedPage = 1;
var submittedArticleFeedPage = 1;

var loadingVoteFeed = false;
var loadingCommentFeed = false;
var loadingSubmittedArticleFeed = false;

// vote feed button function -- start
var voteFeedMore = $('.vote_feed_more');
voteFeedMore.click(function(){
  if (loadingVoteFeed) return;
	var el = $(this);
	var userId = el.attr('id');
  loadingVoteFeed = true;
  var oriText = voteFeedMore.text();
  voteFeedMore.text('Loading...');
  $.getJSON('/users/'+userId+'/pull_feed'+'?category=votes', {page: ++voteFeedPage}, function(data){
		if (data == '') {
			el.hide();
		} else {
			var html = '';
	    $.each(data, function(i){
				html += '<ul>\
									<li>\
										<a href="/articles/'+data[i]['vote']['id']+'">'+data[i]['vote']['title']+'</a>\
									</li>\
								</ul>';
			});
			el.parent().before(html);
		}
		el.text(oriText);
	  loadingVoteFeed = false;
  });
  return false;
});
// vote feed button function -- end

// comment feed button function -- start
var commentFeedMore = $('.comment_feed_more');
commentFeedMore.click(function(){
  if (loadingCommentFeed) return;
	var el = $(this);
	var userId = el.attr('id');
  loadingCommentFeed = true;
  var oriText = commentFeedMore.text();
  commentFeedMore.text('Loading...');
  $.getJSON('/users/'+userId+'/pull_feed'+'?category=comments', {page: ++commentFeedPage}, function(data){
		if (data == '') {
			el.hide();
		} else {
			var html = '';
	    $.each(data, function(i){
				html += '<h5>\
									<a href="/articles/'+data[i]['comment']['id']+'">'+data[i]['comment']['title']+'</a>\
								</h5>\
								<p>'+data[i]['comment']['body']+'</p>';
			});
			el.parent().before(html);
		}
		el.text(oriText);
	  loadingCommentFeed = false;
  });
  return false;
});
// comment feed button function -- end

// submitted article feed button function -- start
var submittedArticleFeedMore = $('.submitted_article_feed_more');
submittedArticleFeedMore.click(function(){
  if (loadingSubmittedArticleFeed) return;
	var el = $(this);
	var userId = el.attr('id');
  loadingSubmittedArticleFeed = true;
  var oriText = submittedArticleFeedMore.text();
  submittedArticleFeedMore.text('Loading...');
  $.getJSON('/users/'+userId+'/pull_feed'+'?category=articles', {page: ++submittedArticleFeedPage}, function(data){
		if (data == '') {
			el.hide();
		} else {
			var html = '';
	    $.each(data, function(i){
				html += '<ul>\
									<li>\
										<a href="/articles/'+data[i]['article']['id']+'">'+data[i]['article']['title']+'</a>\
									</li>\
								</ul>';
			});
			el.parent().before(html);
		}
		el.text(oriText);
	  loadingSubmittedArticleFeed = false;
  });
  return false;
});
// submitted article feed button function -- end