	$(document).ready(function(){
//		$(".obfuscated").defuscate();		

		$(".blogToolbar LI").click(function(){
			$(".blogToolbar LI").removeClass();
			$(this).addClass("blogs_menu_selected");
		});

		// expand / hide comments
		$(".blogToolbar LI .ViewComments").click(function(){	
			var blogComments = $(this).parents(".whitebox").children(".blogs_comments");
			
			if(blogComments.hasClass("visible")){
				$(this).html($(this).html().replace("panel_collapse.gif","panel_expand.gif"));
				$(this).html($(this).html().replace("Hide","Show"));
				blogComments.removeClass("visible");				
			} else {
				$(this).html($(this).html().replace("panel_expand.gif","panel_collapse.gif"));				
				$(this).html($(this).html().replace("Show","Hide"));				
				blogComments.addClass("visible");
			}
		});
		
		$("#"+location.hash.substring(1)).parent().prev().find("LI .ViewComments").click();
		
		// new comment
		$(".blogToolbar LI .Comment").click(function(){	
			var blogComments = $(this).parents(".whitebox").children(".blogs_comments");
			if(!blogComments.hasClass("visible")){ // make sure the comments are visible
				$(this).html($(this).html().replace("panel_expand.gif","panel_collapse.gif"));				
				$(this).html($(this).html().replace("Show","Hide"));				
				blogComments.addClass("visible");				
			}
			
			var blogid = blogComments.attr("id").substring("blog".length);
			var newCommentHtml = $("<div id=\"newComment\"></div>");
			var saveButton = $("<input type=\"button\" value=\"Save\" />").click(function(){
				var fck = FCKeditorAPI.GetInstance("newComment");
				$.post("blogs.ajax.php",
				{	action : "submitcomment",
					id : '',
					blogid : blogid,
					comment : fck.GetHTML()
				},
				function(json) {
					newCommentHtml.remove();
					saveButton.remove();
					cancelButton.remove();
					blogComments.append(json.html);
//					blogComments.append($(json.html).defuscate());
	
					var amountRows = document.getElementById("amountRows"+blogid);
					amountRows.value = parseInt(amountRows.value)+1;
				}, "json"); 			
			});
			var cancelButton = $("<input type=\"button\" value=\"Cancel\" />").click(function(){
				newCommentHtml.remove();
				saveButton.remove();
				cancelButton.remove();
				$.scrollTo(blogComments, {
								speed: 1000,
								easing: 'swing'
							});
			});					
			blogComments.append(newCommentHtml);		
			blogComments.append(saveButton);		
			blogComments.append(cancelButton);					

			$.scrollTo($("#newComment"), {
								speed: 1000,
								easing: 'swing'
							});
			var fck = new FCKeditor("newComment");
			fck.BasePath = "/includes/fckeditor/";
			fck.Width = "100%";
			fck.Height = "320";
			fck.Value = "";
			fck.ToolbarSet = "Slowdays";
			document.getElementById("newComment").innerHTML = fck.CreateHtml();
		});
    });

	function editComment(blogId,commentId){
		var comment = $("#comment"+commentId);
		var oldvalue = comment.html();
		
		var fck = new FCKeditor(comment.attr("id"));
		fck.BasePath = "/includes/fckeditor/";
		fck.Width = "100%";
		fck.Height = "320";
		fck.Value = comment.html();
		fck.ToolbarSet = "Slowdays";
		document.getElementById(comment.attr("id")).innerHTML = fck.CreateHtml();	
		
		var saveButton = $("<input type=\"button\" value=\"Save\" />").click(function(){
			var fck = FCKeditorAPI.GetInstance(comment.attr("id"));
			$.post("blogs.ajax.php",
			{	action : "submitcomment",
				id : commentId,
				blogid : blogId,
				comment : fck.GetHTML()
			},
			function(json) {
				saveButton.remove();
				cancelButton.remove();
				comment.html(json.comment);				
//				comment.html($(json.comment).defuscate());

			}, "json"); 			
		});
		var cancelButton = $("<input type=\"button\" value=\"Cancel\" />").click(function(){
			saveButton.remove();
			cancelButton.remove();
			comment.html(oldvalue);				
		});	

		comment.after(cancelButton);
		comment.after(saveButton);
	}
	
	function FCKeditor_OnComplete( editorInstance )
	{
		editorInstance.Focus();
	}
	
	function deleteComment(blogId,commentId){
		var comment = $("#comment"+ commentId);;
		if(confirm("Are you sure you want to delete this comment?")){
					$.post("blogs.ajax.php",
					{	action : "deletecomment",
						id : commentId,
					},
					function(json) {
						if(json.response == true){
							comment.hide("slow");
							comment.prev().hide("slow");	// the span
							comment.next().hide("slow");	// the hr
							var amountRows = document.getElementById("amountRows"+blogId);
							amountRows.value = parseInt(amountRows.value)-1;
						}
					}, "json"); 			
		}			
	}
	
	function expandAll(){
		$(".blogToolbar LI .ViewComments").each(function(){
			$(this).html($(this).html().replace("panel_expand.gif","panel_collapse.gif"));				
			$(this).html($(this).html().replace("Show","Hide"));				
		});
			$(".blogs_comments").addClass("visible");															 		
	}
	
	function collapseAll(){
		$(".blogToolbar LI .ViewComments").each(function(){
			$(this).html($(this).html().replace("panel_collapse.gif","panel_expand.gif"));
			$(this).html($(this).html().replace("Hide","Show"));
		});
		$(".blogs_comments").removeClass("visible");															 		
	}