$(document).ready(function() {   
function addMega(){
$(this).addClass("hovering");
ContentBlockWidth = 220;
ContainerWidth = 900;
PaddingToRemove = 22;
navcontainerHeight = $("#navcontainer ul").height();
ContainerOffset = $("#navcontainer").offset().left;
currentID = $(this).attr("id");
offsetLeft = $("#" + currentID +" a").offset().left
offsetDif = ( (offsetLeft + ContentBlockWidth) - (ContainerWidth + ContainerOffset) );
// $("#debug").html("menu left gap: " + ContainerOffset + "<br/> item left gap:" + offsetLeft + "<br/> Diff: " + offsetDif);
$("ul#headerMenu li.mega div").css({"width": ContentBlockWidth + "px"});
if ( (ContentBlockWidth + offsetLeft) >= (ContainerWidth + ContainerOffset) ) { offsetLeft = (offsetLeft - offsetDif - PaddingToRemove); }
$("ul#headerMenu li.mega div").css({"margin-top": navcontainerHeight + "px", "left": offsetLeft + "px"});
divContents = $("#display_" + currentID).html();
if (divContents == ""){$("#display_" + LoopID).remove();}
}

function removeMega(){
$(this).removeClass("hovering");
}

var megaConfig = {
    interval: 0,
    sensitivity: 4,
    over: addMega,
    timeout: 0,
    out: removeMega
};
$("li.mega").hoverIntent(megaConfig);
});
