    var map;
    var layers = new Array();
    var admin;
    var pini;
    var wroot;
    var projname;
    var legenda;
    var legendItemWidth = 230;

    /*
    * Nastaveni z-indexu dle dulezitostu (vychozi stanice ma vetsi dulezitost a bude navrchu)
    */
    function importanceOrder(marker,b) {
      return GOverlay.getZIndex(marker.getPoint().lat()) + marker.lprio*1000000;
    }

    if (GBrowserIsCompatible()) { 
      // arrays to hold copies of the markers and html used by the side_bar
      // because the function closure trick doesnt work there
      var gmarkers = [];

      // A function to create the marker and set up the event window
      function createMarker(point, html, icon, id) {
        // pokud neni nadefinovana ikona, zobrazi se vychozi pro gmaps
        if(icon == '' || icon == null) {
          var marker = new GMarker(point);
        } else { 
          var baseIcon = new GIcon();
          baseIcon.iconSize = new GSize(32,34);
          baseIcon.iconAnchor = new GPoint(16,32);
          baseIcon.infoWindowAnchor = new GPoint(16,0);
          var ti = new GIcon(baseIcon, icon, null, null);
          var marker = new GMarker(point, {icon: ti, zIndexProcess:importanceOrder});
        }
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
          if(admin == 1)
            gotoStepThree(id);
        });
        // save the info we need to use later for the side_bar
        // add a line to the side_bar html
        return marker;
      }
      
      /*
      * This function loads preview of Overlay to indentificate marker in admin-mode
      *
      * @param[string] html code
      */
      function loadTextPreview(text){
        var limit = text.length;
        var buffer = '';
        var i = 0;
        var read = true;
        while( buffer.length < 20 && limit > i ){
          if( text.charAt(i) == '<' )
            read = false;
          if(read)
            buffer += text.charAt(i);
          if( text.charAt(i) == '>' )
            read = true;
          i++;
        }
        return buffer;
      }

      function getLayersToShow(){
        var buffer = "";
        var c = 0;
        $.each($('.show_layers'), function (i,n) {
          if(n.checked)
            if(c == 0)
              buffer += "layers[]=" + n.value;
            else
              buffer += "&layers[]=" + n.value;
          c++;
        });
        return buffer;
      }

      function getLayersToShow_array(){
        var buffer = [];
        var c = 0;
        $.each($('.show_layers'), function (i,n) {
          if(n.checked){
            buffer[c] = n.value;
            c++;
          }
        });
        return buffer;
      }
      
      function init_layers( selected ){
        $('#loading').css('display', 'block');
        jQuery.get( wroot + 'index/layers?proj=' + projname, function(jsondata){
          layers = eval("(" + jsondata + ")");
          getLayersToShow();
          if(admin != 1){
            init_legend(true);
          } else {
            buildStep1Body(selected);
          }
          $('#loading').css('display', 'none');
        }, 'jsonp' );
      }

      /*
      * Loads all availible markers from database using ajax
      *
      */
      function init_markers( layersStr, selected ){  
        $('#loading').css('display', 'block');
        map.getInfoWindow().hide();
        gmarkers = [];
        //alert('sending request on: ' + wroot + 'index/markers?lang=' + pini.lang + '&' + layersStr + '&proj=' + projname);
        jQuery.get( wroot + 'index/markers?lang=' + pini.lang + '&' + layersStr + '&proj=' + projname, function(jsondata){
          mRaw = eval("(" + jsondata + ")");
          map.clearOverlays();
          var i = 0;
          for (var id in mRaw) { 
            i++;
            var point = new GLatLng(mRaw[id].x, mRaw[id].y);
            var marker = createMarker(point, '<div  class="marker_window" style="width: 250px;">' + mRaw[id].html + '</div>', mRaw[id].icon, id);
            marker.lprio = layers[(mRaw[id].layer)].prio;
            map.addOverlay(marker);
            gmarkers.push({ data:marker, layer:mRaw[id].layer, id:id});
            if(admin == 1) {
              //alert("bublina " + id + " ma text " + mRaw[id].html);
              step_2_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">['  + id + ']: ' + loadTextPreview(mRaw[id].html) + '...</a><br />';
            }
            if(selected == id)
              marker.openInfoWindowHtml('<div  class="marker_window" style="width: 250px;">' + mRaw[id].html + '</div>');
          }
          if(admin == 1){
            $("#step_2_body").html(step_2_html);
            $("#mlayer option[@value=" +  + "]").attr('selected', 'selected');
          }
          $('#loading').css('display', 'none');
        }, 'jsonp' );
      }
      
      function repaint_markers(visibleLayers){
        map.clearOverlays();
        for(var mid in gmarkers){
          for(var lid in visibleLayers){
            if(gmarkers[mid].layer == visibleLayers[lid]){
              map.addOverlay(gmarkers[mid].data);
            }
          }
        }
      }
      
      function init_legend(force){
        var buffer = '';
        var reload = false;
        if(legenda == 'raw' && map.getZoom() >= 13){
          reload = true;
          legenda = 'fine';
        }
        if(legenda == 'fine' && map.getZoom() <= 12){
          reload = true;
          legenda = 'raw';
        }
        //alert('nacitam legendu force:'+force+' reload:'+reload);
        if(reload == true || force == true){
          switch(legenda){
            case 'fine':
              for(var id in layers){
                if(layers[id].points > 0){
                  buffer += '<div style="display:none;">\n';
                  if(layers[id].show == 0){ // jedna se o projektovou vrstvu, kterou chci v jemne lgende zobrazit skryte
                    buffer += '<input class="show_layers" type="checkbox" value="' + id + '" name="show_layers" checked="checked">\n';
                  }
                  buffer += '</div>\n';
                }
              }
              buffer += '<table border="0"><tr><td><table width="' + legendItemWidth + 'px">\n';
              var count = 0;
              for(var id in layers){
                if(layers[id].points > 0){
                  if(layers[id].show == 1){ // jedna se o projektovou vrstvu, kterou chci v jemne lgende zobrazit skryte
                    count++;
                    if(count == 4){
                      buffer += '</table></td><td><table width="' + legendItemWidth + 'px">';
                    }
                    buffer += '<tr><td><input class="show_layers" type="checkbox" checked="checked" name="layer[]" value="' + id + '"></td>\n';
                    buffer += '<td><img align="absmiddle" src="' + wroot + 'images/markers/' + layers[id].icon + '">\n';
                    buffer += '-&nbsp;&nbsp;' + locales[layers[id].lname] + '</td></tr>\n';
                  }
                }
              }
              buffer += '</tr></table></td></tr><table>\n';
              break;
            default:
              buffer += '<table width="' + legendItemWidth + 'px">\n';
              for(var id in layers){
                if(layers[id].points > 0){
                  if(layers[id].show == 0){ // jedna se o projektovou vrstvu
                    buffer += '<tr><td><input class="show_layers" type="checkbox" checked="checked" name="layer[]" value="' + id + '"></td>\n';
                    buffer += '<td><img align="absmiddle" src="' + wroot + 'images/markers/' + layers[id].icon + '">\n';
                    buffer += '-&nbsp;&nbsp;' + locales[layers[id].lname] + '</td></tr>\n';
                  }
                }
              }
              buffer += '</table>\n';
              break;
          }
          $('#legend').html(buffer);
          listenLayerClick();
          if(force == true)
            init_markers(getLayersToShow(), pini.bid);
          else
            init_markers(getLayersToShow());
        }
      }
      
      // Display the map, with some controls and set the initial location 
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());
      map.enableScrollWheelZoom();
      map.enableContinuousZoom();
      
      //map.openInfoWindow(map.getCenter(),document.createTextNode("Hello, world"));
      
      // db map init
      admin = 0;
      function map_init_project( adm, pin, wrt, prn){
        
        admin = adm;
        pini = pin;
        wroot = wrt;
        projname = prn;
        legenda = 'raw'; // 0 = vrstva prijektu, 1 = vrstva projektu + body zajmu

        if( admin != 1 ){
          GEvent.addListener(map, "zoomend", function() {
              init_legend();
          });
          init_layers();
          var point = new GLatLng(pini.x, pini.y);
          if(pini.bid != null){
            map.setCenter(point, 14);
          } else {
            map.setCenter(point, 11);
          }
          // put the assembled side_bar_html contents into the side_bar div
          // document.getElementById("side_bar").innerHTML = side_bar_layer;
        } else {
          map.setCenter(new GLatLng(50.08963989159396,14.393291473388672),11);
          init_layers();
          google.maps.Event.addListener(map,"click", function(overlay, latlng) {     
            if (latlng) {
              $('#cox').val(latlng.y);
              $('#coy').val(latlng.x);
            }
          });
          initPalete(1);
        }
      }
                 
      // listeners
      function listenLayerClick(){
        $.each($('.show_layers'), function (i,n) {
          $(n).bind("click", function(e){
            //init_markers(getLayersToShow());
            if(admin != 1)
              repaint_markers(getLayersToShow_array());
          });
        });
      }

      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        GEvent.trigger(gmarkers[i].data, "click");
      }


    }
    
    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

