application.mtk.menu.marktbearbeitung = function(map) {
   var static_host = "http://static.toursprung.com";
   var that = map.DZTMenu = this;
   var checkAll = false;
   var omitRefresh = false;
   var dzt_vartaguide_config = { filter: { language: MTK.language == "de" ? "de" : "en" }, clusters: true, handleInfoWindowClick: false, markerLimitPerTile: 3, icon: { url: static_host + "/images/dzt/marker/vartaguide.png", size: [35, 50], anchor: [17, 50] }, shadow: { url: static_host + "/images/dzt/marker/shadow.small.png", size: [47, 61], anchor: [20, 55] } };
   var wetter_com_config = { clusters: false, template: "dzt", markerLimitPerTile: 1, handleInfoWindowClick: false, icon: function(item, map) { return { url: item.icon.image.replace("/images/weather", "/images/dzt/weather"), size: [70,70], anchor: [35,35] } } };
//   var parentIds = jQuery.map([83,84,85], function(p) { return parseInt(p) });
   var parentIds = jQuery.map([], function(p) { return parseInt(p) });
   
   map.config.resources.dzt_pois.filter = map.config.resources.dzt_pois.filter || {};
   map.config.resources.dzt_pois.filter.theme = map.config.resources.dzt_pois.filter.theme || [];
   
   if (!map.config.resources.dzt_pois.filter.theme.length) {
      checkAll = true;
      map.config.resources.dzt_pois.filter.theme = parentIds;
      map.config.resources.dzt_vartaguide = dzt_vartaguide_config;
      map.config.resources.wetter_com = wetter_com_config;
   }
   
   // convert to parent theme-ids
   var themes = [];
   if (map.config.resources.dzt_pois.convertToParentIds != false) {
      jQuery.each(map.config.resources.dzt_pois.filter.theme, function(i, t) {
         var parent = parseInt(t);
         jQuery.each(MTK.DZTThemes2[t] || [], function(i, p) {
            if (jQuery.inArray(parseInt(p), parentIds) != -1) parent = parseInt(p);
         });
         if (jQuery.inArray(parent, themes) == -1) themes.push(parent);
      });
      map.config.resources.dzt_pois.filter.theme = themes;      
   } else {
      map.config.resources.dzt_pois.filter.theme = themes =
         jQuery.map(map.config.resources.dzt_pois.filter.theme, function(t) { return parseInt(t) });
   }

   // city-hack
   //if (jQuery.inArray(77001, themes) == -1) themes.unshift(77001);
   
   var isChecked = function(theme, isTopLevel) {
      if (checkAll) return isTopLevel ? 0 : 1;
      var _themes = map.config.resources.dzt_pois.filter.theme;
      for (var i=0; i<_themes.length; i++) {
         for (var j=0; j<theme.length; j++) {
            if (_themes[i] == theme[j]) return true;            
         }
      }
      return false;
   };
   
	var uncheckAll = function() {
		for (var i in treemenu) {
			treemenu[i]._checked = false;
			treemenu[i].find(".mtk-treemenu-checkbox").attr("src", static_host + "/images/t.gif");
		}
	}

   var menu = jQuery("<div />").attr("id", "mtk-dzt-menu-content-1");
   var treemenu = new MTK.TreeMenu(menu, [{
      title: "Marktbearbeitung &amp; Aktivitäten",
      icon: null,
      checked: isChecked([83,84], true),
      children: [{
         name: "83",
         title: "Auslandsvertretungen",
         checkbox: [static_host + "/images/t.gif", static_host + "/images/dzt/menu/check.png"],
         icon: static_host + "/custom/dzt_pois/icons/83.small.png",
         checked: isChecked([83]),
         onclick: function(bool, cb) { switchTheme(83, bool, cb) }
      }, {
         name: "84",
         title: "Marketing- &amp; Vertriebsagenturen",
         checkbox: [static_host + "/images/t.gif", static_host + "/images/dzt/menu/check.png"],
         icon: static_host + "/custom/dzt_pois/icons/84.small.png",
         checked: isChecked([84]),
         onclick: function(bool, cb) { switchTheme(84, bool, cb) }
      }/*, {
    	  name: "85",
    	  title: "Regionalmanagement",
    	  checkbox: [static_host + "/images/t.gif", static_host + "/images/dzt/menu/check.png"],
    	  icon: static_host + "/custom/dzt_pois/icons/85.small.png",
    	  checked: isChecked([85]),
    	  onclick: function(bool, cb) { switchTheme(85, bool, cb) }
      }/*, {
    	  name: "86",
    	  title: "Destinationsmanagement",
    	  checkbox: [static_host + "/images/t.gif", static_host + "/images/dzt/menu/check.png"],
    	  icon: static_host + "/custom/dzt_pois/icons/86.small.png",
    	  checked: isChecked(86),
    	  onclick: function(bool, cb) { switchTheme(86, bool, cb) }
      }*/]
   }], {
      opened: static_host + "/images/dzt/menu/arrow.open.png",
      closed: static_host + "/images/dzt/menu/arrow.closed.png"
   });
   
   var callbacks = [];
   google.maps.event.addListener(map, "refreshDone", function() {
      jQuery.each(callbacks, function(i, c) { c() });
      callbacks = [];      
   });
   
   var switchTheme = function(theme, bool, cb) {
      // create theme filter
      var _themes = map.config.resources.dzt_pois.filter.theme;
      if (bool) {
         if (jQuery.inArray(theme, _themes) == -1) _themes.push(theme);
      } else {
         _themes = jQuery.grep(_themes, function(t) { return t != theme });
      }
      map.config.resources.dzt_pois.filter = { theme: _themes };
      switchResource(true, "dzt_pois", map.config.resources.dzt_pois, cb);
   };
   
   var switchResource = function(bool, name, config, cb) {
      var resources = map.config.resources;
      if (bool) resources[name] = config;
      else delete(resources[name]);
      if (cb) callbacks.push(cb);
      if (!omitRefresh) map.refresh();
   };
      
   that.switchTab = function(num) {
      jQuery("#mtk-dzt-menu-content-1, #mtk-dzt-menu-content-2, #mtk-dzt-menu-content-3, #mtk-dzt-menu-content-4").hide();
      jQuery(".mtk-dzt-menu-tab").removeClass("mtk-dzt-menu-tab-selected");
      jQuery("#mtk-dzt-menu-content-" + num).show();
      jQuery("#mtk-dzt-menu-tab-" + num).addClass("mtk-dzt-menu-tab-selected");
   };
   
   that.showTooltip = function(num) {
     jQuery("#mtk-dzt-menu-tooltip-tab-" + num).show();
   };

   that.hideTooltip = function(num) {
     jQuery("#mtk-dzt-menu-tooltip-tab-" + num).hide();
   };

   // var location = jQuery("<div />").attr("id", "mtk-dzt-menu-content-2").html("FIXME!").hide();
   map.DZTPlanner = new MTK.DZTRoutePlanner(map);
   map.DZTSearch = new MTK.DZTSearch(map);

   var container = jQuery("<div />").addClass("mtk-dzt-menu")
      .append(jQuery("<div />").addClass("mtk-dzt-menu-tabs")
         .append(jQuery("<div />").addClass("mtk-dzt-menu-tab mtk-dzt-menu-tab-selected").attr("id", "mtk-dzt-menu-tab-1").css('position','relative')
            .append(jQuery("<img />").attr("src", static_host + "/images/dzt/menu/tab1.png").bind({
               click: function() { that.switchTab(1) },
               mouseover: function() { that.showTooltip(1) },
               mouseout: function() { that.hideTooltip(1) }
            })).append(jQuery('<div id="mtk-dzt-menu-tooltip-tab-1" class="mtk-dzt-menu-tooltip"><div class="mtk-dzt-menu-tooltip-left"><div class="mtk-dzt-menu-tooltip-content">' + MTK.i18n.dzt_themen.replace(/\s/g,"&nbsp;") + '</div></div></div>')))
         // .append(jQuery("<div />").addClass("mtk-dzt-menu-tab").attr("id", "mtk-dzt-menu-tab-2").css('position','relative')
         //    .append(jQuery("<img />").attr("src", static_host + "/images/dzt/menu/tab2.png").click(function() { that.switchTab(2) })))
         .append(jQuery("<div />").addClass("mtk-dzt-menu-tab").attr("id", "mtk-dzt-menu-tab-3").css('position','relative')
            .append(jQuery("<img />").attr("src", static_host + "/images/dzt/menu/tab3.png").bind({
               click: function() { that.switchTab(3) },
               mouseover: function() { that.showTooltip(3) },
               mouseout: function() { that.hideTooltip(3) }
            })).append(jQuery('<div id="mtk-dzt-menu-tooltip-tab-3" class="mtk-dzt-menu-tooltip"><div class="mtk-dzt-menu-tooltip-left"><div class="mtk-dzt-menu-tooltip-content">' + MTK.i18n.dzt_kartensuche.replace(/\s/g,"&nbsp;") + '</div></div></div>')))
/*
         .append(jQuery("<div />").addClass("mtk-dzt-menu-tab").attr("id", "mtk-dzt-menu-tab-4").css('position','relative')
            .append(jQuery("<img />").attr("src", static_host + "/images/dzt/menu/tab4.png").bind({
               click: function() { that.switchTab(4) },
               mouseover: function() { that.showTooltip(4) },
               mouseout: function() { that.hideTooltip(4) }
            })).append(jQuery('<div id="mtk-dzt-menu-tooltip-tab-4" class="mtk-dzt-menu-tooltip"><div class="mtk-dzt-menu-tooltip-left"><div class="mtk-dzt-menu-tooltip-content">' + MTK.i18n.dzt_routenplaner.replace(/\s/g,"&nbsp;") + '</div></div></div>')))
*/
      )
      .append(jQuery("<div />").addClass("mtk-dzt-menu-top"))
      .append(jQuery("<div />").addClass("mtk-dzt-menu-content")
         .append(menu)
         // .append(location)
         .append(map.DZTSearch.container.attr("id", "mtk-dzt-menu-content-3").hide())
         .append(map.DZTPlanner.container.attr("id", "mtk-dzt-menu-content-4").hide()))
      .append(jQuery("<div />").addClass("mtk-dzt-menu-bottom"));

   // add to map
   container.get(0).index = 2;
   map.gmap.controls[google.maps.ControlPosition.RIGHT_TOP].push(container.get(0));
   
   // add zoom-logic
   var zoom = map.gmap.getZoom();
   var enabled = [];
   var keys = ["83","84","85","86"];
   google.maps.event.addListener(map.gmap, "zoom_changed", function() {
      var z = map.gmap.getZoom();
      if (zoom < 12 && z >=12) {
         omitRefresh = true;
         enabled = jQuery.grep(keys, function(k) { return treemenu[k] && treemenu[k]._checked });
         jQuery.each(keys, function(i, k) {
            if (!treemenu[k]) return;
            if (!treemenu[k]._checked) treemenu[k].trigger("click");
         });
         omitRefresh = false;
         map.refresh();
      }
      if (z < 12 && zoom >= 12) {
         omitRefresh = true;
         jQuery.each(keys, function(i, k) {
            if (!treemenu[k]) return;
            if (treemenu[k]._checked && jQuery.inArray(k, enabled) == -1) treemenu[k].trigger("click");
         });
         omitRefresh = false;
         map.refresh();
      }
      zoom = z;
   });
};

