
var map = null;
var infoWindow = null;
var overlay = null;
var activeInfoWindowId = 0;
var initZoom = 13;
var currZoom = initZoom;
var autoSettingsSet = false;

function MapOverlay(sw, ne)
{
	//Now initialize all properties.
	this.southWest_ = sw;
	this.northEast_ = ne;
	//Explicitly call setMap() on this overlay
	this.setMap(map);
}

(function () {
	if (!google || !google.maps) return;
	
	MapOverlay.prototype = new google.maps.OverlayView();
	
	MapOverlay.prototype.onAdd = function() {
		//We use this approach so we have access to MapCanvasProjection methods
		//What this function does is that it adds padding around bounds
		var overlayProjection = this.getProjection();
		var sw = overlayProjection.fromLatLngToDivPixel(this.southWest_);
		var ne = overlayProjection.fromLatLngToDivPixel(this.northEast_);
		//Set padding
		var padding = 10;
		sw.x -= padding;
		sw.y += padding;
		ne.x += padding;
		ne.y -= padding;
		//Set and fit adjusted bounds
		this.southWest_ = overlayProjection.fromDivPixelToLatLng(sw);
		this.northEast_ = overlayProjection.fromDivPixelToLatLng(ne);
		bounds = new google.maps.LatLngBounds(this.southWest_, this.northEast_);
		map.fitBounds(bounds);
		//Adjust position with static map (real center)
		map.panBy(0, -17);
	}
	MapOverlay.prototype.draw = function() {
	}
	MapOverlay.prototype.onRemove = function() {
	}
	
	google.maps.Map.prototype.createMarker = function(properties) {
		var geoCoords = new google.maps.LatLng(properties.lat, properties.lng);
		var marker = new google.maps.Marker({
			position: geoCoords
			, map: map
			, title: properties.title
		});
		if (!properties.content || properties.content.length == 0) return;
		google.maps.event.addListener(marker, "click", function() {
			if (infoWindow && activeInfoWindowId > 0) {
				if (properties.id == activeInfoWindowId) return;
				infoWindow.close();
			}
			infoWindow.setContent(properties.content);
			infoWindow.open(map, marker);
			activeInfoWindowId = properties.id;
		});
	};
	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = function() { loadMap() };
	} else {
		window.onload = function() {
			oldonload();
			loadMap();
		}
	}
})();

function loadMap()
{
	if (!google || !google.maps) return;
	//Set map
	var initLatLng = ((mapMarkers && mapMarkers.length > 0) ? [mapMarkers[0].lat, mapMarkers[0].lng] : [0, 0]); //temp. coordinates
	var geoCoords = new google.maps.LatLng(initLatLng[0], initLatLng[1]);
	
	var mapOptions = {
		zoom: initZoom
		, center: geoCoords
		//, noClear: true
		, mapTypeControl: true
		, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
		, mapTypeId: google.maps.MapTypeId.ROADMAP
		//, backgroundColor: '#fff'
	};
	
	map = new google.maps.Map(document.getElementById("GoogleMap"), mapOptions);
	
	google.maps.event.addListener(map, "idle", function() {
		currZoom = map.getZoom();
		if (!autoSettingsSet) { //inic data
			initZoom = currZoom;
			geoCoords = map.getCenter(); 
			autoSettingsSet = true;
		}
	});
	google.maps.event.addListener(map, "click", function() { //dblclick, dragend, bounds_changed
		if (autoSettingsSet && infoWindow && activeInfoWindowId > 0) {
			activeInfoWindowId = 0;
			infoWindow.close();
			if (initZoom >= currZoom) map.setCenter(geoCoords);
		}
	});
	/*
	google.maps.event.addListener(map, "bounds_changed", function() { //intersects
	});
	*/
	
	//Set info window
	infoWindow = new google.maps.InfoWindow();
	google.maps.event.addListener(infoWindow, "closeclick", function() {
		activeInfoWindowId = 0;
		if (initZoom >= currZoom) map.setCenter(geoCoords);
	});
	google.maps.event.addListener(infoWindow, "domready", function() {
		if (initZoom >= currZoom) map.setCenter(geoCoords);
	});
	
	//Set markers
	setMapMarkers();
}

function setMapMarkers()
{
	if (!google || !google.maps || !map || !mapMarkers) return;
	//Create markers
	//Calculate south-west and north-east coordinates
	if (mapMarkers.length > 1) {
		var swLat = 0, swLng = 0;
		var neLat = 0, neLng = 0;
	}
	for (var i = 0; i < mapMarkers.length; i++) {
		mapMarkers[i].id = (i + 1); //Set id
		map.createMarker(mapMarkers[i]);
		if (mapMarkers.length > 1) {
			if (i == 0) {
				swLat = mapMarkers[i].lat;
				swLng = mapMarkers[i].lng;
				neLat = swLat;
				neLng = swLng;
			} else {
				if (mapMarkers[i].lat < swLat) swLat = mapMarkers[i].lat;
				if (mapMarkers[i].lng < swLng) swLng = mapMarkers[i].lng;
				if (mapMarkers[i].lat > neLat) neLat = mapMarkers[i].lat;
				if (mapMarkers[i].lng > neLng) neLng = mapMarkers[i].lng;
			}
		}
	}
	//Set viewport to fit all markers
	if (mapMarkers.length > 1) {
		var southWest = new google.maps.LatLng(swLat, swLng);
		var northEast = new google.maps.LatLng(neLat, neLng);
		//Set and fit initial bounds
		var bounds = new google.maps.LatLngBounds(southWest, northEast);
		map.fitBounds(bounds);
		//We use this so we can set padding around bounds
		overlay = new MapOverlay(southWest, northEast);
	}
}

