/*
*	THIS CLASS REQUIRED jquery.timeago.js
*	CommentBox.js
*/

function Comment(jsonComment){
	this.commentId = jsonComment.commentId;
	this.avatarUrl = jsonComment.avatarUrl;
	this.nickName = jsonComment.nickName;
	this.profileId = jsonComment.profileId;
	this.postedTime = jsonComment.postedTime;
	this.commentDescription = jsonComment.commentDescription;
	this.killcode = jsonComment.killcode;
	this.reply = jsonComment.reply;
	this.GetCommentId = GetCommentId;

	
	this.GetCommentView = GetCommentView;
	this.GetCommentReplyView = GetCommentReplyView;
	this.Ready = Ready;
	this.ReplyComment = ReplyComment;
	this.DeleteComment = DeleteComment;
	this.ShowUserProfile = ShowUserProfile;
}


function ShowUserProfile(){
	alert ('i will Show the profile of user nickName of ' + this.nickName);	
}


function DeleteComment(){
	//alert ('i will Delete comment id' + this.commentId);
	
	
}

/**
 * @desc this function is not used
 * @return
 */
function ReplyComment(){
	alert ('i will reply comment id' + this.commentId);	
}

function Ready(){
	
	$('#commentReplyId'+this.commentId).click(function(){
		var commentId = $(this).attr('commentId');
		var aReplyBox = new ReplyBox(commentId);
		
		$('#commentBoxId'+ $(this).attr('commentId')).html(aReplyBox.GetCommentBoxView());
		aReplyBox.Ready();
		
	});
	$('.commentReply').mouseover(function(){
		$(this).css("color","#ff0000");
	});
	
	$('.commentReply').mouseout(function(){
		$(this).css("color","#535998");
	});
	
	
	
	
	
	$('#commentDeleteId'+this.commentId).click(function(){
		
		if(FmvConfirm("Do you want to delete this comment ?")){
			$.getJSON('Bend/CommentDelete/killcode/' + $(this).attr('killcode'),function(data){
				
				if(data.status=="success"){
					$("#commentContainer"+data.data).effect("highlight", {}, 1000).fadeOut(1000);
				}
			});
		}	
		
	});
	
	$('.commentDelete').mouseover(function(){
			$(this).css("cursor","pointer");
			$(this).css("color","#ff0000");
	});
		
	$('.commentDelete').mouseout(function(){
		$(this).css("color","#000000");
	});
		

		
	$('.profileLink').mouseover(function(){
		$(this).css("color","#ff0000");
	});
	
	$('.profileLink').mouseout(function(){
		$(this).css("color","#535998");
	});
	
	for(var i=0;i<this.reply.length;i++){
		$('#commentDeleteId'+this.reply[i].commentId).click(function(){
			if(FmvConfirm("Do you want to delete this comment ?")){
				$.getJSON('Bend/CommentDelete/killcode/' + $(this).attr('killcode'),function(data){
					
					if(data.status=="success"){
						$("#commentContainer"+data.data).effect("highlight", {}, 1000).fadeOut(1000);
					}
				});
			}		
			
		});
	}
	
}

function GetCommentId(){
	return this.commentId;
}

/*
*	This function return html to display OneCommentBox
*/
function GetCommentView(){
	var replyView = this.GetCommentReplyView();
	var postedTimeAgo = jQuery.timeago(this.postedTime);  
	var deleteThisView="";
	var commentEditView = "";
	/*
	 * Draw delete button only if there is a killcode
	 */
	if(this.killcode!=""){
		deleteThisView = "<span title='Delete this comment' class='commentDelete'  killcode='"+this.killcode+"'  id='commentDeleteId"+this.commentId+"'>Delete</span>";
	}
	
	if(enableCommentEdit==true){
		commentEditView = "<a href='"+hosturl+"/Admin/Comment/id/"+this.commentId+"' title='Edit this comment' class='link'>Edit</a>";
	}else commentEditView="";
		
		formattedDescription = this.commentDescription.replace(/\n/gi,"<br>");
		//formattedDescription = formattedDescription.replace(/ /gi,"&nbsp;");
		/**
		 * do not use hyperlink with profile name in url
		 * to direct user's profile page
		 * use profile id instead
		 */
		var commentView = "<div class='comment-bg' align='left' id='commentContainer"+this.commentId+"'>"+
						"<table><tr><td style='vertical-align:top'><img id src='"+this.avatarUrl+"' width='25px;'></td>"+
                        "<td><table  cellpadding=0 cellspacing=0><tr><td valign='top'>" +
                        "<a href='Profile/"+this.profileId+"'><span class='commentNickName profileLink link' profileId='"+this.profileId+"' style='cursor:pointer;' >"+this.nickName+"</span></a>" +
                        		"<span id='postedTime'>posted "+postedTimeAgo+"</span></td></tr>" +
                        				"<tr><td><div style='width:480px;' ><span id='commentDescription' class='zawgyi'>"+formattedDescription+"</span>" +
                        						"<span style='cursor:pointer;' title='Reply this comment' class='commentReply link' commentId='"+this.commentId+"' id='commentReplyId"+this.commentId+"'>Reply</span>" +
                        								deleteThisView+" "+ commentEditView +"</div></td></tr>"+
                        "<tr><td><div id='replyContainerId"+this.commentId+"'  commentId='"+this.commentId+"'>"+replyView+"</div></td></tr>"+
                        "<tr><td><div id='commentBoxId"+this.commentId+"'  commentId='"+this.commentId+"'></div></td></tr>"+
                        
						"</table></td></tr>"+
                        
                        "</table></div>";
	return commentView;
	
}


/**
 * @desc display reply comment
 */
function GetCommentReplyView(){
	var allReplyView="";
	var formattedDescription ;
	
	for(var i=0;i<this.reply.length;i++){
		formattedDescription = this.reply[i].commentDescription.replace(/\n/gi,"<br>");
		//formattedDescription = formattedDescription.replace(/ /gi,"&nbsp;");
		
		if(this.reply[i].killcode!=""){
			commentReplyDeleteView = "<span title='Delete this comment' class='commentDelete'  killcode='"+this.reply[i].killcode+"'  id='commentDeleteId"+this.reply[i].commentId+"'>Delete</span>";
		}else commentReplyDeleteView="";
		var postedTimeAgo = jQuery.timeago(this.reply[i].postedTime);  
		
		if(enableCommentEdit==true){
			commentEditView = "<a href='"+hosturl+"/Admin/Comment/id/"+this.reply[i].commentId+"' title='Edit this comment' class='link'>Edit</a>";
		}else commentEditView="";
		
		var oneReplyView = "<div  id='commentContainer"+this.reply[i].commentId+"' align='left'  >"+
							"<table><tr><td style='vertical-align:top'><img id src='"+this.reply[i].avatarUrl+"' width='25px;'></td>"+
	                        "<td><table  cellpadding=0 cellspacing=0><tr><td valign='top'>" +
	                        "<a href='Profile/"+this.reply[i].profileId+"'><span class='commentNickName commentReply' profileId='"+this.reply[i].profileId+"' id='profileId"+this.reply[i].commentId+"'>"+this.reply[i].nickName+"</span></a><span id='postedTime'>posted "+postedTimeAgo+"</span></td></tr>" +
	                        "<tr><td><div style='width:400px;'><span id='commentDescription'>"+formattedDescription+"</span>"+commentReplyDeleteView+ " " + commentEditView +"</td><div></tr>"+
	                        "<tr><td><div id='replyContainerId"+this.reply[i].commentId+"'  commentId='"+this.reply[i].commentId+"'></div></td></tr>"+
	                        "<tr><td><div id='commentBoxId"+this.reply[i].commentId+"'  commentId='"+this.reply[i].commentId+"'></div></td></tr>"+
							"</table></td></tr>"+
	                        
	                        "</table></div>";
		 allReplyView += oneReplyView;
	}
	
	return allReplyView;
}
