function GISApp()
{
	
}

GISApp.prototype.showPoint = function(latlng)
{
	document.getElementById(this.divId).style.width = '300px';
	document.getElementById(this.divId).style.height = '250px';
	
	this.map = new GMap2(document.getElementById(this.divId));
	this.map.setCenter(latlng, 15);
	
	var marker = new GMarker(latlng);
	this.map.addOverlay(marker);
	//marker.openInfoWindowHtml(this.address);
	
	document.getElementById(this.latId).value = latlng.lat();
	document.getElementById(this.latId).validFill = 1
	document.getElementById(this.lngId).value = latlng.lng();
	document.getElementById(this.lngId).validFill = 1
}

GISApp.prototype.getPoint = function()
{
	document.getElementById(this.divId).style.width = '900px';
	document.getElementById(this.divId).style.height = '400px';
	
	this.map = new GMap2(document.getElementById(this.divId));
	
	this.map.setCenter(new GLatLng(47.15984,2.988281), 5);
	this.map.addControl(new GSmallMapControl());
	this.map.addControl(new GMapTypeControl());
	
	GEvent.bind(this.map, "click", this, this.onMapClick);
}

GISApp.prototype.onMapClick = function(overlay,point)
{
	if(confirm('Confirmez votre positionnement'))
	{
		if(point.x && point.y)
		{
			this.map.clearOverlays();
			var marker = new GMarker(new GLatLng(point.y,point.x));
			this.map.addOverlay(marker);
			document.getElementById(this.latId).value = point.y;
			document.getElementById(this.lngId).value = point.x;
		}
	}
}

function callGeoCoding(divId,latId,lngId,address,cp,ville,pays)
{
	if(document.getElementById(address).value && document.getElementById(cp).value && document.getElementById(ville).value && document.getElementById(pays).value)
	{
		var address = document.getElementById(address).value + " " + document.getElementById(cp).value + " " + document.getElementById(ville).value + " " + document.getElementById(pays).options[document.getElementById(pays).selectedIndex].text;
		
		startMap 					= new GISApp();
		startMap.divId 		= divId;
		startMap.address 	= address;
		startMap.latId 		= latId;
		startMap.lngId 		= lngId;
		
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address,findGeoCoding);
	}
}

function findGeoCoding(latlng)
{
	if (!latlng)
	{
		alert('Géolocalisation impossible par les informations saisies\nVeuillez cliquer sur la carte pour vous positionner');
	  startMap.getPoint();
	}
	else
	{
		startMap.showPoint(latlng);
	}
}

function startSearchCarto()
{
	//this.map = new GMap2(document.getElementById('srh_colG_mapId'));
	//this.map.setCenter(new GLatLng(47.15984,2.988281), 5);
	
	//startMap 					= new GISApp();
	//startMap.divId 		= divId;
	//startMap.address 	= address;
	//startMap.latId 		= latId;
	//startMap.lngId 		= lngId;
}






//
//	CARTO du moteur de recherche
//

function GISResult(spanId,Lat,Lng,zoomFactor)
{
	// check arg passed integrity
	var Lat 					= parseFloat(Lat);
	var Lng 					= parseFloat(Lng);
	var zoomFactor 		= parseInt(zoomFactor);
	
	this.marker 			= Array();
	this.markerinfo  	= Array();
	
	// Instancy main class
  this.map = new GMap2(document.getElementById(spanId));
  this.map.setCenter(new GLatLng(Lat,Lng),zoomFactor);
  this.map.addControl(new GSmallMapControl());
	this.map.addControl(new GMapTypeControl());
	
	GEvent.bind(this.map, "zoomend", this, 
		function(overlay,point)
		{
				loadXMLDoc('srh_ctrlmapId','../front/MapStoreZoom.php?zoomFactor=' + this.map.getZoom() + "&lat=" + this.map.getCenter().lat() +  "&lng=" + this.map.getCenter().lng());
		}
	);

	GEvent.bind(this.map, "moveend", this, 
		function(overlay,point)
		{
				loadXMLDoc('srh_ctrlmapId','../front/MapStoreZoom.php?zoomFactor=' + this.map.getZoom() + "&lat=" + this.map.getCenter().lat() +  "&lng=" + this.map.getCenter().lng());
		}
	);
}

GISResult.prototype.manageIcon = function(doAction)
{
	var now = new Date();
	timestamp = now.getTime();
	
	GDownloadUrl('../include/fnc_extractcarto.php?timestamp=' + timestamp, this.parseFile);
}



GISResult.prototype.parseFile = function(data, responseCode)
{
	application.map.clearOverlays();
	
	var xml = GXml.parse(data);
  
  var markers = xml.documentElement.getElementsByTagName("marker");

  for (var i = 0; i < markers.length; i++)
  {
		var HTMLData = GXml.value(markers[i]);
		
  	var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));

  	application.map.addOverlay(application.createMarker(point, markers[i].getAttribute("icon"), markers[i].getAttribute("id"), HTMLData,  markers[i].getAttribute("iwidth"), markers[i].getAttribute("iheight")));
 	}
}

GISResult.prototype.createMarker = function(point, iconFilename, markerId,  HTMLData, iWidth, iHeight)
{
	icon = new GIcon();
	icon.image = "../icon/" + iconFilename;
	
	icon.iconSize = new GSize(iWidth, iHeight);
	icon.iconAnchor = new GPoint(6,20);
	icon.infoWindowAnchor = new GPoint(6,20);
	
	this.marker[markerId] = new GMarker(point,icon);
	this.marker[markerId].id = markerId;
  this.markerinfo[markerId] = HTMLData;
	
  // type natif
  GEvent.addListener(this.marker[markerId], "click", function() {
    	this.openInfoWindowHtml(application.markerinfo[this.id]);
  });

	return this.marker[markerId];
}

GISResult.prototype.clearMap = function()
{
	application.map.clearOverlays();
}

GISResult.prototype.markerView = function(markerId, HTMLData)
{
	this.map.setCenter(this.marker[markerId].getLatLng());
	this.marker[markerId].openInfoWindowHtml(this.markerinfo[markerId]);
	application.map.panDirection(-1,0);
}
