$(document).ready(function(){


  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
      fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));


  $("#fylkerSelect").change(function(){
    window.location = $(this).val();
  });
  
  $("#navTop .first span").hide(); // styling is okay in JS

  var leftHeight = $("#leftColumn").height();
  $("#content").css({minHeight:leftHeight+'px'});

    var currentfolder = ips.system.currentfolder, urlroot = ips.system.urlroot;

  // VALIDATE E-MAIL.
  var emailpattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

  // NEWSLETTER SIGN UP
  jQuery("#newsletterForm").submit(function(e){
    e.preventDefault();
    var $this = jQuery(this); //cache
    var $email = $this.find("input.email"); //cache
    if(!$this.find(".response").get()[0]) {
      jQuery("<p class='response' />").prependTo($this);
    }
    var $response = $this.find(".response"); // Cache
    if (emailpattern.test($email.val())) {
      $response.text("Sender...");
      $.ajax({
        url: "?template=newsletterResponse;ajax=true;",
        data: $this.serialize(),
        error: function() {
          $response.text("Feil / Error");
        },
        success: function(data) {
          $response.text("Din e-postadresse er lagt til som mottaker av nyhetsbrev");
          $email.removeClass("error").val("");
        }
      });
    } else {
      $email.addClass("error");
      $response.text("Skriv inn din e-postadresse");
    }
  });
  
  $("#searchForm .query").watermark("s\u00F8k p\u00E5 nettstedet");
  $("#newuser_email").watermark("Din epost adresse");


// this is a small helper extension i stole from
// http://www.texotela.co.uk/code/jquery/reverse/
// it merely reverses the order of a jQuery set.
$.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};

// create two new functions: prevALL and nextALL. they're very similar, hence this style.
$.each( ['prev', 'next'], function(unusedIndex, name) {
    $.fn[ name + 'ALL' ] = function(matchExpr) {
        // get all the elements in the body, including the body.
        var $all = $('body').find('*').andSelf();

        // slice the $all object according to which way we're looking
        $all = (name == 'prev')
             ? $all.slice(0, $all.index(this)).reverse()
             : $all.slice($all.index(this) + 1)
        ;
        // filter the matches if specified
        if (matchExpr) $all = $all.filter(matchExpr);
        return $all;
    };
});

/* EOF */

if($(".today").length) { // for calendar
$("td.today").prevALL("td.day").addClass("inthepast");
$("td.today").prevALL("td.day a").addClass("inthepast");
}

/*Pagination*/

$(".summary, .article").quickpaginate({ perpage: 10, showcounter: true, pager : $("#content") });



});
