var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function initializeMap(idName){
	var options = {
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    }
	return new google.maps.Map(document.getElementById(idName), options);
}

function positionExistsInArray(position, array) {
	for(var i = 0; i < array.length; i++) {
		var existingPosition = array[i];
		if(existingPosition.getPosition().equals(position)) {
			return true;
		}
	}
	return false;
}

function getMarkerPosition(markers, lat, lng) {
	var position = new google.maps.LatLng(lat, lng);
	while(positionExistsInArray(position, markers)) {
		var random_lat = ((Math.floor(Math.random() * 2)*4)-2)/8000; // Random, either -0.00025 or 0.00025
		var random_lng = ((Math.floor(Math.random() * 2)*4)-2)/8000;
		var movedLat = position.lat() + random_lat;
		var movedLng = position.lng() + random_lng;
		position = new google.maps.LatLng(movedLat, movedLng);
	}
	return position;
}

function makeMarkerWithInfoBox(map, markers, lat, lng, info, count, skinPath, useAlphaIcons) {
	var position = getMarkerPosition(markers, lat, lng);

	var markerInfo = {
		map: map,
        position: position
	};

	if((Boolean(useAlphaIcons)) && (count != undefined && count < alpha.length)) {
		markerInfo["icon"] = "http://www.google.com/mapfiles/marker" + alpha.charAt(count-1) + ".png";
	}

    var marker = new google.maps.Marker(markerInfo);

    var infowindow = new google.maps.InfoWindow(
    {
        content: info
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });

    return marker;
}

function getMarkerBounds(markers) {
	var markerBounds = new google.maps.LatLngBounds();
	for (var i in markers) {
		marker = markers[i];
		markerBounds.extend(marker.position);
	}
	return markerBounds;
}

function fitMap(map, markers) {
	var markerBounds = getMarkerBounds(markers);

	map.setCenter(markerBounds.getCenter());

	if(markers.length > 1) {
        map.fitBounds( markerBounds );
    } else {
        map.setZoom(7);
    }
}

