(function($) {

  function strip_leading_slash(path) { return path.substring(1); };

  function root_of_path(path) { return path.split('/')[0]; };

  var current_location  = strip_leading_slash(location.pathname);

  var mapping = {
    'home': ['thank-you'],
    'about': [
              'about/club',
              'about/polo-school',
              'about/training-membership',
              'about/facility',
              'about/directions',
              'about/contact'
              ],
    'club': ['about/club'],
    'polo-school': ['about/polo-school'],
    'training-membership': ['about/training-membership'],
    'facility': ['about/facility'],
    'directions': ['about/directions'],
    'contact': ['about/contact'],
    'sponsors': ['sponsors'],
    'gallery': ['gallery'],
    'events': [/tournaments\/\d\/schedule/, 'events', 'tournaments', 'scores'],
    'specials': ['events'],
    'tournaments': [/tournaments\/\d\/schedule/, 'tournaments'],
    'scores': ['scores']
  };

  var navigation_elements = $('ul.navigation a');

  navigation_elements.each(function(){
    var $this = $(this);
    var key   = $this.attr('rel');
    if (key && mapping[key]) {
      var first_mapping = mapping[key][0];
      if (typeof first_mapping == 'function' || typeof first_mapping == 'object') {
        if (current_location.match(first_mapping)) {
          $this.addClass('selected');
        };
      };
      if (_.include(mapping[key], current_location)) {
        $this.addClass('selected');
      };
    };
  });

  if (current_location.length == 0) {
    $("a[rel='home']").addClass('selected');
  };

})(jQuery);
