$(document).ready(function() {   
function addMega(){
$(this).addClass("hovering");
iColumnsPerRow = 3;
ContentBlockWidth = 226;
ContainerWidth = 920;
PaddingToRemove = 10;
LeftIndent = 0;
totalContentBlocks = $("#" + this.id + " div dl").size();
/* for (var i = 1; i < totalContentBlocks; i++) { if (i % iColumnsPerRow == 0) $("#" + this.id + " div dl:eq(" + i + ")").after('<br clear="all" />');} */
if (totalContentBlocks >= iColumnsPerRow) totalContentBlocks = iColumnsPerRow;
for (var i = 1; i < totalContentBlocks; i++) {
ContentBlockWidth = ContentBlockWidth + 226;
}
navcontainerHeight = $("#navcontainer2 ul").height();
ContainerOffset = $("#navcontainer2").offset().left;
currentID = $(this).attr("id");
offsetLeft = $("#" + currentID +" a").offset().left + LeftIndent;
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: 75,
    sensitivity: 4,
    over: addMega,
    timeout: 0,
    out: removeMega
};
$("li.mega").hoverIntent(megaConfig);
});
