// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$.noConflict();

jQuery(document).ready(function($) {

  function setSidebarContentHeights() {
    if ($("#sidebar").length > 0 && $("#content").length > 0) {
      var sidebar = $("#sidebar");
      var content = $("#content");
      var sidebar_top_margin = parseInt(sidebar.css('margin-top').replace(/px/, ''));
      if ($.browser.mozilla) {
        var correction = -1
      } else {
        var correction = 0;
      }
      if (sidebar.outerHeight() > content.outerHeight()) {
        var h = sidebar.height() + sidebar_top_margin + correction;
        padding_elem = $(".padding:first", content);
        padding = content.height() - padding_elem.height();
        padding_elem.height(h - padding);
      } else {
        var h = content.height() - sidebar_top_margin + correction;
        padding_elem = $(".padding:first", sidebar);
        padding = sidebar.height() - padding_elem.height();
        padding_elem.height(h - padding);
      }
    }
  }
  
  function setCurrentLinks() {
    // $("#sidebar .links li a[href$='" + location.pathname + "']:first").closest("li").addClass('current');
    $("#sidebar .links li a[href$='" + location.pathname + "']:last").wrap('<div class="current" />');
    $("#header .nav li a[href^='/" + location.pathname.split('/')[1] + "']:first").closest("li").addClass("current");
  }
  
  function setTriBoxHeights() {
    var boxes = $(".additional.columns .padding");
    var heights = [];
    boxes.each(function() {
      heights.push($(this).outerHeight());
    });
    var height = heights.sort(function(a,b) {return b - a}).shift();
    boxes.each(function() {
      $(this).height(height);
    });
  }
  
  setSidebarContentHeights();
  setCurrentLinks();
  setTriBoxHeights();
  
  $(window).resize(function() {
    setSidebarContentHeights();
    setCurrentLinks();
  });
  
  $(".page_actions .print, .legal .print").click(function() {
    window.print();
    return false;
  });
  
  $("a[rel='inline_popup']").colorbox({inline:true, href: $(this).attr('href')});
  $("a[rel='popup']").colorbox();
  
  $("a[rel='external']").click(function() {
    window.open($(this).attr('href'));
    return false;
  });
  
  $("a.button").click(function() {
    $(this).closest('form').submit();
    return false;
  });
  
});