(function($) {
	$(function() {

		var textarea = $('textarea[name="comment"]');
		var showText = 'Preview';
		var hideText = 'Hide preview';
		var comment = '';
		var smiles = {";-)":"icon_wink.gif",":|":"icon_neutral.gif",":x":"icon_mad.gif",":twisted:":"icon_twisted.gif",":smile:":"icon_smile.gif",":shock:":"icon_eek.gif",":sad:":"icon_sad.gif",":roll:":"icon_rolleyes.gif",":razz:":"icon_razz.gif",":oops:":"icon_redface.gif",":o":"icon_surprised.gif",":mrgreen:":"icon_mrgreen.gif",":lol:":"icon_lol.gif",":idea:":"icon_idea.gif",":grin:":"icon_biggrin.gif",":evil:":"icon_evil.gif",":cry:":"icon_cry.gif",":cool:":"icon_cool.gif",":arrow:":"icon_arrow.gif",":???:":"icon_confused.gif",":?:":"icon_question.gif",":!:":"icon_exclaim.gif"}
		var smilesDir = 'http://eq2wire.com/wp-includes/images/smilies/';

		textarea.wrap('<div id="jcpWrap"></div>');
		textarea.before('<div id="jcpToolbar"></div>');
		$('#jcpToolbar').prepend('<div id="previewTab">'+ showText +'</div>');
		var toggled = false;

		$('#previewTab').click(function() {
			if (!toggled) {
				comment = textarea.val();
				if (comment != '') comment = comment + '\n\n';
				previewText = comment.replace(/(<\/?)script/g,'$1noscript')
				.replace(/ (http:\/\/\S+)/gi, ' <a href="$1">$1</a>')
				.replace(/\n(http:\/\/\S+)/gi, '\n<a href="$1">$1</a>')
				.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 previewHTML = '<span style="font-family: Times, Times New Roman, serif;"><u>Preview of your message:</u></span>'+ previewText +'';
				var preview = $('<div id="jQueryCommentPreview"></div>');
				$.each(smiles, function(key, value) {
					function str_replace(search, replace, subject) {
						var temp = subject.split(search);
						return temp.join(replace);
					}
					previewHTML = str_replace(key, '<img src="' + smilesDir + value + '" />', previewHTML);
				})
				preview.html(previewHTML);
				textarea.after(preview).hide();
				$(this).text(hideText);
				$('#htmlEditor a').hide();
				toggled = true;
			} else {
				$('#jQueryCommentPreview').remove();
				$('#htmlEditor a').show();
				$(this).text(showText);
				textarea.show().focus();
				toggled = false;
			}
		})


		var htmlEditor = '<div id="htmlEditor"><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">b-quote</a><a href="#" tag="code">code</a></div>';

		$('#jcpToolbar').prepend(htmlEditor);

		function insert(start, end, mid) {
			var midText = '';
			element = document.getElementById(textarea.attr('id'));
			if (document.selection) {
				element.focus();
				sel = document.selection.createRange();
				if (sel.text == '') midText = mid;
				sel.text = start + sel.text + midText + end;
			} else if (element.selectionStart || element.selectionStart == '0') {
				element.focus();
				var startPos = element.selectionStart;
				var endPos = element.selectionEnd;
				var selText = element.value.substring(startPos, endPos);
				if (selText == '') midText = mid;
				element.value = element.value.substring(0, startPos) + start + selText + midText + end + element.value.substring(endPos, element.value.length);
			} else {
				element.value += start + end;
			}
		}

		$('#htmlEditor a').each(function() {
			var text = $(this).html().replace(/\[(.*)\]/, '<b> $1</b>');
			$(this).html(text);
		})

		$('#htmlEditor a').click(function() {
			var mid = '';
			var tag = $(this).attr('tag');
			var attribs = $(this).find('b').text();
			if (tag == 'a') {
				var URL = prompt('Enter the URL', 'http://');
				if (URL) {
					var blank = '';
					if (URL.indexOf(window.location.hostname) == -1) blank = ' target="_blank"';
					attribs = attribs.replace('href=""', 'href="' + URL + '"' + blank);
					var start = '<' + tag + attribs + '>';
					var mid = URL;
					var end = '</' + tag + '>';
				} else {
					var start = end = '';
				}
			} else if (tag == 'img') {
				var image = prompt('Enter the URL of the image', 'http://');
				if (image) {
					var imageAlt = prompt('Enter a description of the image', '');
					attribs = attribs.replace('src=""', 'src="' + image + '"');
					if (imageAlt) attribs = attribs.replace('alt=""', 'alt="' + imageAlt + '"');
					var start = '';
					var end = '<' + tag + attribs + ' />';
				} else {
					var start = end = '';
				}
			} else {
				var start = '<' + tag + attribs + '>';
				var end = '</' + tag + '>';
			}
			insert(start, end, mid);
			return false;
		})
		$('#htmlEditor').append('<a id="jcpSmiles" href="#"><img src="' + smilesDir + 'icon_smile.gif" alt="" /><span></span></a>');
		$.each(smiles, function(key, value) {
			$('#jcpSmiles span').append('<img src="' + smilesDir + value + '" alt="' + key + '" />');
		})
		$('#jcpSmiles').click(function() { return false; })
		$('#jcpSmiles img').click(function() {
			var mid = '';
			insert('', ' ' + $(this).attr('alt') + ' ', mid);
			return false;
		})
		$('#htmlEditor').append('<a href="http://dimox.net/jquery-comment-preview-wordpress-plugin/" target="_blank" title="About the plugin">?</a>');

	}) 
})(jQuery)
