$(document).ready(function() {
  sidebar.init();
  windowLinks();
});

// Ensures window links work on both mouse clicks and keypresses
function windowLinks() {
  // Pick up both clicks and keypresses on the links. If both fail
  // then the site will fall back on the normal href functionality
  $("a.window_link").click(function(event) {
    window.open(this.href,'County','resizable=yes,location=no,menubar=yes,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=500,height=500,left=50,top=50,status');
    return false;
  });

  $("a.window_link").keypress(function(event) {
    window.open(this.href,'County','resizable=yes,location=no,menubar=yes,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,width=500,height=500,left=50,top=50,status');
    return false;
  });
}

var sidebar = new function() {
  // Set up your sidebar links here. Since the site is not only relatively linked, but
  // also available at several urls, these will be generated on the fly based on the page's
  // location in the site.
  //
  // URLs placed here should be absolute
  var links = [
    { url: "./", label: "Nutrition and WIC Home" },
    { url: "./families", label: "Information for Families" },
    { url: "./local_agencies", label: "Information for WIC Local Agencies" },
    { url: "./vendors", label: "Information for WIC Vendors" },
    { url: "./WIC_approved_foods", label: "WIC Approved Food" },
    { url: "./nutrition_education", label: "Nutrition Education" },
    { url: "./breastfeeding", label: "Breastfeeding" },
    { url: "./kansas_WIC", label: "More About Kansas WIC" },
    { url: "./Contact_State_WIC_Staff.html", label: "Contact State WIC Staff" }
  ];

  // The search HTML. It needs to be loaded into the sidebar, but loading straight
  // from an html file in an ajaxy way no longer makes sense given our link changes.
  var searchHtml = 
  '<div align="center" id="searchbox"><br>'+
    '<form name="googlesearch" method="get" action="http://kansas.google.nicusa.com/search">'+
      '<label for="search"><span class="font10pt"><b>KDHE Search:</b></span><br>'+
        '<img src="http://www.kansas.gov/images/spacer.gif" alt="Enter search keyword here" width="1" height="1" border="0" />'+
      '</label>'+
      '<input type="text" name="q" size="9" maxlength="70" id="search" value=" " />'+
      '<input type="submit" name="Submit" value="Search" class="search_font" />'+
      '<input type="hidden" name="site" value="kdhe" />'+
      '<input type="hidden" name="client" value="kdhe" />'+
      '<input type="hidden" name="proxystylesheet" value="kdhe" />'+
      '<input type="hidden" name="output" value="xml_no_dtd" />'+
    '</form>'+
  '</div>';

  // The number of parent directories
  var parentCount = null;

  // Figures out where the root directory is based on the loading
  // of this javascript file.
  this.getParentCount = function() {
    if (parentCount != null) { return parentCount; }

    // Find the scripts in the head & check the for load.js. Selectors can't do 
    // that because the path may differ slightly.
    jQuery.each($("head script"), function(index, elem) {
      var source = $(elem).attr("src");
      if (source != null && source.match("load.js") != null) {
        matches = source.match(/\.\./g)

        if (matches != null) {
          parentCount = matches.length;
        } else {
          parentCount = 0;
        }

        return parentCount;
      }
    });
  }

  // Pass in an integer, get a directory path going back that many
  // directories. 1 == .. ; 2 == ../.. ; etc.
  this.relativePrefix = function(count) { 
    var relative = '';
    for ( i = 0 ; i < count ; i++ ) {
      relative += '../' 
    }
    return relative;
  }

  this.init = function() {
    // Initialize the parentCount variable
    sidebar.getParentCount();

    var sidemenu = $("<div>").attr("id", "sidemenu");
    jQuery.each(links, function(index, link) {
      var p = $("<p>");
      p.append($("<a>").attr("href", sidebar.relativePrefix(parentCount) + link.url).append(link.label));
      sidemenu.append(p);
    });

    $("#sidebar").append($("<div>").attr("class", "menu").append(searchHtml).append(sidemenu));
  }
}
