$(function() {
  // Fix for Gecko 1.9 in FF3 (doesn't seem to hurt other versions) where the centerline of the page is calculated strangely.
  // Otherwise, "margin: 0 auto" centered elements get shifted right by 1px for odd-width windows.
  var ffRoundingFix = function() {
    if (window.outerWidth && window.outerWidth % 2 == 0) {
      javascript:void(window.outerWidth-=1);
    }
  };
  if (navigator.product == 'Gecko') {
    window.onresize = ffRoundingFix;
  }
  ffRoundingFix();
  
  
  // Contact form validation:
  var _validate = function(o, msg, callback) {
    var root = o.id.replace('request_', '');
    var validator = $('#validation_' + root);
    if (callback($(o).val())) {
      $(o).data('valid', false);
      validator.html(msg.replace('{0}', root));
    }
    else {
      $(o).data('valid', true);
      validator.html('&nbsp;');
    }
  };
  
  var validateNonBlank = function(o) {
    _validate(this, 'The {0} field is required.', function(s) {
      return (s == '');
    });
  };
  var validateEmail = function(o) {
    _validate(this, 'The {0} field is invalid.  Please use the format: mailbox@domain.tld', function(s) {
      return (!s.match(/.*@.*\..*/));
    });
  };
  
  $('#request_name, #request_city').change(validateNonBlank);
  $('#request_email').change(validateEmail);
  
  $("#request_form").submit(function() {
    var complete = true;
    $("#request_form input[type!=image]").trigger('change').each(function() {
      if (!$(this).data('valid')) complete = false;
    });
    if (complete) return true;
    return false;
  });
  
  $('#request_name').each(function() { this.focus(); });
  
  
  // Shop scroller:
  var PAGE_WIDTH = 980;
  $('#shop .pager a').click(function() {
    var $this = $(this);
    $('#shop .pager a').removeClass('active');
    $this.addClass('active');
    this.blur();
    
    var page = $this.attr('href').match(/\d/g)[0];
    if (page) {
      var idx = Number(page)-1;
      $('#shop .scroller').stop().animate({
        left: -(idx * PAGE_WIDTH) + 'px'
      }, 'slow');
    }

    return false;
  });
});
