var old_comment_plugin = '';
var new_comment_plugin = '';
var current_reply = false;
var display_reply = false;

$(function()
{
	place_reply(0);
});

function place_reply(t)
{
	if(typeof authors != 'undefined') 
	{
		for(i=0; i < authors.length; i++) {
			if(user_id == authors[i]) {
				display_reply = true;
			}
		}
		
		if(display_reply == true && $('#comments').is('div')) 
		{
			$('.commentaire').each(function() {
				if($(this).find('.commentaire-header').html().indexOf('comment_reply') == -1) {
					$(this).find('.commentaire-header').append(' <a href="javascript:void(0);" onclick="comment_reply('+this.id.substring(8,this.id.length)+',\''+$(this).find('.comment-author').text()+'\',\''+$(this).find('.comment-date').text()+'\');"><img src="http://www.cowblog.fr/plugin.php?f=reply/arrow.png" alt="" /></a>');
				}
			});
		}
	
		$('.commentaire').each(function()
		{
			if($(this).hasClass('reply') == false)
			{		
				var reply_content = $(this).find('.commentaire-txt').html();
				
				if(reply_content.indexOf('<!-- reply @') != -1) 
				{
					var reply_id = reply_content.substring(12,reply_content.indexOf(' //-->'));
					var reply_html = $(this).html();
					var reply_class = $(this).attr('class');
					$(this).remove();
					
					$('#comment_'+reply_id).after('<div id="'+this.id+'" class="'+reply_class+' reply">'+reply_html+'</div>');
					
					//if(t == 1) {
					//	$.scrollTo($('#'+this.id).get(0),800);
					//}
				}
			}
		});
	}
	
	setTimeout('place_reply(1)',1000);
}

function comment_reply(comment_id,comment_author,comment_dt)
{	
	old_comment_plugin = comment_plugin;
	comment_plugin += '&comment_reply='+comment_id;
	new_comment_plugin = comment_plugin;
	current_reply = true;
	
	$('#comment-reply').remove();
	$('#comment_content').after('<br /><span id="comment-reply"><input type="checkbox" value="'+comment_id+'" checked="checked" name="comment-reply" onclick="comment_updreply();" /> Réponse au commentaire de '+comment_author+' posté le '+comment_dt+'</span>');
	$.scrollTo($('#comment-post').get(0),800);
}

function comment_updreply()
{
	if(current_reply == false) {
		current_reply = true;
		comment_plugin += new_comment_plugin;
	}
	else {
		current_reply = false;
		comment_plugin = old_comment_plugin;
	}
}