// JavaScript Document

//http://www.sohtanaka.com/web-design/mega-drop-downs-w-css-jquery/


$(document).ready(function () {

    var i = 1;

    $("ul.sub_sub").each(function () {
        $(this).attr("class", "sub_sub" + i);
        i++;
    });

    $('.sub_sub').wrap('<div class="sub"/>'); //this adds the div class around the list items in mega menu
    $('.category_heading').wrap('<div class="cat_column"/>'); //this adds the div class around the list items in mega menu

    $('.sub_sub1').wrap('<div class="sub1"/>'); //this adds the div class around the list items in mega menu
    $('.sub_sub2').wrap('<div class="sub2"/>'); //this adds the div class around the list items in mega menu
    $('.sub_sub3').wrap('<div class="sub3"/>');
    $('.sub_sub4').wrap('<div class="sub4"/>');
    $('.sub_sub5').wrap('<div class="sub5"/>');
    $('.sub_sub6').wrap('<div class="sub6"/>');
    $('.sub_sub7').wrap('<div class="sub7"/>');

    $('#topnav').css('overflow', 'visible');
    //$('.sub').css('width', 'auto');
    function megaHoverOver() {
        $(this).find(".sub1").stop().fadeTo('fast', 1).show();
        $(this).find(".sub2").stop().fadeTo('fast', 1).show();
        $(this).find(".sub3").stop().fadeTo('fast', 1).show();
        $(this).find(".sub4").stop().fadeTo('fast', 1).show();
        $(this).find(".sub5").stop().fadeTo('fast', 1).show();
        $(this).find(".sub6").stop().fadeTo('fast', 1).show();
        $(this).find(".sub7").stop().fadeTo('fast', 1).show();
//Sub 1 Start//
        //Calculate width of all ul's
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find(".cat_column").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);

        if ($(this).find(".sub_sub1").length > 0) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".sub_sub1").each(function () {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub1").css({ 'width': biggestRow });
            $(this).find(".sub_sub1:last").css({ 'margin': '0' });

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub1").css({ 'width': rowWidth });

        }
    }

    function megaHoverOut() {
        $(this).find(".sub1").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub2").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub3").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub4").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub5").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub6").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
        $(this).find(".sub7").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }
    //Sub 1 End//

   

    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 10, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 10, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    // $('.sub').attr('style', ''); //Used to removed the inline style attached to the .sub diiv

    $("ul#topnav li.mmTrigger ul.sub1").css({ 'opacity': '0' });
    $("ul#topnav li.mmTrigger").hoverIntent(config);

    $("ul#topnav li.mmTriggerSelected ul.sub1").css({ 'opacity': '0' });
    $("ul#topnav li.mmTriggerSelected").hoverIntent(config);

    $(".cat_column:nth-child(6)").css({ "clear": "both" }); //this clears the inner column within the megamenu

    //Nasty IE8 hack!
    $('#topnav li.category_heading ul li a').mouseenter(function () { $(this).addClass('ahover') });
    $('#topnav li.category_heading ul li a').mouseleave(function () { $(this).removeClass('ahover') });
});
