$$.extend({

  ValidateZipCode : function()
  {
    return this.keyup(
      function($e) {
        if (this.value.search(/^\d{3,5}$/) == -1) {
          $(this).addClass('TirebarnStoreLocatorZipCodeIncorrect');
          $(this).removeClass('TirebarnStoreLocatorZipCodeCorrect');
        } else {
          $(this).removeClass('TirebarnStoreLocatorZipCodeIncorrect');
          $(this).addClass('TirebarnStoreLocatorZipCodeCorrect');
        }
      }
    );
  },

  ValidateZipCodeOnSubmit : function($e)
  {
     if (this.val().search(/^\d{3,5}$/) == -1) {
       alert('Please enter a valid five digit zip code.');

       $(this).addClass('TirebarnStoreLocatorZipCodeIncorrect');
       $(this).removeClass('TirebarnStoreLocatorZipCodeCorrect');
       $e.preventDefault();
       return false;
     }

     $(this).removeClass('TirebarnStoreLocatorZipCodeCorrect');
     $(this).removeClass('TirebarnStoreLocatorZipCodeIncorrect');

     return true;
  },

  Tirebarn : {
    Ready : function()
    {
      $('div#TirebarnToolbarInner ul li')
        .mousedown(
          function() {
            $(this).addClass('TirebarnToolbarOn');
          }
        )
        .mouseup(
          function() {
            $(this).removeClass('TirebarnToolbarOn');
          }
        )
        .hover(
          function() {
            $(this).addClass('TirebarnToolbarOver');
          },
          function() {
            $(this).removeClass('TirebarnToolbarOver');
          }
        );

      $('div#TirebarnToolbarInner ul li a').each(
        function() {
          var $path = $(this).attr('href');
          var $file = $path.split('/').reverse();
          
          $file.pop();
          $file = $file.pop();

          var $item = $file.split('.').shift();

          if (location.pathname == $path || location.pathname.indexOf('/' + $item + '/') != -1) {
            $(this).parents('li').addClass('TirebarnToolbarHere');
          }
        }
      );
      
/*
      if (location.pathname.indexOf('/Tires+101/') != -1 || location.pathname.indexOf('/Tires 101/') != -1) {
        $('li#TirebarnToolbarTires101').addClass('TirebarnToolbarHere');
      }
*/

      $('input#TirebarnStoreLocatorZipCode')
        .focus(
          function() {
            if (this.value == 'Enter Zip Code') {
              this.value = '';
              $(this).removeClass('TirebarnStoreLocatorZipCodeDefault');
            }
          }
        )
        .blur(
          function() {
            if (!this.value) {
              this.value = 'Enter Zip Code';
              $(this).addClass('TirebarnStoreLocatorZipCodeDefault');
              $(this).removeClass('TirebarnStoreLocatorZipCodeIncorrect');
              $(this).removeClass('TirebarnStoreLocatorZipCodeCorrect');
            }
          }
        )
        .ValidateZipCode();

      $('input#TirebarnStoreLocatorButton').click(
        function($e) {
          $('input#TirebarnStoreLocatorZipCode').ValidateZipCodeOnSubmit($e);
        }
      );

      var $locationFile = decodeURIComponent(location.href.split('/').pop());

      $('ul#TirebarnSubNavigation a').each(
        function() {
          var $file = decodeURIComponent(this.href.split('/').pop());

          if ($file == $locationFile) {
            $(this).parents('li').addClass('TirebarnSubNavigationMarker');
          }
        }
      );

      $('select#TirebarnAutomobileYear').change(
        function() {
          if (this.value) {
            $$.Tirebarn.GetAutomobileMakes(this.value); 
          }
        }
      );

      $('select#TirebarnAutomobileMake').change(
        function() {
          if (this.value) {
            $$.Tirebarn.GetAutomobileModels(
              $('select#TirebarnAutomobileYear').val(),
              this.value
            );
          }
        }
      );

      $('select#TirebarnAutomobileModel').change(
        function() {
          if (this.value) {
            $$.Tirebarn.GetAutomobileOptions(
              $('select#TirebarnAutomobileYear').val(),
              $('select#TirebarnAutomobileMake').val(),
              this.value
            );
          }
        }
      );
    },

    GetAutomobileMakes : function($year)
    {
      $('select#TirebarnAutomobileMake option:not(:first-child)').remove();
      $('select#TirebarnAutomobileMake').attr('disabled', 'disabled');

      $('select#TirebarnAutomobileModel option:not(:first-child)').remove();
      $('select#TirebarnAutomobileModel').attr('disabled', 'disabled');

      $('select#TirebarnAutomobileOption option:not(:first-child)').remove();
      $('select#TirebarnAutomobileOption').attr('disabled', 'disabled');

      $.getJSON(
        $$.Path(
          '/plugins/Tirebarn/TirebarnTires/getAutomobileMakes', {
            TireAutomobileYear: $year
          }
        ),
        function(json) {
          $(json).each(
            function(key, value) {
              $('select#TirebarnAutomobileMake').append(
                "<option value='" + value + "'>" + value + "</option>"
              ).removeAttr('disabled');
            }
          );
        }
      );
    },

    GetAutomobileModels : function($year, $make)
    {
      $('select#TirebarnAutomobileModel option:not(:first-child)').remove();
      $('select#TirebarnAutomobileModel').attr('disabled', 'disabled');

      $('select#TirebarnAutomobileOption option:not(:first-child)').remove();
      $('select#TirebarnAutomobileOption').attr('disabled', 'disabled');

      $.getJSON(
        $$.Path(
          '/plugins/Tirebarn/TirebarnTires/getAutomobileModels', {
            TireAutomobileYear: $year,
            TireAutomobileMake: $make
          }
        ),
        function(json) {
          $('select#TirebarnAutomobileModel option:not(:first-child)').remove();

          $(json).each(
            function(key, value) {
              $('select#TirebarnAutomobileModel').append(
                "<option value='" + value + "'>" + value + "</option>"
              ).removeAttr('disabled');
            }
          );
        }
      );
    },

    GetAutomobileOptions : function($year, $make, $model)
    {
      $('select#TirebarnAutomobileOption option:not(:first-child)').remove();
      $('select#TirebarnAutomobileOption').attr('disabled', 'disabled');

      $.getJSON(
        $$.Path(
          '/plugins/Tirebarn/TirebarnTires/getAutomobileOptions', {
            TireAutomobileYear: $year,
            TireAutomobileMake: $make,
            TireAutomobileModel: $model
          }
        ),
        function(json) {
          $('select#TirebarnAutomobileOption option:not(:first-child)').remove();

          $(json).each(
            function(key, value) {
              $('select#TirebarnAutomobileOption').append(
                "<option value='" + value + "'>" + value + "</option>"
              ).removeAttr('disabled');
            }
          );
        }
      );
    }
  }
});

$(document).ready(
  function() {
    $$.Tirebarn.Ready();
  }
);