function topicReady() {
	$(document).ready(function () {
		$(window).scroll(function() {
			var fold = $(window).height() + $(window).scrollTop();
	
			if($("#endnavigation.hasmore").offset()) {
				if($("#endnavigation").offset().top <= fold + 1200) {
					loadComments('',$("#endnavigation"));
				}
			}
		});
	});
}
var ReviewfuseForum = new Object();

function refreshNewComments() {
	if(ReviewfuseForum.running == true) {
		return;
	}
	var insertArea = $("#mostrecent");
	var nextSibling = insertArea.next();
	if(nextSibling && nextSibling.attr('id')) {
		var nextCommentId = parseInt(nextSibling.attr('id').split("_",2).pop());
	}	
	$.getJSON('', {ajax:true, post:true, end: nextCommentId,count: 10},
	        function(data){
			  $.each(data['comments'],function(i,item){
				  if(item['id'] <= nextCommentId) return;
				  nextSibling.before(item['content']);
			  });
			  ReviewfuseForum.running = false;
	});
	setTimeout("refreshNewComments()", 20000);
}

function loadComments(topicUrl,insertArea) {
	if(ReviewfuseForum.running == true) {
		return;
	}
	
	ReviewfuseForum.running = true;
    var prevSibling = insertArea.prev("div");
    var nextSibling = insertArea.next("div");
	var prevCommentId = parseInt(prevSibling.attr('id').split("_",2).pop());
	if(nextSibling && nextSibling.attr('id')) {
		var nextCommentId = parseInt(nextSibling.attr('id').split("_",2).pop());
	}
	
	$.getJSON(topicUrl, {ajax:true, start: prevCommentId+1,count: 20},
	        function(data){
			  var lastIdProcessed =0;
			  $.each(data['comments'],function(i,item){
				  if(item['id'] >= nextCommentId) return;
				  insertArea.before(item['content']);
				  lastIdProcessed = parseInt(item['id']);
			  });
			  if(lastIdProcessed >= data['topic']['last_comment_id'] || lastIdProcessed +1 >= nextCommentId) {
				  insertArea.removeClass("hasmore");
				  $("li.next").remove();
			  }
			  ReviewfuseForum.running = false;
	});	
}

function startTopicReady() {
	$(document).ready(function () {
		ReviewfuseForum.infoChanged=false;
		$("#addTags").click(function() {
			addTags($("#textTags").val());
			$("#textTags").val("");
		});
		$("li.popTags").click(function() {
			addTags($(this).text());
		});
		$(".watchChange").keyup(function() {
			ReviewfuseForum.infoChanged=true;
		});
		//setTimeout("checkRelatedTopics()", 20000);
	});
}

function checkRelatedTopics() {
	if(ReviewfuseForum.infoChanged) {
		$("#matchingTopics").append("I just checked for matching topics<br />");
		ReviewfuseForum.infoChanged=false;
	}
	setTimeout("checkRelatedTopics()", 20000);
}
function addTags(string) {
	var tags = string.split(",");
	var existingTags = $("#tags");
	var hiddenTags = $("#hiddenTags");
	if(hiddenTags.val() == '') {
		hiddenTags.val(',');
	}
	$(tags).each(function(i,e) {
		e = jQuery.trim(e);
		if(e=='') return;
		hiddenTags.val(hiddenTags.val()+ e + ",");
		existingTags.append($("<li></li>").append(e));
	});
	
	updatePopularTags();
}


function updatePopularTags() {
	$("#popularTags").empty();
	$("#popularTags").append("<li>Please wait, loading tags...</li>");

	$.getJSON("?ajax=true&currentTags=" + escape($("#hiddenTags").val()),
	    function(data){
		  $("#popularTags").empty();
	      $.each(data, function(i,item){
	       $('<li class="popTags" style="cursor: pointer; cursor: hand"/>').append(item).appendTo("#popularTags");
	      });
  	  	  $("#popularTags li").click(function() {
			addTags($(this).text());
  	  	  });
    });
}
