/* all functions related to google maps */
  var map;
  var markerPosition;
  var marker;

  function googleMapInitialize() {  
    var myOptions = {
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.HYBRID
    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);
	var pos = new google.maps.LatLng(52.089422,5.11008);
	map.setCenter(pos);
	createMarker(map,pos);
    // Try HTML5 geolocation
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = new google.maps.LatLng(position.coords.latitude,
                                         position.coords.longitude);
		
		
        
        updateVoornemen(pos);

        map.setCenter(pos);
      }, function() {
        handleNoGeolocation(true);
      }, {timeout: 5000});
    } else {
      // Browser doesn't support Geolocation
      handleNoGeolocation(false);
    }
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
      var content = 'Error: The Geolocation service failed.';
    } else {
      var content = 'Error: Your browser doesn\'t support geolocation.';
		}

    var pos = new google.maps.LatLng(52.089422,5.11008); // utrecht centraal.

    createMarker(map,pos);
    updateVoornemen(pos);

    map.setCenter(pos);
  }

function geoCodePlace(place) {

  var coder = new google.maps.Geocoder();
    var foundLoc = coder.geocode({'address': place},function(results,status) {
    var pos = results[0].geometry.location;
    markerPosition = pos;
    map.setCenter(markerPosition);
    marker.setPosition(markerPosition);
    updateVoornemen(pos);
  });
}

function createMarker(map,pos) {
	
	var newUserId;
	if(temp_userid){
		newUserId = temp_userid;
  } else if (socialUserId){
		newUserId = socialUserId;
  }
	

	var image = new google.maps.MarkerImage("/wp-content/uploads/map-markers/"+newUserId+"-marker.png",
	// This marker is 20 pixels wide by 32 pixels tall.
	new google.maps.Size(51, 71),
	// 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(11, 71));
	
	var shadow = new google.maps.MarkerImage('/wp-content/plugins/buddypress/bp-themes/actimel/_inc/images/resolutions/google-maps-marker-shade.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(85, 58),

	new google.maps.Point(0,0),

	new google.maps.Point(11, 58));

    marker = new google.maps.Marker({
	position:pos,
	map:map,
	draggable:true,
	icon:image,
	shadow:shadow
	});
	
	var InfoWindowContent ="<div style='position: absolute; overflow:hidden; display:block;height:46px;width:156px; top: 14px; left: 20px; line-height: 14px'><img style='margin-right:5px; float:left; width:25px; height:25px;' align='left' src='"+socialAvatar+"'/><span style='display:block; width:120px; float:left;'><p style='margin-bottom: 0; color: #0193f1; font-family: Trebuchet MS; font-size: 12px;font-weight: bold;'>"+socialName+"</p><p style='color: #6c7c81; font-family: Trebuchet MS; font-size: 10px; font-weight: normal;'>"+voornemenText+"</p></span><span style='clear:both;'></span></div>";

	var myOptions = {
		content: InfoWindowContent
		,disableAutoPan: false
		,maxWidth: 0
		,pixelOffset: new google.maps.Size(-49, -91)
		,zIndex: null
		,boxStyle: { 
			background: "url('/wp-content/plugins/buddypress/bp-themes/actimel/_inc/images/resolutions/google-maps-infobox-bg.png') no-repeat"
			,width: "263px"
			,height: "97px"
		}
		,closeBoxMargin: "17px 80px 0px 0px"
		,closeBoxURL: "/wp-content/plugins/buddypress/bp-themes/actimel/_inc/images/resolutions/google-maps-close-btn.png"
		,infoBoxClearance: new google.maps.Size(1, 1)
		,isHidden: false
		,pane: "floatPane"
		,enableEventPropagation: false
	};

	var ib = new InfoBox(myOptions);
	// ib.open(bmap, bmarker);

    markerPosition = pos;

  	google.maps.event.addListener(marker, "click", function (e) {
		ib.open(map, this);
	});

    google.maps.event.addListener(marker,'dragend',function() { 
        markerPosition = marker.getPosition();
        updateVoornemen(markerPosition);
    })
    return marker;
}
var win = false;
//console_log(socialAvatar);
var infowindow;

function toggleInfoWindow(map,marker) {
	if(win == false) {
		win = true;
		infowindow.open(map,marker);
	} else {
		win = false;
		infowindow.close(map,marker);
	}
}

