/*
GOOGLE MAP CLASS
*/
function gMap() {
	this.mapDivId = '';		//div id where the map will be drawn
	this.mapHt = '500px';	//height of the map div
	this.mapWt = '650px';	//width of the map div
	this.defZoom = 8;		//default zoom level
	
	this.defLat = '18.812718';	//default latitude
	this.defLng = '-70.252075';	//default longitude
	this.defPCode = 'B73 5SR';			//default post code
	this.defUseLatLng = 0;			//1 - use default lat long ; 0 - plot default via post code
	this.defPCodeFail = '';
	this.defDetailsWin = 1;
	this.showCustom = 1;
	
	this.mapDivObj = '';	//the object from the map id
	this.mapObj = '';		//main map object
	this.mapLat = '';		//main map latitude
	this.mapLng = '';		//main map longitude
	this.mapBaseIco = '';	//main map base icon
	
	this.custMType = true;					//show custom controls
	this.custMBtn = 'true||true';			//hybrid, sattelite
	this.custScroll = false;				//scrolling allowed
	
	this.latLng = new Array();				//store all the lat and long to plot [lat, long]
	this.goToFunc = '';
	this.searchMode = 0;
	this.myDetails = new Array();

	this.pollArr = new Array();
	
	// function to load the map
	this.loadMap = function () {
		if (GBrowserIsCompatible()) {
			this.mapDivObj = document.getElementById(this.mapDivId);
			this.mapObj = new GMap2(this.mapDivObj);
			if (this.showCustom==1) this.customOpt();
			if (this.defUseLatLng==1) { 
				this.mapLat = this.defLat;
				this.mapLng = this.defLng;
				this.plotLoc(this.defLat, this.defLng, this.defZoom);
			}else{
				this.convLatLng_PCode(this.defPCode);
			}
			if (this.goToFunc) eval(this.goToFunc + '()');
			//alert(this.mapObj.getBounds().toSpan().toUrlValue());
		}		
	};
	
	//function to customize the map
	this.customOpt = function () {
		if (this.custMType == true) {
			//this.mapObj.enableScrollWheelZoom() ;
			// activation du zoom en fondu
			this.mapObj.enableContinuousZoom() ;
			// affichage des boutons de déplacement et de zoom
			this.mapObj.addControl(new GLargeMapControl()) ;
			// affichage des boutons de sélection du type de carte
			this.mapObj.addControl(new GMenuMapTypeControl()) ;
			// affichage de l'échelle de la carte
			this.mapObj.addControl(new GScaleControl()) ;
					
			// resolution minimale
			G_NORMAL_MAP.getMinimumResolution = function() { return 2 ; }
			// resolution minimale
			G_HYBRID_MAP.getMinimumResolution = function() { return 2 ; }
			// resolution minimale
			G_SATELLITE_MAP.getMinimumResolution = function() { return 2 ; }
					
			this.mapObj.addMapType(G_PHYSICAL_MAP) ;
			// redéfinition de la résolution maximale du type de carte relief
			G_PHYSICAL_MAP.getMaximumResolution = function() { return 15 ; }
			// resolution minimale
			G_PHYSICAL_MAP.getMinimumResolution = function() { return 2 ; }
			
			this.mapObj.setMapType(G_PHYSICAL_MAP) ;
		}
	};
	

	this.plotLoc = function (lati, lngi, zoomLvL) {
		this.mapObj.setCenter(new GLatLng(lati, lngi), zoomLvL);
	};

	
	this.cleanMap = function () {
		this.mapObj.clearOverlays();
		this.plotLoc(this.defLat, this.defLng, this.defZoom);
	};
	
	this.drawMarkersIco = function (dataCnt, dataArr, isLieu) {
		if (dataCnt>0) {
			for (var xI=0; xI<dataCnt; xI++) {
				if (dataArr['showGoogleMap'][xI]==1) {
					var lati = dataArr['lat'][xI];
					var lngi = dataArr['lon'][xI];
					var fleName = (isLieu==1) ? 'fiche.php' : 'fiche_service.php';
					var idName = (isLieu==1) ? 'id_lieu' : 'id_service';
					var extVar = new Array();
					extVar['name'] = dataArr['name'][xI];
					extVar['name'] = dataArr['name'][xI];
					extVar['imgFile'] = dataArr['imgFile1'][xI];
					extVar['imgFileW'] = dataArr['imgFileW'][xI];
					extVar['imgFileH'] = dataArr['imgFileH'][xI];
					extVar['data'] = dataArr['descr'][xI];
					extVar['id'] = dataArr['id'][xI];
					extVar['fleName'] = fleName;
					extVar['idName'] = idName;
					this.drawMarker(lati, lngi, isLieu, extVar);
				}
			}
		}
	};
	
	this.drawMarker = function(lati, lngi, isLieu, extVar) {
		var imgName = (isLieu==1) ? 'yellowEco' : 'redEco';
		var imgNameH = (isLieu==1) ? 'greenEco' : 'blueEco';
		var imgMarker = new GIcon(G_DEFAULT_ICON);
		imgMarker.image = '../images/' + imgName + '.png';
		imgMarker.iconSize = new GSize(32, 32);
		imgMarker.shadowSize = new GSize(0,0);
		imgMarker.iconAnchor = new GPoint(9, 34);
		var marker = new GMarker(new GLatLng(lati, lngi), imgMarker);
		marker.mapObj = this;
		marker.imgName = imgName;
		marker.imgNameH = imgNameH;
		marker.extVars = extVar;

		GEvent.addListener(marker, "mouseover", function() {
			this.setImage('../images/' + this.imgNameH + '.png');
			this.mapObj.showLoc(marker);
		});
		
		GEvent.addListener(marker, "mouseout", function() {
			this.setImage('../images/' + this.imgName + '.png');
		});
		
		this.mapObj.addOverlay(marker);	
	}
	
	this.showLoc = function (markerObj){
		var extVar = markerObj.extVars;
		var htmlTxT = '';
		infoDiv = document.createElement("div");
		infoDiv.style.height = '120px';
		infoDiv.style.width = '300px';
		infoDiv.style.textAlign = 'left';

		htmlTxT = "<table border='0' width='100%' cellpadding='0' cellspacing='0'>";
		htmlTxT += "<tr><td style='height:5px;line-height:5px;' colspan='3'>&nbsp;</td></tr>";
		htmlTxT += "<tr><td colspan='3' style='font-weight:bold;color:#2080b0;font-family:Arial;font-size:13px;'>" + extVar['name'] + "</td></tr>";
		htmlTxT += "<tr><td style='height:5px;line-height:5px;' colspan='3'>&nbsp;</td></tr><tr>";
		htmlTxT += "<td style='width:80px;'><a href='../inc/" + extVar['fleName'] + "?" + extVar['idName'] + "=" + extVar['id'] + "'><img src='" + extVar['imgFile'] + "' width='80' height='80' alt='' title='' border='0' /></a></td>";
		htmlTxT += "<td style='width:5px;'>&nbsp;</td>";
		htmlTxT += "<td valign='top' class='gjblkTxT'>" + extVar['data'] + "<br/><a href='../inc/" + extVar['fleName'] + "?" + extVar['idName'] + "=" + extVar['id'] + "'>>>> Leer más</a></td>";
		htmlTxT += "</tr>";
		htmlTxT += "</table>";
		infoDiv.innerHTML = htmlTxT;
		markerObj.openInfoWindow(infoDiv);
	}
	
}
window.onunload = "GUnload()";
