(function($) {

	$(function() {

		var show_text = 'Preview';
		var hide_text = 'Hide preview';
		var textarea = $('textarea[name="comment"]');
		var comment = '';

		$(textarea).wrap('<div id="jquery-comment-wrap"></div>');
		$(textarea).before('<div id="jquery-comment-preview"></div>');
		$('#jquery-comment-preview').prepend('<div id="preview-tab">'+ show_text +'</div>');

		$('#preview-tab').toggle(
			function() {
				comment = $(textarea).val();
				if ($(textarea).val() != '') comment = comment + '\n\n';
				comment_preview = comment.replace(/(<\/?)script/g,'$1noscript')
				.replace(/(<blockquote[^>]*>)/g, '\n$1')
				.replace(/(<\/blockquote[^>]*>)/g, '$1\n')
				.replace(/\r\n/g, '\n')
				.replace(/\r/g, '\n')
				.replace(/\n\n+/g, '\n\n')
				.replace(/\n?(.+?)(?:\n\s*\n)/g, '<p>$1</p>')
				.replace(/<p>\s*?<\/p>/g, '')
				.replace(/<p>\s*(<\/?blockquote[^>]*>)\s*<\/p>/g, '$1')
				.replace(/<p><blockquote([^>]*)>/ig, '<blockquote$1><p>')
				.replace(/<\/blockquote><\/p>/ig, '</p></blockquote>')
				.replace(/<p>\s*<blockquote([^>]*)>/ig, '<blockquote$1>')
				.replace(/<\/blockquote>\s*<\/p>/ig, '</blockquote>')
				.replace(/\s*\n\s*/g, '<br />');

				var author = $('#author').val();
				var url = $('#url').val();
				if(!$('#url').length) url = '';
				if(!$('#author').length) author = '';
				if(url != '') author = '<a href="'+ url +'">'+ author +'</a>';
				var date = '6 September, 2010 at 2:53 AM';
				var preview_html = '<ol id="comment-preview"><p><strong>'+ author +'</strong> says ('+ date +'):</p>'+ comment_preview +'</li></ol>';

				$(textarea).after('<div id="textarea_clone"></div>');
				$(textarea).clone().appendTo($('#textarea_clone'));
				$('#textarea_clone textarea').text(comment);
				$('#textarea_clone').hide();
				$(textarea).replaceWith('<div id="comment_preview"></div>');
				$('#comment_preview').html(preview_html);
				$('#preview-tab').text(hide_text);
				$('#html-editor a').hide();
			},
			function() {
				$('#textarea_clone').remove();
				$('#comment_preview').replaceWith(textarea);
				$(textarea).text(comment);
				$('#preview-tab').text(show_text);
				$('#html-editor a').show();
				$(textarea).focus();
			}
		)


		var html_editor = '<div id="html-editor"><a href="#" tag="strong" id="ed_strong">b</a><a href="#" tag="em" id="ed_em">i</a><a href="#" tag="a" id="ed_a">link[href=""]</a><a href="#" tag="blockquote">block quotation</a><a href="#" tag=>image[note: this button has a Wordpress conflict. Paste your image URL where you would like it to appear in your post, and erase this message. B/P admins will add the picture on your post - thanks]</a></div>';

		$('#jquery-comment-preview').prepend(html_editor);

		function insert(start, end) {
			element = document.getElementById('comment');
			if (document.selection) {
				element.focus();
				sel = document.selection.createRange();
				sel.text = start + sel.text + end;
			} else if (element.selectionStart || element.selectionStart == '0') {
				element.focus();
				var startPos = element.selectionStart;
				var endPos = element.selectionEnd;
				element.value = element.value.substring(0, startPos) + start + element.value.substring(startPos, endPos) + end + element.value.substring(endPos, element.value.length);
			} else {
				element.value += start + end;
			}
		}

		$('#html-editor a').each(function() {
			var text = $(this).html().replace(/\[(.*)\]/, '<b> $1</b>');
			$(this).html(text);
		})

		$('#html-editor a').click(function() {
			var tag = $(this).attr('tag');
			var attribs = $(this).find('b').text();
			if (tag == 'a') {
				var URL = prompt('Enter the URL', 'http://');
				if (URL) {
					attribs = attribs.replace('href=""', 'href="' + URL + '"');
					var start = '<' + tag + attribs + '>';
					var end = '</' + tag + '>';
				} else {
					var start = end = '';
				}
			} else if (tag == 'img') {
				var start = '';
				var end = ' <' + tag + attribs + ' />';
			} else {
				var start = '<' + tag + attribs + '>';
				var end = '</' + tag + '>';
			}
			insert(start, end);
			return false;
		})

	}) 

})(jQuery)
