var commentTemplate = new Template('<div id="comment-preview" style="display:#{display}"><h3>Prévisualisation</h3><ol class="commentlist"><li class="preview"><p class="info"><span><a class="commentnum" href="#comment-preview">#{commentnum}</a><img class="avatar" alt="" src="http://sunfox.org/images/gravatar.png" />#{author_link}</span>&nbsp;:</p>#{text}</li></ol><br style="clear:both" /></div>');

Event.observe(window, 'load', function() {
	if (!$('commentform'))
		return;
	new Insertion.After('commentform', commentTemplate.evaluate({display:'none'}));
	new Form.Observer('commentform', 0.3, comment_preview_update);
	comment_preview_update();
});

function comment_preview_update() {
	if (!$F('comment'))
		return $('comment-preview').hide();

	var comment = {};
	comment.display = 'block';
	comment.commentnum = $$('#commentlist li').length + 1;

	if ($('author'))
		comment.author = $F('author').escapeHTML().strip() || 'Anonyme';
	else
		comment.author = $('commentform').down('a').innerHTML.escapeHTML();

	comment.url = $('url') ? $F('url').escapeHTML() : "";

	comment.author_link = comment.url ? '<a href="'+comment.url+'">'+comment.author+'</a>' : comment.author;
	comment.text = wpautop(wptexturize($F('comment').stripScripts()));

	$('comment-preview').replace(commentTemplate.evaluate(comment));
}

// wptexturize and wpautop by Jeff & Iacovos Constantinou 
// http://dev.wp-plugins.org/wiki/LiveCommentPreview
function wptexturize(text) {
	text = ' '+text+' ';
	var next 	= true;
	var output 	= '';
	var prev 	= 0;
	var length 	= text.length;
	while (prev < length) {
		var index = text.indexOf('<', prev);
		if (index > -1) {
			if (index == prev)
				index = text.indexOf('>', prev);
			index++;
		} else
			index = length;
		var s = text.substring(prev, index);
		prev = index;
		if (s.substr(0,1) != '<' && next == true) {
			s = s.replace(/---/g, '&#8212;');
			s = s.replace(/--/g, '&#8211;');
			s = s.replace(/\.{3}/g, '&#8230;');
			s = s.replace(/``/g, '&#8220;');
			s = s.replace(/'s/g, '&#8217;s');
			s = s.replace(/'(\d\d(?:&#8217;|')?s)/g, '&#8217;$1');
			s = s.replace(/([\s"])'/g, '$1&#8216;');
			s = s.replace(/(\d+)"/g, '$1&Prime;');
			s = s.replace(/(\d+)'/g, '$1&prime;');
			s = s.replace(/([^\s])'([^'\s])/g, '$1&#8217;$2');
			s = s.replace(/(\s)"([^\s])/g, '$1&#8220;$2');
			s = s.replace(/"(\s)/g, '&#8221;$1');
			s = s.replace(/'(\s|.)/g, '&#8217;$1');
			s = s.replace(/\(tm\)/ig, '&#8482;');
			s = s.replace(/\(c\)/ig, '&#169;');
			s = s.replace(/\(r\)/ig, '&#174;');
			s = s.replace(/''/g, '&#8221;');
			s = s.replace(/(\d+)x(\d+)/g, '$1&#215;$2');
		} else if (s.substr(0,5) == '<code')
			next = false;
		else
			next = true;
		output += s; 
	}
	return output.substr(1, output.length-2);	
}

function wpautop(p) {
	p = p + '\n\n';
	p = p.replace(/(<blockquote[^>]*>)/g, '\n$1');
	p = p.replace(/(<\/blockquote[^>]*>)/g, '$1\n');
	p = p.replace(/\r\n/g, '\n');
	p = p.replace(/\r/g, '\n');
	p = p.replace(/\n\n+/g, '\n\n');
	p = p.replace(/\n?(.+?)(?:\n\s*\n)/g, '<p>$1</p>');
	p = p.replace(/<p>\s*?<\/p>/g, '');
	p = p.replace(/<p>\s*(<\/?blockquote[^>]*>)\s*<\/p>/g, '$1');
	p = p.replace(/<p><blockquote([^>]*)>/ig, '<blockquote$1><p>');
	p = p.replace(/<\/blockquote><\/p>/ig, '<p></blockquote>');	
	p = p.replace(/<p>\s*<blockquote([^>]*)>/ig, '<blockquote$1>');
	p = p.replace(/<\/blockquote>\s*<\/p>/ig, '</blockquote>');	
	p = p.replace(/\s*\n\s*/g, '<br />');
	return p;
}

