function dropUpOver() {
  $(this).children('.dropUpMenu').stop(true,true).show('slide', { direction: "down" }, 500);
}

function dropUpOut() {
  $(this).children('.dropUpMenu').stop(true, true).hide('slide', { direction: "down" }, 500);
}

function subMenuOver() {
  $(this).addClass('active');
  $(this).children('.subMenu').stop(false, true).show('slide', { direction: "up" }, 500);
}

function subMenuOut() {
  $(this).removeClass('active');
  $(this).children('.subMenu').stop(false, true).hide('slide', { direction: "up" }, 500);
}

function shareOver() {
  $(this).children('#shareMenu').stop(true,true).show('slide', { direction: "up" }, 500);
}

function shareOut() {
  $(this).children('#shareMenu').stop(true, true).hide('slide', { direction: "up" }, 500);
}

function contactOver() {
  $(this).children('#contactForm').stop(true,true).show('slide', { direction: "up" }, 500);
  $('#contact').addClass('active');
}

function contactOut() {
  $(this).children('#contactForm').stop(true, true).hide('slide', { direction: "up" }, 500);
  $('#contact').removeClass('active');
  $('#sendMessage').html('');
}

function contactSubmit() {
  var valid = true;
  var name = $.trim($('#name').val());
  var email = $.trim($('#email').val());
  var message = $.trim($('#message').val());
  $('#contactForm .required').css('visibility','hidden');

  if(name == null || name == '' || name == 'Your Name') {
    $('#name + .required').css('visibility','visible');
    valid = false;
  }
  if(email == null || email == '' || email == 'Your Email') {
    $('#email + .required').css('visibility','visible');
    valid = false;
  }
  if(message == null || message == '' || message == 'Type a message') {
    $('#message + .required').css('visibility','visible');
    valid = false;
  }
  if(!valid) {
    return;
  }

  var data = {
    name: name,
    email: email,
    message: message
  };
  
  $('#sendMessage').html("Sending...");
  $.post('/submitContact.ashx', data, function(data){
    if(data == "success"){
      $('#sendMessage').html("Your information has been submitted.");
      $('#name').val('');
      $('#email').val('');
      $('#message').val('');
    }
    else {
      $('#sendMessage').html("Error submitting request.");
    }
  }, 'text');
}

function openShare(url,width,height) {
  var shareUrl = window.location.toString().replace('/#','/').replace('#','/').replace('|','/');
  shareUrl = shareUrl + ((shareUrl.indexOf('?') > -1) ? '&' : '?') + 't=' + new Date().valueOf().toString();
  shareUrl = encodeURIComponent(shareUrl);
  url = url.replace('{0}', shareUrl);
  var win=window.open(url,'','menubar=no,toolbar=no,width='+width+',height='+height+',resizable=no,scrollbars=yes');
  if(win != null) {
    return false;
  }
}

function wireContactForm() {
  $('#name').focus(function(){
    if($(this).val() == 'Your Name') {
      $(this).val('');
    }
  });

  $('#name').blur(function(){
    if($(this).val() == '') {
      $(this).val('Your Name');
    }
  });

  $('#email').focus(function(){
    if($(this).val() == 'Your Email') {
      $(this).val('');
    }
  });

  $('#email').blur(function(){
    if($(this).val() == '') {
      $(this).val('Your Email');
    }
  });

  $('#message').focus(function(){
    if($(this).val() == 'Type a message') {
      $(this).val('');
    }
  });

  $('#message').blur(function(){
    if($(this).val() == '') {
      $(this).val('Type a message');
    }
  });

  $('#send').hover(function(){
                     $('#send').addClass('on').removeClass('off');
                   },
                   function(){
                     $('#send').addClass('off').removeClass('on');
                   });

  $('#send').click(contactSubmit);
}

function resizeBackground() {
  var bg = $('#background');
  var pageWidth = $('#mainPage').outerWidth(true);
  var pageHeight = $('#mainPage').outerHeight(true);

  var aspect = bg.width() / bg.height();
  var width = aspect * pageHeight ;
  var height = pageWidth / aspect;

  if (width < pageWidth)
  {
    width = height * aspect;
  }
  if (height < pageHeight)
  {
    height = width / aspect;
  }

  $('#background').width(width);
  $('#background').height(height);
  $('#background').css('top', (pageHeight - height));
  $('#background').css('left', (pageWidth - width));
}

$(function(){
  $(window).resize(function(){
      resizeBackground();
  });

  /* wire up the drop-up menus in the footer */
  var hoverConfig = {    
     over: dropUpOver,
     timeout: 300,
     out: dropUpOut
  };
  $('.dropUp').hoverIntent( hoverConfig );

  var hoverConfig = {    
     over: subMenuOver,
     timeout: 300,
     out: subMenuOut
  };
  $('.hasSubMenu').hoverIntent( hoverConfig );

  /* wire up the share menu in the header*/
  hoverConfig = {    
     over: shareOver,
     timeout: 300,
     out: shareOut
  };
  $('#share').hoverIntent( hoverConfig );

  /* wire up the contact form in the header*/
  hoverConfig = {    
     over: contactOver,
     timeout: 500,
     out: contactOut
  };
  $('#contact').hoverIntent( hoverConfig );

  wireContactForm();
  
  resizeBackground();
});
