$(function(){ 
  $(".sync").syncHeight();
  
  $("ul.sf-menu").supersubs({ 
    minWidth: 10,
    maxWidth: 50,
    extraWidth: 3
  }).superfish();

  
  var NAV_ARROW = $('#nav .overlay .arrow');
  var NAV_MENU_INDENT = $('ul.sf-menu').position().left;
  var DEFAULT_MENU_ITEM = $('ul.sf-menu > li.current');
  
  var getMenuItemPosition = function(el) {
    el = $(el);
    if (el.length < 1) return -100; // Hide arrow if no element
    
    var left = Math.round(el.position().left);
    var width = Math.round(el.width() / 2);
    // IMPORTANT: Requires padding on .sf-menu > li > a to be in pixels!
    var leftPadding = el.children('a').css('padding-left').replace("px", "");
    return(left + width + NAV_MENU_INDENT - leftPadding / 2);
  }
  
  var animateNavMenuArrow = function(el) {
    NAV_ARROW.stop().animate({ left: getMenuItemPosition(el) })
  }
  
  $('ul.sf-menu > li').hover(
    function() { animateNavMenuArrow(this); },
    function() { animateNavMenuArrow(DEFAULT_MENU_ITEM); }
  );
  animateNavMenuArrow(DEFAULT_MENU_ITEM);
  
});

// Promo area:  Bind to window.onload instead of document.ready so all images are preloaded:
$(window).bind('load', function() {
  var changeTo = function(idx) {
    $('#promo > .item:not(:eq(' + idx + '))').fadeOut('normal');
    $('#promo > .item:eq(' + idx + ')').fadeIn('normal');
    $('#promo .icons img').removeClass('active').filter(':eq(' + idx + ')').addClass('active');
  };

  var timerHandle;
  var cycle = function(time) {
    var CYCLE_DEFAULT = 8000;
    if (time == undefined) time = CYCLE_DEFAULT;
    clearTimeout(timerHandle);
    timerHandle = setTimeout(function() {
      var idx = $('#promo .icons img').index($('#promo .icons img.active'));
      var max = $('#promo .icons').children().length-1;
      idx == max ? changeTo(0) : changeTo(idx+1);
      cycle(CYCLE_DEFAULT);
    }, time);
  };
  
  $('#promo .icons img').click(function() {
      changeTo($('#promo .icons img').index(this));
      clearTimeout(timerHandle);
      cycle(15000); // 15 second timeout after click before cycling again.
  });

  var maxheight = 0;
  $('#promo .item').each(function() {
    var height = $(this).height();
    if (height > maxheight) maxheight = height;
  })
  $('#promo').css('height', maxheight);
  
  changeTo(0);
  cycle();
});


/* 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;
});
