window.size = function()
{
  var w = 0;
  var h = 0;

  //IE
  if(!window.innerWidth)
  {
    //strict mode
    if(!(document.documentElement.clientWidth == 0))
    {
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    }
    //quirks mode
    else
    {
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
  }
  //w3c
  else
  {
    w = window.innerWidth;
    h = window.innerHeight;
  }
  return {width:w,height:h};
}

var clsSubmenu = new Class({
  MyIndex: null,
  BorderLeft: null,
  BorderRight: null,
  Entries: new Array(5),
  HRefs: new Array(5),
  
  initialize: function(Element, BorderLeft, BorderRight, MyIndex) {
    this.BorderLeft = BorderLeft;
    this.BorderRight = BorderRight;
    this.MyIndex = MyIndex;
    table = Element.getFirst();
    if($defined(table)) {
      table.getElements("tr").each(function(el, index) {
        textEl = el.getFirst().getFirst().getFirst();
        if($defined(textEl)) {
          this.Entries[index] = textEl.get("text");
          this.HRefs[index] = textEl.get("href");
        }
      }.bind(this));
    }
  }
});

var clsLastMovements = new Class({
  Movements: new Array(15),
  Index: 0,
  Sum: 0,
  SwitchDirectionLimit: 20,
  
  initialize: function() {
    this.Movements.each(function(item, index, array) {
      array[index] = 0;
    });
  },
  
  addMovement: function(Movement) {
    this.Sum -= this.Movements[this.Index];
    this.Movements[this.Index] = Movement;
    this.Sum += Movement;
    this.Index++;
    if(this.Index >= this.Movements.length) this.Index = 0;
  }
});

var clsMenu = new Class({
  ActiveSail: null,
  Submenu1: null,
  Submenu2: null,
  LastPosition: -1,
  Submenues: new Array(),
  BorderLeft: null,
  BorderRight: null,
  LastMovements: new clsLastMovements(),
  LastSubmenuIndex: null,
  HauptmenueDivs: null,
  ActiveHauptmenueDiv: null,
  FadingActive: null,
  
  initialize: function() {
    this.HauptmenueDivs = $$("div.hauptmenueEl");
    this.BorderLeft = this.HauptmenueDivs[0].getCoordinates().left;
    this.BorderRight = this.HauptmenueDivs.getLast().getCoordinates().right;
    this.HauptmenueDivs.each(function(el, index) {
      Coordinates = el.getCoordinates();
      Submenu = new clsSubmenu(el.getLast(), Coordinates.left, Coordinates.right, index);
      this.Submenues.push(Submenu);
      el.removeClass("hauptmenueElNJ");
      a = el.getFirst();
      a.set('morph', {duration: 200});
      if(a.hasClass("hauptmenueAktiv")) {
        el.store("active", true);
        this.ActiveHauptmenueDiv = el;
      }
    }.bind(this));
    
    this.Submenu1 = $("untermenue1");
    this.Submenu1.setStyles({"top": 66 - window.getScroll().y, "display": "block", "opacity": 0});
    this.Submenu1.set("tween", {onComplete: function() {this.FadingActive = null;}.bind(this)});
    this.Submenu1.store("my:offset", -48);

    this.Submenu2 = $("untermenue2");
    this.Submenu2.setStyles({"top": 66 - window.getScroll().y, "display": "block", "opacity": 0});
    this.Submenu2.set("tween", {onComplete: function() {this.FadingActive = null;}.bind(this)});
    this.Submenu2.store("my:offset", -145);
    
    if(Browser.Engine.trident) {
      this.Submenu1.store("my:offset", -68);
      this.Submenu1.getFirst().getElements("tr").each(function(el, index) {
        td = el.getFirst();
        td.removeClass("untermenuepunkt" + (index + 1));
        td.addClass("untermenuepunktIE" + (index + 1));
      }.bind(this));
      this.Submenu2.getFirst().getElements("tr").each(function(el, index) {
        td = el.getFirst();
        td.removeClass("untermenuepunkt2" + (index + 1));
        td.addClass("untermenuepunktIE2" + (index + 1));
      }.bind(this));
    }
    
    body = $$("body")[0];
    body.addEvent("click", function(event) {
      if($defined(this.ActiveSail)) {
        this.hide(this.ActiveSail);
        this.ActiveSail = null;
      }
      if($defined(this.LastSubmenuIndex)) {
        this.restoreMainmenu();
        this.LastSubmenuIndex = null;
      }
    }.bind(this));
    body.addEvent("mousemove", function(event) {
      if($defined(this.LastSubmenuIndex) && (event.page.y > 380 || event.page.x < this.BorderLeft - 30 || event.page.x > this.BorderRight + 30 || event.page.y < 29)) {
        if($defined(this.ActiveSail)) {
          this.hide(this.ActiveSail);
          this.ActiveSail = null;
        }
        this.restoreMainmenu();
        this.LastSubmenuIndex = null;
      }
    }.bind(this));
    $("hauptmenue").addEvent("mousemove", function(event) {
      Found = false;
      this.LastMovements.addMovement(event.page.x - this.LastPosition);
      this.LastPosition = event.page.x;
      this.setPositionFading(event.page.x);
      this.Submenues.each(function(Submenu) {
        if(event.page.x >= Submenu.BorderLeft && event.page.x <= Submenu.BorderRight) {
          Found = true;
          if(this.LastSubmenuIndex != Submenu.MyIndex) {
            this.restoreMainmenu();
            HauptmenueDiv = this.HauptmenueDivs[Submenu.MyIndex];
            HauptmenueDiv.getFirst().morph({"background-color": "#8DD2E7", "color": "#FFFF78"});
            this.LastSubmenuIndex = Submenu.MyIndex;
            if($defined(Submenu.Entries[0]) && Submenu.Entries[0] != "") {
              this.setMenuEntries(this.Submenu1, Submenu, "");
              this.setMenuEntries(this.Submenu2, Submenu, "2");
            }
          }
          if(!$defined(Submenu.Entries[0]) || Submenu.Entries[0] == "") {
            if($defined(this.ActiveSail)) {
              this.hide(this.ActiveSail);
              this.ActiveSail = null;
            }
            return;
          }
          this.showSubmenu(event);
        }
      }.bind(this));
    }.bind(this));
  },
  
  setMenuEntries: function(SubmenuElement, Submenu, Postfix) {
    SubmenuElement.getFirst().getElements("tr").each(function(el, index) {
      td = el.getFirst();
      td.removeEvents("mouseover");
      td.removeEvents("mouseout");
      a = td.getFirst().getFirst();
      if($defined(Submenu.Entries[index])) {
        a.set("text", Submenu.Entries[index]);
        a.set("href", Submenu.HRefs[index]);
        td.addEvent("mouseover", function(event) {
          /*newDiv = new Element("div", { "class": "untermenuepunkt" + Postfix });
          newA = new Element("a", { "href": Submenu.HRefs[index], "text": Submenu.Entries[index], "class": "untermenuepunktJS" });
          newA.inject(newDiv);
          Coordinates = this.getFirst().getCoordinates();
          newDiv.setStyles({ "opacity": 0, "background-image": "url(res/img/Menuepunkt" + Postfix + (index + 1) + "gelb.png)", "position": "absolute", "left": 0, "top": 0 });
          newDiv.inject(this, "bottom");
          newDiv.fade("in");*/
          if(Browser.Engine.trident) {
            this.setStyle("background-image", "url(fileadmin/templates/res/img/MenuepunktIE" + Postfix + (index + 1) + "gelb.png)");
          } else {
            this.setStyle("background-image", "url(fileadmin/templates/res/img/Menuepunkt" + Postfix + (index + 1) + "gelb.png)");
          }
          this.getFirst().getFirst().setStyle("color", "rgb(141, 210, 231)");
        }.bind(td));
        td.addEvent("mouseout", function(event) {
          /*newDiv = this.getLast();
          newDiv.set("tween", { onComplete: function() { this.dispose(); }.bind(newDiv) });
          newDiv.tween("opacity", 1, 0);*/
          if(Browser.Engine.trident) {
            this.setStyle("background-image", "url(fileadmin/templates/res/img/MenuepunktIE" + Postfix + (index + 1) + ".png)");
          } else {
            this.setStyle("background-image", "url(fileadmin/templates/res/img/Menuepunkt" + Postfix + (index + 1) + ".png)");
          }
          this.getFirst().getFirst().setStyle("color", "rgb(255, 255, 255)");
        }.bind(td));
        td.addEvent("click", function(event) {
          event.stop();
            if($defined(this.ActiveHauptmenueDiv)) {
            this.ActiveHauptmenueDiv.store("active", false);
            this.ActiveHauptmenueDiv.getFirst().morph({"background-color": "#FFFF78", "color": "#8DD2E7"});
          }
          this.HauptmenueDivs[Submenu.MyIndex].store("active", true);
          this.HauptmenueDivs[Submenu.MyIndex].morph({"background-color": "#8DD2E7", "color": "#FFFF78"});
          var href;
          if(event.target.hasClass("untermenuepunktJS")) {
            href = event.target.get("href");
          } else {
            href = event.target.getFirst().get("href");
          }
          window.location=href;
        }.bind(this));
      } else {
        a.set("html", "&nbsp;");
        a.set("href", "#");
      }
    }.bind(this));
  },
  
  showSubmenu: function(event) {
    if($defined(this.ActiveSail)) {
      if(this.LastMovements.Sum > this.LastMovements.SwitchDirectionLimit && this.ActiveSail != this.Submenu1) {
        this.ActiveSail = this.Submenu1;
        this.setPosition(event.page.x);
        this.show(this.Submenu1);
        this.hide(this.Submenu2);
      } else if(this.LastMovements.Sum < -this.LastMovements.SwitchDirectionLimit && this.ActiveSail != this.Submenu2) {
        this.ActiveSail = this.Submenu2;
        this.setPosition(event.page.x);
        this.show(this.Submenu2);
        this.hide(this.Submenu1);
      } else {
        this.setPosition(event.page.x);
      }
    } else {
      if(this.LastMovements.Sum < 0) {
        this.ActiveSail = this.Submenu2;
      } else {
        this.ActiveSail = this.Submenu1;
      }
      this.setPosition(event.page.x);
      this.show(this.ActiveSail);
    }
  },
  
  setPositionFading: function(x) {
    if($defined(this.FadingActive)) {
      this.FadingActive.setStyle("left", x + this.FadingActive.retrieve("my:offset"));
    }
  },
  
  setPosition: function(x) {
    this.ActiveSail.setStyle("left", x + this.ActiveSail.retrieve("my:offset"));
  },
  
  show: function(Submenu) {
    Submenu.fade("1");
  },
  
  hide: function(Submenu) {
    this.FadingActive = Submenu;
    Submenu.tween("opacity", 0);
  },
  
  restoreMainmenu: function() {
    if($defined(this.LastSubmenuIndex)) {
      LastHauptmenueDiv = this.HauptmenueDivs[this.LastSubmenuIndex];
      active = LastHauptmenueDiv.retrieve("active");
      if(!$defined(active) || !active) LastHauptmenueDiv.getFirst().morph({"background-color": "#FFFF78", "color": "#8DD2E7"});
    }
  }
});

window.addEvent('domready', function() {
  Menu = new clsMenu();
});
