/* 
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);
		if(cookie === null || String(cookie).length < 1) {
			$('#' + this.id + '.expandfirst ul:first').show();
		}
		else {			
			$('#' + this.id + ' .' + cookie).next().show();
		}
	});
	$('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);
					}
					else {
						$.cookie(parent, this.className);
					}
					$(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);
				}
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
} 

$(document).ready(function() {InitDivMenus();});

function InitDivMenus() {
	$('#menu .submenu').hide(); 

	$('#menu a').each(function(){$(this).focus(function(){$(this).blur();});});

	$.each($('#menu'), function(){
		var cookie = $.cookie(this.id);
		if(cookie === null || String(cookie).length < 1) {
			//$('#' + this.id + '.submenu:first').show();
		}
		else {			
			$('#' + this.id + ' > #' + cookie).next().show();
		}
	});
	$('#menu .parent-childs').click(
		function() { 
			var cId = this.id;
			var checkElement = $(this).next();
			var parent = this.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					if($(this).next().is(':visible')) {
						$.cookie(parent, null);
					}
					else {
						$.cookie(parent, this.className);
					}
					$(this).next().slideToggle('normal');
				}				
			}
			if((checkElement.is(':visible'))) {
				if($('#' + parent).hasClass('collapsible')) {
					//$('#' + parent + ' div:visible').slideUp('normal');
				}
				checkElement.slideUp('normal');
				$.cookie(parent, null);
				return false;
			}
			if((!checkElement.is(':visible'))) { 
				$(".submenu").each(
					function(){
						if($(this).is(':visible')){$(this).slideUp('normal');}
					}
				);
				checkElement.slideUp('normal');
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					$.cookie(parent, cId, {expires: 30});
				}
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}

$(document).ready(function () {
	$('.parent').append('<div class="hover"><\/div>');
    $('.parent').hover(
		function() {
			$(this).children('a').css("color","#FFFFFF");
			$(this).children('div').fadeIn('1000');	  
		}, 
		function() {
			$(this).children('a').css("color","#2f7f02");
			$(this).children('div').fadeOut('1000');			
		}
	);
	$('.parent-childs').append('<div class="hover"><\/div>');
    $('.parent-childs').hover(
		function() 
		{
			$(this).children('a').css("color","#FFFFFF");
			$(this).children('div').fadeIn('1000');	 
		}, 
		function() {
			$(this).children('a').css("color","#2f7f02");
			$(this).children('div').fadeOut('1000'); 
	});
	$('.child').append('<div class="hover"><\/div>');
    $('.child').hover(
		function() {
			$(this).children('a').css("color","#FFFFFF");
			$(this).children('div').fadeIn('1000');	
			
		}, 
		function() {
			$(this).children('a').css("color","#2f7f02");
			$(this).children('div').fadeOut('1000');
			
	});
	});
