MochiKit.DOM.addLoadEvent(function () {

  var handles = [];

  function clearHandles (handles) {
    forEach(handles,function (handle) {
      disconnect(handle);
    });
  }

  function childIsActive (objs) {
    var childactive = false;
    if (!isUndefinedOrNull(objs)) {
      forEach(objs, function (obj){
        if (obj.id == active || childIsActive(obj.subs)) {
          logDebug(obj.target,"childIsActive");
          childactive = true;
        }
      });
    }
    return childactive;
  }

  function showLI (mom,obj) {
    var myclass = "childli";
    if (!isUndefinedOrNull(mom) && mom.id == "menu") {
      myclass="parentli";
    } else if (!isUndefinedOrNull(mom) && mom.id == "menu_0") {
      myclass="subBlogmenuli";
    }
    var picssrc = lang+menuPic["std"];
    if (obj.id == active || childIsActive (obj.subs)) {
      picssrc=lang+menuPic["active"];
    }
    var content = IMG({"alt": obj.target, "width": obj.menupics[picssrc+"_sizeX"], "height": obj.menupics[picssrc+"_sizeY"], "src": "/"+obj.target+"/"+obj.menupics[picssrc], "id": 'menu_pic_'+obj.id},null);
    if (obj.type == "link") {
      content = A(
        { "href": "/"+lang+"/"+obj.file+"/", "target": "popup"},content
      );
    } else if (obj.type == "include") {
      content = A(
        { "href": "/"+lang+"/"+obj.target+"/"},content
      );
    }
    return LI({"class":myclass},SPAN({'id':'menu_'+obj.id},content));
  }

  if (lang == "de") {
    var mymenu = mymenu_de;
  } else {
    var mymenu = mymenu_en;
  }

  var onmouseoveractive = function (obj) {
    if (obj.id != active) {
      handles[obj.id][handles[obj.id].length] = connect('menu_'+obj.id,"onmouseover",partial(function (obj) {
        getElement('menu_pic_'+obj.id).src="/"+obj.target+"/"+obj.menupics[lang+menuPic["hover"]];
        getElement('menu_pic_'+obj.id).height=obj.menupics[lang+menuPic["hover"]+"_sizeY"];
        getElement('menu_pic_'+obj.id).width=obj.menupics[lang+menuPic["hover"]+"_sizeX"];
      },obj));
      handles[obj.id][handles[obj.id].length] = connect('menu_'+obj.id,"onmouseout",partial(function (obj) {
        getElement('menu_pic_'+obj.id).src="/"+obj.target+"/"+obj.menupics[lang+menuPic["std"]];
        getElement('menu_pic_'+obj.id).height=obj.menupics[lang+menuPic["std"]+"_sizeY"];
        getElement('menu_pic_'+obj.id).width=obj.menupics[lang+menuPic["std"]+"_sizeX"];
      },obj));
    }
  }

  var howMuch = function (char,string) {
    var mystring = string;
    var res = 0; var tmp = mystring.indexOf(char);
    while (tmp !== -1) {
      res=res+1;
      mystring = mystring.substr(tmp+1);
      tmp = mystring.indexOf(char);
    }
    logDebug("found '"+res+"' times '"+char+"' in '"+string+"'");
    return res;
  }

  var showMenu = function (mom,objdata,addElem) {
    logDebug("got showMenu with options: mom="+repr(mom)
    +"objdata="+repr(objdata)+"addElem="+repr(addElem));
    var mySpecialClass = "menulu";
    if (!isUndefinedOrNull(mom) && mom.id == "menu_0") {
      mySpecialClass = "subBlogmenulu";
    }
    if (!isUndefinedOrNull(addElem)) {
      var myclass = mySpecialClass;
      if (howMuch("/",objdata[0].target) > 1) {
        myclass = "submenulu";
      }
      replaceChildNodes(mom,addElem,UL({"class":myclass},map(partial(showLI,mom),objdata)));
    } else {
      replaceChildNodes(mom,UL({"class":mySpecialClass},map(partial(showLI,mom),objdata)));
    }
    forEach(objdata,function (obj) {
      if (isUndefinedOrNull(handles[obj.id])) {
        handles[obj.id] = [];
      }
      onmouseoveractive(obj);
      if (!isUndefinedOrNull(obj.subs)) {
        handles[obj.id][handles[obj.id].length] = connect('menu_'+obj.id,"onclick",partial(showSub,obj,mom,objdata,addElem));
      }
    });
  }

  var hideSub = function (obj) {
    noop();
  }

  var showSub = function (obj,mom,objdata,addElem) {
    logDebug("got showSub with options: obj="+repr(obj)+",mom="+repr(mom)
    +"objdata="+repr(objdata)+"addElem="+repr(addElem));
    showMenu(mom,objdata,addElem);
    clearHandles (handles[obj.id]);
    getElement('menu_pic_'+obj.id).src="/"+obj.target+"/"+obj.menupics[lang+menuPic["active"]];
    getElement('menu_pic_'+obj.id).width=obj.menupics[lang+menuPic["active"]+"_sizeX"];
    getElement('menu_pic_'+obj.id).height=obj.menupics[lang+menuPic["active"]+"_sizeY"];
    var oldelem = getElement('menu_'+obj.id).cloneNode(true);
    showMenu(getElement('menu_'+obj.id),obj.subs,oldelem);
  }

  if (active != -1) {

    showMenu(getElement("menu"),mymenu);

    showmenuparentfunc = partial(showMenu,getElement("menu"),mymenu,null);

    connect("closeMenuHandler","onclick",showmenuparentfunc);
    //fixed things won't work with FUCKING ie
    if (navigator.appName.indexOf("Explorer") != -1){
      connect(document.body,"onclick",showmenuparentfunc);
    }
  }
});
