var poly, map;
var markers = [];
var path = new google.maps.MVCArray;
var curr_infw; 
var mapFillColors = ['#f68620', '#22bdf6','#f68620', '#22bdf6'];
var mapStrokeColors = ['#f68620', '#22bdf6','#f68620', '#22bdf6'];
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 dragableMarker;

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 locationIconBold = new google.maps.MarkerImage('/template/gfx/poi/miejsce2.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 locationIconNear = new google.maps.MarkerImage('/template/gfx/poi2/wokolicy.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 dragableMarker(lat, lng, editorForm) {
	var latLng = new google.maps.LatLng(lat, lng);
	dragableMarker = new google.maps.Marker({
        position: latLng,
        map: map,
        draggable: true,
        icon: locationIcon,
        shadow: shadowPoint
      });
      
      google.maps.event.addListener(dragableMarker, 'dragend', function() {
        dPos = dragableMarker.getPosition();
    	//alert( dPos.lat());
    	myForm = document.getElementById(editorForm);
    	myForm.latitude.value = dPos.lat();
    	myForm.longitude.value = dPos.lng();
      }
      );
	
}


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)||(mIcon == locationIcon)||(mIcon == locationIcon2)) myShadow = null;
   
   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 map3drawPath(pathCoords, colorId) {
	var myPath = new google.maps.Polyline({    
			path: pathCoords,    
			strokeColor: mapStrokeColors[colorId],    
			strokeOpacity: mapStrokeOpacity[colorId],    
			strokeWeight: 2  
		});  
	myPath.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);
}



