/* 
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(){
		$('#' + this.id + '.expandfirst ul:first').show();
	});
	$('ul.menu li a').click(
		function() {
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;

			if($('#' + parent).hasClass('noaccordion')) {
				$(this).next().slideToggle('normal');
				return false;
			}
			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');
				checkElement.slideDown('normal');
				return false;
			}
		}
	);
}
*/

jQuery.cookie = function(name, value, options) {
				
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
		
            var cookies = document.cookie.split(';');
		//	alert(cookies.length);
            for (var i = 0; i < cookies.length; i++) {
			//	alert(i);
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
			//		alert(cookieValue);
//                    break;
                }
            }
        }
        return cookieValue;
    }
};

 function initMenus() {

	 $('ul.menu ul').hide();
	 $.each($('ul.menu'), function(){
	
		 var cookie = $.cookie(this.id);
		 var valuecook;
		 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);
						 $.cookie(parent, this.className, { path: "/", http:document.domain});
					 }
					 $(this).next().slideToggle('normal');
				 }
			 }
			
			 if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				 if($.cookie("menu") == "m1")
					window.location="http://" +document.domain + "/Participating-in-microFIT/index.php";	
				if($.cookie("menu") == "m2")
					window.location="http://" +document.domain + "/Renewable-Energy-Projects/index.php";	
				if($.cookie("menu") == "m3")
					window.location="http://" +document.domain + "/microFIT-Program-Rules/index.php";
				if($.cookie("menu") == "m4")
					window.location="http://" +document.domain + "/Managing-your-contract/index.php";
				if($.cookie("menu") == "m5")
					window.location="http://" +document.domain + "/Program-resources/index.php";	
				if($.cookie("menu") == "m6")
					window.location="http://" +document.domain + "/Public-Consultation/index.php";	
				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);
					$.cookie(parent, this.className, { path: "/", http:document.domain});
				 }
				 checkElement.slideDown('normal');
				 
				 if($.cookie("menu") == "m1")
					window.location="http://" +document.domain + "/Participating-in-microFIT/index.php";	
				if($.cookie("menu") == "m2")
					window.location="http://" +document.domain + "/Renewable-Energy-Projects/index.php";	
				if($.cookie("menu") == "m3")
					window.location="http://" +document.domain + "/microFIT-Program-Rules/index.php";
				if($.cookie("menu") == "m4")
					window.location="http://" +document.domain + "/Managing-your-contract/index.php";
				if($.cookie("menu") == "m5")
					window.location="http://" +document.domain + "/Program-resources/index.php";	
				if($.cookie("menu") == "m6")
					window.location="http://" +document.domain + "/Public-Consultation/index.php";	
	
			 return false;
			}
		 }
	 );
 }

//$(document).ready(function() {initMenus();});
/*
// non-working cookie-based
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;
}
}
);
}
*/