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

	var container = $('<div class="jcp-container"></div>');
	var textarea = $('textarea[name="comment"]').before(container).appendTo(container);
	var showText = 'Preview';
	var hideText = 'Hide preview';

	var previewTab = $('<a class="jcp-preview-tab">'+ showText +'</a>');
	var htmlEditor = $('<div class="jcp-html-editor"><a data-tag="strong" class="btn-strong">b</a><a data-tag="em" class="btn-em">i</a><a data-attr="href=&quot;&quot;" data-tag="a" class="btn-a">link</a><a data-attr="src=&quot;&quot; alt=&quot;&quot;  class=&quot;&quot;" id="show-for-admin" data-tag="img" class="btn-img">img</a></div>');
	var toolbar = $('<div class="jcp-toolbar"></div>').insertBefore(textarea).prepend(previewTab).prepend(htmlEditor);
	var preview = $('<div class="jcp-preview"></div>').hide();
	var bracketLeft = '<';
	var bracketRight = '>';

	previewTab.click(function() {
		if ( preview.is(':hidden') ) {
			if ( textarea.val() !== '' ) {
				container.addClass('jcp-loading');
				htmlEditor.hide();
				var format = $.post(
					'https://dr-no.co.uk/wp-content/plugins/jquery-comment-preview/ajax.php',
					{ content: textarea.val(), tags: '' }
				).done(function(previewText) {
					container.removeClass('jcp-loading');
					var email = $('input[name="email"]').val();
					if (!email) email = '';
					var md5 = MD5(email);
					var avatar = '<img src="http://www.gravatar.com/avatar/' + md5 + '?s=40" alt="" class="avatar" />';
					var author = $('input[name="author"]').val();
					var url = $('input[name="url"]').val();
					if (!$('input[name="url"]').length) url = '';
					if (!$('input[name="author"]').length) author = '';
					if (url !== '') author = '<a href="'+ url +'">'+ author +'</a>';
					var date = 'January 25, 2025 at 1:59 pm';
					var previewHTML = '<div class="comment">'+ avatar +'<span class="comment-author">'+ author +'</span><span class="comment-date">'+ date +'</span>'+ previewText +'</div>';
					previewTab.text(hideText);
					preview.html(previewHTML);
					textarea.hide();
					preview.insertAfter(textarea).show();
					if (typeof jcpPreviewLoaded === 'function') jcpPreviewLoaded();
				});
			}
		} else {
			preview.remove();
			htmlEditor.show();
			previewTab.text(showText);
			textarea.show().focus();
		}
	});

	function insert(start, end, mid) {
		var midText = '';
		element = textarea[0];
		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.find('a').click(function() {
		var start = '',
				mid = '',
				end = '';
		var tag = $(this).data('tag');
		var attribs = $(this).data('attr');
		if (!attribs) attribs = '';
		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);
				start = bracketLeft + tag + ' ' + attribs + bracketRight;
				mid = URL;
				end = bracketLeft + '/' + tag + bracketRight;
			} else {
				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 imageWPID = prompt('Enter the WP ID number', '');
				if (imageWPID) attribs = attribs.replace('class=""', 'class="wp-image-' + imageWPID + '"');
				start = '<!-- wp:image {\"id\":';
				start = start + imageWPID;
				start = start + ',\"sizeSlug\":\"large\",\"linkDestination\":\"none\"} -->';
				start = start + '\n<figure class="wp-block-image size-large">';
				end = bracketLeft + tag + ' ' + attribs + bracketRight;
				end = end + '</figure>\n<!-- \/wp:image -->';
			} else {
				start = end = '';
			}
		} else {
			if (attribs !== '') attribs = ' ' + attribs;
			start = bracketLeft + tag + attribs + bracketRight;
			end = bracketLeft + '/' + tag + bracketRight;
		}
		insert(start, end, mid);
	});
	htmlEditor.append('<a style="display: none; href="http://dimox.net/jquery-comment-preview-wordpress-plugin/" target="_blank" title="About the plugin">?</a>');

});
})(jQuery);
