//================================================================================
// MyMap - LGPL Copyright (c) 2006 Lionel Lask?
//
// This file is part of MyMap.
//
// MyMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// MyMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with MyMap; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
//================================================================================

var map = null;
var geo = null;
var infoWindow = null;

var MYMODE_MAP = google.maps.MapTypeId.ROADMAP;
var MYMODE_SATELLITE = google.maps.MapTypeId.SATELLITE;
var MYMODE_MIXTE = google.maps.MapTypeId.HYBRID;

var MYMARKER_TYPE1 = null;
var MYMARKER_TYPE2 = null;
var MYMARKER_SHADOW = null;

function MyMapInitialize(mapname, lat, lng, zoom, mode, showMapTypeControl) {
	var latlng = new google.maps.LatLng(lat, lng);
	var myOptions = {
		zoom: zoom,
		center: latlng,
		mapTypeId: mode,
		mapTypeControl: showMapTypeControl,
		navigationControl: true
	};
	map = new google.maps.Map(document.getElementById(mapname), myOptions);

	geo = new google.maps.Geocoder();

	// Create a single instance of the InfoWindow object which will be shared
	// by all Map objects to display information to the user.
	infoWindow = new google.maps.InfoWindow();

	MYMARKER_SHADOW = new google.maps.MarkerImage('http://www.google.com/intl/en_us/mapfiles/arrowshadow.png',
		new google.maps.Size(37, 34),
		new google.maps.Point(0, 0),
		new google.maps.Point(9, 34)
	);

	MYMARKER_TYPE1 = new google.maps.MarkerImage('http://maps.google.com/mapfiles/marker.png',
		new google.maps.Size(20, 34),
		new google.maps.Point(0, 0),
		new google.maps.Point(9, 34)
	);

	MYMARKER_TYPE2 = new google.maps.MarkerImage('http://maps.google.com/mapfiles/arrow.png',
		new google.maps.Size(20, 34),
		new google.maps.Point(0, 0),
		new google.maps.Point(9, 34)
	);
}

function MyMapTerminate() {
}

function MyMapAddMarker(lat, lng, markertype, info)
{
	var newMarker = new google.maps.Marker({
		position: new google.maps.LatLng(lat, lng),
		shadow: MYMARKER_SHADOW,
		icon: markertype
	});
	newMarker.setMap(map);

	if (info != null) {
		google.maps.event.addListener(newMarker, 'click', function() {
			MyMapOpenWindow(newMarker, info);
		});
	}

	return newMarker;
}

function MyMapRemoveMarker(marker)
{
	marker.setMap(null);
}

function MyMapOpenWindow(marker, info)
{
	if (info != null) {
		infoWindow.setContent(info);
		infoWindow.open(map, marker);
	}
}

function MyMapGoto(lat, lng) {
	map.panTo(new google.maps.LatLng(lat, lng));
}

function MyMapSetZoom(zoom) {
	map.setZoom(zoom);
}

function MyMapPoint() {
	this.lat = 0;
	this.lng = 0;
}

function MyMapGeocode(address, callback) {
	if (geo == null) {
		geo = new google.maps.Geocoder();
	}
	
	geo.geocode({'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			var res = new MyMapPoint();
			res.lat = results[0].geometry.location.lat();
			res.lng = results[0].geometry.location.lng();
			callback(res);
		}
		else {
			callback(null);
		}
	});
}

