$(function(){

  $.path = function(name, fn) {
    if(window.location.pathname.match(name)) fn();
  };

  $('#help_link').facebox({ 
    loadingImage: '/hippo-assets/loading.gif',
    closeImage: '/hippo-assets/closelabel.gif'
  });

  $('.process-slideshow').cycle();

  $.path('/discounts', function(){

    var config = {};
    config.rules = {};

    for(var n = 1; n <= 5; n++){
      var field = 'mailer[email_' + n + ']';
      config.rules[field] = {};
      config.rules[field].required = true;
      config.rules[field].email = true;
    }

    $.validator.setDefaults({ debug: true });
    
    $('#emails_form').validate(config);        
  });

  $.path('/$', function(){
    $('.hippo-wear')
      .click(function(){
        window.location.pathname = '/discounts';
      })
      .mouseover(function(){
        $(this).css('cursor', 'pointer');
      })
      .mouseout(function(){
        $(this).css('cursor', 'auto');
      });
  });

 /* --- tshirt --- */

 $.path('/cart/all_products|/cart/show', function(){
    // Don't move onto the next step unless they've selected a t-shirt
    // or denied their free t-shirt
    $('input[type="submit"]').click(function(){
      if($('.tshirt select').val() != 'Select a size'){
        $(this).unbind();
        $(this).click();
      } else {
        $.facebox('Please select a t-shirt size. If you would like to skip receiving your free t-shirt, please select "No Thanks"');
      }
      return false;
    });
  });

  /* --- preferred delivery date datepicker --- */

  $('#quote_preferred_delivery_date')
    .datepicker({
      dateFormat: 'yy-mm-dd'
    })
    .css('cursor', 'pointer');

  $('#delivery_date_updater #quote_preferred_delivery_date').change(function(){
    $('#quote_form').ajaxSubmit();
    return false;
  });

});
