jQuery(document).ready(function($) {
  var currentfolder = ips.system.currentfolder,
    urlroot = ips.system.urlroot,
    articleResults = ips.system.articleResults,
    calendarResults = ips.system.calendarResults;

  var urlroot = ips.system.urlroot;
  var params = ips.data.params;

  if (jQuery("#commentsArchive").length) {
    jQuery("#commentsArchive").ajaxStart(function() {
      $(this).addClass("loading");
    }).ajaxStop(function() {
      $(this).removeClass("loading");
    });

    var folderid = jQuery("#commentsArchive").attr("rel");
    jQuery("#commentsArchive").load(urlroot + '?template=ajaxLatestComments&id=' + folderid + '&pagesize=10&page=1');

    jQuery("#commentsArchive .paginator a").live("click", function(e) {
      e.preventDefault();
      var page = jQuery(this).attr("rel");
      jQuery("#commentsArchive").load(urlroot + '?template=ajaxLatestComments&id=' + folderid + '&pagesize=10&page=' + page);
    });
  }
  var $body = $("body");
  if ($body.hasClass("dualsearch")) {
    // SEARCH
    var $articleResults = jQuery(".articleResults").load(encodeURI(ips.system.articleResults), function(response) {
      jQuery(this).html(response);
    });
    var $calendarResults = jQuery(".calendarResults").load(encodeURI(ips.system.calendarResults), function(response) {
      jQuery(this).html(response);
    });

    // Quickfix
    jQuery(".articleResults .paginator a").live("click", function(e) {
      e.preventDefault();
      jQuery(".articleResults").load(jQuery(this).attr("href"));
    });

    jQuery(".calendarResults .paginator a").live("click", function(e) {
      e.preventDefault();
      jQuery(".calendarResults ").load(jQuery(this).attr("href"));
    });

  } // END SEARCH 

  
  // DISABLE SUBMIT BUTTON FOR COMMENTS UNLESS NAME PROVIDED
  /*function signatureProvided() {
    var provided_name = jQuery('input#signature').val();
    if (provided_name.length >= 3) {
      return true;
    } else {
      return false;
    }
  }

  jQuery("#commentForm").click(function(event) {
    if (!signatureProvided()) {
      event.preventDefault();
      jQuery('h4').css('color', 'red');
    }
  });*/
  // new Ajax.Updater('objectResponse', './?template=ajaxresponse&_ips_encoding=UTF-8', 
  // {asynchronous:true, parameters:Form.serialize(this)}); $('commentForm').reset(); return false;
  jQuery("#commentForm").live("submit", function(e){
    e.preventDefault();
    var $this = jQuery(this);
    if ($("#signature").val() == '' || $("#comment").val() == ''){
      alert("Vennligst fyll inn feltene og pr\u00F8v igjen.");
      return false;
    }
    jQuery.ajax({
      url: "./?template=ajaxresponse&_ips_encoding=UTF-8",
      type: "POST",
      data: $this.serialize(),
      success: function(data) {
            //called when successful
            $this.find("h5").text(data).css("color","red");
            $("#comment,#signature").val("");
     },    
      error: function(data) {
            //called when there is an error
            //console.log(data);
      }
    });
  });
  
  // END DISABLE BUTTON

});
