﻿// Menu Version 0.0.1 (Nécessite jQuery)

(function($) {
    
    var helper = {};

    $.menu = {
        defaults: {
        	activeElement: 'mainDefault',
        	isChildren: false,
            callBack: null
        }
    };

    $.fn.extend({
        menu: function(settings) {
            settings = $.extend({}, $.menu.defaults, settings);
            return this.each(function() {
                if (mysettings(this) == null) {
                    var localSettings = createHelper(this, settings);
                    localSettings = $.extend({}, settings, localSettings);
                    $.data(this, 'wyg-menu', localSettings);
                }
            });
        }
    });

    function createHelper(domElement, curSet) {
        var jq = $(domElement), settings = {};
            jq.attr('wygMenu', 'true');
        
        settings.globales = curSet;
        jq.children('li').each(function() {
			activeElement(this,settings);
        	displayMenuHover(this,settings);
        });

        return settings;
    }
    
    function activeElement(element,settings) {
    	var element = $(element),
    		activeElement = settings.globales.activeElement,
    		isChildren = settings.globales.isChildren;
    		
    	$('#'+ activeElement).attr('class','menuSelect');
    	if(isChildren) $('#'+ activeElement).parent().parent().attr('class','menuSelect');
    }
    
    function displayMenuHover(element,settings) {
    	var element = $(element);
    	
		if(element.children('ul').length > 0) {
			$('<div class="menuHoverShadowTop"></div>').appendTo(element);
			$('<div class="menuHoverShadowRight"></div>').appendTo(element).css({
				'height': (element.children('ul').children('li').length * 30) -8
			});
			$('<div class="menuHoverShadowBottom"></div>').appendTo(element).css({
				'top': (element.children('ul').children('li').length * 30) +2
			});
		}
		
		element.bind('mouseenter', function() {
			if($.browser.msie) {
				element.children('ul').show();
				element.children('.menuHoverShadowTop').show();
				element.children('.menuHoverShadowRight').show();
				element.children('.menuHoverShadowBottom').show();
			}
			else {
				element.children('ul').fadeIn(250).show();
				element.children('.menuHoverShadowTop').fadeIn(250).show();
				element.children('.menuHoverShadowRight').fadeIn(250).show();
				element.children('.menuHoverShadowBottom').fadeIn(250).show();
			}
		}).bind('mouseleave',function() {
			element.children('ul').hide();
			element.children('.menuHoverShadowTop').hide();
			element.children('.menuHoverShadowRight').hide();
			element.children('.menuHoverShadowBottom').hide();
		});	
    }

    function mysettings(element) {
        return $.data(element, 'wyg-menu');
    }

})(jQuery);
