$(function() {
  $('body').addClass('js-enabled');
  var anchor = document.location.hash;
  
  
  /* Email Replacement */
  $("span.mailto").each(function(){
    exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
    match = exp.exec($(this).text());
    addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
    emaillink = match[2] ? match[2] : addr;
    subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
    $(this).after('<a href="mailto:'+addr+subject+'">'+ emaillink + '</a>');
    $(this).remove();
  });
  
  
  /* Satchmo Search Form */
  var satchmo_search = $('#satchmo_search_form input')
  var initial_search_value = 'search'
  
  satchmo_search.focus(function() {
    if ($(this).val() == initial_search_value) {
      $(this).val('');
    }
  });
  
  satchmo_search.blur(function() {
    if (satchmo_search.val() == '') {
      satchmo_search.val(initial_search_value);
    }
  });
  satchmo_search.blur();
  
  
  /* Hide product search when products drop-down is displayed */
  $('ul.nav > li').hover(function(event) {
    if ($(this).find('ul').length) {
      $('#satchmo_search_form').hide();
    }
  }, function(event) {
    $('#satchmo_search_form').show();
  });
  
  
  /* Store Locations */
  $('#page-locations .page-menu a').click(function(event) {
    var vcard_id = $(this).attr('href');
    $('#page-locations li.vcard').removeClass('active');
    $('#page-locations .page-menu a').removeClass('active');
    $(this).addClass('active');
    $(vcard_id).addClass('active');
    event.preventDefault();
  });
  if (anchor) {
    $('#page-locations .page-menu a[href="'+ anchor +'"]').click();
  }
  else {
    $('#page-locations .page-menu a').first().click();
  }
  
});

