$(document).ready(function() {

  // Header Search
  $("#search-input").focus(function() {
    $(this).attr("value", "");
    $(this).removeClass("faded-input");
  });
  $("#search-input").blur(function() {
    if($(this).attr("value") == "") {
      $(this).attr("value", "Search Bonfire");
      $(this).addClass("faded-input");
    }
  });

  // Jump to page functionality in the nav_paginate element
  if ($("form.jump-to-page").length > 0) {
    // Bind the submit function to change the url
    $("form.jump-to-page").each(function() {
      $(this).bind("submit", function(e) {
        var url = document.location.href;
        var parts = url.split(":"); // splits the url into 3 parts, the "http", the url itself, and the page number
        var base = '';
        if (parts[1].indexOf('/admin/') != -1 && parts[1].indexOf('/a_index/') != -1) {
          // If we're in the admin area and already using the right url
          base = parts[0]+":"+parts[1];
        } else if (parts[1].indexOf('/admin/') != -1 && parts[1].indexOf('/a_index/') == -1) {
          // In the admin area, but not using the right url yet
          base = parts[0]+":"+parts[1]+"/a_index/page";
        } else {
          // We're not in the admin area
          base = parts[0]+":"+parts[1];
        }
        var p = $(this).find(".jump-to-page-input").val();
        if (!p) { p = 1; }
        window.location = base+":"+p;
        e.preventDefault();
      });
    });
  }

});