$(document).ready(function() {
//On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    if($(this).parent().is("ul#hp-explore")) {
      $(this).children("a").css('background-position', 'left -25px');// home page images use sprites
    }
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

}
//On Hover Out
function megaHoverOut(){
  if($(this).parent().is("ul#hp-explore")) {
    $(this).children("a").css('background-position', 'left top'); // home page images use sprites
  }
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
} 
//Set custom configurations
var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};
// home page
$("ul#hp-explore li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#hp-explore li").hoverIntent(config); //Trigger Hover intent with custom configurations
// other pages
$("ul#headernav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#headernav li").hoverIntent(config); //Trigger Hover intent with custom configurations

});
// open and close contact div.
var arrowstr='<img src="/sites/all/themes/ash/images/contact-arrow.png" width="13" height="15"  alt="contact arrow" class="arrow" />';
var arrowup=arrowstr.replace("contact-arrow.png", "contact-arrow-up.png");
$("div#sitemenu a[href='/links']").before('<a href="#" class="contact-link">'+arrowstr+'Contact us</a> | ');
$("a.contact-link").click(function(){
  if($("div#contact").css("display")=="none") {
    $("div#contact").slideDown();
    $("a.contact-link").addClass("open");
    $("a.contact-link").html(arrowup+'Contact us');
  }else{
     $("div#contact").slideUp();
     $("a.contact-link").removeClass("open");
     $("a.contact-link").html(arrowstr+'Contact us');
  }
});
$("#contact a.close-link").click(function() {
  $("div#contact").slideUp();
  $("a.contact-link").removeClass("open");
  $("a.contact-link").html(arrowstr+'Contact us');

});