

function Mapa() {
	var mapTypes = G_DEFAULT_MAP_TYPES;
	for(var i = 0; i < mapTypes.length; i++){
		mapTypes[i].getMinimumResolution = function(latlng){ return 8;};
	}
	this.map = new GMap2(document.getElementById("mapa"), {mapTypes: mapTypes});
	this.center = new GLatLng(42.05337156043361,2.89764404296875);
	this.zoom = 8;
  	this.map.setCenter(this.center, this.zoom);
	
	this.map.addControl(new GSmallMapControl ());
	this.map.enableContinuousZoom();
  
	this.baseIcon = new GIcon();
	this.baseIcon.image = "imatges/icones/mm_20_black.png";
	this.baseIcon.iconSize = new GSize(20, 25);
	this.baseIcon.iconAnchor = new GPoint(8,25);

	this.groupIcon = new GIcon();
	this.groupIcon.image = "imatges/icones/mm_35.png";
	this.groupIcon.iconSize = new GSize(20, 20);
	this.groupIcon.iconAnchor = new GPoint(10, 10);

	this.iconaRealPuntSeleccionat = null;
	this.puntSeleccionat = null;
	this.iconaPuntSeleccionat = "imatges/icones/mm_25_seleccio.png";
	this.puntSeleccionatMogut = false;
	
	this.markerManager = new GMarkerManager(this.map, { borderPadding: 50 });
	this.punts = new Array;
}

Mapa.prototype.ferBounds = function(bounds) {
	this.map.setCenter(bounds.getCenter(),this.map.getBoundsZoomLevel(bounds));
	var mida=this.map.getSize();
	var sw = this.map.fromLatLngToDivPixel(bounds.getSouthWest());
	var ne = this.map.fromLatLngToDivPixel(bounds.getNorthEast());
	var points = new Array();
	points[0]=bounds.getSouthWest();
	points[1]=bounds.getNorthEast();
	if ((sw.x < 10)||(sw.y > mida.height-10)||(ne.x > mida.width-10)||(ne.y < 20)) {
		this.zoom--;
		this.map.setCenter(this.center,this.zoom);
	}
}

Mapa.prototype.refresca = function() {
	this.map.checkResize();
	this.bounds = this.map.getBounds();
	this.zoom = this.map.getZoom();
	this.center = this.map.getCenter();
}
Mapa.prototype.centraPunt = function (punt) {
	this.map.setCenter(punt.getLatLng(),this.zoom);
	while (!punt._isVisible) {
		if (this.zoom<17) this.zoom++;
		this.map.setCenter(punt.getLatLng(),this.zoom);
	}
}





Mapa.prototype.createMarker = function (id, nom, url, point, zoom, icona, can_drag, afegir) {
	if (afegir==undefined) afegir=false;
	var marker = new GMarker(point, {draggable: can_drag, icon: icona, title: nom});
	this.punts[id]=marker;
//	marker.zoom = zoom;

	GEvent.addListener(marker, "click", function() {
		if (url) {
			if (url.substr(0,4)=="http") window.open(url);
			else window.location=url.replace(/&amp;/g,"&");
		}
	});
	
	if (can_drag) {
//		marker.disableDragging(); lat: 41.981250782878995, lng:2.823486328125
		GEvent.addListener(marker,"dragstart",function() {
			appMapa.puntSeleccionatMogut=true;
		});
		GEvent.addListener(marker,"dragend",function() {
			var p = marker.getLatLng();
			var elm=document.getElementById("input_lat");
			elm.value=p.lat();
			var elm=document.getElementById("input_lng");
			elm.value=p.lng();
		});
	}
	
  	return marker;
}

function info(str) {
return;
	var elm=document.getElementById("dreta_dins");
	elm.innerHTML += str+"<br />";
}




function afegir_punt() {
	var ic = new GIcon(appMapa.baseIcon, "imatges/icones/mm_25_seleccio.png");
	var p=appMapa.center;
	var marker=appMapa.createMarker(0,"punt nou",p,9,ic,true);
	appMapa.markerManager.addMarker(marker, 9, 17);
//	appMapa.seleccionaPunt(marker);
//	appMapa.carregaInfoPunt(0,true,p.lat(),p.lng());
}
function afegir_punt2(categoria,nom,lat,lng) {
	var ic = new GIcon(appMapa.baseIcon, "imatges/icones/"+appMapa.categories[categoria].icona);
	var marker=appMapa.createMarker(0,nom,new GLatLng(lat,lng),9,ic,true);
	appMapa.markerManager.addMarker(marker, 9, 17);
//	appMapa.seleccionaPunt(marker);
//	appMapa.centraPuntSeleccionat();
//	appMapa.carregaInfoPunt(0,true,lat,lng);
}


