var _cAJ;
var chURL = "/sites/titsplz/comments-helper.php";

$(document).ready(function(){

});
/*
    [ID] => 1104
    [ResourceID] => 18485
    [UserID] => 1117
    [Comment] => bottom half of tit, looking up from underneath
    [PostDate] => 2011-04-27 00:01:30
    [SiteID] => TITSPLZ
    [Score] => 0
    [ReplyTo] => 
*/
	
function GetComments(id){
	debug.log("c: Loading comments for #"+id);
	$cCont = $("#cComments");
	
	$cCont.LoadMsg(true);
	
	if(_cAJ !== undefined){
		_cAJ.abort();
	}
	
	$cCont.fadeOut('fast',function(){$cCont.html("");});
	
	_cAJ = $.getJSON(chURL,{
		"do":	"get",
		"id":	id,
		"json":	""
	},function(data){
		//debug.log("!!!!!!!!!!!!--GetComments");
	//	debug.log(data);
		$cCont
			.html("")
			.stop(false,true).show();
		var inData = $.parseJSON($("#c"+id).attr("inData"));
		
		$("#piComments").text(data["comments"].length);
		if(_Luser["ID"] == 0){
			var lim = "<span>Login to Add a Comment</span>";
		}else{
			var lim = "<span>Add Yours</span>";
		}
		
		if(data["comments"].length > 0){
			var nc = "<div id='cNONE'>";
				nc +=data["comments"].length+" Comments "+lim;
				nc +="</div>";
		
			$cCont.append(nc);
			
			$.each(data["comments"],function(){ //
			//debug.log(inData);
			//debug.log("------->"+this["UserID"] + " : " + inData["UserID"]);
			
				var OwnerComment = ((this["UserID"] == inData["OwnerID"]) ? "Owner" : ""); 
				var Reply = ((this["ReplyTo"] !== null) ? "Reply" : ""); 
				
				var g = ((this["profile"]["Gender"] == "M") ? "male" : "female");
				if(this["profile"]["ProfilePic"] != null){
					var img = "/tn/?sm"+this["profile"]["ProfilePic"]+".jpg";
				}else{
					var img = "/images/nouser.gif";
				}
				
				var Username = this["profile"]["UserName"];
				
				var nc = "<div id='c"+this["ID"]+"' class='cContainer "+Reply+"'>";
					nc +=	"<div class='cRate'>&uarr;<br>"+this["Score"]+"<br>&darr;</div>";
					nc +=	"<img src='"+img+"' class='cPFimg'>";
					nc +=	"<div class='cUsername "+OwnerComment+" "+g+"'>"+this["profile"]["UserName"]+"</div>";
					nc +=	"<div class='comment'>"+this["Comment"]+"</div>";
					nc +=	"<div class='bottombar'><span class='btnReply'>reply</span><span class='postDate'>"+this["PostDate"]+"</span></div>";
					nc +="</div>";
				$cCont.append(nc);
				
				$cCont.children(":last").slideDown("fast");
				
				//debug.log($("#c"+this["ID"]+" .cUsername"));
				
				$("#c"+this["ID"]+" .cUsername").click(function(e){
						window.location = "/album.php?u="+Username;
				});
				
			});
		}else{
			var nc = "<div id='cNONE'>";
				nc +="No Comments Yet "+lim;
				nc +="</div>";
		
			$cCont.append(nc);
			
		}
		
		//$cCont.LoadMsg(false);
		c_RD();
	});
}

function c_RD(){
	$("#cComments .btnReply").click(function(){
		$this = $(this);
		var $tc = $this.parents(".cContainer");
		if(_Luser["ID"] == 0){
			window.location = "/login.php?r2=id|"+_cur;
		}else{
			$tc.addComment();
		}
	});
	
	$("#cNONE").click(function(){
		$this = $(this);
		var $tc = $this;
		if(_Luser["ID"] == 0){
			window.location = "/login.php?r2=id|"+_cur;
		}else{
			$tc.addComment();
		}
	});
}

jQuery.fn.addComment = function() {
	$this = $(this);
	
	var $oldAC = $("#addComment");
	if($oldAC.length > 0){
		$oldAC.remove();
	}
	
	var inData = $.parseJSON($this.attr("inData"));
	if(inData === null){
		var inData = [];
			inData["ResourceID"] = _cur;
			inData["ID"] = "";
	}
	
	var plTxt = []; // Rynadnamic placeholder text
	
	var nc = "<div id='addComment' class=''>";
		nc +=	"<img src='/images/nouser.gif' class='cPFimg'>";
		nc +=	"<div class='cUsername'>"+_Luser["UserName"]+"</div>";
		nc +=	"<table><tr><td width='100%'><textarea id='cNewComment'>Try not to make an ass of yourself...</textarea></td><td width='65px'><div id='btnAddComment'>Post</div></td></tr></table>";
		//nc +=	"<div id='addCTool' class=''></div>";
		nc +=	"<div class='bottombar'>Posting personal information, such as email addresses and phone numbers is <i>not recommended</i>. It's <i>the internet</i> afterall.</div>";
		nc +="</div>";
		
	$this.after(nc);
	
	var $aC = $("#addComment");

	$("#btnAddComment")
		
		.click(function(e){
			$this = $(this);
			
			$this.attr("disabled",true);
			var comment = $("#cNewComment").val();
			$("#AddComment").LoadMsg(true);
			
			if(comment != ""){
				$.post(chURL,{
					"do"	:	"add",
					"rid"	:	inData["ResourceID"],
					"data"	:	comment,
					"reply2":	inData["ID"],
					"json"	:	""
				},function(data){
					//debug.log("!!!!!!!!!!!!--btnAddComment");
					//debug.log(data);
					Pulse("ADDCOMMENT",{"rid":inData["ResourceID"]});
					refreshPicInfo(inData["ResourceID"]);
				});
			}else{
				refreshPicInfo(inData["ResourceID"]);
			}
		});
		
	if($.browser.msie){
		$aC.fadeIn();
		$("#cNewComment")
			.val("")
			.focus();
	}else{
		$aC.slideDown(850, "easeOutBounce",function(){$("#cNewComment").val("").focus();});
	}
	
};
