/* 
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() {
	$('.menu .dd').hide();
	$.each($('.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();
		//}
		$('#' + this.id + ' .dd.expandfirst').show();
	});
	$('.menu a').click(
		function() {

			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;
			var right_arrow = '&#10010'; // &larr; &#10010;
			var down_arrow = '<style="font-size:8px">&#9602</style>'; // &darr; &#9602;
      
      
			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('.dd')) && (checkElement.is(':visible'))) {
			  //if($('#' + parent).hasClass('collapsible')) {
					/*
					$('#' + parent + ' .dd:visible').slideUp('normal');
		 		  $('#' + parent + ' .dd:visible').parents('.white').removeClass('selected');
					$('#' + parent + ' .dd:visible').parents('.white').children('a').children('h1').children('.arrow').html(right_arrow);
					*/
					checkElement.slideUp('normal');
		 		  checkElement.parents('.white').removeClass('selected');
					checkElement.parents('.white').children('a').children('h1').children('.arrow').html(right_arrow);
				//}
				return false;
			}
			if((checkElement.is('.dd')) && (!checkElement.is(':visible'))) {
        //$('#' + parent + ' .dd:visible').slideUp('normal');
				//$('#' + parent + ' .dd:visible').parents('.white').removeClass('selected');
				//$('#' + parent + ' .dd:visible').parents('.white').children('a').children('h1').children('.arrow').html(right_arrow);
				if((String(parent).length > 0) && (String(this.className).length > 0)) {
					//$.cookie(parent, this.className);
				}
				checkElement.slideDown('normal');
				checkElement.parents('.white').addClass('selected');
				checkElement.parents('.white').children('a').children('h1').children('.arrow').html(down_arrow);
				return false;
			}
		}
	);
}
$(document).ready(function() {initMenus();});
