
		var gmarkers = [];
		var bgmarkers = [];
		var cache_marker = [];
		var is_cache_marker = [];

		var clicks=new Array();
		var clicktimes=0;
		var tmp_clicks=clicks;
		var tmp_clicktimes=clicktimes;
		var tmp_zoom;
		var tmp_center;

    var to_htmls = [];
    var from_htmls = [];
		var isclicked=false;
		var iswindowopen=false;

		var map;
    var geo = new GClientGeocoder(); 
		var center = new GLatLng(51.919438, 19.145136)
		var mgr;
		var om;
		var markc;
		
		var granice;
		var graniceTop;
		var graniceTopLeft;
		var graniceTopRight;
		var graniceLeft;
		var graniceRight;
		var graniceBottomLeft;
		var graniceBottomRight;
		var hint_message=true;
    var allowedBounds;

    function tohere(iv) {
			document.getElementById("direct"+iv).innerHTML = to_htmls[iv];
    }

    function fromhere(iv) {
			document.getElementById("direct"+iv).innerHTML = from_htmls[iv];
    }

		function concato(fid) {
			document.getElementById("saddr_"+fid).value = document.getElementById("to_"+fid).value + ", Polska";  
		}

		function concafrom(fid) {
			document.getElementById("daddr_"+fid).value = document.getElementById("from_"+fid).value + ", Polska";  
		}

		function setClickedFalse() {
			isclicked=false;
		}

		function setWindowOpenFalse() {
			iswindowopen=false;
		}

		function disable_hint() {
			if (hint_message) {
				document.getElementById('searchaddress').value = '';
				document.getElementById('searchaddress').style.color="#000000";
				hint_message=false;
			}
		}


      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
      }



		// Funkcja tworzaca punkt
		function createMarker(point,i,id,icon) {
  		var marker = new GMarker(point,icon);

  		GEvent.addListener(marker, "click", function() {
				if (is_cache_marker[id]==true) { marker.openInfoWindowHtml(cache_marker[id]) }
				else { 
					marker.openInfoWindowHtml("<div style='width: 320px; height: 290px;' id='m"+id+"'>Proszę czekać<br /><img src='./images/wait-1.gif' /></div>");
					GDownloadUrl("getmarkerinfo.php?id="+id, function(data, responseCode) {
						is_cache_marker[id]=true;
						cache_marker[id]="<div style='width: 320px; height: 290px;' id='m"+id+"'>"+data+'<div style="height:60px;" class="directions" id="direct'+i+'">&nbsp;Wskazanie drogi: <a href="javascript:tohere('+i+')">Do tego miejsca</a> - <a href="javascript:fromhere('+i+')">Z tego miejsca</a></div>'+"</div>";
						marker.openInfoWindowHtml(cache_marker[id]);
        // The info window version with the "to here" form open
				        to_htmls[i] = 'Wskazanie drogi: <b>Do tego miejsca</b> - <a href="javascript:fromhere(' + i + ')">Z tego miejsca</a>' +
				           '<br />Adres początkowy:<form action="http://maps.google.com/maps" method="get" target="_blank" onsubmit="javascript:concato('+i+')">' +
				           '<input type="text" SIZE=20 MAXLENGTH=40 id="to_'+i+'" name="saddrtmp" value="" />&nbsp;' +
				           '<INPUT class="directions" value="Pokaż dojazd" TYPE="submit">' +
				           '<input type="hidden" name="saddr" id="saddr_'+i+'" value="" />&nbsp;' +
				           '<input type="hidden" name="daddr" value="'+ point.lat() + ',' + point.lng() + '"/>';

				        from_htmls[i] = 'Wskazanie drogi: <a href="javascript:tohere(' + i + ')">Do tego miejsca</a> - <b>Z tego miejsca</b>' +
				           '<br />Adres końcowy:<form action="http://maps.google.com/maps" method="get" target="_blank" onsubmit="javascript:concafrom('+i+')">' +
				           '<input type="text" SIZE=20 MAXLENGTH=40 id="from_'+i+'" value="" name="daddrtmp" />&nbsp;' +
				  				 '<INPUT class="directions" value="Pokaż dojazd" type="submit">' +
				           '<input type="hidden" name="daddr" id="daddr_'+i+'" value="" />&nbsp;' +
				           '<input type="hidden" name="saddr" value="'+ point.lat() + ',' + point.lng() + '"/>';
					})
				}
			});
		return marker;
		}

 			var howto=[];
 			howto[0]="<div style='line-height: 1.4; font-size: 11px;text-align: center;'><h1 style='font-weight: bold; color: #cc0000; font-size: 14px; text-align: left; padding: 0; margin: 0;'>Konferencje w Polsce</h1><strong style='color: #cc0000; font-size: 14px;'> Szukaj poprzez mapę</strong><br /> <em><strong>Kliknij dwukrotnie lewym przyciskiem</strong></em> myszy nad interesującym Cię obszarem,<br />  aby uzyskać <em><strong>powiększenie</strong></em>. <br />Możesz skorzystać z <span id='plus'>&nbsp;</span> lub  <span id='minus'>&nbsp;</span>  znajdujących się<br />w lewym górnym rogu mapy.</div>";
 			howto[4]="<strong>Klikając lewym przyciskiem</strong> myszy na baloniku uzyskasz informacje o danym miejscu konferencyjnym.<br /><strong>Klikając i przytrzymując lewy przycisk</strong> myszy na mapie możesz ją przesuwać.<br />Aby wrócić do poprzedniego widoku <strong>kliknij prawym przyciskiem</strong> myszy.";
			howto[1]=howto[4];
 			howto[2]=howto[4];
			howto[3]=howto[4];
			howto[5]=howto[4];
			howto[6]=howto[4];

      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Znalazłem";
      reasons[G_GEO_MISSING_ADDRESS]    = "Brakujący adres";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Nieznany adres";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Niedostępny adres: Geolokalizacja danego adresu nie może zostać przeprowadzona z powodów prawnych lub umowy kontraktowej.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Za dużo zapytań: Dzienny limit geokodowania dla tej strony został wyczerpany.";
      reasons[G_GEO_SERVER_ERROR]       = "Błąd serwera: żądanie geokodowania nie może zostać zrealizowane.";


      function showAddress() {
				if (document.getElementById("searchaddress").value!='' && hint_message==false) {
        var qsearch = "Poland, "+document.getElementById("searchaddress").value;
        geo.getLocations(qsearch, function (result)
          {

            if (result.Status.code == G_GEO_SUCCESS) {
               	document.getElementById("message").innerHTML = "";

              var p = result.Placemark[0].Point.coordinates;

								tmp_zoom=map.getZoom();
								tmp_center=map.getCenter();
 								tmp_clicktimes=clicktimes;
								tmp_clicks=clicks;
								map.removeOverlay(granice);
     								map.setZoom(12);
								map.enableDragging();
								clicks[1]=new GLatLng(p[1],p[0]);
 								clicks[2]=new GLatLng(p[1],p[0]);
 								clicks[3]=new GLatLng(p[1],p[0]);
								clicktimes=3;
  							map.panTo(new GLatLng(p[1],p[0]));
            }

            else {
              var reason="Code "+result.Status.code;
              if (reasons[result.Status.code]) {
                reason = reasons[result.Status.code]
              } 
               document.getElementById("message").innerHTML = reason+" dla "+ qsearch;
            }
          }
        );
			}
      }


	function graniceOn() {
		map.addOverlay(granice);
 		map.addOverlay(graniceTop);
 		map.addOverlay(graniceTopLeft);
 		map.addOverlay(graniceTopRight);
 		map.addOverlay(graniceLeft);
 		map.addOverlay(graniceRight);
 		map.addOverlay(graniceBottom);
 		map.addOverlay(graniceBottomLeft);
 		map.addOverlay(graniceBottomRight);
	}

	function graniceOff() {
		map.removeOverlay(granice);
 		map.removeOverlay(graniceTop);
 		map.removeOverlay(graniceTopLeft);
 		map.removeOverlay(graniceTopRight);
 		map.removeOverlay(graniceLeft);
 		map.removeOverlay(graniceRight);
 		map.removeOverlay(graniceBottom);
 		map.removeOverlay(graniceBottomLeft);
 		map.removeOverlay(graniceBottomRight);
	}

	
function checkEquals(point)
{
	for (var i=0; i<gmarkers.length; i++)
	{
		if (gmarkers[i].getLatLng().equals(point) )
			return true;
		else
			return false;
	}
}


 function load() {
   if (GBrowserIsCompatible()) {


	    map = new GMap2(document.getElementById("map"));
		markc = new MarkerClusterer(map);
			map.disableDragging();
			document.getElementById("map").style.cursor="crosshair";
			map.setCenter(center, 6);
			mgr = new GMarkerManager(map, {borderPadding:1});
			var samzoom = new GSmallZoomControl();
			var suwakzoom = new GSmallMapControl();
			map.addControl(samzoom);
 			om = new OverlayMessage(document.getElementById('map'));
			om.SetBackgroundColor("#DDDFE4");
			om.SetBorderColor("#6E6F72");
 			om.Set('Trwa ładowanie mapy...');
 			var X = (map.getBounds().getNorthEast().lat() - map.getBounds().getSouthWest().lat()); 
 			var Y = (map.getBounds().getNorthEast().lng() - map.getBounds().getSouthWest().lng());

				//TOP
// 			var sw = new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng()-Y);
// 			var ne = new GLatLng(map.getBounds().getNorthEast().lat()+X, map.getBounds().getNorthEast().lng()+Y);

				//LEFT
//			var sw = new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getSouthWest().lng()-Y);
//			var ne = new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng());

				//RIGHT
//			var sw = new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng());
//			var ne = new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getNorthEast().lng()+Y);

				//BOTTOM
//			var sw = new GLatLng(map.getBounds().getSouthWest().lat()-X, map.getBounds().getSouthWest().lng()-Y);
//			var ne = new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng()+Y);

//			graniceTop = new GGroundOverlay("hole.gif", new GLatLngBounds(new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng()-Y), new GLatLng(map.getBounds().getNorthEast().lat()+X, map.getBounds().getNorthEast().lng()+Y)));
// 			graniceBottom = new GGroundOverlay("hole.gif", new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat()-X, map.getBounds().getSouthWest().lng()-Y), new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng()+Y)));

			var hole = "hole.gif";

			graniceTopLeft = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng()-Y), new GLatLng(map.getBounds().getNorthEast().lat()+X, map.getBounds().getSouthWest().lng())));
			graniceTop = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng()), new GLatLng(map.getBounds().getNorthEast().lat()+X, map.getBounds().getNorthEast().lng())));
			graniceTopRight = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getNorthEast().lng()), new GLatLng(map.getBounds().getNorthEast().lat()+X, map.getBounds().getNorthEast().lng()+Y)));
			graniceLeft = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getSouthWest().lng()-Y), new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng())));
			graniceRight = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng()), new GLatLng(map.getBounds().getNorthEast().lat(), map.getBounds().getNorthEast().lng()+Y)));
 			graniceBottomLeft = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat()-X, map.getBounds().getSouthWest().lng()-Y), new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getSouthWest().lng())));
 			graniceBottom = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat()-X, map.getBounds().getSouthWest().lng()), new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng())));
 			graniceBottomRight = new GGroundOverlay(hole, new GLatLngBounds(new GLatLng(map.getBounds().getSouthWest().lat()-X, map.getBounds().getNorthEast().lng()), new GLatLng(map.getBounds().getSouthWest().lat(), map.getBounds().getNorthEast().lng()+Y)));

			allowedBounds = map.getBounds();

 			clicks[0]=center;


				// Laduje Ikony
      	var icon = new GIcon();
      	icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
      	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      	icon.iconSize = new GSize(12, 20);
      	icon.shadowSize = new GSize(22, 20);
      	icon.iconAnchor = new GPoint(6, 20);
      	icon.infoWindowAnchor = new GPoint(5, 1);   

			
		var bicon = new GIcon();
		bicon.image = "http://twojsylwester.pl/images/blue_marker.png";
		bicon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		bicon.iconSize = new GSize(20, 34);
		bicon.shadowSize = new GSize(37, 34);
       	bicon.iconAnchor = new GPoint(6, 20);
       	bicon.infoWindowAnchor = new GPoint(5, 1);


				GEvent.addListener(map, "infowindowopen", function() { if (clicktimes==0) { isclicked=false; iswindowopen=true; } });
				GEvent.addListener(map, "infowindowclose", function() { if (clicktimes==0) { isclicked=false; map.panTo(clicks[clicktimes]);  } });

 				GEvent.addListener(map, "click", function(first, second) {
					if (clicktimes==0 && !iswindowopen) {
						if (!isclicked) {
							isclicked=true;
							setTimeout("setClickedFalse()",250);
						} else {
							isclicked=false;
							map.enableDragging();
    					map.setZoom(map.getZoom() + 2);
 							graniceOff();
							clicktimes++;
							clicks[clicktimes]=second;
							map.panTo(clicks[clicktimes]);
  					}
					} else {
						if (map.getInfoWindow().isHidden()) iswindowopen=false;
					}
 				});


      GEvent.addListener(map, "move", function() {
         if (clicktimes>0) checkBounds();
      });


				GEvent.addListener(map, "singlerightclick", function(marker) {
					if (clicktimes>0) {
						clicktimes--;
    				map.setZoom(map.getZoom() - 2);
  						if (clicktimes==0) { 
  							graniceOn();
								map.disableDragging();
  						}
  						map.panTo(clicks[clicktimes]);
					}
				});

      GEvent.addListener(map, "zoomend", function(oldlevel, newlevel) {
				if (newlevel==5 && oldlevel==6) {
					map.setCenter(center, 6);
				}	else if (newlevel==7 && oldlevel==6) {
					map.enableDragging();
					graniceOff();
					map.setZoom(8);
					clicktimes=1;
					clicks[clicktimes]=map.getCenter();
				} 
				else if (newlevel==9 && oldlevel==8) {
   				map.setZoom(10);
					clicktimes=2;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==11 && oldlevel==10) {
   				map.setZoom(12);
					clicktimes=3;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==13 && oldlevel==12) {
   				map.setZoom(14);
					clicktimes=4;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==15 && oldlevel==14) {
   				map.setZoom(16);
					clicktimes=5;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==17 && oldlevel==14) {
   				map.setZoom(18);
					clicktimes=6;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==17 && oldlevel==18) {
   				map.setZoom(16);
					clicktimes=5;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==15 && oldlevel==16) {
   				map.setZoom(14);
					clicktimes=4;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==13 && oldlevel==14) {
   				map.setZoom(12);
					clicktimes=3;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==11 && oldlevel==12) {
   				map.setZoom(10);
					clicktimes=2;
					clicks[clicktimes]=map.getCenter();
				} else if (newlevel==9 && oldlevel==10) {
   				map.setZoom(8);
					clicktimes=1;
					clicks[clicktimes]=map.getCenter();
				}
				else if (newlevel==7 && oldlevel==8) {
					clicktimes=0;
   				map.setZoom(6);
					map.panTo(center);
					graniceOn();
					map.disableDragging();
				}
	 				document.getElementById("howto").innerHTML=howto[clicktimes];
					if (clicktimes==1) {
						map.removeControl(samzoom);
						map.addControl(suwakzoom);
					} else if (clicktimes==0) {
						map.removeControl(suwakzoom);
						map.addControl(samzoom);
					}
      });

				GDownloadUrl("getmarkers.php", function(data, responseCode) {
  				var xml = GXml.parse(data);
  				var markers = xml.documentElement.getElementsByTagName("marker");
  				
  				for (var ix = 0; ix < markers.length; ix++) {
  					var point = new GLatLng(parseFloat(markers[ix].getAttribute("lat")), parseFloat(markers[ix].getAttribute("lng")));
  					
  					if (checkEquals(point))
  						var point = new GLatLng(parseFloat(markers[ix].getAttribute("lat")), parseFloat(markers[ix].getAttribute("lng"))+0.0003 );
  					
					if (markers[ix].getAttribute("type")=="mini") gmarkers.push(createMarker(point,ix,parseFloat(markers[ix].getAttribute("id")),icon)); 
//							if (markers[ix].getAttribute("type")=="maxi") bgmarkers.push(createMarker(point,ix,parseFloat(markers[ix].getAttribute("id")),bicon,true));
  				}
 					//mgr.addMarkers(gmarkers,6,17);
 					//mgr.refresh();
  					markc.addMarkers(gmarkers);
 					om.Clear();
 					document.getElementById("howto").innerHTML=howto[clicktimes];
  					granice = new GGroundOverlay("granice2.jpg", map.getBounds());
					graniceOn();
  					markc.resetViewport();
				});

      }
}

