var comPage = 1;	

	$(document).ready(function(){
	if(comReferenceID != ''){
		$("#comment_form").submit(function() {					   
			$(".comments_notice").fadeIn("slow");
			if($("#c_comment").val() == ''){
				show_notice('You need to insert a comment');
			} else {
				show_notice('loading...');		
				$.post("/tv/classes/ajax.comment.php", { insert_comments: "yes", referenceID:comReferenceID, comment:escape($("#c_comment").val()), name:escape($("#c_name").val()), submitted_comment:"1"},function(data){
					if(data.match("<!-- error -->") != null){
						var result = data.split("<!--||||");
						show_notice(result[0],'red');
						$('#c_comment').val(result[1]);
						$('#c_name').val(result[2]);						
					} else {
						show_notice('Comment inserted');						
						$("#comments_wrapper").html(data);
						setup_comments();
						$("#c_comment").val('');
						$("#c_name").val('');
						comPage = 1;
					}
				});
			}
			return false;			
		});
		setup_comments();	
		return false;
	}
	});
	
	function setup_comments(){
		if(window.frameElement){
			window.frameElement.height = $('body').height()+35;
		}
		
		$('.comabuse a').hide();
		$('.comment').bind("mouseenter", function(){$('.comabuse a',this).show();});
		$('.comment').bind("mouseleave", function(){$('.comabuse a',this).hide();});	
		
		$(".comments_paging a").click(function(){
			show_notice('Loading...');											   
			$('#comments_wrapper').load("/tv/classes/ajax.comment.php?pag="+$(this).attr("href").match(/pag=([0-9]+)/i)[1]+"&get_comments=yes&referenceID="+comReferenceID,{},
				function() {
					show_notice('');
					setup_comments();
			});
			return false;
		});
		
		$("a[rel='delete']").click(function(){
			$("#comments_wrapper").animate({"opacity": 0.01},"fast");
			
			var regex = new RegExp("IDcomment=([0-9]+)");
			var match = regex.exec($(this).attr("href"));
			$.get("/tv/classes/ajax.comment.php", { todo: "delete" , pag:comPage, IDcomment:match[1], referenceID:comReferenceID},function(data){
				$("#comments_wrapper").html(data);
				$("#comments_wrapper").animate({"opacity": 0.99},"fast");
				show_notice('Comment deleted');				
				setup_comments();
			});
			return false;
		});		
	}
	function show_notice(text,errClass){
		if(text != ''){
			$(".comments_notice").html('<div class="content-box '+errClass+'"><strong>'+text+'</strong></div><br class="md_spc" />');
		} else {
			$(".comments_notice").html('');
		}
	}	
	function reportAbuse(el,ID){
		$.get("/tv/classes/ajax.comment.php", { id_abuse: ID , todo: "abuse" });
		$("#abuse_"+ID).html('<span class="red">Abuse Reported</span>');
		return false;
	}