function splitSubMenu(columns) {


    jQuery("div.sub").each(function (i) {

        var itemCounts = new Array();
        var items = jQuery(this).find('ul');
        var totalItems = 0;
        var slice;

        var childItems = false;

        for (var i = 0; i < items.length; i += 1) {
            var subItems = jQuery(items[i]).find('li');
            itemCounts[i] = subItems.length;
            totalItems = totalItems + itemCounts[i];
            if (subItems.length != 1) { childItems = true }

        }

        var newColumns = new Array(); var currentCol = 0; var lastSlice = 0; newColumns[0] = 0;
        var maxColLength = 8;

        for (var o = 0; o < itemCounts.length; o += 1) {
            if (Math.round(totalItems / columns) >= (newColumns[currentCol] + itemCounts[o]) ||
                (newColumns[currentCol] == 0) || (currentCol + 1 == columns) ||
                (newColumns[currentCol] + itemCounts[o]) <= maxColLength)
                {
                    newColumns[currentCol] = newColumns[currentCol] + itemCounts[o];
                }
            else {
                slice = items.slice(lastSlice, o);
                slice.wrapAll("<div class=\"menuCol\"></div>");
                lastSlice = o;
                currentCol++
                newColumns[currentCol] = itemCounts[o];
            }


            //special columns
            if (    childItems == false &
                    totalItems <= (maxColLength * 2) &
                    totalItems > maxColLength &
                    (newColumns[currentCol] - 1) == (totalItems / 2) & 
                    currentCol == 0) 
            {
                slice = items.slice(lastSlice, o);
                slice.wrapAll("<div class=\"menuCol\"></div>");
                lastSlice = o;
                currentCol++
                newColumns[currentCol] = itemCounts[o];
            }

            /*last one is special */
            if (o == (itemCounts.length - 1)) {
                slice = items.slice(lastSlice);
                slice.wrapAll("<div class=\"menuCol menuColLast\"></div>");
            }


        }

    });


}

jQuery(document).ready(function () {
	//Calculate height of top level menu and set top style for menu placement
	jQuery('ul.megamenu li .sub').css('top', jQuery('ul.megamenu > li').height());

	// set hover class to parent item
	jQuery('li.level0 div').mouseover(function () {
		jQuery(this).closest('li.level0').find('a.level0').addClass("megahover")
	}).mouseout(function () {
		jQuery(this).closest('li.level0').find('a.level0').removeClass("megahover")
	});

	function megaHoverOver() {
		jQuery(this).find(".sub").stop().fadeTo('fast', 1).show();


		//Calculate width of all ul's
		(function (jQuery) {
			jQuery.fn.calcSubWidth = function () {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function () {
					rowWidth += $(this).width();
				});
			};
		})(jQuery);
        jQuery(this).find(".sub").css({ 'width': jQuery(this).find(".menuCol").length * 218 });


        if ($(document).width() <= (jQuery(this).find(".sub").width() + jQuery(this).find(".sub").offset().left)){
            jQuery(this).find(".sub").css("left", jQuery(this).width() - jQuery(this).find(".sub").width() + "px");
        }

	}

	function megaHoverOut() {
		jQuery(this).find(".sub").stop().fadeTo('fast', 0, function () {
			jQuery(this).hide();
		});
	}


	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: 100, // number = milliseconds delay before onMouseOut    
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	jQuery("ul.megamenu li .sub").css({ 'opacity': '0' });
	jQuery("ul.megamenu li").hoverIntent(config);

});

jQuery(window).load(function () {
    
});




