    var mapdata = [];
    var styles = [];
    var map = null;
    var markers = [];
    var markerClusterer = null;
    var jumpAtPosition = false;
    var jumper = null;
    var uri = window.document.location.hash.split('/');
    function getVarFromUri() {
        var rt = uri[0].split('#');
        if (typeof(rt[1]) == 'undefined') {
            rt[1] = 'zobraz-vse';
        }
        return rt[1];
    }
    function switchbox(box) {
        document.getElementById('blue').style.display = "none";
        document.getElementById('green').style.display = "none";
        document.getElementById('orange').style.display = "none";
        document.getElementById('brown').style.display = "none";
        document.getElementById('pink').style.display = "none";
        document.getElementById(box).style.display = "block";
    }
    function switchmap(id) {
        var title = document.getElementById('mapy_title').getElementsByTagName('h1')[0];
        title.innerHTML = 'NAČÍTÁM DATA...';
        document.getElementById('search').value = 'HLEDEJ...';
        GDownloadUrl("/json/ajax/"+id, function(response) {
                    mapdata = eval( '(' + response + ')' );
                    markers = [];
                    uri = window.document.location.hash.split('/');
                    document.getElementById('mapy_lista').className = mapdata.style;
                    var searchEl = document.getElementById("submit");
                    searchEl.removeAttribute("src",0);
                    searchEl.setAttribute("src", "/images/search-" + mapdata.style + '.jpg');
                    title.innerHTML = mapdata.title + ' NALEZENO ' + mapdata.count + 'x';
                    loadMap();
        });
    }
    function sendSearch(searchForm) {
        var title = document.getElementById('mapy_title').getElementsByTagName('h1')[0];
        var titleBackup = title.innerHTML;
        var searchBackup = searchForm.search.value;
        title.innerHTML = 'NAČÍTÁM DATA...';
        window.location.hash=getVarFromUri();
        if (searchForm.search.value == '' || searchForm.search.value == 'HLEDEJ...') {
            alert('Je potřeba zadat vyhledávaný výraz');
            title.innerHTML = titleBackup;
        } else {
            GDownloadUrl("/json/ajax/"+ getVarFromUri() + "?search=" + searchForm.search.value, function(response) {
                        mapdata = eval( '(' + response + ')' );
                        markers = [];
                        document.getElementById('mapy_lista').className = mapdata.style;
                        var searchEl = document.getElementById("submit");
                        searchEl.removeAttribute("src",0);
                        searchEl.setAttribute("src", "/images/search-" + mapdata.style + '.jpg');
                        title.innerHTML = mapdata.title + ' NALEZENO ' + mapdata.count + 'x (hledaný výraz : "' + searchBackup + '")';
                        loadMap();
            });
        }
        searchForm.search.value = 'HLEDEJ...';
    }
    function initialize() {
        if(typeof(uri[1])!='undefined') {
            jumpAtPosition = true;
            jumper = uri[1];
        }
        switchmap(getVarFromUri());
    }
    function loadMap() {
        if(GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById('mapa'));
            map.setCenter(new GLatLng(49.830885,15.597383), 8);
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            var icons= [];
            for (var i = 0; i < 6; ++i) {
                var icon = new GIcon(G_DEFAULT_ICON);
                icon.image = "/images/chart"+ i +".png";
                icons.push(icon);
            }
            for (var i = 0; i < mapdata.count; ++i) {
              var latlng = new GLatLng(mapdata.points[i].latitude, mapdata.points[i].longitude);
          
              markers[markers.length] = createMarker(latlng, icons[mapdata.points[i].kategorie], mapdata.points[i].id);
              if (jumpAtPosition) {
                  if (mapdata.points[i].id == jumper) {
                      map.setCenter(new GLatLng(mapdata.points[i].latitude,mapdata.points[i].longitude), 15);
                  }
              }
            }
            refreshMap();
        }
    }
    function refreshMap() {
        if (markerClusterer != null) {
            markerClusterer.clearMarkers();
        }
        var zoom = 15;
        var size = 60;
        var style = 2;
        markerClusterer = new MarkerClusterer(map, markers, {maxZoom: zoom, gridSize: size, styles: mapdata.style});
    }
    function createMarker(latlng,ico,id) {
        var marker = new GMarker(latlng, {icon: ico});
        
        GEvent.addListener(marker, "click", function () {
            GDownloadUrl("/getinfo/"+id, function(data) {
                    var output = eval( '(' + data + ')' ); 
                    marker.openInfoWindowHtml(output.html);
                    eval(output.js);
                    window.location.hash=getVarFromUri() + '/' + id;
            });}
        )
        if (jumpAtPosition) {
            if (id == jumper) {
                GDownloadUrl("/getinfo/"+id, function(data) {
                    var output = eval( '(' + data + ')' );
                    marker.openInfoWindowHtml(output.html);
                    eval(output.js);
                   //marker.openInfoWindowHtml(data);
                });
            }
        }
      return marker;
    }
    function inputEnter(element, val, leave) {
            if (leave == undefined)
                    leave = '';
            if (element.value == val) {
                    element.value = leave;
            }
    }

    function inputLeave(element, val, leave) {
            if (leave == undefined)
                    leave = '';
            if (element.value == leave) {
                    element.value = val;
            } else if (element.value == '') {
                    element.value = val;
            }
    }