(function($) {
	$(function() {

		var show_text = 'Preview';
		var hide_text = 'Ascunde preview';
		var textarea = $('textarea[name="comment"]');
		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://www.worldit.info/wp-includes/images/smilies/';

		$(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(/ (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 email = $('#email').val();
				if(!email) email = '';
				var md5 = MD5(email);
				var avatar = '<img src="http://www.gravatar.com/avatar/' + md5 + '?s=32" alt="" class="avatar" />';
				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 = 'Septembrie 10, 2010 at 7:24 pm';
				var preview_html = '<div  id="comment-preview"><div>'+ avatar +'<cite class="fn"><span>'+ author +'</span></cite> <span class="says">says:</span></div><div><span>'+ date +'</span></div><div><p>'+ comment_preview +'</p></div>';

				$(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);
				var previewHTML = $('#comment_preview').html();
				$.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);
				})
				$('#comment_preview').html(previewHTML);
				$(this).text(hide_text);
				$('#html-editor a').hide();
			},
			function() {
				$('#textarea_clone').remove();
				$('#comment_preview').replaceWith(textarea);
				comment = comment.replace(/\n\n+/g, '\n\n');
				$(textarea).text(comment);
				$(this).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">b-quote</a><a href="#" tag="code">code</a></div>';

		$('#jquery-comment-preview').prepend(html_editor);

		function insert(start, end) {
			element = document.getElementById($(textarea).attr('id'));
			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) {
					var blank = '';
					attribs = attribs.replace('href=""', 'href="' + URL + '"' + blank);
					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;
		})
		$('#html-editor').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() {
			insert('', ' ' + $(this).attr('alt') + ' ');
			return false;
		})
	}) 
})(jQuery)
