this.init = function() {
  /* Image hover. */
  $("a.hover").hover(
    function() {
      $(this).find(":first-child").hide();
      $(this).find(":last-child").show();
    },
    function() {
      $(this).find(":last-child").hide();
      $(this).find(":first-child").show();
    }
  );
  /* List browse. */  
  $("ul.browse").each(function(i) {
    max = 0;
    $(this).children("li").each(function(i) {
      /* Temporarily display the element to get the real height. */
      $(this).show();
      max = $(this).height() > max ? $(this).height() : max;
      if (i > 0) {
        $(this).hide();
      }
    });
    $(this).children("li").height(max);
  });
   
  $("ol.browse-controls a").click(function() {
    $(this).blur();
    var controls = $(this).parents("ol");
    var browse = controls.parent().find("ul.browse");
    $("li", browse).hide();
    $("li:nth-child(" + this.rel + ")", browse).show();
    $("li", controls).removeClass("selected");
    $(this).parent("li").addClass("selected");
    return false;
  });
    
  /* Newletter signup. */
  var signup = $("#newsletter-signup");
  $("#newsletter-signup-button").click(function(event) {
    if (signup.is(":hidden")) {
      signup.show();
      signup.find("input[type=radio]:first").attr("checked", "checked");
      $("html").click(function() {
        signup.hide();
      });
      signup.click(function(event) {
        event.stopPropagation();
      });
    } else {
      signup.hide();
    }
    return false;
  });
  var email_input = $("input#newsletter-email");
  email_input.focus(function() {
    $(this).val("");
  });
  $("#newsletter-subscribe").click(function() {
    subscribe(email_input.val());
    return false;
  });
  email_input.keypress(function(e) {
    if (e.keyCode == "13") {
      subscribe($(this).val());
    }
  })
  function subscribe(email) {
    $.get(
      "/async.php",
      {
        email: email,
        list: signup.find("input[type=radio]:checked").val()
      },
      function(data) {
        $("p:first", signup).replaceWith(data);
      }
    );
  }
  
  var swf_params = {
    bgcolor: "#000000",
    allowFullScreen: true,
    wmode: "opaque"
  };

  /* Video preview inplace. */
  $("div.video-preview.inplace a").click(function() {
    var link = this;
    var id = $(this).parent();
    $("img", id).hide();
    /* Embed flash or redirect to swf. */
    swfobject.embedSWF(link.href, id.attr("id"), id.parent().width(), id.parent().height(),
      "8", null, null, swf_params, null, function(e) { if (!e.success) location.href= href; });
    return false;
  });

  /* Video preview popup. */
  $("div.video-preview.popup a").click(function(e) {
    var link = $(this), id = "youtube-player", href = link.attr("href");
    var width =  576, height = 324;
    
    /* Find article id and log hit. */
    var url = link.closest("div.box").find("h5 a").attr("href");
    log_hit(url.substring(url.lastIndexOf("/") + 1));
        
    $.fancybox({
      "padding" : 0,
      "width" : width,
      "height" : height,
      "autoSize" : false,
      "content" : "<div id='" + id + "'></div>",
      "beforeShow" : function() {
        /* Embed flash or redirect to swf. */
        swfobject.embedSWF(href, id, width, height, "8", null, null, swf_params, null,
          function(e) { if (!e.success) location.href= href; });
      }
    });
    e.preventDefault();
  });
  
  /* Searching. */
  var search_input = $("input#search-value");
  search_input.focus(function() {
    $(this).val("");
  });
  search_input.keypress(function(e) {
    if (e.keyCode == "13") {
      search($(this).val());
    }
  });
  $("#search img").click(function() {
     search(search_input.val());
     return false;
  });
  $("select#sort-option").change(function() {
    window.location.href = window.location.href.replace(/\/sort\/[^\/]+/i, "") + "/sort/" + $(this).val();
  });
  $("select#offset-option").change(function() {
    window.location.href = window.location.href.replace(/\/offset\/[^\/]+/i, "") + "/offset/" + $(this).val();
  });
  function search(text) {
    window.location.href = window.location.pathname.match(/^\/search\//i) ?
      window.location.href.replace(/\/(text|offset)\/[^\/]+/ig, "") + "/text/" + text :
      "http://" + window.location.hostname + "/search/text/" + text
    ;
  }
  
  /* Update article hits. */
  var article = $("div.article.stand-alone");
  if (article.length > 0) {
    log_hit(article.attr("id").replace("article-", ""));
  };
  function log_hit(id) {
    $.get("/async.php", { log_hit: id } );
  }
  
  /* Load rest of AddThis. */
  addthis.init();
  
  /* Tweet box. */
  $("div.tweet div.content").tweet({
    avatar_size: 32,
    username: "euobs",
    count: 3,
    refresh_interval: 180
  });
  
  /* Delegate. */
  carousel();
};

/* Analytics event tracking for outbound links.  */
function track_outbound_link(link, category, action, label) {
  $(link).attr("target", "_blank");
  _gaq.push(['_trackEvent', category, action, label]);
}

/* News carousel. */
this.carousel = function() {
  /* Elements. */
  var carousel = $("div#carousel"), scroll = carousel.find("ol.scroll");
  var shown = carousel.find("div.shown"), hidden = carousel.find("div.hidden");
  var close = shown.find("div.close"), open = hidden.find("div.open");
  var nav = shown.find("div.nav"), next = shown.find("div.nav.next"), prev = shown.find("div.nav.prev"); 
  var current = null;
  
  /* Pixel metrics. */
  var size = scroll.find("li:first-child").outerWidth(true);
  var items = 3, distance = size * items, max_left = get_left();
  var min_left = -1 * (scroll.find("li").size() * size - distance) + max_left;
  
  /* Delays. */
  var scroll_duration = 300, nav_fade_delay = 150, slide_delay = 300, first_show_delay = 1500;
  
  /* Locks. */
  var lock = false;
  
  /* Cookies. */
  var cookie_state = "carousel-state", cookie_left = "carousel-left", opened = "opened", expire = 21;
  
  /* Initial load. */
  setTimeout(function() {
    if ($.cookie(cookie_state) == opened) {      
      scroll.css("left", $.cookie(cookie_left) + "px");
      shown.fadeIn(function() { update_nav(); });  
    } else {
      hidden.fadeIn();
    }
  }, first_show_delay);
  
  /* Open button. */
  open.click(function() {
    slide_out(hidden, function() {
      slide_in(shown);
    });
    set_cookie(cookie_state, opened, expire);
  });
  
  /* Close button. */
  close.click(function() {
    slide_out(shown, function() {
      slide_in(hidden);
    });
    set_cookie(cookie_state, null);
  });
   
  /* Next button. */
  next.click(function() {
    if (lock) return; lock = true;
    scroll.animate({
      left: "-=" + distance + "px"
    }, scroll_duration, "swing", function() {
      update_nav(function() { set_cookie(cookie_left, get_left()); });
    });
  });
  
  /* Previous button. */
  prev.click(function() {
    if (lock) return; lock = true;
    scroll.animate({
      left: "+=" + distance + "px"
    }, scroll_duration, "swing", function() {
      update_nav(function() { set_cookie(cookie_left, get_left()); });
    });    
  });
  
  /* Hide carousel when scrolling to (near) bottom of page. */
  $(window).scroll(function() {
    if ($(this).scrollTop() + $(this).height() > $(document).height() - 200) {
      carousel.fadeOut();
    } else if (carousel.is(":hidden")) {
      carousel.fadeIn();
    }
  });
  
  /* State cookie. */
  function set_cookie(name, val, expire) {
    $.cookie(name, val, { expires: expire, path: "/" });
  }
    
  /* Display or hide navigation buttons. */
  function update_nav(callback) {
    max_left <= get_left() ? prev.fadeOut(nav_fade_delay) : prev.fadeIn(nav_fade_delay);
    min_left >= get_left() ? next.fadeOut(nav_fade_delay) : next.fadeIn(nav_fade_delay);
    nav.promise().done(function() {
      if (callback) callback();
      lock = false;
    });
  }
  
  /* Slide carousel out of page. */
  function slide_out(elm, callback) {
    nav.fadeOut(nav_fade_delay);
    nav.promise().done(function() {
      elm.slideUp(slide_delay, callback);
    });
  }
  
  /* Slide carousel into page. */
  function slide_in(elm) {
    nav.hide();
    elm.slideDown(slide_delay, function() {
      update_nav();
    });
  }
  
  /* Get CSS left margin of scrollable list. */
  function get_left() {
    return parseInt(scroll.css("left"));
  }
}

/* Survey 2011. */
this.survey = function() {
  var cookie = "survey-2011";
  
  if ($.cookie(cookie) == null) {
    set_cookie(1);  
  }
  if ($.cookie(cookie) == 1) {
    $.fancybox({
      "href" : "https://docs.google.com/a/euobserver.com/spreadsheet/embeddedform?formkey=dFc1TjFLU0FIdTIycXllbXp0XzMxdnc6MQ",
      "type" : "iframe",
      "afterClose" : function() { set_cookie(2); }
    });
  }
  
  function set_cookie(val) {
    $.cookie(cookie, val, { expires: 21, path: "/" });
  }
}
