/*<![CDATA[*/
function setsubmenus() { }
/*]]>*/

var totalWidth = 1; // IE9 afrondings fix

$(function () {

    $('.menu>ul>li').hover(function () {

//        $('.submenu').stop(true, true);
//        $('.submenu').queue(function () {
//            setTimeout(function () {
//                $('.submenu').dequeue();
//            }, 100);
//        });


        $('.submenu').html("");
        $('.submenu>ul').css("width", "0px");

        if($(this).children("ul").length == 0){
            $('.submenu').css("visibility", "hidden");
            return;
        }

        $('.submenu').append($(this).children("ul").clone());

        var thisElement = $(this);

        berekenSubmenuBreedte();
        berekenSubmenuBreedte();
        setTimeout(function () {

            berekenSubmenuBreedte();


            var topParent = thisElement.offset().top;
            var leftParent = thisElement.offset().left;

            var leftMin = $('.menu>ul>li:first').offset().left + 24;
            var leftMax = $('.menu>ul>li:last').offset().left + 134 - totalWidth - 12;

            var topMe = topParent + 40 - 1;
            var leftMe = leftParent - Math.ceil(totalWidth / 2);
            leftMe += 65; //Half the width of parent item

            if (leftMe < leftMin) {
                leftMe = leftMin;
            }

            if (leftMe > leftMax) {
                leftMe = leftMax;
            }

            $('.submenu>ul').offset({ top: topMe, left: leftMe });
            $('.submenu').css("visibility", "visible");

        }, 100);

    }, function () {

//        $('.submenu').stop(true, true);
//        $('.submenu').queue(function () {
//            setTimeout(function () {
//                $('.submenu').dequeue();
//            }, 50);
//        });
        $('.submenu').css("visibility", "hidden");
    });

    $('.submenu').hover(function () {
        $('.submenu').css("visibility", "visible");
    }, function () {
        $('.submenu').css("visibility", "hidden");
    });


});

function berekenSubmenuBreedte() {

    totalWidth = 1;

    $('.submenu>ul>li').each(function () {

        totalWidth += $(this).width();
        totalWidth += 20 //Padding

    });

    $('.submenu>ul').css("width", totalWidth + "px");


}
