(function(){

/*
	Class: ListViewItem 
	The base class for a collection of navigation components 
*/
function ListViewItem()
{

	this.htmlObj = null;
	SBui.ListViewItem.superclass.constructor.call(this);
	this.titleViewArea = null;
	this.titleEditArea = null;
	this.title = null;
	this.p_config = null;
	this.commentItem = null;
	this.commentsArea = null;
	this.comments = "";
	this.commentsLevel = 0;
	this.textArea = null;
	this.textAreaLabel = null;
	this.sendButton = null;
	this.commentsInteractArea = null;
	this.commentsBranchArea = null;
	this.commentSet = new window['SB']['util']['List'];
	
	this.junk;
}
//ListViewItem extends ItemView.  
YAHOO.lang.extend( ListViewItem, window['SB']['ui']['ItemView']);

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

//Group: Basic Methods 
/*
	Function: setConfig
	Establishes the basic setup for the ListViewItem.
	
	.innerHTMLinConfig - A configuration array (key/value)

	Configuration Array Options:
	id - the id of the div to use as a template (default:none)
*/
ListViewItem.prototype.setConfig = function( inConfig )
{
	inConfig.id = "sb_ui_list_item"; 
	inConfig.dd = "sb_box_list_view_2"; 

	if( inConfig.from)
		this.from = inConfig.from;
	else
		this.from = 0;
	
	SBui.ListViewItem.superclass.setConfig.call(this, inConfig);

	this.htmlObj.id = "listview_item_"+inConfig.item_obj.item_id;

	YUIEvent.addListener( this.htmlObj, "mouseover", this.toggleButtonBar, this, true );
	YUIEvent.addListener( this.htmlObj, "mouseout", this.toggleButtonBar, this, true );

	var arr = YUIDom.getElementsByClassName( "sb_box_list_view_image_container_2", "div", this.htmlObj );
	if( arr[0] )
	{
		var img = document.createElement("img");
		img.style.msInterpolationMode="bicubic";
		var factor = 1.0;
		if( inConfig.width > 300 ) 
		{
			factor = ( 300 / inConfig.width );	
		}

		img.style.width = (factor * inConfig.width) + "px";
		img.style.height = (factor * inConfig.height) + "px";
		img.className = "sb_box_list_view_img_this";
		img.src = inConfig.item_obj.image_url;

		arr[0].appendChild( img );
		YUIEvent.addListener( img, "click", this.showLightBox, this, true );

		this.image = img;
	}	
	
	var arr2 = YUIDom.getElementsByClassName( "sb_box_list_view_comment_item", "div", this.htmlObj );
	if(arr2[0]) {
		this.commentItem = arr2[0];
	}

	if( inConfig.username != window["sb_username"] ) //Hide stuff if item is not owned by current user
	{
		YUIDom.getElementsByClassName("sb_box_list_view_header_title_control","a", this.htmlObj, 
			function(el) { el.style.display="none"; }); 

		YUIDom.getElementsByClassName("tag_edit","div", this.htmlObj, 
			function(el) { el.style.display="none"; }); 

	}

	var arr3 = this.htmlObj.getElementsByTagName("a");
	if( arr3[0] ) {
		YUIEvent.addListener( arr3[0], "click", this.titleEdit, this, true );
		YUIEvent.addListener( arr3[1], "click", this.saveTitleEdit, this, true );
		YUIEvent.addListener( arr3[2], "click", this.cancelTitleEdit, this, true );
		arr3[3].href=this.item_obj.link;
		YUIEvent.addListener( arr3[4], "click", this.showLightBox, this, true );
				
	}

	var arr4 = YUIDom.getElementsByClassName( "sb_box_list_view_header_title_view_area", "div", this.htmlObj );
	if(arr4[0]) {
		this.titleViewArea = arr4[0];
		var arr5 = YUIDom.getElementsByClassName( "sb_box_list_view_header_title_str", "div", arr4[0]);
		if (arr5[0]) { this.title = arr5[0]; this.title_text = inConfig.title; }
	}

	var arr6 = YUIDom.getElementsByClassName( "sb_box_list_view_header_title_edit_area", "div", this.htmlObj );
	if(arr6[0]) {this.titleEditArea = arr6[0];}
	
	var arr7 = this.htmlObj.getElementsByTagName("img");
	if(arr7[0]) {arr7[0].src="http://"+inConfig.link_host+"/favicon.ico";}
	
	var arr8 = YUIDom.getElementsByClassName( "sb_box_list_view_comments_area", "div", this.htmlObj );
	if(arr8[0]) {this.commentsArea = arr8[0];}
	
	var arr9 = YUIDom.getElementsByClassName( "prompt", "div", this.htmlObj );
	if(arr9[0]) {
		this.textAreaLabel = arr9[0];
		YUIEvent.addListener( this.textAreaLabel, "click", this.showTextArea, this, true );
		
	}
	
	if( !inConfig.item_obj.link )
	{
		YUIDom.getElementsByClassName( "listitem_visit_source", "div", this.htmlObj, function(el) { el.style.display="none";  } ); 
	}

	YUIDom.getElementsByClassName( "sb_boxedby", "span", this.htmlObj, 
			function( inEl ){ inEl.innerHTML = inConfig.username;}  ); 

	var dt = new Date(1000 * inConfig.date_added);

	YUIDom.getElementsByClassName( "sb_dateboxed", "span", this.htmlObj, 
			function( inEl ){ inEl.innerHTML = dt.toLocaleDateString();}  ); 

	SButil.EventManager.addListener("ItemCommentAdded", onCommentAdded, this);
	SButil.EventManager.addListener("ItemCommentDeleted", onCommentDeleted, this);
	SButil.EventManager.addListener("TagsUpdated", onTagsUpdated, this);

	var tagwrapper = YUIDom.getElementsByClassName( "tag_wrapper", "div", this.htmlObj );
	if( tagwrapper[0] )
	{
		var tagSet = inConfig.item_obj.tags;

		for( var t = 0; t < tagSet.length; t++ )
		{
			var tagdiv = document.createElement("div");
			tagdiv.className = "tag";
			tagdiv.innerHTML = tagSet[t];
			tagwrapper[0].appendChild(tagdiv);
		}
	}

	var tagedit = YUIDom.getElementsByClassName( "tag_edit", "div", this.htmlObj );
	if( tagedit[0] )
	{
		YUIEvent.addListener(tagedit[0],"click", this.showTagEdit, this, true );		
	}

	this.showComments();
};

ListViewItem.prototype.toggleButtonBar = function( ev )
{
	if (ev.type == "mouseover") 
	{
		YUIDom.getElementsByClassName('sb_box_list_view_body_footer', 'div', this.htmlObj, 
			function(el) { 
					el.style.backgroundColor='#FFA026';
					el.style.visibility='visible';
			             } );
		YUIDom.getElementsByClassName('sb_box_list_view_img_body', 'div', this.htmlObj, function(el) { el.style.borderColor='#FFA026'; } );
		
	} else {

		YUIDom.getElementsByClassName('sb_box_list_view_body_footer', 'div', this.htmlObj, 
			function(el) { 
					el.style.backgroundColor='#FFF';
					el.style.visibility='hidden';
			             } );
		YUIDom.getElementsByClassName('sb_box_list_view_img_body', 'div', this.htmlObj, function(el) { el.style.borderColor='#FFF'; } );
	}
};

ListViewItem.prototype.didAttach = function()
{
	this.showTitle(this.item_obj.title);

	if( this.dd )
	{
		var arr = YUIDom.getElementsByClassName("sb_box_list_view_header","div", this.htmlObj );
		if( arr[0] )
		{
			var id = YUIDom.generateId( arr[0], "sb_gen" );
			this.dd.setHandleElId( id );
		}

		arr = YUIDom.getElementsByClassName("sb_box_list_view_img_body","div", this.htmlObj );
		if( arr[0] )
		{
			var id = YUIDom.generateId( arr[0], "sb_gen_b" );
			this.dd.setHandleElId( id );
		}
	}

	//YUIEvent.addListener( window, "resize", this.resize, this, true );
	SButil.EventManager.addListener("ItemUpdated", onItemUpdated, this ); 

};

ListViewItem.prototype.didDetach = function()
{
	//YUIEvent.removeListener( window, "resize", this.resize );
};


ListViewItem.prototype.dragStart = function()
{
          YUIDom.getElementsByClassName('sb_box_list_view_body_footer', 'div', this.htmlObj)[0].style.backgroundColor='#FFFFFF';
          YUIDom.getElementsByClassName('sb_box_list_view_body_footer', 'div', this.htmlObj)[0].style.visibility='hidden';
          YUIDom.getElementsByClassName('sb_box_list_view_img_body', 'div', this.htmlObj)[0].style.borderColor='#FFFFFF';
};

ListViewItem.prototype.showComments = function()
{
	var children = YUIDom.getChildren( this.commentsArea );	

	//Get rid of everything after inItem
	for( el in children )
	{
		this.commentsArea.removeChild( children[el] ); 
	}

	this.commentSet.removeAll();

	if( this.item_obj.comments.length )
	{
		var nm = 0;

		for( c in this.item_obj.comments )
		{
			nm++;
			if( nm > 10 ) break;

			var branchObj = new SBui.ListViewCommentView;
			branchObj.setConfig( { listitemview:this, comment_obj:this.item_obj.comments[c] } );

			this.commentSet.append( branchObj );
			this.commentsArea.appendChild( branchObj.htmlObj  );
		}
	}
	else
	{
		var el = YUIDom.get("listview_emptycomment");
		var newel = el.cloneNode(true);
		newel.id = "emptycomment_"+this.item_obj.item_id;
		this.commentsArea.appendChild(newel);
	}
};

ListViewItem.prototype.titleEdit = function()
{
	var arr = this.titleEditArea.getElementsByTagName("input");
	arr[0].style.width = (this.title.parentNode.offsetWidth - 100) +"px";

	this.titleViewArea.style.display = "none";
	this.titleEditArea.style.display = "block";
	arr[0].style.backgroundColor ="#FFFFFF";
	arr[0].value = this.item_obj.title;
};
ListViewItem.prototype.cancelTitleEdit = function()
{

	this.titleViewArea.style.display = "block";
	this.titleEditArea.style.display = "none";
};

ListViewItem.prototype.saveTitleCallback = function(obj)
{
	var resp = YAHOO.lang.JSON.parse(obj.responseText);
	if( resp != "0" )
	{
		this.arguments.obj.item_obj.title = resp;
		this.arguments.obj.showTitle(resp);
	}

	this.arguments.obj.cancelTitleEdit();
};

ListViewItem.prototype.saveTitleEdit = function(e)
{
	var arr = this.titleEditArea.getElementsByTagName("input");
	if (!arr[0]) return;
	var sUrl = "/item/updatetitle/?bid="+this.item_obj.item_id+
				"&title="+encodeURIComponent(arr[0].value);
			
	YUIRequest.asyncRequest( "GET", sUrl, { success:this.saveTitleCallback, arguments:{ obj:this } } );
	arr[0].style.backgroundColor ="#FFEEAA";
	arr[0].value=SBtranslation[31];
};	

ListViewItem.prototype.showTitle = function(str)
{
	this.title.innerHTML = "";
	if(!str) {
		return;
	}
	this.title.innerHTML = str;
	
	
	var arr = YUIDom.getElementsByClassName( "sb_box_list_view_header_title", "div", this.htmlObj );
	if (!arr[0]) return;
	
	var boxWidth = arr[0].offsetWidth + 150;
	var title = str;
	var truncTitle = title.truncate( boxWidth, arr[0].offsetHeight, false);
	//document.getElementById("lightboxboxname").innerHTML = trancTitle;
	this.title.innerHTML = truncTitle;

};

ListViewItem.prototype.showTagEdit = function(inEvent)
{
	var wrapper = YUIDom.getElementsByClassName( "tag_wrapper", "div", this.htmlObj );
	if( wrapper[0] )
	{
		while( wrapper[0].childNodes.length )
		{
			wrapper[0].removeChild(wrapper[0].firstChild);		
		}

		var taglist = this.item_obj.tags.join(",");

		var edittagsbl = YUIDom.get("edit_tags_block").cloneNode(true);
		this.edittagsblock = edittagsbl;
		var inpdiv = YUIDom.getElementsByClassName( "tagsinput", "input", edittagsbl )[0]; 
		inpdiv.style.width="370px";
		inpdiv.value = taglist;

		var savelink = YUIDom.getElementsByClassName( "savetags", "div", edittagsbl )[0]; 

		var cancellink = YUIDom.getElementsByClassName( "canceledittags", "div", edittagsbl )[0]; 

		wrapper[0].appendChild(edittagsbl);
		YUIEvent.addListener(cancellink,"click",this.closeTagEdit, this, true );
		YUIEvent.addListener(savelink,"click",this.setTags, this, true );

	}


	var editlink = YUIDom.getElementsByClassName( "tag_edit", "div", this.htmlObj );
	if( editlink[0] )
	{
		editlink[0].style.visibility = "hidden";
	}

};

ListViewItem.prototype.setTags = function(inEvent)
{
	if( !this.edittagsblock ) return;

	var taglistinput = YUIDom.getElementsByClassName( "tagsinput", "input", this.edittagsblock )[0]; 

	if( taglistinput )
	{
		var tagSet = taglistinput.value.split(",");

		var tagwrapper = YUIDom.getElementsByClassName( "tag_wrapper", "div", this.htmlObj );

		var tagFlat = "";

		this.item_obj.tags = [];

		if( tagwrapper[0] )
		{
			for( var t = 0; t < tagSet.length; t++ )
			{
				if( tagSet[t].length )
				{

				var tagdiv = document.createElement("div");
				tagdiv.className = "tag";
				tagdiv.innerHTML = tagSet[t];
				tagwrapper[0].appendChild(tagdiv);

				this.item_obj.tags.push( tagSet[t] );

				tagFlat += encodeURIComponent(tagSet[t]);
				if( (t+1) < tagSet.length )
					tagFlat += ",";
				}
			}
		}

		//Send the new tags
		var req = "/item/updatetags";
		var pd = "id="+this.item_obj.item_id+"&tags="+tagFlat;

		YUIRequest.asyncRequest( "POST", req, null, pd );
	}
	var savelink = YUIDom.getElementsByClassName( "savetags", "div", this.edittagsblock)[0]; 
	var cancellink = YUIDom.getElementsByClassName( "canceledittags", "div", this.edittagsblock )[0]; 

	YUIEvent.removeListener(cancellink,"click");
	YUIEvent.removeListener(savelink,"click");

	this.edittagsblock.parentNode.removeChild(this.edittagsblock);
	this.edittagsblock = null;

	var editlink = YUIDom.getElementsByClassName( "tag_edit", "div", this.htmlObj );
	if( editlink[0] )
	{
		editlink[0].style.visibility = "visible";
	}


};

ListViewItem.prototype.closeTagEdit = function(inEvent)
{
	if( !this.edittagsblock ) return;

	var tagwrapper = YUIDom.getElementsByClassName( "tag_wrapper", "div", this.htmlObj );
	if( tagwrapper[0] )
	{
		var tagSet = this.item_obj.tags;

		for( var t = 0; t < tagSet.length; t++ )
		{
			if( tagSet[t].length )
			{
				var tagdiv = document.createElement("div");
				tagdiv.className = "tag";
				tagdiv.innerHTML = tagSet[t];
				tagwrapper[0].appendChild(tagdiv);
			}
		}
	}

	var savelink = YUIDom.getElementsByClassName( "savetags", "div", this.edittagsblock)[0]; 
	var cancellink = YUIDom.getElementsByClassName( "canceledittags", "div", this.edittagsblock )[0]; 

	YUIEvent.removeListener(cancellink,"click");
	YUIEvent.removeListener(savelink,"click");

	this.edittagsblock.parentNode.removeChild(this.edittagsblock);
	this.edittagsblock = null;

	var editlink = YUIDom.getElementsByClassName( "tag_edit", "div", this.htmlObj );
	if( editlink[0] )
	{
		editlink[0].style.visibility = "visible";
	}
};

ListViewItem.prototype.resize = function()
{
	//this.showTitle( this.item_obj.title );	
};

ListViewItem.prototype.hideTextArea = 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.parentNode.removeChild( comblock );
		comblock = null;
	}

	YUIDom.getElementsByClassName( "prompt", "div", this.htmlObj , function(el) { el.style.display="block"; } );

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

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

ListViewItem.prototype.showTextArea = 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";


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

	YUIDom.getElementsByClassName( "prompt", "div", this.htmlObj , function(el) { el.style.display="none"; } );
	YUIDom.getElementsByClassName( "edit", "div", this.htmlObj , function(el) { el.appendChild( comblock ); } );
	YUIDom.getElementsByClassName( "commenttextarea", "textarea", comblock , function(el) { el.focus(); } );

	comblock.closer = this.hideTextArea;

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

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

	YUIEvent.stopEvent( ev );
};

ListViewItem.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,
				"color":window["sb_bubble_color"]
			    } );

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

	this.hideTextArea(null);	

	//Is this the first comment?  Then kill the empty comment block
	if( !this.item_obj.comments.length )
	{
		var emptycommentblock = YUIDom.get("emptycomment_"+this.item_obj.item_id);
		if( emptycommentblock )
		{
			emptycommentblock.style.display = "none";
			emptycommentblock.parentNode.removeChild(emptycommentblock);
		}
	}

	this.item_obj.addComment( ncomment );

	var branchObj = new SBui.ListViewCommentView;
	branchObj.setConfig( { listitemview:this, comment_obj:ncomment, noreply:true} );
	YUIDom.setStyle( branchObj.htmlObj, "opacity", 0 );
	this.commentsArea.insertBefore( branchObj.htmlObj, this.commentsArea.firstChild );

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



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

function addCaptcha( el )
{
	/*
	var imp = null;
	if( imp = YUIDom.get("captcha_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_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 );
	*/
};


ListViewItem.prototype.sendComment = function()
{
	var comment = this.textArea.value;
	this.saveComment(this.textArea.value);
};

ListViewItem.prototype.saveComment = function(comment)
{
	if(!comment) {
		return;
	}
	var sURL = "/comment/add/"
	var pd = "item_id="+this.item_obj.item_id+"&comment="+encodeURIComponent(comment);

	var cb = { success:this.saveCommentSuccess, arguments:{ item:this} };
	YUIRequest.asyncRequest( "POST", sURL, cb, pd );
		
	this.textArea.style.backgroundColor ="#FFEEAA";
	this.textArea.value=SBtranslation[30];
};

function onCommentAdded( inEvent, inArgs )
{
	if( inArgs[0].id == this.item_obj.item_id )
	{	
		this.item_obj.comments = this.item_obj.comments.reverse();

		this.showComments();
	}
}

function onCommentDeleted( inEvent, inArgs )
{
	if( inArgs[0].item_id == this.item_obj.item_id )
	{	
		this.showComments();
	}
}

function onTagsUpdated( inEvent, inArgs )
{
	if( inArgs[0].item_id == this.item_obj.item_id )
	{	
		var tagwrapper = YUIDom.getElementsByClassName( "tag_wrapper", "div", this.htmlObj );
		if( tagwrapper[0] )
		{
			while( tagwrapper[0].childNodes.length )
			{
				tagwrapper[0].removeChild(tagwrapper[0].firstChild);		
			}

			var tagSet = inArgs[0].tags;

			for( var t = 0; t < tagSet.length; t++ )
			{
				var tagdiv = document.createElement("div");
				tagdiv.className = "tag";
				tagdiv.innerHTML = tagSet[t];
				tagwrapper[0].appendChild(tagdiv);
			}
		}
	}
}

ListViewItem.prototype.getComments = function()
{
	var sURL = "/comment/getbyitem"
	var pd = "item_id="+this.item_obj.item_id+"&level=0&result=10&oldest=1";	
	
	var cb = { success:this.getCommentsSuccess, arguments:{ item:this} };
	YUIRequest.asyncRequest( "POST", sURL, cb, pd );
};

ListViewItem.prototype.getCommentsSuccess = function(obj)
{
	var commentsJSON = YAHOO.lang.JSON.parse(obj.responseText);

	YUIEvent.purgeElement( this.arguments.item.commentsArea, true);

	var fch = null;
	while ( fch=this.arguments.item.commentsArea.firstChild) {
		this.arguments.item.commentsArea.removeChild(fch);
	}
	this.arguments.item.showComments();
};

ListViewItem.prototype.showReplyBranch = function(obj)
{
	var branchObj = new window['SB']['ui']['ListViewItemComments'];
	var commentsBranchArea = null;
	var arr14 = YUIDom.getElementsByClassName( "sb_box_list_view_comments_branch", "div", this );
	if(arr14[0]) {commentsBranchArea = arr14[0];}

	var param = { parentItem:this, 
			htmlObj:this, 
			comment_id:this.comment_id, 
			commentsArea:commentsBranchArea,
			item_id:this.item_obj.item_id,
			branch:true};
	branchObj.setConfig( param );
};

function onItemUpdated( inEv, inArgs )
{
	if( inArgs[0].item.id == this.item_obj.id )
	{
		var img = this.image;
		img.style.msInterpolationMode="bicubic";
		var factor = 1.0;
		if( this.item_obj.width > 300 ) 
		{
			factor = ( 300 / this.item_obj.width );	
		}
		img.style.width = (factor * this.item_obj.width) + "px";
		img.style.height = (factor * this.item_obj.height) + "px";
		img.className = "sb_box_list_view_img_this";
		
		if( this.item_obj.url.match(/http/) ){
			img.src = this.item_obj.url;
		} else {
			img.src = "http://"+window["sb_host"]+this.item_obj.url;
		}
			
	}
}

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


})();


