$(document).ready(function () {
 var root = $('#drevo');
 $('li', root).each(function (index) {
    this.id = 'n' + index;
 });
 $('li:has("ul")', root).find('a:first').prepend('<em class="marker"></em>');

// var current_id = $.cookie('current_node');
// if(current_id) $('#'+current_id).find('a:first').toggleClass('current');

 $('li span', root).click(function () {
    $('a.current', root).removeClass('current');
    var a = $('a:first',this.parentNode);
    a.toggleClass('current');
    var current_id = a.parent('li').attr('id');
    setCookie('current_node',a.parents('li').attr('id') || null);
    toggleNode(this.parentNode);
 });
 openNodes();
})

function toggleNode(Node) {
 prepareLast(Node);
 var ul=$('ul:first',Node);
 if (ul.length) {
    ul.slideToggle(200);
    var em=$('em:first',Node);
    em.toggleClass('open');
    saveTreeState();
 }
}

function prepareLast(Node) {
 $(Node).each(function(){
    if (!$(this).next().length) {
       $(this).find('ul:first > li').addClass('last');
    }
 })
}
function postLoad(){
 var url = window.location.toString();
 var max = 0;
 var a = null;
 $('#drevo li span a').each(function(){
    if(url.indexOf(this.href) >= 0 && this.href.length > max){
       a = this;
       max = this.href.length;
    }
 });
 if ($(a).is(':hidden') || $(a).parents(':hidden').length) {
    var li = $(a).parents().filter('li');
    prepareLast(li);
    toggleNode(li);
 }
 if (a) {
    $(a).toggleClass('current');
 }
 else {
    $('#drevo li span a:first').toggleClass('current');
 }
}

function GetOpenedNodes(items) {
  var str = [];
  $(items).each(function() {
    var res = $(this).attr('id');
    var state = $('em:first',this).hasClass('open') ? 1 : '';
    if(res && state){
      str.push(res);
    }
  });
  return str.join(',');
}

function saveTreeState(){
  var open_id = GetOpenedNodes($('#drevo li:has("ul")')) || null;
  setCookie("open_nodes", open_id);
  return false;
}

function openNodes(){
  var open_nodes = $.cookie("open_nodes");
    if(open_nodes) {
    var nodes = open_nodes.split(',');

    if(nodes[0]){
      for(var node in nodes){
        nodes[node] = '#' + nodes[node];
      }
      var ids = nodes.join(',');
      $(ids).each(function() {
         toggleNode($(this));
      });
    }
  }
  return false;
}

function setCookie(name, value){
  var DAY = 24 * 60 * 60 * 1000;
  var date = new Date();
  date.setTime(date.getTime() + (1 * DAY));
  $.cookie(name, value, {expires: date});
}


