/**
 * Ajax Comments
 */
function addComment() {
	var data = $("#commentForm").serialize();
	$.ajax({
   		type: "post",
   		url: "/comments/add/",
   		data: data,
   		dataType: "json",
		success: function(response, status) {
			commentCallback(response, status);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			commentError(XMLHttpRequest, textStatus, errorThrown);
		}
	});
 
	return false;
} 
/**
 * Handle the AJAX callbacks
 */
function commentCallback(response, status) {
	// Response was a success
	if (response.success === true) {
 
		var newComment = '<div class="comment"><div class="comment_separator"></div><div class="usericon"><img src="/files/img_usericon/default_usericon.jpg" border="0"  width="50" /></div><div class="comment_body"><p class="comment_created"><a href="/users/view/'+response.data.user_id+'">'+response.user+'</a> on '+response.time+'</p><p class="comment_content">'+response.data.comment+'</p></div></div>';
 
		$("#comment").prepend(newComment).slideDown();
		$('#commentForm').get(0).reset();
 
	// Response contains errors
	} else {
		var errors = new Array;
 
		if (typeof(response.data) == ("object" || "array")) {
			$.each(response.data, function(key, value) {
				var text = (isNaN(key)) ? key +": "+ value : value;
 
				errors[errors.length] = "<li>"+ text +"</li>";
			});
		} else {
			errors[errors.length] = "<li>"+ response.data +"</li>";
		}
 
		errors = errors.join("\n");
		$("#commentError").html(errors).slideDown();
	}
 
	// Remove box after 5 seconds
	setTimeout(function() {
		$(".commentError").slideUp();
	}, 5000);
 
	return false;
}
/**
 * Handle an AJAX failure
 */
function commentError(XMLHttpRequest, textStatus, errorThrown) {
	var error = "<li>An unexpected error has occurred.</li>";
	$("#commentError").html(error).slideDown();
}	

/**
 * Show and hide DIV's
 */
function showHide(shID) {
	if (document.getElementById(shID)) {
		if (document.getElementById(shID+'-show').style.display != 'none') {
			document.getElementById(shID+'-show').style.display = 'none';
			document.getElementById(shID+'-hide').style.display = 'block';
			document.getElementById(shID).style.display = 'block';
		} else {
			document.getElementById(shID+'-show').style.display = 'block';
			document.getElementById(shID+'-hide').style.display = 'none';
			document.getElementById(shID).style.display = 'none';
		}
	}
}
