$(document).ready(function(){
	loadGpMap();
});
   
var tx_gplocations_geocoder;
var tx_gplocations_map;
var tx_gplocation_markerstore = [];
	
// On page load, call this function
function loadGpMap() {
      
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = 'fileadmin/sys/gfx/pin_shadow.png';
	baseIcon.iconSize = new GSize(20, 35);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(16, 38);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);	  
	  
	// Create new map object
	tx_gplocations_map = new GMap2(document.getElementById("tx_gplocations_googlemap"));   
	tx_gplocations_map.addControl(new GLargeMapControl());
	tx_gplocations_map.addControl(new GMapTypeControl());
	tx_gplocations_map.addControl(new GScaleControl());
	tx_gplocations_map.addControl(new GOverviewMapControl());   
	tx_gplocations_map.enableScrollWheelZoom();  
	
	// Create new geocoding object
	tx_gplocations_geocoder = new GClientGeocoder();

	if (selectedLocationUid > 0) {
		var selectedLocationObj = eval('locationInfo_' + selectedLocationUid);	
		var centerPoint = new GLatLng(selectedLocationObj.lat, selectedLocationObj.lng);
		var zoomLevel =defaultDetailsZoom;
	} else {
			// Retrieve the latitude and longitude
		var centerPoint = new GLatLng(defaultMapCenter.lat, defaultMapCenter.lng);
		var zoomLevel = defaultMapZoom;
	}

    // Center the map on this point
    tx_gplocations_map.setCenter(centerPoint, zoomLevel);   	  	   

	var markerArray = [];
	 $.each(locationInfos, function() {
		// Retrieve the latitude and longitude
		point = new GLatLng(this.lat, this.lng);		  

		var customIcon = new GIcon(baseIcon);

		if(this.office == 1) {
			customIcon.image = "fileadmin/sys/gfx/pin_gp_offices.png";
		} else {
			customIcon.image = "fileadmin/sys/gfx/ping_gp_cooperations.png";
		}
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:customIcon };

		// Create a marker		
		marker = new GMarker(point, markerOptions);
		marker.infoHtml = this.infoHtml;		
		GEvent.addListener(marker, "click", function(){this.openInfoWindowHtml(this.infoHtml)});
		markerArray.push(marker);
		tx_gplocation_markerstore[this.uid] = marker;

		if (this.uid == selectedLocationUid) {			
			tx_gplocations_map.addOverlay(marker);			
			marker.openInfoWindowHtml(marker.infoHtml);
		}
	});
	
	mgr = new MarkerManager(tx_gplocations_map);
	mgr.addMarkers(markerArray,1);
	mgr.refresh();
}

function goToGpLocation(locationUid) {
	var locationObj = eval('locationInfo_' + locationUid);	
	var point = new GLatLng(locationObj.lat, locationObj.lng);
	tx_gplocations_map.setZoom(defaultDetailsZoom);
	tx_gplocations_map.panTo(point);

	var infoWindow = tx_gplocations_map.getInfoWindow();
	infoWindow.show();

	window.setTimeout("tx_gplocation_markerstore[" + locationUid + "].openInfoWindowHtml('" + locationObj.infoHtml + "');", 50);

}
