/**
 *
 * This script hides menu according to current level, 
 * so that template always prints menu, but portion of it
 * is displayed.
 *
 * Menu needs to be in #menu element and a-elements with class "level#"
 * are hidden (1-10)
 * 
 * Current parent levels need "parentmenu" class and current menu "currentmenu"
 *
 * Author: FRC oy, Juha-Pekka Rajaniemi
 * 
 * 
 */

$(document).ready(function() {
  for(i=2; i < 10; i++) {
    $(".level"+i).hide();

    var etu = "";
    for (j = 1; j < i; j++)
      var etu = etu + "-";

    $(".level"+i).each(function() {
      $(this).html(etu + $(this).html())
    });
  }

  $('#menu a[@class*=parentmenu]').each(function(i){
    //alert(getLevel(this));
    showNextLevel(this, getLevel(this));
  });

  $('#menu a[@class*=currentmenu]').each(function(i) {
    showNextLevel(this, getLevel(this));
  });

});

function getLevel(element)
{
  for (i=1; i < 10; i++) {
    if ($(element).hasClass("level"+i))
      return i;
  }
}

function isLesserLevel(element, i)
{
  for (j = 0; j < i; j++)
    if ($(element).hasClass("level"+j))
      return true;

  return false;
}

function showNextLevel(element, i)
{
  element = $(element).next();

  if ($(element).hasClass("level"+i))
    return;

  if (isLesserLevel(element, i))
    return;

  if ($(element).hasClass("level"+(i+1)))
    $(element).show();

  showNextLevel(element, i);
}

