// Simple Google Maps

	var marker;
	
	var markers = [];

    // obiekt mapy
	var map = null;
	
	var geocoder;
	
	var gadata;
	
	var bounds;
	
	var addr;

	function google_map(element_id) {

      if (GBrowserIsCompatible()) {
      	// tworzymy mape
        map_constainer = document.getElementById(element_id);
      	
        map = new GMap2(map_constainer);

        // dodajemy kontrolki:
		// - suwak powiekszenia i polozenia mapy
        map.addControl(new GLargeMapControl());
        
        //map.setMapType(G_HYBRID_MAP); 

        // - minimapka w prawym dolnym rogu
        map.addControl(new GOverviewMapControl());
		  geocoder = new GClientGeocoder();
      }
    }

    // funkcja dodajaca marker na mapie
    function addMarker(latlng, desc) {
		marker = new GMarker(latlng);

		marker.desc = desc;//+"<br /><a onclick='removeMarker("+markers.length+");'>usuń marker</a>";

		// co sie dzieje po kliknieciu na ktorys z markerow?
		GEvent.addListener(marker, "click", function() {
			// wyswietlamy opis, w ktorym moze znalezc sie html
			//this.openInfoWindowHtml(this.desc);
			this.openInfoWindowHtml((addr!=undefined?"Ten punkt zostanie przypisany do Twojego obiektu: <br>"+addr:this.desc));
			$('#position').attr('value', this.getLatLng().toUrlValue());
			$('#info').text('');
      	});

		// co sie dzieje po najechaniu na ktorys z markerow?
		GEvent.addListener(marker, "mouseover", function() {
			// wyswietlamy opis, w ktorym moze znalezc sie html
			//this.openInfoWindowHtml(this.desc);
      	});

      	/*GEvent.addListener(marker, "mouseout", function() {
			// zamykamy okienko z opisem
			this.closeInfoWindow();
      	});*/

      	// dodajemy marker na mapie
      	map.addOverlay(marker);

      	return marker;
	}
	
/*	function addAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
	       if (!point) {
    	    		$('#info').html("<input type=\"hidden\" name=\"error\" value=\"1\"><div class=\"note note-error\"><div class=\"inner\"><p>Adres \"" + address + "\" nie został znaleziony</p></div></div>");
       	 } else {
       			$('#info').html("");
       	 		if (marker)
       	 			map.removeOverlay(marker);
       	   	addMarker(point, address);
					map.setCenter(point,16);
           }
          }
        );
      }
    }*/
    
    function addAddress_a(address, adata)
    {
    	if (markers.length>0)
	    for (i=0;i<markers.length;i++)
       	 	map.removeOverlay(markers[i]);
       	markers = [];
    	var gadata = adata;
    	//$.get('ajax-pos.php?addr='+address, function(data) {
    	$.ajax({
			url: '/ajax-pos.php?addr='+address,
      		global: false,
      		async: true,
      		type: "GET",
      		data: "",
      		dataType: "html",
      		success: function(data){
	    		//alert(data);
	    		var info = $('#info')[0];//document.getElementById("info");
				info.innerHTML = "";
	    		if (data != '') {
	    			markers.push(addAddress2(address, data));
	    			$('#position').attr('value', data);
	    			addAddress(address, gadata, false);
	    		} else {
	    			//info.innerHTML = "Adres \"" + address + "\" nie został znaleziony";
	    			addAddress(address, gadata, true);
	    		}
    		}
    	});
    }
    
    function addAddress(address, adata, clear) {
		var info = $('#info')[0];//document.getElementById("info");
		info.innerHTML = "";
		if (adata.length>0) {
			addr = address;
		}
		gadata = adata;
      if (geocoder) {
        geocoder.getLocations(
          address,
          function(data) {
          	if (data.Status.code == 602) {
          		if (gadata.length>0)
          		{
          			addAddress(gadata[1] + ', ' + gadata[2], []);
          		} else {
          			if (!clear)
          				info.innerHTML = "Adres \"" + addr + "\" nie został znaleziony";
          		}
          	} else {
	          	if (markers.length>0 && clear != 'undefined' && clear)
	          	{
		          	for (i=0;i<markers.length;i++)
	       	 			map.removeOverlay(markers[i]);
    	   	 		markers = [];
    	   	 	}
       	 		
				bounds = null; bounds = new GLatLngBounds(); 
       	 		cnt = 0;
       	 		var point;
       	 		for (i=0;i<data.Placemark.length;i++)
       	 		{
       	 			place = data.Placemark[i];    
						if (place.AddressDetails.Country != undefined)
						{   
							point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]); 
   	    	  		 		markers.push(addMarker(point, address));
   	    	  		 		bounds.extend(point);
							cnt++;
						}
				}
				if (cnt == 1) {
					$('#position').attr('value', point.toUrlValue());
				} else {
						$('#position').attr('value', '');
					if (cnt == 0) {
						info.innerHTML = "Adres \"" + addr + "\" nie został znaleziony";
						return;
					} else {
						info.innerHTML = "<b>Wybierz na mapie oczekiwaną pozycję dla podanego adresu</b>";
					}
				}
   	    	  	zoom = map.getBoundsZoomLevel(bounds);
   	    	  	map.setCenter(bounds.getCenter(), (zoom<=14?zoom:14)); 
          	}
         }
        );
      }
    }
    
    function addAddress2(address, position) {
		if (position != '') {
			point = GLatLng.fromUrlValue(position); 
			m = addMarker(point, address);
			map.setCenter(point,14);
			return m; 
		} else {
			if (geocoder) {
				geocoder.getLatLng(
					address,
					function(point) {
						if (!point) {
							$('#info').html("<input type=\"hidden\" name=\"error\" value=\"1\"><div class=\"note note-error\"><div class=\"inner\"><p>Adres \"" + address + "\" nie został znaleziony</p></div></div>");
						} else {
							$('#info').html("");
							if (marker)
								map.removeOverlay(marker);
							m = addMarker(point, address);
							map.setCenter(point,14);
							return m;
						}
					}
				);
			}
		}
	}
