$(function() {
  $('#navDropdown').change(function() {
    var value = $(this).find('option:selected').get(0).value;
    if (value && value != "")
      window.location = 'practice_areas.php#' + value;
  });
  
  // Fix for Gecko 1.9 in FF3 (doesn't seem to hurt other versions) where the centerline of the page is calculated strangely.
  // Causes the "margin: 0 auto" elements within the wrappers to be 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();
  
  
  $('#nav li').hover(
    //function() { $(this).find('img').each(function() { this.src = this.src.replace('.png', '_hl.png'); }); },
    //function() { $(this).find('img').each(function() { this.src = this.src.replace('_hl.png', '.png'); }); }
    function() { $(this).find('a').addClass('hover'); },
    function() { $(this).find('a').removeClass('hover'); }
  );

  // Highlight current page in navigation menu.  (Assumes single-tier site.)
  var currentPage = location.href.substr(location.href.lastIndexOf('/')+1);
  $('ol#nav li a').each(function() {
    if (currentPage == $(this).attr('href'))
      $(this).addClass('active');
  });
  
  
  /* Timeline functionality: */
  var PAGE_WIDTH = 689;
  var SLIDER_INTERVAL = 27;
  var LAST_PAGE = 4;
  var currentPage = 0;
  
  $('#home .hotspot').html('<img src="img/timeline_hotspot.png" alt="" /><div class="caption">' + $('#home .hotspot').html() + '</div>');
  $('#home .hotspot').hover(
    function() {
      $(this).find('.caption').stop().css('opacity', 0).show().fadeTo('normal', 0.95);
    },
    function() {
      $(this).find('.caption').stop().fadeTo('normal', 0, function() { $(this).hide(); });
    }
  );
  
  $('#home #slideshowControls #sliderContainer').slider({
    step: 25, /* Why 25 and not 27 (the actual spacing), I have no idea.  It works, though.  Percentage? */
    slide: function(event, ui) {
      currentPage = Math.floor(Number(ui.value)/25);
      goToPage();
    }
  });


  var goToPage = function() {
    $('#home #timeline').stop().animate({left: -(currentPage * PAGE_WIDTH) + 'px'}, 'slow');
    $('#home #slideshowControls #slider').stop().animate({left: (currentPage * SLIDER_INTERVAL) + 'px'}, 'slow');

    $('#home #slideshowControls')
      .find('#pages div').css('background-color', '')
      .end()
      .find('#page' + currentPage).css('background-color', '#c26f15');
  };
  goToPage();

  $('#home #slideshowControls')
    .find('#pages div').click(function() {
      currentPage = Number(this.id.replace('page', ''));
      goToPage();
    })
    .end()
    .find('#next').click(function() {
      if (currentPage < LAST_PAGE) {
        currentPage++;
        goToPage();
      }
    })
    .end()
    .find('#prev').click(function() {
      if (currentPage > 0) {
        currentPage--;
        goToPage();
      }
    });


  /* Form validation: */
  
  var _validate = function(o, msg, callback) {
    var root = o.id.replace('request_', '');
    var validator = $('#validation_' + root);
    if (callback($(o).val())) {
      $(o).data('invalid', true);
      validator.html(msg.replace('{0}', root));
    }
    else {
      $(o).data('invalid', false);
      validator.empty();
    }
  };

  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 mailbox@domain.tld', function(s) {
      return (!s.match(/.*@.*\..*/));
    });
  };
  var validatePhone = function(o) {
    _validate(this, 'The {0} field is invalid.  Please use xxx-yyy-zzzz', function(s) {
      return (!s.replace(/[()-. ]/g, '').match(/[0-9]{10}/));
    });
  };

  $('#request_name, #request_title, #request_company, #request_address').change(validateNonBlank);
  $('#request_phone').change(validatePhone);
  $('#request_email').change(validateEmail);

  $("#request_form").submit(function() {
    var complete = true;
    $("#request_form input[type!=image]").trigger('change').each(function() {
      if ($(this).data('invalid')) complete = false;
    });
    if (complete) return true;
    return false;
  });
});
