
$(function () 
{
  var inputFields = $('input.store_locator_search');

  /**
   * Attach events to label and input fields to handle user's changes
   * and update states of labels according to corresponding input field.
   */
  function setupInputFields() {
    for (var i = 0, length = inputFields.length; i < length; i++) {
      var label = $(inputFields[i]).prev('label');
 	  
      if (inputFields[i].id === document.activeElement.id || inputFields[i].value !== '') {
        label.hide();
      }

      $(inputFields[i]).focus(function (event) {
        showEmptyFieldLabel();
        $(event.target).prev('label').hide();
      });
      
      $(label).click(function (event) {
    	  $(this).next("input").focus();
      });
    }
  }
  
  
  /**
   * Show text input field label if user did not enter any text.
   */
  function showEmptyFieldLabel() {
    for (var i = 0, length = inputFields.length; i < length; i++) {
      if (inputFields[i].value === '' && inputFields[i].id !== document.activeElement.id) {
        $(inputFields[i]).prev('label').show();
      }
    }
  }  

  
  // Search field events
  var searchForm = $('.store_locator_form');
  var searchField = $('.store_locator_search', searchForm);
  var clearSearch = $('img.clear_search', searchForm);
  
  if (searchField.attr('value') === '') {
    clearSearch.css('display', 'none');
  }
  
  $('img.do_search', searchForm).click(function () { searchForm[0].submit(); });
  
  clearSearch.click(function () { 
    searchField.attr('value', '');
    clearSearch.css('display', 'none');
    searchField.prev('label').show();
  });
  
  searchField.focus(function () {
    clearSearch.css('display', 'block');
  });
  
  searchField.blur(function (event) {
    if (searchField.attr('value') === '') {
      clearSearch.css('display', 'none');
      searchField.prev('label').show();
    }
  });
  
  // Contact Us TFN HACK
  $('#contactUsForm .cont_info .section_end:contains("Business Sales")').html('Business Sales:<br>1.866.306.2333');
  $('#contactUsForm .cont_info .section_end:contains("Ventes aux entreprises")').html('Ventes aux entreprises : <br>1.866.907.3038');


  
});
