(function(){
/*
 * class ItemComments
 * ( subclass of ItemView )
 *
 * @item     <object> - current item;
 * @htmlObj  <object> - related HTML object;
 * 
 */
ItemComments = function(item, htmlObj)
{
	var obj = item;
	var htmlObj = htmlObj;
	
	this.getItem = function() {return obj;}
	this.getHtmlObj = function() {return htmlObj;}
}


ItemComments.prototype.whoIsTalking = function()
{
		return {
			commentSrc:document.getElementById('commentbox'),
			commentDest:document.getElementById ("lightboxcomments"),
			mode:1
			};

	var item = this.getItem();
	var htmlObj = this.getHtmlObj();
	
	var arr_a = YUIDom.getElementsByClassName( "sb_area", "div", htmlObj); 
	
	if (arr_a[0]) { //LightBox
	} 
}

ItemComments.prototype.addComment = function()
{
	var item = this.getItem();
	var who = this.whoIsTalking();
	var sUrl = "/comment/add/";
	var comments = removeTagsFromInput( who.commentSrc.value );
	var config = { success:this.requestComments, failure:addCommentFailedCallback, theObj:this };
	var data = "item_id="+item.item_id+"&comment=" + encodeURIComponent(comments);
	YAHOO.util.Connect.asyncRequest("POST", sUrl, config, data);
};

ItemComments.prototype.requestComments = function()
{
	var item = null;
	var who = null; 
	var sUrl = "";
	var config = null;
	
	var txtField = YAHOO.util.Dom.get("sb_comment_word_count");
	
	txtField.value = "1000"; 
	
	if (this.theObj) { 
		/*
		 * requestComments() function is called as a callback
		 * so scope "this" is changed.
		 */
		item = this.theObj.getItem();
		who = this.theObj.whoIsTalking();
		config = 
		{ 
			success:this.theObj.requestCommentsCallback, 
			failure:requestCommentsFailedCallback,
			theObj:this.theObj
		};
		
	} else { 
		item = this.getItem();
		who = this.whoIsTalking();
		config = 
		{ 
			success:this.requestCommentsCallback, 
			failure:requestCommentsFailedCallback,
			theObj:this
		};
		
	}

	var mode = who.mode;
	if ( mode == 1) { //LIGHTBOX
		sUrl = '/item/getnewcomments/bid/'+ item.item_id +"/showall/1";//get all comments
	}
	
	who.commentSrc.value = "";
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, config); 
}
	
ItemComments.prototype.requestCommentsCallback = function(o){ 

	var comdiv = null;
	var who = this.theObj.whoIsTalking();
	var comdiv = who.commentDest;
	var list = eval( o.responseText);
	
	// clear out any previous comments by deleting the child nodes
	if ( comdiv.hasChildNodes()){
		while (comdiv.firstChild){
   			//The list is LIVE so it will re-index each call
   			comdiv.removeChild(comdiv.firstChild);
		}
	}

	if( list.length == 1 ){ // this is no comment and each field has the box id in it
		
		data = document.createElement("div");
		data.style.paddingTop="4px";
		data.style.paddingLeft="3px";
		data.style.fontSize="9pt";
		
		data.innerHTML = SBtranslation[0];
		
		comdiv.appendChild( data );
		
	}else{ // parse JSON data for comments
		commentstr = "";
		for( i in list ){
			if( i > 0 ){ // skip first row which is boxid ( which corresponds to the comment div id
				username= list[i]["username"];
				comment=list[i]["comment"];
				timestamp =list[i]["timestamp"];
				
				datacontainer = document.createElement("div");
				datacontainer.className="sb_comment_listrow";
				comdiv.appendChild( datacontainer );
				datal = document.createElement("div");
				datal.className="tdleft";
				datal.style.fontSize="8pt";
				
				datadivid = "commentrow_lb_"+i;
				datal.setAttribute('id', datadivid  );
				uname = username;
				origname = uname;
				if( uname.length > 13){ // shorten this so it works for the display
					uname = uname.substr( 0,13 ) + "...";		
				}
				datal.innerHTML = "<span onMouseOut=\"javascript:hideToolTip('userToolTipModal');\" onMouseOver=\"javascript:displayToolTip('"+datadivid +"','"+origname+"','userToolTipModal');\" style=\"color:#558\">"+uname+"</span><br />"+timestamp;
				datacontainer.appendChild( datal );
				datar = document.createElement("div");
				datar.className="tdright";
				datar.style.fontSize="8pt";
				datar.style.paddingLeft="12px";
				datar.innerHTML = comment;
				datacontainer.appendChild( datar );
				dataclear = document.createElement("div");
				dataclear.className="clear";
				datacontainer.appendChild( dataclear );
			}
		}
	}
}

function addCommentFailedCallback(){alert("addCommentFailedCallback");}
function requestCommentsFailedCallback(o){alert('requestCommentsFailedCallback');}
	
window['SB']['ui']['ItemComments'] = ItemComments;

})();
