(function($) {
	$(function() {
	
		var textarea = $('textarea[name="comment"]');
		var show_text = 'Podgląd';
		var hide_text = 'Ukryj podgląd';
		var comment = '';

		$('#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 author = $('input#author').length ? $('input#author').val() : ($('a#author').length ? $('a#author').html() : null);
				var preview_html = '<ol id="comment-preview"><li><p><strong>'+ author +'</strong></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);
				$(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();
			}
		)

		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').click(function() {
			var tag = $(this).attr('tag');
			var attribs = $(this).find('b').text();
			if (tag == 'a') {
				var URL = prompt('Podaj 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 end = '</' + tag + '>';
				} else {
					var start = end = '';
				}
			} else {
				var start = '<' + tag + attribs + '>';
				var end = '</' + tag + '>';
			}
			insert(start, end);
			return false;
		})
	}) 
})(jQuery)

