$( document ).ready( function() {


	/* COMMENT PREVIEW */

	function some_html(string) {
		string = string.replace(/<\/?[^>]+>/gi, "");
		return string.replace(/\n/g, "<br />");
	}

	$("#comment-form div").append('<input class="preview" type="button" value="Preview" />');

	$("#comment-form input.preview").click(function() {

		var url = $("#comment-form").attr("action");
		url = url.replace("/comments/comments-post.php", "");

		var avatar = '<img alt="avatar" class="avatar avatar-48" src="'+url+'/media/2010/01/someone-1.png" style="width: 48px; height: 48px;" />';

		var name = $("#comment-form #author").val();
		if(name == '') {
			name = 'Anonymous';
		}

		var link = jQuery.trim( $("#comment-form #url").val() );
		if( link != "" ) {
			if( link.indexOf("http://") == -1 && link.indexOf("https://") == -1 ) {
				link = "http://"+link;
			}
			name = '<span class="author"><a href="'+link+'">'+name+'</a></span>';
		}
		else {
			name = '<span class="author">'+name+'</span>';
		}

		var comment = $("#comment-form textarea[name='comment']").val();
		comment = comment.replace(/<\/?[^>]+>/gi, "");
		comment = comment.replace(/\n/g, "<br />");

		$("#comments li.preview").remove();

		if( $("#comments ol").length == 0 ) {
			$("#comments h3").after("<ol></ol>");
		}

		$("#comments ol").append('<li class="comment preview"><header>'
		+avatar
		+name
		+'</header>'
		+'<div>'+comment+'</div>'
		+'<footer>PREVIEW</footer>'
		+'</li>');

		$("#comments li.preview").hide();
		$("#comments li.preview").fadeIn("slow");

	});

	$("#comment-form textarea[name='comment']").keyup(function() {
		if($("#comments li.preview").length > 0) {
			var comment = $("#comment-form textarea[name='comment']").val();
			comment = some_html(comment);
			$("#comments li.preview div").html(comment);
			if($("#comments li.preview div").text() == '') {
				$("#comments li.preview").fadeOut("slow");
			}
		}
	});



	/* I LOVE THIS POST */

	$( ".ilovethis" ).click( function() {
		if( !( document.cookie && document.cookie.search( "," + $( this ).attr( "id" ) + "," ) > -1 ) ) {

			$.post(
				"themes/3weeks/functions.php",
				{ lovedthispost: $( this ).attr( "id" ) }
			);
			$( this ).hide();
			var counter = $( this ).text().replace( "♥ ", "" );
			counter++;
			$( this ).text( "♥ " + counter );
			$( this ).fadeIn( "normal" );
			if( document.cookie ) {
				document.cookie = "loved=" + document.cookie.replace( "loved=", "" ) + "," + $( this ).attr( "id" ) + ",";
			}
			else {
				document.cookie = "loved=," + $( this ).attr( "id" ) + ",";
			}

		}
	} );


} );