$(function() {
  // nav
  
  $('#secondary-nav a').click(function(e) {
    if ($(this).parent().children('ul').length > 0) {
      e.preventDefault();
      
      if ($(this).parent().hasClass('open')) {
        $(this).parent().removeClass('open').children('ul').slideUp('fast');  
      } else {
        $(this).parent().parent().children('li.open').removeClass('open').children('ul').slideUp('fast');
        $(this).parent().addClass('open').children('ul').slideDown('fast'); 
      }
    }
  });
  
  // open nav if in a sub section
  var loc = location.pathname;
  var nav = $('#secondary-nav a[href="' + loc + '"]');
  var p1 = nav.parent().parent();
  if ((p1[0] != undefined) && (p1[0].tagName == 'UL')) {
    p1.addClass('open').show();
    p1.parent().addClass('open');
  }
  
  var p2 = p1.parent().parent();
  if ((p2[0] != undefined) && (p2[0].tagName == 'UL')) {
    p2.addClass('open').show();
    p2.parent().addClass('open');
  }
  

  $('#help-business').click(function (e) {
    e.preventDefault();
    
    checkOverlaySize(); 
    
    $(window).resize(function() {
      checkOverlaySize(); 
    });
    
    if (($(window).scrollTop() + 25 + $('#contact-form').height()) > $('body').height()) {
      $('#contact-form').css('top', $('body').height());
      $('#contact-form').css('top', $('body').height() - $('#contact-form').height() + 'px');
    } else {
      $('#contact-form').css('top', $(window).scrollTop() + 25 + 'px');
    }
    
    
    $('#overlay').show();
    $('#contact-form').fadeIn();
    
    //$.scrollTo(200, 200);
    $('#full_name').focus();
  });
  
  $('#overlay, #close_contact').click(function(e) {
    e.preventDefault();
    
    $('#overlay').hide();
    $('#contact-form').hide();
    
    $(window).unbind('resize');
  });
  
  $(document).keydown(function(e) {
    if (e.keyCode == 27)  {
      $('#overlay').fadeOut('fast');
      $('#contact-form').hide();
    }
  });
  
  // validate
  $('#contact-form #submit').click(function (e) {
    var ok = true;
    
    var req_fields = ['verification', 'full_name', 'company', 'city', 'province', 'phone', 'email', 'comment'];
    
    $.each(req_fields, function(i,o) {
      var f = $('#' + o);
      if (f.val().length == 0) {
        ok = false;
        f.addClass('error');
      } else {
        f.removeClass('error');
      }
    });
    
    if (!ok) {
      e.preventDefault();
      $('#contact-form p.message').addClass('error').text('Please complete the highlighted fields.');
    }
    
  });
  
  $('form.validate').submit(function(e) {
    var ok = true;
    
    var req_fields = ['verification', 'full_name', 'company', 'city', 'province', 'phone', 'email'];
    
    $.each(req_fields, function(i,o) {
      var f = $('#' + o);
      if (f.val() == '') {
        ok = false;
        f.addClass('error');
      } else {
        f.removeClass('error');
      }
    });
    
    if (!ok) {
      e.preventDefault();
      $('p.message').addClass('error').text('Please complete the highlighted fields.');
      $('input.error')[0].focus();
    }
  });
  
  if (window.location.href.search('success')) {
    $.scrollTo(200);
  }
});

checkOverlaySize = function()
{
  if ($(window).width() > $(document).width()) {
    $('#overlay').width($(window).width());
  } else {
    $('#overlay').width($(document).width());
  }
  
  if ($(window).height() > $(document).height()) {
    $('#overlay').height($(window).height());
  } else {
    $('#overlay').height($(document).height());
  }
}
