// Google Maps implementation of Oberon Maps
// intro  prototype for all the functions here
// OMap.prototype.google_
OMap.prototype.google_initMap = function(container_id){
	this._map = new GMap2(document.getElementById(container_id));
	this._geocoder = new GClientGeocoder();
}

OMap.prototype._smallZoomControl;
OMap.prototype._gmapTypeControl;

OMap.prototype.google_initCenter =  function(point, zoom, maptype) {
	if (GBrowserIsCompatible()) {
		this._smallZoomControl = new GSmallZoomControl();
		this._map.addControl(this._smallZoomControl);
		this._gmapTypeControl = new GMapTypeControl();
		this._map.addControl(this._gmapTypeControl);
		this._map.enableDoubleClickZoom();
		this._map.setCenter(point._latlng, zoom);
		//alert(maptype);
		switch (maptype){
			case 'r':	this._map.setMapType(G_NORMAL_MAP);
						break;
			case 'a':	this._map.setMapType(G_SATELLITE_MAP);
						break;
			case 'h':	this._map.setMapType(G_HYBRID_MAP);
						break;
			default:	this._map.setMapType(G_HYBRID_MAP);
						break;
		}
	}
}

OMap.prototype.google_unLoadMap =  function(){
	GUnload();
}

OMap.prototype.google_setMapType = function(new_type) {
	switch(new_type) {
		case 'r':	this._map.setMapType(G_NORMAL_MAP);
					break;
		case 'a':	this._map.setMapType(G_SATELLITE_MAP);
					break;
		case 'h':	this._map.setMapType(G_HYBRID_MAP);
					break;
		default:	this._map.setMapType(G_HYBRID_MAP);
					break;
	}
}

OMap.prototype.google_setCenter = function(point) {
	this._map.setCenter(point._latlng);
}

OMap.prototype.google_setCenterAndZoom = function(point, zoomlevel) {
	this._map.setCenter(point._latlng, zoomlevel);
}

OMap.prototype.google_getCenter = function(){
	var point = this._map.getCenter();
	return new OLatLng(point.lat(),point.lng());
}

OMap.prototype.google_getBounds = function(){
	var latlngbounds = this._map.getBounds();
	var result = new Array();
	result.push(new OLatLng(latlngbounds.getSouthWest().lat(),latlngbounds.getSouthWest().lng()));
	result.push(new OLatLng(latlngbounds.getNorthEast().lat(),latlngbounds.getNorthEast().lng()));
	return result;
}

OMap.prototype.google_setZoom = function(new_zoom){
	this._map.setZoom(new_zoom);
}

OMap.prototype.google_setCenterAndZoomFromPoints = function(points, default_level){
	var maxlat = -90;
	var minlat = 90;
	var maxlng = -180;
	var minlng = 180;
	for (var i=0; i<points.length; i++){
		if (points[i].lat()>maxlat){
			maxlat = points[i].lat();
		}
		if (points[i].lat()<minlat){
			minlat = points[i].lat();
		}
		if (points[i].lng()>maxlng){
			maxlng = points[i].lng();
		}
		if (points[i].lng()<minlng){
			minlng = points[i].lng();
		}
	}
	if ((maxlat!=minlat) && (maxlng!=minlng)){
		var latlngbounds = new GLatLngBounds(new GLatLng(minlat,minlng),new GLatLng(maxlat,maxlng));
		var zoomlevel = this._map.getBoundsZoomLevel(latlngbounds);
		var centerPoint = new GLatLng( (minlat+maxlat)/2, (minlng+maxlng)/2 );
		this._map.setCenter( centerPoint, zoomlevel);
	} else {
		this._map.setCenter( new GLatLng( minlat, minlng ), default_level );
	}
}

OMap.prototype.google_getZoom = function(){
	return this._map.getZoom();
}

OMap.prototype.google_panTo = function(point){
	this._map.panTo(point._latlng);
}

OMap.prototype.google_addOverlay = function(overlay){
	var g_overlay;
	if (overlay instanceof OMarker){
		g_overlay = overlay._marker;
	} else if (overlay instanceof OPolygon){
		g_overlay = overlay._polygon;
	}
	if (g_overlay!=undefined){
		this._map.addOverlay(g_overlay);
	}
}

OMap.prototype.google_removeOverlay = function(overlay){
	var g_overlay;
	if (overlay instanceof OMarker){
		g_overlay = overlay._marker;
	} else if (overlay instanceof OPolygon){
		g_overlay = overlay._polygon;
	}
	if (g_overlay!=undefined){
		this._map.removeOverlay(g_overlay);
	}
}

OLatLng.prototype.google_LatLng = function(lat,lng){
	return new GLatLng(lat, lng);
}

OLatLng.prototype.google_lat = function(){
	return this._latlng.lat();
}

OLatLng.prototype.google_lng = function(){
	return this._latlng.lng();
}

OLatLngBounds.prototype.google_LatLngBounds = function(point1, point2){
	var maxlat = -90;
	var minlat = 90;
	var maxlng = -180;
	var minlng = 180;
	var points = new Array();
	points.push(point1);
	points.push(point2);
	for (var i=0; i<points.length; i++){
		if (points[i].lat()>maxlat){
			maxlat = points[i].lat();
		}
		if (points[i].lat()<minlat){
			minlat = points[i].lat();
		}
		if (points[i].lng()>maxlng){
			maxlng = points[i].lng();
		}
		if (points[i].lng()<minlng){
			minlng = points[i].lng();
		}
	}
	return new GLatLngBounds(new GLatLong(minlat,minlng), new GLatLong(maxlat,maxlng));
}

OPolygon.prototype.google_Polygon = function(points){
	var gpoints = new Array();
	for (var i=0; i<points.length; i++){
		gpoints.push(new GLatLng(parseFloat(points[i].lat()),parseFloat(points[i].lng())));
	}
	
	var p = new GPolyline(gpoints, "#ff0000", 2, 0.7);
	return p;
}

OPolygon.prototype.google_setPoints = function(points,curmap){
	curmap._map.removeOverlay(this._polygon);
	this._polygon = this.google_Polygon(points);
	curmap._map.addOverlay(this._polygon);
}

OPolygon.prototype.google_hide = function(){
	this._polygon.hide();
}

OPolygon.prototype.google_show = function(){
	this._polygon.show();
}

OMarker.prototype._clickEvent;

OMarker.prototype.google_Marker = function(point, icon){
	if (icon!=undefined){
		return new GMarker(point._latlng,icon._icon);
	} else {
		return new GMarker(point._latlng);
	}
}

OMarker.prototype.google_setTitle = function(title){
}

OMarker.prototype.google_setDescription = function(description){
}

OMarker.prototype.google_setImage = function(image){
}

OMarker.prototype.google_setLinktitle = function(linktitle){
}

OMarker.prototype.google_setLink = function(link){
}

/**
 * google_setIcon
 * the icon cannot be changed after creation in google...
 */
OMarker.prototype.google_setIcon = function(icon){
}

OMarker.prototype.google_setLogo = function(logo){
}

OMarker.prototype.google_useHTML = function(html){
	if (html!=undefined){
		if (this._clickEvent!=undefined){
			GEvent.removeListener(this._clickEvent);
		}
		this._clickEvent = GEvent.addListener(this._marker,'click',function(){this.openInfoWindowHtml(html);});
	}
}

OMarker.prototype.google_getLatLng = function(){
	var res = this._marker.getPoint();
	return new OLatLng(res.lat(),res.lng(),'google');
}

OMarker.prototype.google_setLatLng = function(point){
	this._marker.setPoint(point._latlng);
}

OMarker.prototype.google_hide = function(){
	this._marker.hide();
}

OMarker.prototype.google_show = function(){
	this._marker.show();
}

OImageIcon.prototype.google_ImageIcon = function(image,offsetX,offsetY,sizeX,sizeY,shadow){
	var gicon = new GIcon();
	gicon.image = image;
	gicon.iconAnchor = new GPoint(offsetX,offsetY);
	gicon.iconSize=new GSize(sizeX,sizeY);
	gicon.shadowSize=new GSize(20,20);
	gicon.infoWindowAnchor=new GPoint(sizeX,sizeY/2);
	if (shadow!=undefined){
		gicon.shadow = shadow;
	} else {
		gicon.shadow = null;
	}
	return gicon;
}

OEvent.google_addEvent = function(trigger,curevent,handler){
	var gtrigger;
	if (trigger instanceof OMap){
		gtrigger = trigger._map;
	} else if (trigger instanceof OMarker){
		gtrigger = trigger._marker;
	}
	if (gtrigger!=undefined){
		if ((curevent=='click') && (trigger instanceof OMap)){
			var returnfunc = function(overlay, point){
				if (point!=undefined){
					handler(new OLatLng(point.lat(),point.lng(),'google'));
				}
			}
			var returnval = GEvent.addListener(gtrigger,curevent,returnfunc);
		} else if ((curevent=='click') && (trigger instanceof OMarker)){
			var returnfunc = function(){
				handler(trigger);
			}
			var returnval = GEvent.addListener(gtrigger,curevent,returnfunc);
		} else {
			var returnval = GEvent.addListener(gtrigger,curevent,handler);
		}
	}
	return returnval;
}

OEvent.google_deleteEvent = function(trigger,curevent,handle){
	GEvent.removeListener(handle);
}

OGeocoder.prototype.google_Geocoder = function(amap){
	return new GClientGeocoder();
}

OGeocoder.prototype.google_findLocation = function(address,handler){
	this._geocoder.getLatLng(address,function(point){
		if (point==null){
			handler(false,null);
		} else {
			handler(true,new OLatLng(point.lat(),point.lng(),'google'));
		}
	});
}