You don't track which marker has infowindow open. One possible solution is to add global variable which will contain information about marker with opened infowindow:
var infowindow = null;
var markerRed;
And change icon to red color when marker is clicked:
google.maps.event.addListener(marker, 'click', function() {
if (markerRed) {
markerRed.setIcon('https://www.google.com/mapfiles/marker.png');
}
infowindow.close();
infowindow.setContent(contentString);
infowindow.open(map,marker);
marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
markerRed = marker;
});
Update: icon change on close of infowindow:
var infowindow = new google.maps.InfoWindow({
content: ''
});
google.maps.event.addListener(infowindow, 'closeclick', function() {
if (markerRed) {
markerRed.setIcon('https://www.google.com/mapfiles/marker.png');
}
});