var comment_base;
var add_comment_link=new Object();
var comment_title;

$(document).ready(function(){
	// Get the clp in there
	comment_base=location.href;
	comment_base=comment_base.replace(/^(.*?\/\/.*?\/.*?\/.*?\/).*$/,"$1");
	comment_base+="util/comment/";

	$('.comments').each(function(i) {
		if ($(this).attr('view')=='all') {
			$(this).load( comment_base+'list_comments?show_all=1&location='+$(this).attr('id') );
		} else {
			$(this).load( comment_base+'list_comments?location='+$(this).attr('id') );
		}
	});

});

function addComment( comment_id ) {
	add_comment_link[ comment_id ]=$('#'+comment_id+' .add_comment').html();
	$('#'+comment_id+' .add_comment').load( comment_base+'add','location='+comment_id,function() {
		$(this).find('#name').focus();
		comment_title=$(this).find('#add_comment_title').html();
	});
}

function cancelComment( comment_id ) {
	$('#'+comment_id+' .add_comment').html(add_comment_link[ comment_id ]);
}

function submitComment( comment_id ) {
	var name=$('#'+comment_id+' #name').val();
	var email=$('#'+comment_id+' #email').val();
	var comment=$('#'+comment_id+' #comment').val();

	// Missing data? Point that out and focus the field...

	if (!(name) || !(email) || !(comment)) {
	 	$('#'+comment_id+' .comment_error').css('opacity',100);

		setTimeout(function() {
			if ($('#'+comment_id+' .comment_error').css('opacity')>0) {
				$('#'+comment_id+' .comment_error').animate({
					opacity:0
				},1400,'swing');
			}
		},3000);

		if (!(name)) {
			$('#'+comment_id+' #name').focus();
		} else if (!(email)) { 
			$('#'+comment_id+' #email').focus();
		} else if (!(comment)) {
			$('#'+comment_id+' #comment').focus();
		}
		return;
	}

	$('#'+comment_id+' .comment_button_span').html('<div style="padding:10px;font-weight:bold;color:red;">Sending...</div>');

	$('#'+comment_id+' .add_comment').load(comment_base+"add",{ add: 1, location: comment_id, name: name, email: email, comment: comment });

	setTimeout(function() {
		$('#'+comment_id+' .add_comment').animate({
			opacity:0
		},1400,'swing');
	},5000);

	setTimeout(function() {
		$('#'+comment_id+' .add_comment').html(add_comment_link[ comment_id ]);
		$('#'+comment_id+' .add_comment').animate({
			opacity:1
		},1400,'swing');
	},6400);
}

function viewAllComments( comment_id ) {
	$('#'+comment_id+' .all_comments').load(comment_base+"list_hidden_comments", { location: comment_id });
}
