var feed_changetimers = [];

$(document).ready(function(){   
	$(".feed_cantoggle").each(function(){
		var id = $(this).attr("id");
		$("#"+id+" .feed_title").click(function(){
			$("#"+id+" .feed_content").animate({height: "toggle"}, "slow", "swing");
		});
	});

	$(".feed_item").each(function(){
		var id = $(this).attr("id");
		var itemid = $(this).attr("itemid");
		var groupid = $(this).attr("groupid");
		var textarea = $("#"+id+" .feed_contentedit textarea");
		var titlediv = $("#"+id+" .feed_title");
		
		// UBB content
		feed_updatecontent(id);

		// Preview
		feed_changetimers.push(-1);
		timerindex = feed_changetimers.length - 1;
		textarea.keyup(function(){
			clearTimeout(feed_changetimers[timerindex]);
			feed_changetimers[timerindex] = setTimeout("feed_updatecontent('"+id+"')", 1000);
		});
		
		// Edit button
		$(".feed_edit", $(this)).click(function(){
			$("#"+id+" .feed_contentedit").animate({height: "toggle"}, "slow", "swing");
		});
		
		// Remove button
		$(".feed_remove", $(this)).click(function(){
			//$("#"+id+" .feed_content").hide();
			xajax_xFeed_post_remove(itemid);
		});
		
		// Post button
		$(".feed_submit", $(this)).click(function(){
			xajax_xFeed_post_edit(textarea.val(), titlediv.html(), itemid, groupid);
		});
		
		// Editing Toolbar
		$(".feed_contentedit", $(this))
			.prepend("<button class='imgbutton'>Plaatje...</button><br />")
			.prepend("Title: <input type='text' value='"+$(".feed_title", $(this)).html()+"' class='titleedit' /><br />");
		$(".titleedit", $(this)).change(function(){
			var value = $(this).attr("value");
			$("#"+id+" .feed_title").html(value);
		});
		$(".imgbutton", $(this)).click(function(){
			img_instance.view({
				onSelect: function(imageid, imagename){
					textarea.val(textarea.val()+"[img="+imageid+" width=400 align=left]").keyup();
					img_instance.hide();
				}
			});
		});
	});
});

function feed_updatecontent(id)
{
	$("#"+id+" .feed_contentmain").html(ubb_parse($("#"+id+" .feed_contentedit textarea").val()));
	$("#"+id+" .feed_contentmain .image").each(function(){
		var image = $(this);
		var imageid = (image.attr("image") == undefined)?(""):(image.attr("image"))
		var imagename = (image.attr("name") == undefined)?(""):(image.attr("name"))
		var imagedescription = (image.attr("description") == undefined)?(""):(image.attr("description"))

		image.click(function(){
			tb_show("<h3>"+imagename+"</h3>"+imagedescription, "image.php?id="+imageid, 'HAXZ0RS');
		});
	});
}