(function(){

/*
	Class: ListViewItemComments 
	The class for a making comments tree
*/
function ListViewItemComments(level)
{
	if( level )
		this.level = level;
	else
		this.level = 0;
	
	this.commentItem = null;
	this.commentsArea = null;
	//this.comments = "";
	this.textArea = null;
	this.sendButton = null;
	this.commentsInteractArea = null;
	this.itemTemplate = null;
	this.p_config = null;
	this.numReplies = 0;
	this.branch = false;
	
}

ListViewItemComments.prototype.toString = function() { return "[SB.ui.ListViewItemComments]"; };

ListViewItemComments.prototype.setConfig = function( inConfig )
{
	this.p_config = inConfig;
	this.commentItem = inConfig.htmlObj;
	this.parentCommentId = inConfig.comment_id;
	this.commentsArea = inConfig.commentsArea;
	this.item_id = inConfig.item_id;
	this.branch = inConfig.branch;

	if( inConfig.fulllist )
		this.fulllist= inConfig.fulllist;
	else
		this.fullist= false;

	//this.commentsArea.innerHTML = "";
	var arr1 = YUIDom.getElementsByClassName( "sb_box_list_view_comment_item", "div", document.body );
	if(arr1[0]) {this.itemTemplate = arr1[0];}

	if( this.parentCommentId != 0 )
		this.isReply = true;
	else
		this.isReply = false;

	if(inConfig.branch) {
		//this.branch = true;
		this.getComments();
	}
};

ListViewItemComments.prototype.showComments = function(commObj, branch)
{
	var replyImg = null;
	var replyImgArea = null;
	for(ix in commObj) 
	{
		var commentItemClone = this.itemTemplate.cloneNode(true);
		commentItemClone.item_id = commObj[ix].item_id;
		commentItemClone.reply_to = commObj[ix].reply_to;
		commentItemClone.num_replies = commObj[ix].num_replies;
		commentItemClone.style.display = "block";

		if( this.isReply )
		{
			if( ix == 0 )
				commentItemClone.className = "sb_box_list_view_comment_item_reply_first";
			else
				commentItemClone.className = "sb_box_list_view_comment_item_reply";
		}

		//commentItemClone.comment_id = commObj[ix].id;
		commentItemClone.comment_id = (commObj[ix].id ? commObj[ix].id : commObj[ix].comment_id);
		
		commentItemClone.thisObj = this;
		
		this.commentsArea.appendChild(commentItemClone);
		this.populateCommentItem(commentItemClone, commObj[ix], branch);
		
		//replyImg = commentItemClone.getElementsByTagName("img")[0];
		replyImg = YUIDom.getElementsByClassName( "sb_box_list_view_comment_icon_reply_area", "div", commentItemClone );

		if( this.level )
			replyImg[0].style.marginRight = (20 + (this.level * 5)) + "px";

		YUIEvent.addListener(replyImg, "click", this.showItemTextArea, commentItemClone, true );

			
			
		var arr11 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_save_button", "table", commentItemClone );
		if(arr11[0]) {
			YUIEvent.addListener( arr11[0], "click", this.saveComment, commentItemClone, true );
			//YUIEvent.addListener( arr11[0], "click", this.saveComment, commentItemClone, false );
		}
		
		var arr13 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_cancel_button", "span", commentItemClone );
		if(arr13[0]) {
			YUIEvent.addListener( arr13[0], "click", this.hideTextArea, commentItemClone, true );
		}

		commentItemClone = null;
	}
	
};

ListViewItemComments.prototype.populateCommentItem = function(commentItemClone, comm, branch) 
{
	var arr = YUIDom.getElementsByClassName( "sb_box_list_view_comment_item_text", "div", commentItemClone );
	if(arr[0]) {arr[0].innerHTML = comm.text.replace(/\n/g,"<br />");}
	
	var arr3 = YUIDom.getElementsByClassName( "sb_box_list_view_comment_link_to_replies", "div", commentItemClone );
	arr3[0].innerHTML = "";
	if(comm.num_replies > 0) 
	{
		var linkStr = "";
		
		if( window["sb_language"] == "ja" )
			linkStr = SBtranslation[29]+comm.num_replies;
		else
			linkStr = (comm.num_replies <2 ) ? (""+comm.num_replies+" reply"):(""+comm.num_replies+" replies");
		arr3[0].innerHTML = linkStr;
		YUIEvent.addListener(arr3[0], "click", this.showReplyBranch, commentItemClone, true );
	} 

	var arr21 = YUIDom.getElementsByClassName( "sb_box_list_view_comment_item_info_name", "div", commentItemClone );
	if(arr21[0]) {
		if( comm.user.length > 18 ) 
		{
			arr21[0].innerHTML = comm.user.substr(0,18);
			arr21[0].innerHTML += "...";
		}
		else
		{
			arr21[0].innerHTML = comm.user;
		}
	}
	
	var arr22 = YUIDom.getElementsByClassName( "sb_box_list_view_comment_item_info_date", "div", commentItemClone );
	if(arr22[0]) {
		var dt = new Date();
		arr22[0].innerHTML += dt.toLocaleDateString(comm.date_added);
	}
	if(comm.replyto > 0) {
		commentItemClone.getElementsByTagName("img")[0].style.display = "block";
	} else {
		commentItemClone.getElementsByTagName("img")[0].style.display = "none";	
	}
};

ListViewItemComments.prototype.showReplyBranch = function(obj)
{
	var commentsBranchArea = null;
	var fch = null;
	var arr14 = YUIDom.getElementsByClassName( "sb_box_list_view_comments_branch", "div", this );
	if(arr14[0]) {commentsBranchArea = arr14[0];}
	
	var target = YUIEvent.getTarget(obj, false) ;
	var numRepliesStr = new String(target.innerHTML);

	if (numRepliesStr == SBtranslation[28] ) {
		if( window["sb_language"] == "ja" )
			target.innerHTML = SBtranslation[29]+this.num_replies;
		else
			target.innerHTML = (this.num_replies <2 ) ? (""+this.num_replies+" reply"):(""+this.num_replies+" replies");

		while ( fch=commentsBranchArea.firstChild) {
			YUIEvent.purgeElement(fch, true);
			commentsBranchArea.removeChild(fch);
		}
	} else {
		 target.innerHTML  = SBtranslation[28];
		var branchObj = null;

		if( this.thisObj && this.thisObj.level )
	        	branchObj = new ListViewItemComments( this.thisObj.level + 1 );
		else
	        	branchObj = new ListViewItemComments( 1 );

		var param = { parentItem:this, 
				htmlObj:this, 
				comment_id:this.comment_id, 
				commentsArea:commentsBranchArea,
				item_id:this.item_id,
				branch:true};

		branchObj.setConfig( param );
	}

};

ListViewItemComments.prototype.hideTextArea = function()
{
	this.textAreaLabel.style.display = "block";
	this.commentsInteractArea.style.display = "none";
	this.textArea.style.backgroundColor ="#FFFFFF";
	this.textArea.value="";
};

ListViewItemComments.prototype.saveCommentSuccess = function()
{
	this.arguments.item.hideTextArea();
	this.arguments.item.getComments();
};

ListViewItemComments.prototype.showTextArea = function()
{
	this.textAreaLabel.style.display = "none";
	this.commentsInteractArea.style.display = "block";
};

ListViewItemComments.prototype.getComments = function()
{
	var sURL = "";
	var pd = "";

	if(this.parentCommentId > 0) {
		sURL = "/comment/getbyparent"
		pd = "parent_id="+this.parentCommentId+"&item_id="+this.item_id;
	} else {
		sURL = "/comment/getbyitem"
		
		if( this.fulllist )
			pd = "item_id="+this.item_id+"&level=0";	
		else 
			pd = "item_id="+this.item_id+"&level=0&result=10&oldest=1";	

		this.level = 0;
	}
	var cb = { success:this.getCommentsSuccess, arguments:{ item:this} };
	YUIRequest.asyncRequest( "POST", sURL, cb, pd );
};

ListViewItemComments.prototype.getCommentsSuccess = function(obj)
{
	var commentsJSON = YAHOO.lang.JSON.parse(obj.responseText);
	var fch = null;
	while ( fch=this.arguments.item.commentsArea.firstChild) {
		YUIEvent.removeListener(fch, "click");
		this.arguments.item.commentsArea.removeChild(fch);
	}

	var arr = YUIDom.getElementsByClassName("itemdetail_stat", "span", this.arguments.commentItem);
	if( arr[0] && commentsJSON.total_comments ) 
		arr[0].innerHTML = commentsJSON.total_comments;

	this.arguments.item.showComments(commentsJSON.comments, true);
};


ListViewItemComments.prototype.showItemTextArea = function()
{
	var commentsInteractArea = null;
	var arr12 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_interactive_area", "div", this );
	if(arr12[0]) {commentsInteractArea = arr12[0];}

	if( SButil.StateManager.is_public && commentsInteractArea.style.display != "block" )
	{
		var arr = YUIDom.getElementsByClassName("lightbox_comment_captcha_block", "td", this, addCaptcha );
	}

	commentsInteractArea.style.display = "block";
};

function addCaptcha( el )
{
	var imp = null;
	if( imp = YUIDom.get("captcha_image_block_imp") )
	{
		imp.parentNode.removeChild( imp );
		imp = null;
	}

	var baseBlock = YUIDom.get("captcha_image_block");

	var children = YUIDom.getChildren( el );	
	for( elm in children ) { el.removeChild( children[elm] ); }

	var block = baseBlock.cloneNode(true);

	block.id="captcha_image_block_imp";

	el.style.display="block"; 

	var captcha_uid = Math.round(Math.random() * 1000000);
	el.captcha_uid = captcha_uid;

	YUIDom.getElementsByClassName("lightbox_comment_captcha_img", "img", block, 
		function(elm) { elm.src="/extras/captcha/?uid="+captcha_uid});

	YUIDom.getElementsByClassName("lightbox_comment_captcha", "input", block, 
		function(elm) { elm.id="captcha_"+captcha_uid; });

	el.appendChild( block );
}

ListViewItemComments.prototype.hideTextArea = function()
{
	var arr12 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_interactive_area", "div", this );
	if(arr12[0]) {
		arr12[0].style.display = "none";
		arr12[0].style.backgroundColor ="#FFFFFF";
		arr12[0].value="";
	}

};
/*------------------------------------------------------------*/
	
ListViewItemComments.prototype.saveComment = function()
{

	var arr10 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_input", "textarea", this );
	if(!arr10[0] || !arr10[0].value) {
		return;
	}


	var sbcaptcha = "";
	var captcha_uid = ""; 

	if( this.captcha_uid )
	{
		var captcha = YUIDom.get("captcha_"+this.captcha_uid);
		if( captcha ) sbcaptcha = captcha.value;

		captcha_uid = this.captcha_uid;
	}			

	var sURL = "/comment/add/";
	var pd = "item_id="+this.item_id+"&comment="+encodeURIComponent(arr10[0].value)+"&replyto="+this.comment_id;
	    pd += "&sbcaptcha="+sbcaptcha+"&captcha_uid="+captcha_uid;

	var arr12 = YUIDom.getElementsByClassName( "sb_box_list_view_addcomments_interactive_area", "div", this );
	
	var cb = { success:this.thisObj.saveCommentSuccess, arguments:{ obj:this.thisObj, ta:arr12[0]} };
	YUIRequest.asyncRequest( "POST", sURL, cb, pd );
	
	arr10[0].style.backgroundColor ="#FFEEAA";
	arr10[0].value=SBtranslation[30];
	
};
ListViewItemComments.prototype.saveCommentSuccess = function()
{
	//SButil.EventManager.fireEvent( "ItemCommentAdded", { id:this.arguments.obj.item_id } );

	this.arguments.ta.style.display = "none";
	this.arguments.ta.style.backgroundColor ="#FFFFFF";
	this.arguments.ta.value="";
		
	this.arguments.obj.getComments();
};

/*----------------------------------------------------------*/

	
window['SB']['ui']['ListViewItemComments'] = ListViewItemComments;


})();


