    var map;
    var mgr;
    var gdir;
    var icons = {};
    var allmarkers = [];
    var initCenter = new GLatLng(54.52, 15.25);
    var initZoom = 2;
    var YSLIDERLENGTH = 62;
    var MAXZOOM = 17;    

    function gmap_load() {
      if (GBrowserIsCompatible())
      {
        map = new GMap2(document.getElementById("map"),{size:new GSize(670,307)});
        map.setMapType(G_HYBRID_MAP);
        //map.addControl(new GLargeMapControl());
        //map.addControl(new GOverviewMapControl());
        map.addControl(new NKBControl());
        map.setCenter(initCenter, initZoom);
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
		GEvent.addListener(map, "zoomend", function(oldZoomLevel, newZoomLevel) {
			if(newZoomLevel > MAXZOOM) map.setZoom(MAXZOOM);			
		});        
        
        mgr = new MarkerManager(map, {trackMarkers:true});
        window.setTimeout(setupOfficeMarkers, 0);
        window.setTimeout(setupCountryChooser, 0);
        gdir = new GDirections(map);
        GEvent.addListener(gdir, "error", handleErrors);
      }
    }

    function getIcon(images) {
      var icon = null;
      if (images) {
        if (icons[images[0]]) {
          icon = icons[images[0]];
        } else {
          icon = new GIcon();
          icon.image = "images/" 
              + images[0] + ".png";
          var size = iconData[images[0]];
          icon.iconSize = new GSize(size.width, size.height);
          icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
          icons[images[0]] = icon;
        }
      }
      return icon;
    }

    function setupOfficeMarkers() {
      allmarkers.length = 0;
      for (var i in officeLayer) {
        var layer = officeLayer[i];
        var markers = [];
        for (var j in layer["places"]) {
          var place = layer["places"][j];
          var icon = getIcon(place["icon"]);
          var title = place["name"];
          var posn = new GLatLng(place["posn"][0], place["posn"][1]);
          var marker = createMarker(posn,title,icon);       
          markers.push(marker);
          allmarkers.push(marker);
        }
        mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
      }
      mgr.refresh();
    }
    
    
    function setupCountryChooser() {
        var item = officeLayer[0];
        var myOptions = {};
        for(var j in item["places"])
        { 
           myOptions[item["places"][j]["posn"]] = item["places"][j]["name"];            
        }
        $("#countryKS").addOption(myOptions,false);
        $('.inputSelectGmaps').selectbox();
		$('#countryKS_container').click(function () {
			KSFinder($('#countryKS').val(), $('#countryKS_input').val());
		});        
    }
    
  
    function createMarker(posn, title, icon) {
      var marker = new GMarker(posn, {title: title, icon: icon, draggable:false, clickable:true});
          GEvent.addListener(marker, 'click', function(){ 
          
       		var item = officeLayer[1];
       		var current = '';
	        for(var i in item["places"])
	        {
	        	if(title == item["places"][i]['name']){
	        		current = item["places"][i];
	        	}
	        }
	        if(current){
	        	/*
	        	var popup_imgs = '';
	        	if(current["bilder"]){
	        		var class_pic = "gmapPopupPic";
	        		var pic_count = 0; var num = 0;
		        	for(var j in current["bilder"][0]) pic_count++;
		        	for(var j in current["bilder"][0]){
		        		num++;		        	
		        		if(pic_count == num) class_pic = "gmapPopupPicLast";
		        		else class_pic = "gmapPopupPic";
		        		popup_imgs += '<div class="'+class_pic+'"><img src="'+current["bilder"][0][j]+'" border="0"></div>';
		        	}
	        	}
	        	*/
	        	var href = ""; var email = "";
	        	if(current['email']!=""){
	        		email = '<a href="mailto:'+current['email']+'" class="whiteGeorgia11">&raquo;&nbsp;'+current['email']+'</a><br>';
	        	}
	        	if(current['href']!=""){
	        		href = '<a href="'+current['href']+'" class="whiteGeorgia11" target="_blank">&raquo;&nbsp;'+current['href']+'</a>';
	        	}	        	
            	$("#map_popup").html('<script type="text/javascript">$("#fromAddress").focus(function () {$(this).val("");$("#fromAddress").removeClass("error");});</script><div id="gmapPopupConteiner"><div id="gmapPopupButton"><a href="javascript:void(1);" onclick="$(\'#map_popup\').hide();"><img src="images/gmaps_popup_close.gif" border="0"></a></div><div id="gmapPopupContent"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td height="10" colspan="3"><img src="images/blank.gif" border="0" height="10"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="whiteGeorgia11" align="left" valign="top"><table width="460" border="0" cellpadding="0" cellspacing="0"><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="greenGeorgia12">'+current['name']+'</td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="10" colspan="3"><img src="images/blank.gif" border="0" height="10"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td height="105" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="50%" class="whiteGeorgia11 line15" valign="top">'+current['subline']+'</td><td width="50%" class="whiteGeorgia11 line15" valign="top" nowrap="nowrap">'+current['text']+'<br><br>'+email+href+'</td></tr></table></td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="1" colspan="2" class="contentBlock"><img src="images/blank.gif" border="0" height="1"></td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="15" colspan="3"><img src="images/blank.gif" border="0" height="15"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="greenGeorgia11">Get Directions:</td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="12" colspan="3"><img src="images/blank.gif" border="0" height="12"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td colspan="2"><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="128"><input type="text" size="25" id="fromAddress" class="inputText" name="from" style="width:126px;" value="from (start)"/></td><td width="136"><input type="text" readonly="readonly" class="inputText" style="width:126px;" value="to ('+current['ort']+')" /><input type="hidden" size="25" id="toAddress" class="inputText" name="to" value="'+current['posn']+'" /></td><td align="left"><a href="#" onclick="setDirections($(\'#fromAddress\').val(), $(\'#toAddress\').val(), \'esp\'); return false">&nbsp;go!&nbsp;&raquo;</a></td></tr></table></td></tr><tr><td height="12" colspan="3"><img src="images/blank.gif" border="0" height="12"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="whiteGeorgia11 line15" id="distance"></td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="whiteGeorgia11 line15" id="duration"></td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr></table></td></tr><tr><td height="10" colspan="3"><img src="images/blank.gif" border="0" height="10"></td></tr></table></div></div>');
            	$("#map_popup").show();
            }else{
		        var item = officeLayer[0];
		        for(var j in item["places"])
		        { 
		        	if(title == item["places"][j]["name"]){
		        		var c = item["places"][j]["posn"]+"";
		        		c = c.split(",");
		            	map.setCenter(new GLatLng(c[0],c[1]),5);
		        	}           
		        }            
            }
            //$("#map_popup").load("kite_school.html", function(){ 
            //	$("#map_popup").show();
            //});
          });  
      return marker;
    }

    function deleteMarker() {
      var markerNum = parseInt(document.getElementById("markerNum").value);
      mgr.removeMarker(allmarkers[markerNum]);
    }
   
    function clearMarkers() {
      mgr.clearMarkers();
    }
   
    function reloadMarkers() {
      setupOfficeMarkers();
    }
    
    function KSFinderAddress(c)
    {
      if(c=="")
      {
        map.setCenter(initCenter, initZoom);
      }else{
        c = c.split(",");
        map.setCenter(new GLatLng(c[0],c[1]),14);
      }  
    }    
    
	var sort_by = function(field, reverse, primer){
	
	   reverse = (reverse) ? -1 : 1;
	
	   return function(a,b){
	
	       a = a[field];
	       b = b[field];
	
	       if (typeof(primer) != 'undefined'){
	           a = primer(a);
	           b = primer(b);
	       }
	
	       if (a<b) return reverse * -1;
	       if (a>b) return reverse * 1;
	       return 0;
	
	   }
	}     
    
    function KSFinder(c, land)
    {
      if(c=="")
      {
        map.setCenter(initCenter, initZoom);
        $("#KSFinder_adressList").html("");         
      }else{
        c = c.split(",");
        map.setCenter(new GLatLng(c[0],c[1]),5);
        
        var item = officeLayer[1];
        var address_list = '<div class="KSFinder_adress"><table width="464" border="0" cellpadding="0" cellspacing="0"><tr><td height="1" class="contentBlock"><img src="images/blank.gif" border="0" height="1"></td></tr></table></div>';
        var places = item["places"];
        places.sort(sort_by('plz', false, function(a){return a.toUpperCase()}));

        for(var j in places)
        { 
        	if(places[j]['land'] == land){
        		var item2 = officeLayer[1];
        		var posn = '';
		        for(var i in item2["places"])
		        {
		        	if(places[j]['name'] == item2["places"][i]['name']){
		        		posn = item2["places"][i]['posn'];
		        	}      	
		        }
	        	var href = ""; var email = "";
	        	if(places[j]['email']!=""){
	        		email = '<a href="mailto:'+places[j]['email']+'" class="whiteGeorgia11">&raquo;&nbsp;'+places[j]['email']+'</a><br>';
	        	}
	        	if(places[j]['href']!=""){
	        		href = '<a href="'+places[j]['href']+'" class="whiteGeorgia11" target="_blank">&raquo;&nbsp;'+places[j]['href']+'</a>';
	        	}		        
	        	address_list+='<div class="KSFinder_adress"><table width="464" border="0" cellpadding="0" cellspacing="0"><tr><td height="14" colspan="3"><img src="images/blank.gif" border="0" height="14"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td class="greenGeorgia12i">'+places[j]['name']+'</td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="10" colspan="3"><img src="images/blank.gif" border="0" height="10"></td></tr><tr><td width="10"><img src="images/blank.gif" border="0" width="10"></td><td><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="50%" class="whiteGeorgia11" valign="top">'+places[j]['subline']+'</td><td width="50%" class="whiteGeorgia11" valign="top">'+places[j]['text']+'<br><br>'+email+href+'</td></tr><tr><td height="25" colspan="2"><img src="images/blank.gif" border="0" height="25"></td></tr><tr><td colspan="2" class="whiteGeorgia11" valign="bottom" align="right"><a href="#" class="whiteGeorgia12i" onclick="KSFinderAddress(\''+posn+'\');">show on map &raquo;</a></td></tr></table></td><td width="10"><img src="images/blank.gif" border="0" width="10"></td></tr><tr><td height="25" colspan="3" class="contentBlock"><img src="images/blank.gif" border="0" height="25"></td></tr></table></div>';
        	}
        }    
        $("#KSFinder_adressList").html(address_list); 
		if(jQuery.browser.msie && jQuery.browser.version<7){
			$('.textContent').removeClass("greyBg");
			$('.textContent').addClass("greyBg");
		}        
      }  
    }    
    
    function setDirections(fromAddress, toAddress, locale) {
      if(locale=='ger')
      {
         locale='de';
      }else if(locale=='rus'){
         locale='ru';
      }else{
         locale='en';
      }
      gdir.clear();
      gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
      GEvent.addListener(gdir,"load", function() {
      	$("#fromAddress").removeClass("error");
      	$("#distance").html('The distance is ' + gdir.getDistance().html);
      	$("#duration").html('About ' + gdir.getDuration().html);
      });
                            
    }    
    
    function handleErrors(){
	   
	   if (gdir.getStatus().code == G_GEO_SUCCESS)
	     $("#fromAddress").removeClass("error");   
	   else if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
	     $("#fromAddress").addClass("error");
	   }
	   /*
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.\nError code: " + gdir.getStatus().code);
	   */
	}
    
    
    
    function NKBControl() {
    
    }
    
    NKBControl.prototype = new GControl();

    // Creates a one DIV for each of the buttons and places them in a container
    // DIV which is returned as our control element. We add the control to
    // to the map container and return the element for the map class to
    // position properly.
    NKBControl.prototype.initialize = function(map) {
      
      this.map = map;
      var that=this;
      var container = document.createElement("div");
      container.style.width='35px';
      container.style.height='175px';
      
      var containerNav = document.createElement("div");
      containerNav.style.width="32px";
      containerNav.style.height="32px";
      containerNav.style.border="1px solid #576ba1";
      containerNav.style.backgroundColor="#1a1a1a";
      containerNav.style.backgroundImage="url(images/gmaps/gmaps_control_hgnavi.gif)";
      
      
      var NavHandDiv = document.createElement("img");
      NavHandDiv.src = "images/gmaps/gmaps_control_hand.gif";
      NavHandDiv.style.position="absolute";
      NavHandDiv.style.top="12px";
      NavHandDiv.style.left="12px";
      NavHandDiv.style.cursor = "pointer";
      containerNav.appendChild(NavHandDiv);
      GEvent.addDomListener(NavHandDiv, "click", function() {
         map.setCenter(initCenter, initZoom);
      });        
      
      var NavUpDiv = document.createElement("img");
      NavUpDiv.src = "images/gmaps/gmaps_control_pfeil_oben.gif";
      NavUpDiv.style.position="absolute";
      NavUpDiv.style.top="7px";
      NavUpDiv.style.left="13px";
      NavUpDiv.style.cursor = "pointer";
      containerNav.appendChild(NavUpDiv);
      GEvent.addDomListener(NavUpDiv, "click", function() {
         map.panDirection(0,1);
      });      
      
      
      var NavDownDiv = document.createElement("img");
      NavDownDiv.src = "images/gmaps/gmaps_control_pfeil_unten.gif";
      NavDownDiv.style.position="absolute";
      NavDownDiv.style.top="22px";
      NavDownDiv.style.left="13px";
      NavDownDiv.style.cursor = "pointer";
      containerNav.appendChild(NavDownDiv);
      GEvent.addDomListener(NavDownDiv, "click", function() {
         map.panDirection(0,-1);
      }); 
      
      
      var NavLeftDiv = document.createElement("img");
      NavLeftDiv.src = "images/gmaps/gmaps_control_pfeil_links.gif";
      NavLeftDiv.style.position="absolute";
      NavLeftDiv.style.top="13px";
      NavLeftDiv.style.left="7px";
      NavLeftDiv.style.cursor = "pointer";
      containerNav.appendChild(NavLeftDiv);
      GEvent.addDomListener(NavLeftDiv, "click", function() {
         map.panDirection(1,0);
      });      
      
      
      var NavRightDiv = document.createElement("img");
      NavRightDiv.src = "images/gmaps/gmaps_control_pfeil_rechts.gif";
      NavRightDiv.style.position="absolute";
      NavRightDiv.style.top="12px";
      NavRightDiv.style.left="22px";
      NavRightDiv.style.cursor = "pointer";
      containerNav.appendChild(NavRightDiv);
      GEvent.addDomListener(NavRightDiv, "click", function() {
         map.panDirection(-1,0);
      });      
      
                    
      
      var containerZoom = document.createElement("div");
      containerZoom.style.width="32px";
      containerZoom.style.height="135px";
      containerZoom.style.position="absolute";
      containerZoom.style.top="40px";
      containerZoom.style.left="0px";
      containerZoom.style.border="1px solid #576ba1";
      containerZoom.style.backgroundColor="#1a1a1a";  
        
      var zoomInDiv = document.createElement("img");
      zoomInDiv.src = "images/gmaps/gmaps_control_plus.gif";
      zoomInDiv.style.cursor = "pointer";
      containerZoom.appendChild(zoomInDiv);
      GEvent.addDomListener(zoomInDiv, "click", function() {
         map.zoomIn();
      });
      
      
      
      var zoomScrollerDiv = document.createElement("div");
      zoomScrollerDiv.style.width="32px";
      zoomScrollerDiv.style.height="83px";
      zoomScrollerDiv.style.backgroundImage = "url('images/gmaps/gmaps_control_hgslider.gif')";
      
      
      this.maske = document.createElement("div"); 
      this.maske.style.position="absolute";
      this.maske.style.top = "25px";
      this.maske.style.left = "12px";
      this.maske.style.width="4px";
      this.maske.style.height="83px";
      
      zoomScrollerDiv.appendChild(this.maske);
      
      this.knob = document.createElement("img"); 
      this.knob.src= "images/gmaps/gmaps_control_scroll_bar.gif";
      
      this.maske.appendChild(this.knob);
      
      this.slide=new GDraggableObject(this.knob, {container:this.maske, left:0, top: 54});        
      
      containerZoom.appendChild(zoomScrollerDiv);      
     

      
      var zoomOutDiv = document.createElement("img");
      zoomOutDiv.src = "images/gmaps/gmaps_control_minus.gif";
      zoomOutDiv.style.cursor = "pointer";
      containerZoom.appendChild(zoomOutDiv);
      GEvent.addDomListener(zoomOutDiv, "click", function() {
         map.zoomOut();
      });       
      
      container.appendChild(containerNav);
      container.appendChild(containerZoom);
    

      map.getContainer().appendChild(container);
      
      // Listen for other things changing the zoom level and move the slider
      GEvent.addListener(map, "zoomend", function(a,b) {that.setSlider(b)});
      // Listen for the slider being moved and set the zoom level
      GEvent.addListener(this.slide, "dragend", function() {that.setZoom()});      
      
      return container;
    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
    NKBControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
    }
    
    // This function positions the slider to match the specified zoom level ==    
    NKBControl.prototype.setSlider = function(zoom) {
        var top = - Math.round(((zoom-MAXZOOM)*YSLIDERLENGTH)/MAXZOOM);
        //alert(top);
        this.slide.top = top;
        this.knob.style.top = top+"px";
    }

    // This function reads the slider and sets the zoom level ==
    NKBControl.prototype.setZoom = function() {
        
        var z= MAXZOOM - Math.round(this.slide.top*MAXZOOM/YSLIDERLENGTH);
        this.map.setZoom(z);
    }