var t = new Array;  // Cash of timers
var mp = new Array; // Cash of animated items

/**
 * Close submenu by parent indentificator
 */
function closeThis(i) {
    mp[i].find('ul').stop(false,true).animate({opacity:-0.5});
    mp[i].stop(false,true).slideUp();
}

// run wen documetn is ready
$(function() {
    
    $('.mp-drop-menu-list').find('ul').hide();

    $('.mp-warper').each( 
        function() {
            // Store indentificator
            $(this).data('i', $('.mp-warper').index(this) );
        }
    ).hover( 
        // mouse over
        function() {
            i = $(this).data('i');
            
            clearTimeout( t[i] );
            // Show submenus
            
            $('.mp-drop-menu-list', this).stop(false,true).slideDown();
            $('.mp-drop-menu-list', this).find('ul').stop(false,true).css({opacity:-0.5}).show().animate({opacity:1},'normal');
        },
        // mouse out
        function() {
            // Get item indentificator
            i = $(this).data('i');
            // Put in cash animated item
            mp[i] = $('.mp-drop-menu-list', this);
            
            clearTimeout( t[i] );
            // When time runs out, hide submenus
            t[i] = setTimeout( 'closeThis('+i+')', 800 );
            
        }
    );
    
    
    $(document).click( function(e) {
        // Check object parents on reqester class
        function checkParrents(o) {
            b = o.parents().filter('.mp-warper')
            if ( b.length != 0 )
                return true;
            return false;
        }
        
        if ( (!$(e.target).hasClass('mp-warper')) && ( !checkParrents( $(e.target) ) ) ) {
            clearTimeout(t);
            $('.mp-drop-menu-list').find('ul').stop(false,true).animate({opacity:-0.5});
            $('.mp-drop-menu-list').stop(false,true).slideUp();
        }
    });
    
});