 (function($) {
	
    $(function() { 
        $('ul.menu ul').hide();
        $('ul.menu a').removeClass('activopapa');
        $('ul.menu li a').removeClass('activo');

	if (expandeMenu){
                var checkCookie = $.cookie("Submenu");
                if (checkCookie != "") {
                        $('#'+checkCookie).addClass('activopapa');
                }

                checkCookie = $.cookie("enlace");
                if (checkCookie != "") {
                        $('#'+checkCookie).addClass('activo');
                }
          

		$.each($('ul.menu'), function(){
			var cookie = $.cookie(this.id);
		        if ($.cookie('Ver')=="hide"){
					$('#' + this.id + ' .' + cookie).next().hide();
			}else{
					$('#' + this.id + ' .' + cookie).next().show();
			}
		});
	}else {
	    //limpiar cookies
		$.cookie('Ver',null,{ path: '/' });
		$.cookie('enlace',null,{ path: '/' });
            	$.cookie('Submenu',null,{ path: '/' });
		$.each($('ul.menu'), function(){
			$.cookie(this.id, null,{ path: '/' });
		});
	}
    

	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			$.cookie("enlace",this.id,{ path: '/' });
            
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
                                        $('ul.menu a').removeClass('activopapa');
                                        $.cookie('Ver',"hide",{ path: '/' });
                                        $('#' + parent + ' ul:visible').slideUp('normal');
                                        $('#' + parent + ' ul:visible a').removeClass('activo');
                    
				}
				return false;
			}

			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('ul.menu a').removeClass('activopapa');
                                $('#' + parent + ' ul:visible').slideUp('normal'); 
                                $('#' + parent + ' ul:visible a').removeClass('activo'); 
                                
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					$.cookie(parent, this.className,{ path: '/' });
				}
                               
				checkElement.slideDown('normal');

                                $(this).addClass('activopapa');
                                $.cookie('Submenu',this.id,{ path: '/' });
                                $.cookie('Ver',"show",{ path: '/' });
				return false;
			}

		}
	);
      	
    });
})(jQuery);

