$(function() {

  var tm = null;
  var oy = $('#content').offset().top;
  var mn = parseInt($('#menu-content').css('marginTop'));
  $(window).bind('scroll', function() {
    clearTimeout(tm);
    tm = setTimeout(function() {
      var tmp = Math.max(oy, $(window).scrollTop()) + mn;
      $('#menu-content').stop().animate({ 'marginTop': (tmp - oy) + 'px'}, 800);
    }, 400);
  } );

  ( function(elmID) {

    var btns = $('.tgl-btn', elmID);
    var pnls = $('.tgl-pnl', elmID);

    btns.click(function() {
      var thisIndex = btns.index(this);
      var thisPnl = $(pnls.get(thisIndex));
      if (elmID == '#menu-content') {
        thisPnl.toggle("normal", function() { setTimeout(function() {
          $.cookie('visible_menu_index', getMenuPath($('#menu-tree')).join('-'), { path: '/' });
        }, 100); } );
        thisPnl.parent().siblings("li").children("ul").not(thisPnl).hide("normal");
      }
      else {
        thisPnl.toggle("normal");
        pnls.not(thisPnl).hide("normal");
      }
    } )
    .hover(
      function() { $(this).css('textDecoration', 'underline'); },
      function() { $(this).css('textDecoration', 'none'); }
    )
    .css('cursor', 'pointer');

    return arguments.callee;

  } )('#menu-content')('#main-content');

} );

  function oWin(o) {
    open(o.href, '', '');
    return false;
  }

  function getMenuPath(jqObj) {
    var index = -1, thisObj = null;
    jqObj.children('li').children('ul').each(function(i) {
      if ($(this).is(':visible')) {
        index = i;
        thisObj = $(this);
        return false;
      }
      return true;
    } );
    if (index < 0) return []; else
      return [index].concat(arguments.callee(thisObj));
  }

  function setMenuPath(jqObj, ar) {
    if (ar.length > 0 && ar[0].length > 0) {
      var index = parseInt(ar[0]);
      for (var i = 0; i < ar.length; i++) ar[i] = ar[i + 1];
      ar.length--;
      arguments.callee(jqObj.children('li').children('ul').eq(index).show(), ar);
    }
  }

