var navigationTimer = currentNavImg = currentBlockId = null;

var navDuration = 1000;
window.addEvent('domready',function(){
    $('navigation').getElements('a.lvl0-link').each(function(item){
        var id = item.get('id').replace('lvl0-','');
        item.addEvents({
            'mouseenter': function(e){
                e.stop();
                if(currentNavImg != null) hideSubnav(currentBlockId);
                currentBlockId = id; currentNavImg = item.getElement('img');
                displaySubnav(id);
            },
            'mouseleave': function(e){
                navigationTimer = function(){hideSubnav(id)}.delay(navDuration);
            }
        });
        
    });

    $('navigation').getElements('.nav_container').each(function(item){
        var id = item.get('id').replace('bloc-','');
        item.addEvents({
            'mouseenter': function(){
                $clear(navigationTimer);
            },
            'mouseleave': function(){
                navigationTimer = function(){hideSubnav(id)}.delay(navDuration);
            }
        });
    });

    function displaySubnav(id)
    {
        $clear(navigationTimer);
        if(currentNavImg != null) changeNavImg(true)
        $('bloc-'+id).setStyle('display','block');
    }

    function hideSubnav(id)
    {
        if(currentNavImg != null) changeNavImg(false);
        $('bloc-'+id).setStyle('display','none');
    }

    function changeNavImg(toOn)
    {
        var from = '-on'; to = '-off';
        if(toOn) { from = '-off'; to = '-on'; }
        currentNavImg.set('src',currentNavImg.get('src').replace(from,to));
    }
});



