
var actual_map = false;

//var actual_alt=11;
//var actual_lat=41.65;
//var actual_lon=-0.90;


Mapa.onLoad = function()
{
  if(window.GBrowserIsCompatible && GBrowserIsCompatible()) actual_map= new Mapa();
} 

Mapa.onUnload = function()
{
   if(actual_map) GUnload();
   actual_map = false;
}

///////////////////////////////////////////////////////////////////
function Mapa()
{
	var alt, lat, lon;
	var centerPoint;
	var container;	//div container map
	
	if(arguments[0]) this.div = arguments[0];
	else             this.div    = "map_div";
	
	if(arguments[1]) this.caja = arguments[1];
	else             this.caja  = this.div;
	
	this.marcas = new Array;
	
	container = $("map_div");
//	container.style.width = '600px';
//	container.style.height = '400px';
//	container.style.display = '';
//	show("mapcaja", true);	

	alt = 0;//actual_alt;
	lat = 0;//actual_lat;
	lon = 0;//actual_lon;

	centerPoint = new GLatLng(lat, lon);
	
	this.mapa = new GMap2(container, {draggableCursor:"crosshair"});

	this.mapa.setCenter(centerPoint, alt);
	this.mapa.addControl(new GScaleControl());
	this.mapa.addControl(new GLargeMapControl());
	this.mapa.addControl(new GMapTypeControl());
	
    GEvent.addListener(this.mapa, "click", Mapa.onClick);
    GEvent.addListener(this.mapa, "singlerightclick", Mapa.onRightClick);

// Create our "tiny" marker icon
	this.picon = new GIcon();
	this.picon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	this.picon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	this.picon.iconSize = new GSize(12, 20);
	this.picon.shadowSize = new GSize(22, 20);
	this.picon.iconAnchor = new GPoint(6, 20);
	this.picon.infoWindowAnchor = new GPoint(5, 1);
}
Mapa.prototype.show = function(f)
{
	show(this.caja, f);	
}
Mapa.prototype.getEstado = function(act)
{
	var center;
	center  = this.mapa.getCenter();
	act.alt = this.mapa.getZoom();
	act.lat = center.lat();
	act.lon = center.lng();
}
Mapa.prototype.setEstado = function(act)
{
	var center;
	center = new GLatLng(act.lat, act.lon);
	this.mapa.setCenter(center, act.alt);
}
Mapa.prototype.resetMarcas=function()
{
	this.marcas = new Array();
	this.mapa.clearOverlays();
}
Mapa.prototype.updateMarcas=function()
{
	this.mapa.clearOverlays();
	for(i in this.marcas){
		this.mapa.addOverlay(this.marcas[i]);
	}
}
Mapa.prototype.nuevaMarca = function(obj, tipo)
{
	var marca = new GMarker(new GLatLng(obj.lat, obj.lon), tipo);
	if(obj.info) marca.info = obj.info;
	marca.obj = obj;
	this.marcas.push(marca);	
	this.mapa.addOverlay(marca);

	if(obj.onDragEnd) 	GEvent.addListener(marca, "dragend", Mapa.DragEnd);
//    if(obj.onDragStart)	GEvent.addListener(marca, "dragstart", Mapa.DragStart);
	return this.marcas.length - 1;//return nš de marca
}
Mapa.prototype.getMarca = function(i)
{
	return this.marcas[i];
}
Mapa.prototype.nuevoTramo = function(rec)
{
	var pts = new Array();
	if(rec.length < 2) return;
	for(i in rec){
		pts.push(new GLatLng(rec[i].lat, rec[i].lng));
	}
	this.mapa.addOverlay(new GPolyline(pts));
}
Mapa.prototype.enfocarMapa = function(rec)
{
	var e,w,n,s;
	if(rec.length < 4) return;
	e = -180;
	w = +180;
	n = -90;
	s =  90;
	for(i in rec){
		if(rec[i].tipo < 0) continue;
		if(rec[i].lng > e) e = rec[i].lng;
		if(rec[i].lng < w) w = rec[i].lng;
		if(rec[i].lat > n) n = rec[i].lat;
		if(rec[i].lat < s) s = rec[i].lat;
	}
	var centro = new GLatLng((n+s)/2, (e+w)/2);

	var ne = new GLatLng(n, e);
	var sw = new GLatLng(s, w);
	
	var b  = new GLatLngBounds(sw, ne);
	var z  = this.mapa.getBoundsZoomLevel(b);
	
	this.mapa.setCenter(centro, z);
}

Mapa.prototype.enfocarMarcas = function()
{
	var e,w,n,s;
	var i, pos;
	
	e = -180;
	w = +180;
	n = -90;
	s =  90;
	
	
	for(i in this.marcas){
		pos = this.marcas[i].getLatLng();
		if(pos.lng() > e) e = pos.lng();
		if(pos.lng() < w) w = pos.lng();
		if(pos.lat() > n) n = pos.lat();
		if(pos.lat() < s) s = pos.lat();
	}
	var centro = new GLatLng((n+s)/2, (e+w)/2);


	var ne = new GLatLng(n, e);
	var sw = new GLatLng(s, w);
	
	var b  = new GLatLngBounds(sw, ne);
	var z  = this.mapa.getBoundsZoomLevel(b);
	
	this.mapa.setCenter(centro, z);
}


////////////////////////////////////////////////////////////////////
Mapa.DragEnd = function()
{
//	var point = this.getPoint();
//    this.openInfoWindowHtml("<br>" + point);
    if(this.obj && this.obj.onDragEnd) this.obj.onDragEnd(this); 
}
///////////////////////////////////////////////////////////////////
Mapa.onRightClick = function(point, srcm, marker)
{
	//if(marker && marker.user) Marker_onRightClick(point, marker)
}
Mapa.onClick = function(marker) 
{
	if(marker && marker.obj && marker.obj.onClick){
		marker.obj.onClick(marker);
		return;
	}
    if(marker && marker.openInfoWindowHtml && marker.info){
		marker.openInfoWindowHtml(marker.info);
    }
}
/////////////////////////////////////////////////////////////////

