var poly, map;
var markers = [];
var path = new google.maps.MVCArray;
var curr_infw; 
var mapFillColors = ['#1e5eac', '#ff6600','#1eac5e', '#ff6600'];
var mapStrokeColors = ['#1e5eac', '#ff6600','#1eac5e', '#ff6600'];
var mapStrokeWeight = [2, 2, 2, 1];
var mapStrokeOpacity = [0.95, 0.95, 0.8, 0.7];
var mapFillOpacity =   [0.1 , 0.1 , 0.1, 0.3];
var eMarker;

function mapsInitialize(mapdiv) {
    var latlng = new google.maps.LatLng(51.189312, 17.05341);
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN,
      disableDefaultUI: true,
      navigationControl: true,
      navigationControlOptions: google.maps.NavigationControlStyle.SMALL
    };
    map = new google.maps.Map(document.getElementById(mapdiv), myOptions);
  }


function mapsInitialize2(mapdiv) {
    var latlng = new google.maps.LatLng(52.2303, 21.0100);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      disableDefaultUI: true,
      navigationControl: true,
      navigationControlOptions: google.maps.NavigationControlStyle.SMALL,
      mapTypeControl: true,
      mapTypeControlOptions: google.maps.MapTypeControlStyle.DROPDOWN_MENU
    };
    map = new google.maps.Map(document.getElementById(mapdiv), myOptions);
}
function normalMarker(lat,lng) {
   var latlng = new google.maps.LatLng(lat, lng);
   var marker = new google.maps.Marker({
        position: latlng, 
        map: map
    });		
}

var curr_infw; 
function flagMarker(lat,lng,mTitle, mContent) {
   var image = new google.maps.MarkerImage('/template/gfx/poi/miejsce.png',
		      // This marker is 20 pixels wide by 32 pixels tall.
		      new google.maps.Size(21, 38),
		      // The origin for this image is 0,0.
		      new google.maps.Point(10,38));
		      // The anchor for this image is the base of the flagpole at 0,32.
   var shadow = new google.maps.MarkerImage('/template/gfx/poi/cien_miejsce.png',
		      // The shadow image is larger in the horizontal dimension
		      // while the position and offset are the same as for the main image.
		      new google.maps.Size(39, 35),
		      new google.maps.Point(10,38));   
   var latlng = new google.maps.LatLng(lat, lng);
   var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        icon: image,
        shadow: shadow,
        title: mTitle
    });
   if (mContent) {
	   var infowindow = new google.maps.InfoWindow({
	        content: mContent
	    });
	   google.maps.event.addListener(marker, 'click', function() {
		      if(curr_infw) { curr_infw.close();} 
		      infowindow.open(map,marker);
		      curr_infw = infowindow; 
	   });
   }
}
function setBounds(latmin, lngmin, latmax, lngmax) {
	var latlng1 = new google.maps.LatLng(latmin, lngmin);
	var latlng2 = new google.maps.LatLng(latmax, lngmax);
	var bounds = new google.maps.LatLngBounds(latlng1, latlng2);
	map.fitBounds( bounds );
}
function panTo(lat,lng) {
	var latlng = new google.maps.LatLng(lat, lng);
	map.panTo(latlng);
}
function editableMarker(lat,lng) {
   var latlng = new google.maps.LatLng(lat, lng);
   map.setCenter(latlng);
   eMarker = new google.maps.Marker({
        position: latlng, 
        map: map,
        draggable: true
    });
   google.maps.event.addListener(eMarker, 'dragend', function() {
	   //alert(eMarker.get_position().lat());
	   document.getElementById("latitude").value = eMarker.getPosition().lat();
	   document.getElementById("longitude").value = eMarker.getPosition().lng();
  });
}



var locationIcon = new google.maps.MarkerImage('/template/gfx/poi/miejsce.png',
	      // This marker is 20 pixels wide by 32 pixels tall.
	      new google.maps.Size(21, 38),
	      // The origin for this image is 0,0.
	      new google.maps.Point(0,0),
	      // The anchor for this image is the base of the flagpole at 0,32.
	      new google.maps.Point(10,38));
var shadowPoint = new google.maps.MarkerImage('/template/gfx/poi/cien_miejsce.png',
	      // The shadow image is larger in the horizontal dimension
	      // while the position and offset are the same as for the main image.
	      new google.maps.Size(39, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(10,38));
	      // Shapes define the clickable region of the icon.
	      // The type defines an HTML <area> element 'poly' which
	      // traces out a polygon as a series of X,Y points. The final
	      // coordinate closes the poly by connecting to the first
	      // coordinate.		  

var shadow = new google.maps.MarkerImage('/template/gfx/poi/cien.png',
	      new google.maps.Size(54, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(30, 35));

var locationIcon1 = new google.maps.MarkerImage('/template/gfx/poi/synagoga.png',
	      new google.maps.Size(30, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(30, 35));
var locationIcon2 = new google.maps.MarkerImage('/template/gfx/poi/cmentarz.png',
	      new google.maps.Size(30, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(30, 35));
var locationIcon3 = new google.maps.MarkerImage('/template/gfx/poi/martyrologia.png',
	      new google.maps.Size(30, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(30, 35));
var locationIcon4 = new google.maps.MarkerImage('/template/gfx/poi/judaika.png',
	      new google.maps.Size(30, 35),
	      new google.maps.Point(0,0),
	      new google.maps.Point(30, 35));

var pinIcon = new google.maps.MarkerImage('/template/gfx/poi/macewa-pin1.png',
	      new google.maps.Size(10, 15),
	      new google.maps.Point(0,0),
	      new google.maps.Point(5, 13));
var pinGrayIcon = new google.maps.MarkerImage('/template/gfx/poi/macewa-pin2.png',
	      new google.maps.Size(10, 15),
	      new google.maps.Point(0,0),
	      new google.maps.Point(5, 13));

function map3init(mapDivID, lat, lng) {
	var mapCenter = new google.maps.LatLng(lat, lng);
	map = new google.maps.Map(document.getElementById(mapDivID), {
		zoom: 1,
		center: mapCenter,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
}


function normalMarker(lat,lng) {
   var latlng = new google.maps.LatLng(lat, lng);
   var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        icon: locationIcon,
        shadow: shadowPoint
    });		
}
function flagMarker(lat,lng, mIcon, mTitle, mContent) {
   var latlng = new google.maps.LatLng(lat, lng);
   var myShadow = shadow;
   if ( (mIcon == pinIcon)||(mIcon == pinGrayIcon)) myShadow = null;
   if ( mIcon == locationIcon ) myShadow = shadowPoint;
   var marker = new google.maps.Marker({
        position: latlng, 
        map: map,
        icon: mIcon,
        shadow: myShadow,
        title: mTitle
    });
   if (mContent) {
	   var infowindow = new google.maps.InfoWindow({
	        content: mContent
	    });
	   google.maps.event.addListener(marker, 'click', function() {
		      if(curr_infw) { curr_infw.close();} 
		      infowindow.open(map,marker);
		      curr_infw = infowindow; 
	   });
   }
}

function map3InsertPoly() {
    poly = new google.maps.Polygon({
        strokeWeight: 2,
        strokeColor: '#1e5eac',
    	strokeOpacity: 0.95,
        fillColor: '#1e5eac',
        fillOpacity: 0.3
    });
    poly.setMap(map);
    poly.setPaths(new google.maps.MVCArray([path]));
    google.maps.event.addListener(map, 'click', map3addPointEV);
	
}

function map3addPoint(latLng) {
    path.insertAt(path.length, latLng);

    var marker = new google.maps.Marker({
      position: latLng,
      map: map,
      draggable: true
    });
    markers.push(marker);
    marker.setTitle("#" + path.length);
    map3updatePointsText(poly);
    
    google.maps.event.addListener(marker, 'click', function() {
      marker.setMap(null);
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      markers.splice(i, 1);
      path.removeAt(i);
      map3updatePointsText(poly);
      }
    );

    google.maps.event.addListener(marker, 'dragend', function() {
      for (var i = 0, I = markers.length; i < I && markers[i] != marker; ++i);
      path.setAt(i, marker.getPosition());
      map3updatePointsText(poly);
      }
    );
}

function map3addPointEV(event) {
	map3addPoint(event.latLng);
}
function map3addPointLatLng(lat,lng) {
	map3addPoint(new google.maps.LatLng(lat, lng));
}

function map3updatePointsText(obj) {
	
	  // Since this Polygon only has one path, we can call getPath()
	  // to return the MVCArray of LatLngs
	  var vertices = obj.getPath();
	  var contentString = "";
	  
	  // Iterate over the vertices.
	  for (var i =0; i < vertices.length; i++) {
	    var xy = vertices.getAt(i);
	    contentString += xy.lat() +"," + xy.lng() + "\n";
	  }
	
	  document.getElementById('areapoints').value = contentString;
	}

function map3drawPolygon(pathCoords, colorId) {
	myPolygon = new google.maps.Polygon({
	    paths: pathCoords,
        strokeWeight: mapStrokeWeight[colorId],
        strokeColor: mapStrokeColors[colorId],
    	strokeOpacity: mapStrokeOpacity[colorId],
        fillColor: mapFillColors[colorId],
        fillOpacity: mapFillOpacity[colorId]
	  });

	 myPolygon.setMap(map);
}
function map3ZoomBounds(latMin, lngMin, latMax, lngMax) {
	map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(latMin, lngMin), new google.maps.LatLng(latMax, lngMax)));
}
function map3Zoom(zoomLevel) {
	map.setZoom(zoomLevel);
}



