	var map = null;
    
    var geocoder = null;
	
	var mmgr = null;
	
	var area = null;
	
	// Polska:
	var defaultCenterW = 51.919438;
	var defaultCenterL = 19.145136;

	var preciseLocations = new Array(1);
	
	var index = 0;
	
	var resizeMapFlag = true;
	
	var disableAtsStatus = false;
	
	var minZoom = 12;
	var maxZoom = 19;
	
	var markers=[];

	function Pos(wplatomatStatus, ownerPicture, typePicture, posPicture, type, city, street, voivodship, latitude, longitude, availability) {
		this.wplatomatStatus = wplatomatStatus;
		this.ownerPicture = ownerPicture;
		this.typePicture = typePicture;
		this.posPicture = posPicture;
		this.type = type;
		this.city = city;
		this.street = street;
		this.voivodship = voivodship;
		this.latitude = latitude;
		this.longitude = longitude;
		this.availability = availability;
	}

	
	// wstepna inicjalizacja danych:
	preciseLocations[0] = new Pos("", "", "", "", "", "", "", "", "", "1", ""); // nie spelnia warunku: if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false)
	
	
	function initialize()  
	{  
	    if(GBrowserIsCompatible())  // sprawdzamy, czy przegladarka jest kompatybilna  
	    {  
			// tworzymy nowy obiekt mapy, i umieszczamy go w elemencie blokowym o ID "map"  
	        map = new GMap2(document.getElementById('map'));

			// dodanie mapy Google Earth:
			map.addMapType(G_SATELLITE_3D_MAP);

	        mmgr = new GMarkerManager(map,{maxZoom: 19});
	        
			// obszar widocznosci markerow
			area = new GLatLngBounds();
			
			// ustawienie domyslnego srodka
			map.setCenter(new GLatLng(defaultCenterW, defaultCenterL),6);

			// dodanie kontrolki trybu wyswietlania
			map.addControl(new GMapTypeControl());

			// dodanie kontrolki sterowania zoomem
			map.addControl(new GLargeMapControl());

			geocoder = new GClientGeocoder();

			// dodanie markerow
			setMarkers(preciseLocations);
			
			// dodanie skali
			map.addControl(new GScaleControl());
			
			// dodanie odswiezania markerow za kazdym razem gdy wybrana zostanie inna mapa
			GEvent.addListener(map, "maptypechanged", function() {
				  mmgr.refresh();
			});

	    }  
	}
	

	/* funkcja odpowiedzialna za dopasowanie parametrow mapy (center, zoom) do aktualnego obszaru */
	function resizeMap(){
		
		// wyciagamy zoom
		var zoom = map.getBoundsZoomLevel(area);
		
		// wyciagamy srodek obszaru
		var centerPoint = area.getCenter();
		
		// centrujemy do tego punktu i poziomu zoom
		map.setCenter(centerPoint,zoom);
	}


	
	/* ustawienie srodka mapy */
	function setCenter(address) {
		if (geocoder) {
	        geocoder.getLatLng(
	          address,
          function(point) {
		    if (!point) {
				alert(address + " not found");
		    } else {
		    	area.extend(point);
				map.setCenter(point, 6);
            }
          }
		);
      }
	}



	/* dodawanie markerow do mapy */
	function setMarkers(preciseLocations) {
	
		// zaznaczenie Pos'ow, dla ktorych okreslono dokladne polozenie geograficzne:
		for (i=0; i<preciseLocations.length; i++) {
			
			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false) {
			
				var precisePoint = new GLatLng(parseFloat(preciseLocations[i].latitude), parseFloat(preciseLocations[i].longitude));
				area.extend(precisePoint);

				if ("Outlet" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/blue-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else if ("CreditCenter" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/green-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else if ("ATM" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/orange-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else if ("Wrzutnia" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/yellow-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else if ("ATS" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/pink-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else if ("medical" == preciseLocations[i].type) {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/blue-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				} else {
					var posIcon = new GIcon(G_DEFAULT_ICON);
					posIcon.iconSize=new GSize(32,32);
					posIcon.shadowSize=new GSize(56,32);
					posIcon.iconAnchor=new GPoint(16,32);
					posIcon.infoWindowAnchor=new GPoint(16,32);
					posIcon.image = 'http://maps.google.com/mapfiles/ms/micons/red-dot.png';
					var preciseMarker = new GMarker(precisePoint, { 'icon':posIcon });
					preciseMarker.bindInfoWindowHtml(createInfoWindowHtml(preciseLocations[i]));
					markers.push(preciseMarker);
				}

				if (resizeMapFlag) {resizeMap();}
			}
		}
		
		mmgr.addMarkers(markers,minZoom,maxZoom);
		mmgr.refresh();
	}



	
	/* funkcja odpowiedzialna za stworzenie html'a do pokazania w chmurce */
	function createInfoWindowHtml(loc){

		var infoHtml = '<div class="infoWind" style="width: 288px; height: 90px;">';
		
		infoHtml += '<div class="column_1">';
		
			if(loc.posPicture.blank() == false) {
				infoHtml += '<img src="' + loc.posPicture + '" alt="" />';
			}
			
		infoHtml += '</div>';
		infoHtml += '<div class="column_2">';
		
			if(loc.type.blank() == false) {
				if ("Outlet" == preciseLocations[i].type) {
					infoHtml += '<h2>Plac&#243;wka</h2>';
				} else if ("CreditCenter" == preciseLocations[i].type) {
					infoHtml += '<h2>Centrum Kredytowe</h2>';
				} else if ("ATM" == preciseLocations[i].type) {
					infoHtml += '<h2>Bankomat</h2>';
				} else if ("Wrzutnia" == preciseLocations[i].type) {
					infoHtml += '<h2>Wrzutnia</h2>';
				} else if ("ATS" == preciseLocations[i].type) {
					infoHtml += '<h2>Wp&#322;atomat</h2>';
				}
			}
			
			if(loc.city.blank() == false) {
				if(loc.street.blank() == false) {
					infoHtml += '<h3>' + loc.city + ', ' + loc.street + '</h3>';
				} else {
					infoHtml += '<h3>' + loc.city + '</h3>';
				}
			} else {
				if(loc.street.blank() == false) {
					infoHtml += '<h3>' + loc.street + '</h3>';
				}
			}
	
			infoHtml += '<div class="details">';
	
			if(loc.voivodship.blank() == false) {
				infoHtml += '<p>woj. ' + loc.voivodship + '</p>';
			}
			
			if(loc.availability.blank() == false) {
				infoHtml += '<p>' + loc.availability + '</p>';
			}

			infoHtml += '</div>';
			
		infoHtml += '</div>';
		infoHtml += '<div class="column_3">';
			
			if(loc.ownerPicture.blank() == false) {
				infoHtml += '<img src="' + loc.ownerPicture + '" alt="" />';
			}
			
			if(loc.typePicture.blank() == false) {
				infoHtml += '<img src="' + loc.typePicture + '" alt="" />';
			}
			
			if (!disableAtsStatus) {
				if(loc.wplatomatStatus.blank() == false && loc.type.blank() == false) {
					if ("ATS" == loc.type) {
						if(loc.wplatomatStatus == "disabled") {
							infoHtml += '<img src="/images/21/pos_list/wplatomat_nieczynny.gif" alt="" title="nie dzia&#322;a" />';
						} else if(loc.wplatomatStatus == "enabled") {
							infoHtml += '<img src="/images/21/pos_list/wplatomat_dziala.gif" alt="" title="dzia&#322;a" />';
						}
					}
				}
			}
			
		infoHtml += '</div>';
		infoHtml += '</div>';

		return infoHtml;
	}

	function showAddress(address) {
		if (geocoder) {
	        geocoder.getLatLng(
	          address,
          function(point) {
		    if (!point) {
              alert(address + " not found");
		    } else {				
			  map.setCenter(point, 6);
              var marker = new GMarker(point);
		      //map.addOverlay(marker);
		      mmgr.addMarker(marker,minZoom);
		      if (resizeMapFlag) {resizeMap();}
              marker.openInfoWindowHtml(address);
		    }
          }
		);
      }
	}
	
	/* Odswieza markery na mapie. */
	function refreshMarkers() {
		
		// Punkty obslugi klienta okreslonego typu prezentowane sa na mapie wowczas, gdy checkboks wyszukiwarki odpowiadajacy typowi danego Pos'a jest zaznaczony.
					
		mmgr.refresh();

		for (i=0; i<preciseLocations.length; i++) {

			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false && preciseLocations[i].type == "Outlet") {
				if (document.getElementById('first').checked == true) {
					markers[i].show();
					continue;
				} else {
					markers[i].hide();
					continue;
				}
			}

			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false && preciseLocations[i].type == "CreditCenter") {
				if (document.getElementById('second').checked == true) {
					markers[i].show();
					continue;
				} else {
					markers[i].hide();
					continue;
				}
			}

			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false && preciseLocations[i].type == "ATM") {
				if (document.getElementById('third').checked == true) {
					markers[i].show();
					continue;
				} else {
					markers[i].hide();
					continue;
				}
			}

			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false && preciseLocations[i].type == "Wrzutnia") {
				if (document.getElementById('fourth').checked == true) {
					markers[i].show();
					continue;
				} else {
					markers[i].hide();
					continue;
				}
			}

			if(preciseLocations[i].latitude.blank() == false && preciseLocations[i].longitude.blank() == false && preciseLocations[i].type == "ATS") {
				if (document.getElementById('fifth').checked == true) {
					markers[i].show();
					continue;
				} else {
					markers[i].hide();
					continue;
				}
			}
			
		}

	}

