var urlroot = window.location.host.indexOf('idium.n') !== -1 ? window.location.pathname.substring(0,window.location.pathname.indexOf('/',2)) + "/" : "/";

jQuery(window).load(function($) {
     var $comments = jQuery(".comments"); // CACHE
  if ($comments.length) {
        $comments.find(".ajax").each(function(){
         var commentsHtml = $comments.find(".ajax").attr("href");
	 $comments.load(commentsHtml, function(){
            var writeACommentText = jQuery("#writeACommentText") ? jQuery("#writeACommentText").text() : "Skriv en kommentar." ;
            var commentPostedText = jQuery("#commentPostedText") ? jQuery("#commentPostedText").text() : "Kommentaren er lagt til og venter moderering." ;
            var $commentForm = jQuery("#commentForm"); //CACHE
	    if($commentForm.length) {
	      $commentForm.submit(function(event){
	         var $this = jQuery(this), //CACHE
	             $response = jQuery("#objectResponse"),  //CACHE
	             $signature = $this.find("#signature"),  //CACHE
	             $comment = $this.find("#comment"); //CACHE
	         event.preventDefault();
	         if ($comment.val() === "") {
	           $response.text(writeACommentText);
	           $comment.addClass("error");
	         } else {
	           jQuery.post(urlroot, $this.serialize(), function(){
                 jQuery("<li class='comment'><p>" + $signature.val() + "</p><p><strong>" + $comment.val() + "</strong></p></li>").appendTo("ul.commentList");
	             $response.text(commentPostedText);
                     jQuery(".noComments").hide();
	             $comment.removeClass("error").val("");
	           });
	         }
	      });
	    } 
	  });
	});
  } // END COMMENTS


  });
