$(document).ready(
    function() {
        setListenerMainMenu();
    });

function setListenerMainMenu() {

    // Main Menu
    var nVoiceMainMenu = $('.firstLevel > li').length;
    
    //
    $('.firstLevel > li').hoverIntent(function() {

        // MouseOver
        var sClass = $(this).attr('class');
        sClass = '.' + sClass.substring(0, 3);
        var basePath = '.firstLevel > ' + sClass;
        if ($(basePath + ' > .menuSecond').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sSecondMenuId = $(basePath + ' > .menuSecond').attr('id');
            sIdSecondMenu = sSecondMenuId.replace('menuSecond', '');

            // carico il contenuto del div
            $('#' + sSecondMenuId).html($('#content_menuSecond' + sIdSecondMenu).html());            

            setListenerSecondMenu(sSecondMenuId);

        }
    }, function() {
        // MouseOut
        var sClass = $(this).attr('class');
        sClass = '.' + sClass.substring(0, 3);
        var basePath = '.firstLevel > ' + sClass;
        if ($(basePath + ' > .menuSecond').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sSecondMenuId = $(basePath + ' > .menuSecond').attr('id');
            sId = sSecondMenuId.replace('menuSecond', '');

            // carico il contenuto del div
            $('#' + sSecondMenuId).html('');
        }
    });
}

function setListenerSecondMenu(sSecondMenuId) {
    //Aggiungo i listener per le sottovoci
    $('#' + sSecondMenuId + ' > .secondLevel > li').hoverIntent(function() {
        var sIdSecondLi = $(this).attr('id');
        if ($('#' + sIdSecondLi + ' > .menuThird').attr('id') != undefined) {
            
            // recupero l'id del div che contiene le voci del sottomenu
            var sThirdMenuId = $('#' + sIdSecondLi + ' > .menuThird').attr('id');
            sIdThirdMenu = sThirdMenuId.replace('menuThird', '');

            // Cambio classe per sottomenu aperto
            $('#' + sSecondMenuId + ' > .secondLevel > #' + sIdSecondLi + ' > a').attr({ 'class': 'submenu active' });
            $('#menuThird' + sIdThirdMenu).html($('#content_menuThird' + sIdThirdMenu).html());

            // Setto i listener delle voci del sottomenu
            setListenerThirdMenu(sThirdMenuId);

        }
    }, function() {
        var sIdSecondLi = $(this).attr('id');
        if ($('#' + sIdSecondLi + ' > .menuThird').attr('id') != undefined) {
            
            // recupero l'id del div che contiene le voci del sottomenu
            var sThirdMenuId = $('#' + sIdSecondLi + ' > .menuThird').attr('id');
            sIdThirdMenu = sThirdMenuId.replace('menuThird', '');
            
            // Cambio classe per sottomenu aperto
            $('#' + sSecondMenuId + ' > .secondLevel > #' + sIdSecondLi + ' > a').attr({ 'class': 'submenu' });
            $('#menuThird' + sIdThirdMenu).html('');
        }
    });
}

function setListenerThirdMenu(sThirdMenuId) {
    $('#' + sThirdMenuId + ' > .thirdLevel > li').hoverIntent(function() {
        var sIdThirdLi = $(this).attr('id');
        if ($('#' + sIdThirdLi + ' > .menuFourth').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sFourtMenuId = $('#' + sIdThirdLi + ' > .menuFourth').attr('id');
            sIdFourtMenuId = sFourtMenuId.replace('menuFourth', '');

            // Cambio classe per sottomenu aperto
            $('#' + sThirdMenuId + ' > .thirdLevel > #' + sIdThirdLi + ' > a').attr({ 'class': 'submenu active' });            
            $('#menuFourth' + sIdFourtMenuId).html($('#content_menuFourth' + sIdFourtMenuId).html());
        }
    }, function() {
        var sIdThirdLi = $(this).attr('id');
        if ($('#' + sIdThirdLi + ' > .menuFourth').attr('id') != undefined) {            

            // recupero l'id del div che contiene le voci del sottomenu
            var sFourtMenuId = $('#' + sIdThirdLi + ' > .menuFourth').attr('id');
            sIdFourtMenuId = sFourtMenuId.replace('menuFourth', '');

            // Cambio classe per sottomenu aperto
            $('#' + sThirdMenuId + ' > .thirdLevel > #' + sIdThirdLi + ' > a').attr({ 'class': 'submenu' });
            $('#menuFourth' + sIdFourtMenuId).html('');
        }
    });
}
