﻿///* 
//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() {
////    var varLastClicked = document.getElementById('LastClicked');
////    if (varLastClicked.value == '1') {
////        $('ul.menu ul').hide();
////        $('#' + varLastClicked.value).addClass('topp')
////    }
////    else {
////        var menu_array = (varLastClicked.value).split(".");
////    }
////    $.each($('ul.menu'), function() {
////            $('#' + this.id + '.expandfirst ul:first').show();
////        });
////        $('ul.menu li a').click(
////		function() {
////		    var nextElement = $(this).next();
////		    var test = varLastClicked.value;
////		    //varLastClicked.value = this.parentNode.id; //$(this).attr('id');
////		    var parent = this.parentNode.parentNode.id;

////		    /* if ($('#' + parent).hasClass('noaccordion')) {
////		    $(this).next().slideToggle({ duration: 250, easing: 'easein' });
////		    return false;
////		    } */
////		    if ((nextElement.is('ul')) && (nextElement.is(':visible'))) {
////		        if ($('#' + parent).hasClass('collapsible')) {
////		            $('#' + parent + ' ul:visible').slideUp({ duration: 400, easing: 'easein' });
////		        }
////		        return false;
////		    }
////		    if ((nextElement.is('ul')) && (!nextElement.is(':visible'))) {
////		        $('#' + parent + ' ul:visible').slideUp({ duration: 150, easing: 'easein' });
////		        nextElement.slideDown({ duration: 1000, easing: 'bounceout' });
////		        return false;
////		    }

////		}
////	);
////}
////$(document).ready(function() {initMenus();});

//function initMenus() {
//    $('ul.menu ul').hide(); //hide all
//    $.each($('ul.menu'), function() { //add a function to create a cookie with current id
//        var cookie = $.cookie(this.id);
//        if (cookie != null && String(cookie).length < 1) { //if there is a current cookie
//            $('#' + this.id + '.expandfirst ul:first').show(); //show elements  that have id of * .expandfirst ul:first
//        }
//        else {
//            $('#' + this.id + ' .' + cookie).next().show(); //
//        }
//    });
//    $('ul.menu li a').click(
//function() {
//    var nextElement = $(this).next();
//    var prevElement = this.parentNode;
//    var parent = this.parentNode.parentNode.id;
//    if ((nextElement.is('ul')) && (nextElement.is(':visible'))) {
//        if ($('#' + parent).hasClass('collapsible')) {
//            $('#' + parent + ' ul:visible').slideUp('normal');
//        }
//        return false;
//    }
//    if ((nextElement.is('ul')) && (!nextElement.is(':visible'))) {
//        $('#' + parent + ' ul:visible').slideUp('normal');
//        if ((String(parent).length > 0) && (String(this.className).length > 0)) {
//            $.cookie(parent, this.className);
//        }
//        nextElement.slideDown('normal');
//        return false;
//    }
//    if ((nextElement.length == 0)) {
//        $.cookie(parent, null);
//    }
//}
//);
//}
//$(document).ready(function() { initMenus(); });

// JavaScript Document

function initMenus() {
    $('ul.menu ul').hide(); //when reloading page
    //$('.lev1_selected').toggleClass('lev1_selected');
    var cookie = $.cookie('menu');
    
    $.each($('ul.menu'), function() { //add a function which reads cookie content
        var cookie = $.cookie(this.id);
        //$('.' + cookie).css('background-color','#fff333');
        //$('#' + this.id).hasClass('.' + cookie).next().show();
        //$('#' + this.id + ' .' + cookie).next().show(); //
        if (cookie != null) {
            $('#' + this.id + ' .' + cookie.substring(0, 2)).prev().show(); //
            //$('#' + this.id + ' .' + cookie).removeClass('mid');
            //$('#' + this.id + ' .' + cookie).addClass('mid_selected');
            $.cookie(this.id, null); //NEW
        }
        else {
            $('#' + this.id + ' .' + cookie).next().show(); //
           
        }

    });
    
    $('ul.menu li a').click(function() {
        var checkElement = $(this).next();
        var parent = this.parentNode.parentNode.id;
        if (checkElement.is('ul')) {
            if (checkElement.is(':visible')) {
                if ($('#' + parent).hasClass('collapsible')) {
                    $('#' + parent + ' ul:visible').slideUp({ duration: 400, easing: 'easein' });
                    $.cookie(parent, null); //NEW
                }
                return false;
            }
            if (!checkElement.is(':visible')) {
                $('#' + parent + ' ul:visible').slideUp({ duration: 150, easing: 'easein' });
                if ((String(parent).length > 0) && (String(this.className).length > 0)) {
                    $.cookie(parent, this.className);
                }
                checkElement.slideDown({ duration: 1000, easing: 'bounceout' });
                return false;
            }
        }
        else {
            $.cookie('menu', this.className.substring(0,2)); 
        }
    });
}
$(document).ready(function() { initMenus(); });
