var minuleUrl = "";
var urlProBodyServer = "";
var geocoder = new GClientGeocoder();
var limitBodu = 0;
var pocetOvetrenychBublin = 0;

function gMapOnMove(){     
 pocetOvetrenychBublin = 0;
    var mapBounds = map.getBounds();
    var southWest = mapBounds.getSouthWest();
    var northEast = mapBounds.getNorthEast();          
    centerPoint = map.getCenter();
    zoomLevel = map.getZoom();
 
    nahrejBody(southWest.y, southWest.x, northEast.y, northEast.x);
}

function AddMapEventHandlers() {
 if(pocetOvetrenychBublin == 0){
  moveEventListener = GEvent.addListener(map, "moveend", function(){gMapOnMove();});
  zoomEventListener = GEvent.addListener(map, "zoomend", function(){gMapOnMove();});
  gMapOnMove();
 }
}

function RemoveMapEventHandlers(){ 
    if(moveEventListener) GEvent.removeListener(moveEventListener);
    if(zoomEventListener) GEvent.removeListener(zoomEventListener);
}

function nahrejBody(minLatitude, minLongitude, maxLatitude, maxLongitude){
 if(minuleUrl != urlProBodyServer){
  map.clearOverlays();
  var urlProBodyServer = urlProBody + "?MinLatitude=" + minLatitude + "&MinLongitude=" + minLongitude + "&MaxLatitude=" + maxLatitude + "&MaxLongitude=" + maxLongitude;
  minuleUrl = urlProBodyServer
  najdiBody(urlProBodyServer);
 }
}

function najdiBody(url){
	var httpRequest;

    if(typeof window.ActiveXObject != 'undefined'){
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        httpRequest = new XMLHttpRequest();
    }
    httpRequest.open("GET", url, true);
    httpRequest.onreadystatechange= function ()
    {
       processRequest(httpRequest)
    };
    httpRequest.send(null);
}

function processRequest(httpRequest){

    if (httpRequest.readyState == 4){
        if(httpRequest.status == 200){
		     if(httpRequest.responseText!="") {

				  var xml = httpRequest.responseXML;
				  var polozky = xml.documentElement.getElementsByTagName("body");
				  var pocet = polozky.length;

				  if(pocet>limitBodu){
						for(var a=0;a<pocet;a++){
							if(a==maxMarkeru) break;
							var bod = polozky.item(a).attributes;
								lat = bod.getNamedItem('lat').nodeValue;
								lon = bod.getNamedItem('lon').nodeValue;
								ikonka = bod.getNamedItem('ikonka').nodeValue;
								icowidth = bod.getNamedItem('icowidth').nodeValue;
								icoheight = bod.getNamedItem('icoheight').nodeValue;
								nazev = bod.getNamedItem('nazev').nodeValue;
								street = bod.getNamedItem('street').nodeValue;
								city = bod.getNamedItem('city').nodeValue;
								odkaz = bod.getNamedItem('url').nodeValue;


								var point = new GLatLng(lat,lon);
								var html = "<a href='"+odkaz+"'><b>"+nazev+"</b></a><br>"+street+"<br>"+ city;
								var blueIcon = new GIcon(G_DEFAULT_ICON);
								blueIcon.iconSize = new GSize(icowidth, icoheight); 
								blueIcon.iconAnchor = new GPoint(14,30); 
								blueIcon.shadowSize = new GSize(0, 0); 
								blueIcon.image = ikonka;
								
								option = { icon:blueIcon };
								if (odkaz != ""){active = true;}else{active = false;} 
								map.addOverlay(zavedBodNaMapu(point, a+1,html,option, active));

						}
				  }else{
					//zadna polozka
				  }
			 }
		}
	}
}

function zavedBodNaMapu(point, cislo,html, option){
 var bodnamape = new GMarker(point, option);
  bodnamape.value = cislo;
 
 GEvent.addListener(bodnamape, "click", function() {
    pocetOvetrenychBublin++;
          RemoveMapEventHandlers();
    bodnamape.openInfoWindowHtml( html, {onCloseFn:function(){ pocetOvetrenychBublin--;AddMapEventHandlers();}  });
 });
 
 return bodnamape;
}


function hledat(id){
	var inp = document.getElementById(id).value;
	if(inp!=""){
		zoomLevel = 14;
		address = inp+" Czech republic";
//		showAddress("Česká republika "+inp);
		  geocoder.getLatLng(
			address,
			function(point) {
			  if (!point) {
				alert(address + " not found");
			  } else {
				map.setCenter(point, zoomLevel);    
			  }
			}
		  );

	}else{
		alert("Nesmí být prázdno!");
	}
}


