
window.addEvent('load', function () {
	if (typeof google != 'undefined') {
		displayMaps();
	}
});

var mapsLoaded = false;
function displayMaps () {
	if (mapsLoaded) {
		return;
	}
	mapsLoaded = true;
	maps.each(function (m) {
		if (typeof m.lat != 'undefined' && typeof m.lng != 'undefined') {
			var LatLng = new google.maps.LatLng(m.lat, m.lng);
			var map = new google.maps.Map($(m.element), {
				zoom: typeof m.zoom != 'undefined' ? m.zoom : 15,
				center: LatLng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			});
			new google.maps.Marker({
				map: map,
				position: LatLng
			});
		}
		else {
			var map = new GoogleMap(m.element);
			m.markers.each(function (markerOptions) {
				$extend(markerOptions, {
					onComplete: function (marker) {
						map.getMap().setCenter(marker.getPosition());
						google.maps.event.addListener(marker.getMarker(), 'click', function() {
							window.location.href = 'http://maps.google.com/?q=' + escape(marker.getAddress()) + '&iwloc=A';
						});
					}
				});
				marker = new GoogleMarker(markerOptions);
				marker.setMap(map.getMap());
			});
		}
	});
}

var maps = [];
function loadMap (map) {
	maps.push(map);
	if (typeof google.load != 'undefined') {
		google.load('maps', '3', {'other_params': 'sensor=false'});
		google.setOnLoadCallback(displayMaps);
	}
}

var GoogleMarker = new Class({
	Implements: [Options, Events],
	options: {
		title: '',
		address: ''
	},
	map: null,
	marker: null,
	initialize: function(options) {
		this.setOptions(options);
		this.marker = new google.maps.Marker(options);
		this.geocode();
	},
	getMarker: function () {
		return this.marker;
	},
	setMap: function (map) {
		this.marker.setMap(map);
	},
	getMap: function () {
		return this.marker.getMap();
	},
	setPosition: function (latlng) {
		this.marker.setPosition(latlng);
	},
	getPosition: function () {
		return this.marker.getPosition();
	},
	setAddress: function (address) {
		this.options.address = address;
	},
	getAddress: function () {
		return this.options.address;
	},
	setTitle: function (setTitle) {
		this.options.title = title;
		this.marker.setTitle(this.options.title);
	},
	getTitle: function () {
		return this.marker.getTitle();
	},
	geocode: function () {
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({
			address: this.options.address
		}, this.evtGeocoded.bind(this));
	},
	evtGeocoded: function (response, status) {
		if (status == 'OK' && response.length > 0) {
			var result = response[0];
			this.marker.setPosition(result.geometry.location);
		}
		this.fireEvent('complete', this);
	}
});

var GoogleMap = new Class({
	Implements: Options,
	options: {
		zoom: 13,
		coords: {
			x: 52.522906,
			y: -1.845703
		},
		mapTypeControl: true,
		mapTypeControlOptions: {style: 2}, // google.maps.MapTypeControlStyle.DROPDOWN_MENU
		navigationControl: true,
		navigationControlOptions: {style: 0}, // google.maps.NavigationControlStyle.DEFAULT
		mapTypeId: 'roadmap' // google.maps.MapTypeId.ROADMAP
	},
	element: null,
	initialize: function (element, options) {
		this.element = $(element);
		this.setOptions(options);

		this.element.empty();
		this.setMap(new google.maps.Map(this.element, this.options));
	},
	getElement: function () {
		this.element;
	},
	getMap: function () {
		return this.map;
	},
	setMap: function (map) {
		this.map = map;
	}
});
