(function(){

/*
	Class: CommentView 
	The class for a making comments tree
*/
function CommentView() { }

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

/*
	Function: setConfig
	comment_obj - SBCore.Comment object
	block_template - The id of the html block to clone
*/

CommentView.prototype.setConfig = function( inObj )
{
	this.comment_obj = inObj.comment_obj;

	this.depth = inObj.depth;

	var tmpHtmlObj = YUIDom.get( inObj.template );
	if( tmpHtmlObj )
	{
		this.htmlObj = tmpHtmlObj.cloneNode( true );
		this.htmlObj.id = "comment_"+this.comment_obj.id;

		var comment = this.comment_obj;
		var comobj = this;
			
		YUIDom.getElementsByClassName( "comment_name", "div", this.htmlObj, function(el) { el.innerHTML = comment.user; }); 

		var comdat = YUIDom.getElementsByClassName( "comment_date", "div", this.htmlObj);
		
		if( comdat.length )
		{
			var dt = new Date( parseInt(comment.date_added) * 1000);
			comdat[0].innerHTML = (dt.getMonth()+1)+"/"+dt.getDate()+"/"+dt.getFullYear();
		}

		var comtxt = YUIDom.getElementsByClassName( "comment_text", "div", this.htmlObj);
		if( comtxt.length )
		{
			var text = comment.text;
			text = text.replace(/</g,"&lt;");
			text = text.replace(/>/g,"&gt;");
			text = text.replace(/  /g,"&nbsp;&nbsp;");	
			text = text.replace(/\r/g,"<br />");
			text = text.replace(/\n/g,"<br />");
			comtxt[0].innerHTML = text; 
		}


		//Show Replies Button
		this.commentreplytext = YUIDom.getElementsByClassName( "comment_reply_toggle", "div", this.htmlObj )[0];
		setCommentReplyText( this );	

		//Delete Button
		
		YUIDom.getElementsByClassName( "delete_button", "div", this.htmlObj,
		function(el) {	
			if( window["sb_is_signed_in"] != "0" && (comment.user == window["sb_username"] || comment.item.box_username == window["sb_username"]) )
			{
				el.style.display = "block";

				if( typeof inObj.noreply == "undefined" || inObj.noreply === false )
				{
					var sep = YUIDom.getPreviousSibling( el );
					sep.style.display = "block";
				}
				else
				{
					var sep = YUIDom.getPreviousSibling( el );
					sep.style.display = "none";
				}

				YUIEvent.addListener( el, "click", comobj.deleteComment, comobj, true );
			}
		});
		
		//Set Reply link

		YUIDom.getElementsByClassName( "reply_button", "div", this.htmlObj, 
			function(el) { YUIEvent.addListener( el, "click", comobj.showAdd, comobj, true ); });


		this.replyarea = YUIDom.getElementsByClassName( "comment_reply_list", "div", this.htmlObj )[0];
		this.replyentryarea = YUIDom.getElementsByClassName( "reply", "div", this.htmlObj )[0]; 

		if( inObj.noreply )
		{
			YUIDom.getElementsByClassName("reply_button", "div", this.htmlObj, function(el) { YUIDom.setStyle( el, "display","none" ); } );	
		}


	}
};

CommentView.prototype.recalcNumReplies = function()
{
	YUIEvent.removeListener( this.commentreplytext, "click" );

	if( parseInt(this.comment_obj.num_replies) > 0 )
	{
		var sep = YUIDom.getPreviousSibling( this.commentreplytext );
		sep.style.display = "block";

		this.commentreplytext.innerHTML = this.comment_obj.num_replies + " " + ((this.comment_obj.num_replies > 1)?SBtranslation[32]:SBtranslation[29]); 
		YUIEvent.addListener( this.commentreplytext, "click", this.toggleReplyDisplay, this, true );
	}
	else
	{
		var sep = YUIDom.getPreviousSibling( this.commentreplytext );
		sep.style.display = "none";

		this.commentreplytext.innerHTML = "";
	}
};


function setCommentReplyText( obj )
{
	if( parseInt(obj.comment_obj.num_replies) > 0 )
	{
		var sep = YUIDom.getPreviousSibling( obj.commentreplytext );
		sep.style.display = "block";

		this.replies_displayed = false;

		obj.commentreplytext.innerHTML = obj.comment_obj.num_replies + " " + ((obj.comment_obj.num_replies > 1)?SBtranslation[32]:SBtranslation[29]); 
		YUIEvent.addListener( obj.commentreplytext, "click", obj.toggleReplyDisplay, obj, true );
	}
}


CommentView.prototype.showAdd = function( ev )
{
	var oldcom = YUIDom.get("comment_entry");
	if( oldcom ) { oldcom.closer(); }

	var comblock = YUIDom.get("newcomment_block").cloneNode(true);
	comblock.id = "comment_entry";
	comblock.className = "addcommentblock";
	comblock.closer = this.hideAdd;

	this.replyentryarea.style.display="block"; 
	this.replyentryarea.appendChild( comblock ); 

	if( parseInt(this.comment_obj.num_replies) > 0 )
		this.showReplies( false );


	var viewobj = this;

	/*
	if( window["sb_is_signed_in"] == "0" )
		YUIDom.getElementsByClassName( "captcha_loc", "td", comblock, function(el) { viewobj.addCaptcha(el); } );
	*/

	YUIDom.getElementsByClassName( "cancelcomment", "div", comblock, 
		function(el) { YUIEvent.addListener( el, "click", viewobj.hideAdd, viewobj, true ); });

	YUIDom.getElementsByClassName( "postcomment", "div", comblock, 
		function(el) { YUIEvent.addListener( el, "click", viewobj.addComment, viewobj, true ); });

	var textarea = YUIDom.getElementsByClassName( "commenttextarea", "textarea", comblock )[0];
	textarea.focus();

	YUIEvent.stopEvent( ev );
};
	
CommentView.prototype.hideAdd = function( ev )
{
	var comblock = YUIDom.get("comment_entry");

	YUIDom.getElementsByClassName( "cancelcomment", "div", comblock, 
		function(el) { YUIEvent.removeListener( el, "click" ); });

	YUIDom.getElementsByClassName( "postcomment", "div", comblock, 
		function(el) { YUIEvent.removeListener( el, "click" ); });

	if( comblock )
	{
		comblock.closer = null;
		comblock.parentNode.removeChild( comblock );
		comblock = null;
	}


	if( ev ) YUIEvent.stopEvent( ev );
};

CommentView.prototype.addComment = function( ev )
{
	var comblock = YUIDom.get("comment_entry");
	var textarea = YUIDom.getElementsByClassName( "commenttextarea", "textarea", comblock )[0];
	var ncomment = new SBCore.Comment;
	var d = new Date();
	ncomment.setConfig( { 	"user":window["sb_username"], 
				"date_added":(d.getTime() / 1000), 
				"text":textarea.value,
				"replyto":this.comment_obj.id
			    } );


	/*
	if( window["sb_is_signed_in"] == "0" )
	{
		var captcha_block = YUIDom.get("captcha_0");	
		if( captcha_block )
		{
			ncomment.captcha_value = captcha_block.value;
		}
	}
	*/

	this.hideAdd(null);	

	setCommentReplyText(this);
	ncomment.item = this.comment_obj.item;	

	ncomment.postComment();

	this.replies_displayed = true;

	if( ncomment.text.length )
	{

		this.comment_obj.num_replies = parseInt(this.comment_obj.num_replies) + 1;

		var branchObj = new SBui.ReplyCommentView;
		branchObj.setConfig( { comment_obj:ncomment, depth:(this.depth+1), noreply:true } );
		branchObj.parentView = this;

		YUIDom.setStyle( branchObj.htmlObj, "opacity", 0 );
		this.replyarea.appendChild( branchObj.htmlObj );

		var attributes = { opacity:{ to:1 } };	
		var anim = new YUIAnim( branchObj.htmlObj, attributes, 0.3, YUIEasing.easeIn);
		anim.animate();
	}

	if( ev ) YUIEvent.stopEvent( ev );
};

CommentView.prototype.deleteComment = function()
{
	if( window["dlgManager"] )
	{
		window["dlgManager"].showDeleteCommentDialog( this );
	}
};

CommentView.prototype.destroy = function()
{
	if( this.parentView )
	{
		this.parentView.comment_obj.num_replies = parseInt(this.parentView.comment_obj.num_replies) - 1;
		this.parentView.comment_obj.replies.splice(0,this.parentView.comment_obj.replies.length);
		this.parentView.recalcNumReplies();
	}

	this.htmlObj.style.display="none";

	if( this.htmlObj.parentNode )
		this.htmlObj.parentNode.removeChild( this.htmlObj );

};
	
CommentView.prototype.showReplies  = function( inForce )
{
	this.replies_displayed = true;

	//If we don't have the replies, we gotta fetch them
	if( this.comment_obj.num_replies && !this.comment_obj.replies.length )
	{
		var loadingdlg = YUIDom.get("sb_loading_replies").cloneNode(true);
		this.replyarea.appendChild( loadingdlg );

		var sURL = "/comment/getbyparent"
		var pd = "parent_id="+this.comment_obj.id+"&item_id="+this.comment_obj.item.item_id;		
		var config = { success:fetchRepliesSuccessful, arguments:{ srcobj:this } };
		YUIRequest.asyncRequest( "POST", sURL, config, pd );
	}

	YUIDom.setStyle( this.replyarea, "display", "block" );
}

CommentView.prototype.hideReplies = function()
{
	this.replies_displayed = false;
	YUIDom.setStyle( this.replyarea, "display", "none" );
}

CommentView.prototype.toggleReplyDisplay = function( ev )
{
	if( !this.replies_displayed )
		this.showReplies( false );
	else
		this.hideReplies();

	if( ev ) YUIEvent.stopEvent( ev );
};

CommentView.prototype.addCaptcha = function( 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"; 

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

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

	el.width = "180";

	el.appendChild( block );
	*/
};

function fetchRepliesSuccessful( inReq )
{
	var commentsJSON = YAHOO.lang.JSON.parse(inReq.responseText);

	var replyarea = this.arguments.srcobj.replyarea;
	var srcobj = this.arguments.srcobj;

	//Get rid of everything 
	var children = YUIDom.getChildren( replyarea );
	for( el in children ) { replyarea.removeChild( children[el] ); }
	
	for( c in commentsJSON.comments )
	{
		var comment = new SBCore.Comment;
		comment.setConfig( commentsJSON.comments[c] );
		comment.item = srcobj.comment_obj.item;

		var commentView = new SBui.ReplyCommentView;
		commentView.setConfig( { comment_obj:comment, depth:(srcobj.depth+1) } );

		srcobj.comment_obj.replies.push( comment );

		commentView.parentView = srcobj;

		replyarea.appendChild( commentView.htmlObj );
	}
}

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


})();


