$(document).ready(function() {
  /* PARENTS */
  $("div.menu_container").find("ul").eq(0).children("li.parent").children("a").each(function() {
    $(this).bind("click", function() {
      $.cookie('submenu_open', '');
      $.cookie('menu_open', null);

      $("li.child").each(function() {
        $(this).hide();
      });

      $("div.menu_container").find("ul").eq(0).children("li").children("a").each(function() {
        $(this).removeClass('selected');
      });

      $.cookie('menu_open', $(this).parent("li").attr("id"), { expires: 0, path: '/', domain: 'alexco.nl'});

      $(this).addClass('selected');
    });
  });

  if ($.cookie('menu_open') && $.cookie('menu_open').length > 0) {
    if ($("#" + $.cookie('menu_open'))) {
      $("#" + $.cookie('menu_open')).children("a").addClass("selected");

      $("li[id^='"+ $.cookie('menu_open') +"']").show();

    }
  }

  /* CHILDS */
  $("div.menu_container").find("ul").eq(0).children("li.child").children("a").each(function() {
    $(this).bind("click", function() {
      $.cookie('submenu_open', null);

      $("div.menu_container").find("ul").eq(0).children("li.child").children("a").each(function() {
        $(this).removeClass('selected');
      });

      $.cookie('submenu_open', $(this).parent("li").attr("id"), { expires: 0, path: '/', domain: 'alexco.nl'});

      $(this).addClass('selected');
    });
  });

  if ($.cookie('submenu_open') && $.cookie('submenu_open').length > 0) {
    if ($("#" + $.cookie('submenu_open'))) {
      $("#" + $.cookie('submenu_open')).children("a").addClass("selected");

      $("li[id='"+ $.cookie('submenu_open') +"']").show();
    }
  }

});

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(';');
            for (var i = 0; i < cookies.length; 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));
                    break;
                }
            }
        }
        return cookieValue;
    }
};