module('apps.GoogleMaps').requires().toRun(function() {

Global.GoogleMapsLoadingDone = function() {
	apps.GoogleMaps.loadingDone()
}

Object.extend(apps.GoogleMaps, {
	divId: 'GoogleMapsCanvas',
	createMapDivInXenoMorph: function() {
		var xeno = new apps.GoogleMaps.XenoMorph(new Rectangle(0,0, 300, 200))
		xeno.openInWorld();
		return xeno;
	},
	loadingDone: function() {
		alertOK('Google Maps loaded');
	},
	loadScript: function() {
		Loader.loadJs('http://maps.google.com/maps/api/js?sensor=false&callback=GoogleMapsLoadingDone', null, false, true);
	},
	addSampleMap: function(latitude, longitude) {
		var myLatlng = new google.maps.LatLng(latitude, longitude),
			options = {
				zoom: 8,
				center: myLatlng,
				disableDefaultUI: true,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			},
		map = new google.maps.Map(document.getElementById(apps.GoogleMaps.divId), options);
		return map;
	},
});

XenoMorph.subclass('apps.GoogleMaps.XenoMorph',
'settings', {
	style: {borderWidth: 1, borderColor: Color.black, fill: null, suppressGrabbing: true},
},
'initializing', {
	defaultNode: function() {
		var div = XHTMLNS.create('div');
		div.setAttribute('id', apps.GoogleMaps.divId);
		div.style.width = '100px'
		div.style.height = '100px'
		return div;
	},
},
'accessing', {
	getMapDiv: function() { return this.foRawNode.childNodes[0] },
},
'updating', {
	translateBy: function($super, delta) {
		$super(delta);
		this.updateBounds();
		return this;
	},
	updateBounds: function() {
		var pos = this.worldPoint(this.shape.bounds().topLeft()),
			extent = this.shape.bounds().extent(),
			div = this.getMapDiv();
		div.style.position = 'absolute'
		div.style.left = pos.x + 'px'
		div.style.top = pos.y + 'px'
		div.style.width = extent.x + 'px'
		div.style.height = extent.y + 'px'
	},
},
'layouting', {
	adjustForNewBounds: function($super) {
		$super();
		this.updateBounds();
	},
},
'events', {
	handlesMouseDown: Functions.True,
	onMouseDown: Functions.True,
	onMouseMove: function(evt) {
		if (!evt.mouseButtonPressed && !this.hasHandles()) this.checkForControlPointNear(evt);
	},
});

}) // end of module