//$('#statusUpdateForm').submit(function (e) {
//	e.preventDefault();
//
//	if ($(this).find('[name=title]').val().length < 5) {
//		alert("Too short");
//		return;
//	}
//
//	$.ajax({
//		url: '/project/add?ajax',
//		type: 'post',
//		data: $(this).serialize(),
//		success: function (d) {
//			console.log(d);
//
//			reloadComment(d);
//		}
//	})
//
//	$(this)[0].reset();
//	$('#statusUpdateForm select').val('');
//});

function reloadComment(project_id)
{
	//alert('fired it...' + project_id);
	//return false;
	$.ajax({
		url: '/timeline/getStatusUpdate/?id=' + project_id,
		success: function (r) {
			var el = $('.status_update[project_id=' + project_id + ']');
			if (el.length == 0) {
				console.log("Status element is missing");

				el = $('<div/>', {
					class: 'status_update',
					project_id: project_id
				});

				console.log(el);

				$('.status-updates').prepend(el);
			}
			el.html(r);
		}
	})
}

////////////////////////////////////////////////////////////////////////////
///////////////////         SUBCOMMENTS LEVEL 1	         ///////////////////
////////////////////////////////////////////////////////////////////////////
//ORIG: data: $(form).serialize(),
//NEW: data : $('#form').serialize() + "&par1=1&par2=2&par3=232"

function ajax_comment(form)
{
	var id = $(form).find('[name=project_id]').val();

	$.ajax({
		url: '/timeline/addComment/',
		type: 'post',
		data: $(form).serialize() + "&project_id=0",
		success: function (d) {
			//alert(data.join(' '));
			//var json = JSON.stringify(data);
			//alert('JSON: ' + json);
			if (d != 'ok') {
				//alert('D is not ok, it is: ' + data);
				return;
			}
			//alert('D is ok');
			//location.reload();
			reloadComment(id);
		}
	});

	return false;
}

////////////////////////////////////////////////////////////////////////////
///////////////////         SUBCOMMENTS LEVEL 2	         ///////////////////
////////////////////////////////////////////////////////////////////////////

function commentReply(el)
{
	//alert('Clicked');
	$('.commentThreadReply').remove();

	var project_id = $(el).parents('.comments').find('[name=project_id]').val();
	var parent_id = $(el).parents('.comments').find('[name=parent_comment]').val();

	var comment_id = $(el).attr('comment_id');

	var subparent = $(el).attr('subparent');

	var prj = $(el).attr('prj');
	if (prj == 0) {
		comment_id = subparent;
	}

	//var project_id = $(el).attr('project_id');
	//var comment_id = 4;
	//var feature_id = 2;

	//alert('Parent ID: ' + parent_id + ' | Project ID: ' + project_id + ' | Comment ID: ' + comment_id + ' PRJ: ' + prj);
	//return false;

	cFrm = $("<form>", {class: "commentThreadReply"});
	cFrm.submit(function (e) {
		e.preventDefault();

		$.ajax({
			url: '/timeline/addComment/',
			type: 'post',
			data: $(this).serialize(),
			success: function () {
				reloadComment(project_id);
				//location.reload();
			}
		});
	});
	// THIS IS THE SUB-COMMENT INPUT FIELD
	cFrm.append("<textarea class='subcominput' name='content' style='margin:5px 0; width:90%; padding:10px;' rows='1' placeholder='Write a comment/reply....'></textarea>");
	cFrm.append("<input type='hidden' name='parent_id' value='" + parent_id + "' />");
	cFrm.append("<input type='hidden' name='project_id' value='" + comment_id + "' /><br />");
	cFrm.append("<input type='submit' class='btn btn-post' value='Reply' />");

	$(el).parent().after(cFrm);
}