// Cookie reader
function readCookie(name) {
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0) {
    offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      end = document.cookie.indexOf(';', offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

// Show more press based on cookie
function more_press() {
  $("loader").show();
  var current = readCookie("show_posts");
  // How many more posts to show on each click
  var more = parseInt(current)+5;
  new Ajax.Request("/press/more?p="+more, {method: "get"});
  Event.addBehavior.reassignAfterAjax = true;
  // Set window state for lightbox
  window.acInit = false;
} 

Event.addBehavior({
  // Track lightbox clicks
  '.excerpt a.lbOn:click': function() {
    pageTracker._trackPageview(this.href);
    return false;
  },
  // Track lightbox clicks
  '#jobs-list a.lbOn:click': function() {
    pageTracker._trackPageview(this.href);
    return false;
  },
  // Show more press
  "#more-press a:click":function() { 
    more_press();
    Effect.ScrollTo("more-press", {offset: -25});
    return false;
  },
  // Hide error notices
  "#error-notice:click": function() {
    this.hide();
  }
})

// Adds _blank in non-deprecated way
Event.observe(window, "load", function() {
  $$('a[rel="external"]').each(function(link){
    link.writeAttribute("target","_blank");
    link.writeAttribute("title","Opens in new window");
  })
  $$("input").each(function(input){
    input.writeAttribute("title","");
  })
})
