$(document).ready(function() {

  /*
   *  MAIN MENU
   */

  // each dropdown has its own timeout, named after the parent li class
  var hideTimer = new Array();

  $("#mainmenu > li").hover(
    function () {
      $(this).find("a:first").addClass('active');
      var submenu = $(this).find("ul");

      // 1. find the position, size and margin for the parent li
      var pos = $(this).position();
      var width = $(this).width();
      var height = $(this).height();
      var leftmargin = $(this).css("margin-left").replace('px', '');
      var topMargin = $(this).css("margin-top").replace('px', '');

      // for some reason this needs some browser-specific tweaking
      // alert(jQuery.browser.version);
      if (jQuery.browser.msie) {                                                       // internet explorer
        var x = pos.left + 142;
        var y = pos.top + 7;
      } else if (jQuery.browser.safari) {                                              // safari
        var x = pos.left + 137;
        var y = pos.top + 2;
      } else if (jQuery.browser.mozilla && jQuery.browser.version >= 1.9) {            // firefox 3 onwards
        var x = pos.left + 143;
        var y = pos.top + 7;
      } else {                                                                         // anything else
        var x = pos.left + 138;
        var y = pos.top + parseInt(topMargin);
      }

      // 2. position the popup
      submenu.css('left', x+'px');
      submenu.css('top', y+'px');

      // 3. cancel any pending hide operations and show the menu
      var intervalKey = $(this).attr('class');
      clearInterval(hideTimer[intervalKey]);
      submenu.fadeIn(300);
      submenu.find("ul").css('display', 'none');  // don't show anything past the 2nd level
    },
    function () {
      // insert a short delay before hiding
      $(this).find("a:first").removeClass('active');
      var submenu = $(this).find("ul");
      var intervalKey = $(this).attr('class');
      hideTimer[intervalKey] = window.setTimeout(function() {
        submenu.hide();
      }, 350);
    }
  );



  /*
   *  HOME VIDEO (add here so we can hide it from incompatible browsers)
   */
  if (jQuery.browser.msie) {
    $("#homevideo").css('clear', 'both');
    $("#homevideo").css('text-align', 'center');
    $("#homevideo").css('margin', '1em');
    $("#homevideo").html('<iframe width="450" scrolling="no" height="280" frameborder="0" src="http://clikingo.dayport.com/viewer/viewerpage.php?Art_ID=83&tf=clik450red.tpl"></iframe><br /><span style="font-size:10px;">This video will only play in Internet Explorer</span>');
  }



  /*
   * Fancy box

  $(".imagepopup").fancybox({
    overlayShow: true,
    overlayOpacity: 0.9
  });

  */ 


  /*
   *  FORMS
   */    

  $("#theform input[type=text]").css('width', '240px');
  $("#theform textarea").css('width', '240px');
  $("#theform textarea").css('height', '80px');
  $("#theform input[type=radio]").css('border', 'none');
  $("#theform input[type=checkbox]").css('border', 'none');

  $("a[title='Submit application']").click(function(e) {
    document.theform.submit();
    return false;
  })

});
