/* 
Simple JQuery menu.
HTML structure to use:

Notes: 

1: each menu MUST have an ID set. It doesn't matter what this ID is as long as it's there.
2: each menu MUST have a class 'menu' set. If the menu doesn't have this, the JS won't make it dynamic

Optional extra classnames:

noaccordion : no accordion functionality
collapsible : menu works like an accordion but can be fully collapsed
expandfirst : first menu item expanded at page load

<ul id="menu1" class="menu [optional class] [optional class]">
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
<li><a href="#">Sub menu heading</a>
<ul>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
<li><a href="http://site.com/">Link</a></li>
...
...
</ul>
...
...
</ul>

Copyright 2008 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/

function initMenus() {
	$('ul.menu ul').hide();
	$.each($('ul.menu'), function(){
		var cookie = $.cookie(this.id);
		var cakecookie = $.cookie('CakeCookie[menu1]');
		if(cookie === null || String(cookie).length < 1) {
			cookie = cakecookie;
		}
		
		
		if(cookie === null || String(cookie).length < 1) {
			$('#' + this.id + '.expandfirst ul:first').show();
		} else {			
			$('#' + this.id + ' .' + cookie).next().show();
			/* open sub menu */
			var cookie2 = $.cookie('menu2');
			if(cookie2 !== null && String(cookie2).length) {
				
				if(cookie2.indexOf(' ') != -1) {
					var space = cookie2.indexOf(' ');
					var theid = cookie2.substring(0, space);
					var children = $('#' + theid).next();
				} else {
					var children = $('.' + cookie2).next();
				}
				
				children.show();
			}
		}
	});
	
	$("a[class^='smm'],a[id^='smm']").click(function() {
		var children = $(this).next();
		if(children.css('display') != 'none') {
			children.slideUp('normal');
			
		} else {
			$.cookie('menu2', this.className, { path: '/' });
		}
	});
	
	
	$('ul.menu li a').click(
		function() {

			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;
			
			if($('#' + parent).hasClass('noaccordion')) {
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					if($(this).next().is(':visible')) {
						$.cookie(parent, null, { path: '/' });
					} else {
						$.cookie(parent, this.className, { path: '/' });
					}
					$(this).next().slideToggle('normal');
				}				
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				$('#' + parent + ' ul:visible').slideUp('normal');
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					$.cookie(parent, this.className, { path: '/' });
				}
				checkElement.slideDown('normal');
				return false;
			}
		
			if(!$(this).parent().parent().parent().children('a').hasClass('sub')) {
				/* just under /laboratorio */
				var layout = $.cookie('CakeCookie[layout]');
				if(layout.match('/^laboratorio/')) {
					$.cookie('menu2', null, { path: '/' });
				}
			}
		}
	);
	
	
	$("a[rel='cookiecleaner']").click(function() {
		$.cookie('menu2', null, { path: '/' });
		$.cookie('menu1', null, { path: '/' });
		$.cookie('menu', null, { path: '/' });
	});
}

/* add reallyvisible function */
jQuery.extend(
	jQuery.expr[ ":" ], 
	{ reallyvisible : "!(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length)" }
);

jQuery(document).ready(function() {
	initMenus();
});