TestVar = 0;

ExtMenu = {
  config: {
    hideTimeoutMilis: 100
  },
  elements: [],
  lock: true,
  current: null,
  hideTimeout: null,
  loadNext: null,
  
  init: function() {
    this.lock = false;
  },
  
  add: function(elemMain, elemSub) {
    elemMain.css('position', 'relative');
    if (elemSub !== null) {
      $('a', elemSub).click(function(ev){
        location.href = $(this).attr('href');
        return false;
      });
      
      elemSub.addClass('extmenu_submenu');
      elemSub.appendTo(elemMain);
    }
    
    this.elements.push({
      main: elemMain,
      sub: elemSub
    });
    elemMain.mouseover(function(e){
      ExtMenu.show(e.currentTarget);
    });
    elemMain.mouseout(function(e){
      ExtMenu.hide(e.currentTarget);
    });
    if (elemSub !== null) {
      elemSub.mouseover(function(e){
        ExtMenu.show(e.currentTarget);
      });
      elemSub.mouseout(function(e){
        ExtMenu.hide(e.currentTarget);
      });
    }
  },
  
  show: function(target) {
    if (this.lock)
      return;
    var i = this.findElement(target);
    if (i === false)
      return;
    if (this.current !== null && this.current != i) {
      this.loadNext = i;
      this.hide(null);
      return;
    }
    if (this.hideTimeout != null) {
      clearTimeout(this.hideTimeout);
      this.hideTimeout = null;
    }
    this.current = i;
    this.elements[i].main.addClass('marked');
    if (this.elements[i].sub !== null)
      this.elements[i].sub.show();
  },
  
  hide: function(target) {
    if (this.lock)
      return;
    if (target !== null) {
	    var i = this.findElement(target);
	    if (i !== false && this.loadNext == i)
	      this.loadNext = null;
    }
    if (this.hideTimeout != null) {
      clearTimeout(this.hideTimeout);
      this.hideTimeout = null;
    }
    this.hideTimeout = setTimeout(function(){ ExtMenu.hideAll(); }, this.config.hideTimeoutMilis);
  },
  
  hideAll: function() {
    for (var i=0; i<this.elements.length; i++) {
	  this.elements[i].main.removeClass('marked');
	  if (this.elements[i].sub !== null)
	    this.elements[i].sub.hide();
	}
    
    this.current = null;
    if (this.loadNext !== null) {
      var tmp = this.elements[this.loadNext].main.get(0);
      this.loadNext = null;
      setTimeout(function(){ ExtMenu.show(tmp); }, 1);
    }
    if (this.hideTimeout != null) {
      clearTimeout(this.hideTimeout);
      this.hideTimeout = null;
    }
  },
  
  findElement: function(element) {
    for (var i=0; i<this.elements.length; i++) {
      if ( (this.elements[i].main.get(0) == element) || (this.elements[i].sub !== null && this.elements[i].sub.get(0) == element) )
        return i;
    }
    return false;
  }
}

$(function(){
  $('#menu > a').each(function(index){
    switch(index) {
      case 0:
        ExtMenu.add($(this), $('#warstwa'));
        break;
      case 1:
        ExtMenu.add($(this), $('#warstwa_info'));
        break;
      default:
        ExtMenu.add($(this), null);
        break;
    }
  });
  ExtMenu.init();
});

