$(document).ready(function() {
  
  // Add mozilla class to html element where applicable
  if ($.browser.mozilla) {
    $('html').addClass('mozilla');
  }
  
  // Center submenus on main navigation
  $('nav > ol > li').each(function() {
    var $this = $(this),
        $child = $this.children('ol'),
        parentWidth = $this.width(),
        childWidth = $child.width(),
        offset = (childWidth - parentWidth) / 2;
    
    $child.css('left', -offset + 'px');
  });
  
  // Make all footer items same height
  var footerHeight = 0;
  $('footer > ul > li').each(function() {
    var $this = $(this),
        height = $this.height();
    
    if (height > footerHeight) footerHeight = height;
  }).css('min-height', footerHeight + 'px');
  
  // Stay Up-To-Date pulldown
  $('.subscribe a').click(function(e) {
    e.preventDefault();
    $('.subscribe form').css('max-height', '200px');
  });
  $('body').click(function(e) {
    if ($(e.target).closest('.subscribe').length == 0) {
      $('.subscribe form').css('max-height', '0');
    }
  });
  
  // Service page slider
  var $slider;
  if (($slider = $('.page-services .slider, .page-service .slider')).length > 0) {
    $slider.advancedSlider({
      height: $slider.height(),
      shadow: false,
      width: $slider.width(),
      xmlSource: $slider.data('source')
    });
  }
  
  // Service page image lightboxes
  $('.page-service .service a').fancybox({
    padding: 6
  });
  
  // Service page inquire lightbox
  $('.page-service .inquire-cta a').fancybox({
    onComplete: function() {
      Recaptcha.create($('#recaptcha').data('public-key'), 'recaptcha');
      
      var $form = $('.form-contact');
      
      $form.validationEngine({
        scroll: false,
        onValidationComplete: function(form, success) {
          if (success) {
            $.fancybox.showActivity();
            
            $.post($form.attr('action'), $form.serialize(), function(data) {
              $.fancybox(data, {
                onComplete: function() {
                  $('.thanks-contact a').click(function(e) {
                    e.preventDefault();
                    $('.page-service .inquire-cta a').click();
                  });
                }
              });
            });
          }
        }
      });
    },
    onClosed: function() {
      $('body').validationEngine('hideAll');
    }
  });
  
  // Fix spacing between service slider and upgrades list
  (function() {
    var $main = $('.page-service .main');
    if ($main.length == 1) {
      var $slider = $('.page-service .slider'),
          $titleHeight = $('.page-service .page-title').outerHeight();
      
      $main.css('margin-top', ($slider.height() + parseInt($slider.css('top')) - $titleHeight + 25 + 12) + 'px')
    }
  })();
  
  function show_or_hide(elem) {
    if ($(elem).attr('state') == 'open') {
      $('div.shadow', elem).slideUp('slow', function() {
 				$('div.title', this).addClass('bl');
 				$('div:eq(0)', this).addClass('br');
 				$('div.title h3', this).removeClass('border-bottom');
 				$(elem).attr('state', 'closed');
 			});
    } else {
     $('div.title', elem).removeClass('bl');
     $('div:eq(0)', elem).removeClass('br');
     $('div.title h3', elem).addClass('border-bottom');
     $('div.shadow', elem).slideDown('slow');
     $(elem).attr('state', 'open');
    }
  }
  
  $('div.manufacturer-popup').click(function(event) {
  		if ($(event.target).is('a')) {
  			window.location = event.target.href;
  		} else {
  			show_or_hide(this);
  		}
  		return false;
  	});

  $('div.manufacturer-popup').each(function() {
    $(this).attr('state', 'closed').animate({opacity: 1.0}, 3000, function() {
      if ($(this).attr('state') == 'closed') {
        $(this).click().animate({opacity: 1.0}, 11000, function() {
          if ($(this).attr('state') == 'open') $(this).click();
         });
       }
    });
  });

});
