/*
 * Top menu items postion management
 *
 **/


jQuery('document').ready(function() {
    fleuranceTopMenuInit();
});

var timer = false;

function fleuranceTopMenuInit() {
    jQuery('.top-menu li.category').each(function() {

        jQuery(this).addClass('unfold');

        
        if (jQuery(this).hasClass('normal')) {
            var topPartWidth = 0;
            
            jQuery(this).find('.content_link.virtual-categories, .content_link_bis div').each(function() {
                topPartWidth = topPartWidth + jQuery(this).outerWidth(true);
            });

            var bottomPartWidth = 0;

            jQuery(this).find('.content_bas .content_link, .content_bas .bloc_product').each(function() {
                bottomPartWidth = bottomPartWidth + jQuery(this).outerWidth(true);
            });

            Math.max(topPartWidth, bottomPartWidth);

            jQuery(this).find('.content_center').css('width', Math.max(topPartWidth, bottomPartWidth) + "px")

        }
        

        /* Left position */
        var panelNode     = jQuery(this).find('div.content').get(0);
        
        var panelWidth    = jQuery(panelNode).width();
        var menuItemWidth = jQuery(this).width();
        var rootNodeWidth = jQuery(this.parentNode).width();

        var leftPosition  = (menuItemWidth - panelWidth) / 2

        if (this.offsetLeft < - leftPosition) {
            leftPosition = - this.offsetLeft;

        } else if((leftPosition + panelWidth + this.offsetLeft) > rootNodeWidth) {
            
            leftPosition = rootNodeWidth - this.offsetLeft - panelWidth + 13;

        }

        jQuery(panelNode).css({left: leftPosition + "px"});

        
        jQuery(this).removeClass('unfold');

    })


    jQuery('.top-menu li.category').bind('mouseenter', function() {

        var linkNode = this;
        
        timer = setTimeout( function() {
            jQuery(linkNode).addClass('unfold');
            timer = false;
        }, 300);
        
    });

    jQuery('.top-menu li.category').bind('mouseleave', function() {

        if (timer) {
            clearTimeout(timer);
        }
        
        jQuery(this).removeClass('unfold');
    });
    
    
}

