function Navigation() {
  this.id_open = false;
  this.id2open = false;
  this.id2close = false;
  this.id_hover = false;
  this.open_delay = 100;
  this.close_delay = 250;
  this.arr_open = new Array();
  this.arr_size = new Array();
  
  this._open = function() {
    window.clearInterval(this.interval_open);
    window.clearInterval(this.interval_close);
    if (!this.id2open || (this.id2open == this.id_open && this.id_hover_last == this.id2open))
      return;
    this._close(true);
    this.arr_open[this.id2open] = true;
    
    !jQuery.isArray(this.arr_size[this.id2open]) ? this.arr_size[this.id2open] = new Array : 0;
    !this.arr_size[this.id2open]['w'] ? this.arr_size[this.id2open]['w'] = $(this.id2open).outerWidth() : 0;
    !this.arr_size[this.id2open]['h'] ? this.arr_size[this.id2open]['h'] = $(this.id2open).outerHeight() : 0;
    var w = this.arr_size[this.id2open]['w'];
    var h = this.arr_size[this.id2open]['h'];
    var hp = h + 5;
    $(this.id2open).addClass('n');
    $(this.id2open).css('width', '0');
    $(this.id2open).css('height', '0');
    typeof(this.arr_size[this.id2open]['mt']) != 'undefined' ? $(this.id2open).css('margin-top', this.arr_size[this.id2open]['mt']) : this.arr_size[this.id2open]['mt'] = $(this.id2open).css('margin-top');
    $(this.id2open).show();
    var p = $(this.id2open).offset();
  	p.top + hp >= $(window).height() + $(window).scrollTop() ? $(this.id2open).css('margin-top', parseInt($(this.id2open).css('margin-top')) + ($(window).height() + $(window).scrollTop() - hp - p.top)) : 0;
    navi.id_hover_last = false;
    $(this.id2open).animate({
      width: w,
      height: h
    }, 'fast', function() {
      navi.id_open = navi.id2open;
      navi._close();
    });
  }
  
  this._close = function(b_force) {
    window.clearInterval(this.interval_close);
    $("div[id^=navi_left_sub]").each(function(index) {
      if (b_force) {
        $(this).hide();
      } else {
        var id = '#' + $(this).attr('id');
        if (id != navi.id_hover || navi.id_hover == false) {
          if (navi.arr_open[id] && ((navi.id2close && id == navi.id2close) || !navi.id2open || (navi.id2open && id != navi.id2open) || !navi.id_open || (navi.id_open && id != navi.id_open))) {
            $(this).hide();
            navi.arr_open[id] = false;
            navi.id2close = false;
            navi.id_open == id ? navi.id_open = false : 0;
          }
        }
      }
    });
    for (var id in this.arr_open) {
      if (id != this.id_hover && id != this.id_open) {
        if ((this.id2close && id == this.id2close) || (this.id2open && id != this.id2open)) {
          $(id).hide();
          this.arr_open[id] = false;
          this.id2close = false;
          this.id_open == id ? this.id_open = false : 0;
        }
      }
    }
  }
  
  this.close = function(obj) {
    this.id2close = navi.get_id(obj);
    if ($(this.id2close).length > 0) {
      window.clearInterval(this.interval_close);
      this.interval_close = window.setInterval('navi._close()', this.close_delay);
    }
  }
  
  this.get_id = function(obj) {
    return '#navi_left_sub' + $(obj).attr('id').replace(/navi_left_(main|sub)/, '');
  }
}

var navi = new Navigation();

$(document).ready(function() {
  $("a[id^=navi_left_main][target]").click(function() {
    $('div[class="sel dark"]').remove();
    $(this).blur().parent().prepend('<div class="sel dark"></div>');
  });
  $("a[id^=navi_left_main]").hover(
    function() {
      $(this).addClass('medium');
      //navi.id_hover = false;
      window.clearInterval(navi.interval_open);
      var id2open = navi.get_id(this);
      if ($(id2open).length > 0) {
        navi.id2open = id2open;
        navi.interval_open = window.setInterval('navi._open()', navi.open_delay);
      } else {
        navi.close(this);
        navi.id2open = false;
      }
    },
    function() {
      $(this).removeClass('medium');
      navi.id2open = false;
      navi.close(this);
    })
  $("div[id^=navi_left_sub]").hover(
    function() {
      navi.id_hover = navi.get_id(this);
      navi.id2open = false;
      navi.id_hover_last = navi.id_hover;
    },
    function() {
      navi.id_hover = false;
      navi.id2open = false;
      navi.close(this);
    }
  );
  $("div[class^=navi_left_nodes]").hover(
    function() {
      $(this).addClass('medium');
      $(this).removeClass('bright');
    },
    function() {
      $(this).addClass('bright');
      $(this).removeClass('medium');
    }
  );
});
