/*
File : zones.js 
18/01/09
Découpage surface terre en zones. Manipulation de ces zones
geolinko project
copyright : lpCom
*/

var map = null;
var geocoder = null;

var markerLocalisation = null;
var currentBounds = null;

function setLatLngCookies(point) {
	setCookie('current_lat', point.lat());
	setCookie('current_lng', point.lng());	
}

function setMarkerPosition(latlng) {
    if (!latlng) {
        jAlertWarning(ALERT_NAH_AJAX_MESS1, '');
        } else {	
				map.panTo(latlng);					
				setLatLngCookies(latlng);				
				if (markerLocalisation) {		
					markerLocalisation.setLatLng(latlng);
					latlngAnnounce=latlng; // ajout 26/08/09
					if (mode==1) display_tabMHO(latlngAnnounce, polyZoneIntervention);				
					}
					else {
						if (!isIE6()) var ico = new GIcon(G_DEFAULT_ICON, ICON_DIR + ICON_LOCAL + ICON_FILE_EXT);
							else var ico = new GIcon(G_DEFAULT_ICON);
						markerLocalisation = new GMarker(latlng, {clickable:false, draggable:true, icon:ico});
						GEvent.addListener(markerLocalisation, "dragend", function() {
							latlngAnnounce=markerLocalisation.getLatLng();
							setLatLngCookies(latlngAnnounce);							
							showGPS(latlngAnnounce);
							if (!isIE6()) showZones(latlngAnnounce);
							if (mode==1) display_tabMHO(latlngAnnounce, polyZoneIntervention);
							});					
						map.addOverlay(markerLocalisation);	
						}
				/*markerLocalisation.openInfoWindowHtml(address);	*/			
				showGPS(latlng);						
				if (!isIE6()) showZones(latlng);					
				latlngAnnounce=latlng;
				}			
}

function isTown(address) {
	if (address == initial_address_shown) return true;
	var words = address.split(' ');
	if (words.length==1) return true; /*!!!!*/
	return false;
}

function deltaPoint(point) {
	var x1=Math.random()/100;
	var s1=Math.random();
	if (s1>0.5) s1=1; else s1=-1;
	var x2=Math.random()/100;
	var s2=Math.random();
	if (s2>0.5) s2=1; else s2=-1;				
	return new GLatLng(point.lat()+(s1*x1), point.lng()+(s2*x2));
}
	
/* setMarkerAdress(address) : positionne sur la carte un marker pour l'adresse */
function setMarkerAddress(address, createSearchFlag) {
      if (geocoder) {
        geocoder.getLatLng(address, function (point) {
            if (!point) {
              jAlertWarning(address + ALERT_NAH_AJAX_MESS2, '');
            } else {
				//on génère un léger décalage aléatoire pour que les markers ne s'empile pas au même endroit
				// si l'adresse n'est a priori que le nom d'une ville
				if ((mode==2) && isTown(address))		
					point = deltaPoint(point);
				setCookie('current_address', address);
				if (polyZoneIntervention) 
					movePolyzone(polyZoneIntervention, latlngAnnounce, point);				
				setMarkerPosition(point);
				if (createSearchFlag)
					createPolyzone();
				if (mode == 1) showServices(); //rajouté pour visualisation automatiques des annonces présentes
				}
			});
        }
    }

function getZoneId(lat, lng) {
var zoneId;

	if (lng >= 0) {
		zoneId = (Math.floor((90-lat)/GAMMAZONE) * N2ZONE) + Math.ceil(lng/GAMMAZONE);
	} else {
		zoneId = (Math.ceil((90-lat)/GAMMAZONE) * N2ZONE) - Math.floor(-1*lng/GAMMAZONE);
	}
	return zoneId;
}

function getZoneRow(zoneId) {
	return((zoneId-(zoneId%N2ZONE))/N2ZONE)+1;
}

function getZoneCol(zoneId) {
	return zoneId%N2ZONE;
}

function showGPS(point) {
if (show_gps_mode == '0') return;
	document.getElementsByName("gpsLat")[0].value = point.lat();
	document.getElementsByName("gpsLng")[0].value = point.lng();	
	document.getElementsByName("gpsZone")[0].value = getZoneId(point.lat(),point.lng());
	document.getElementsByName("gpsRow")[0].value = getZoneRow(getZoneId(point.lat(),point.lng()));
	document.getElementsByName("gpsCol")[0].value = getZoneCol(getZoneId(point.lat(),point.lng()));	
}


function showZones(point) {/* attention : fonction à revoir (fin/début de rangée non traité)*/
if (show_gps_mode == '0') return;
var lat=point.lat();
var lng=point.lng();
var zone=getZoneId(lat,lng);
	tracerZone(zone);
	tracerZone(zone-N2ZONE+1);
	tracerZone(zone-N2ZONE);
	tracerZone(zone-N2ZONE-1);
	tracerZone(zone+1);
	tracerZone(zone-1);
	tracerZone(zone+N2ZONE+1);
	tracerZone(zone+N2ZONE);
	tracerZone(zone+N2ZONE-1);	
}
	
function tracerZone(nZone) {
var latO = 90 - (Math.ceil(nZone/N2ZONE)*GAMMAZONE);
var alpha = (nZone-(Math.floor(nZone/N2ZONE)*N2ZONE)-1)*GAMMAZONE;
var lngO = 0;
if ((alpha<=180) && (alpha>=0)) lngO = alpha; else lngO = alpha-360;
map.addOverlay(new GPolyline([new GLatLng(latO, lngO),
							  new GLatLng(latO+GAMMAZONE, lngO),
							  new GLatLng(latO+GAMMAZONE, lngO+GAMMAZONE),	
							  new GLatLng(latO, lngO+GAMMAZONE),
							  new GLatLng(latO, lngO)],							  
							  "#ff0000", 1,1, {clickable:false}));
}	
	


function inBounds(cB, nB) {
if (nB[0]>=cB[0] && nB[1]>=cB[1] && nB[2]<=cB[2] && nB[3]<=cB[3])
	return true;
	else return false;
}

function max(x,y) {if (x>y) return x; else return y;}
function min(x,y) {if (x<y) return x; else return y;}
function extendBounds(cB, nB) {
	return new Array(min(nB[0],cB[0]), min(nB[1],cB[1]), max(nB[2],cB[2]), max(nB[3],cB[3]));
}

function getDeltaBounds() {
var newBounds = getZoneBounds(polyZoneIntervention);
var dataBounds = '';
var extBounds;
//if (currentBounds && newBounds[0]==currentBounds[0] && newBounds[1]==currentBounds[1] && newBounds[2]==currentBounds[2] && newBounds[3]==currentBounds[3]) return null; /* no change*/
if (currentBounds && inBounds(currentBounds, newBounds)) return null;/* no change*/
if (currentBounds && !inBounds(currentBounds, newBounds)) {
	extBounds = extendBounds(currentBounds, newBounds);
	dataBounds = '&minusnrow='+currentBounds[0]+'&minusncol='+currentBounds[1]+'&minussrow='+currentBounds[2]+'&minusscol='+currentBounds[3];
	}
	else extBounds = newBounds;
dataBounds += '&nrow='+extBounds[0]+'&ncol='+extBounds[1]+'&srow='+extBounds[2]+'&scol='+extBounds[3];
currentBounds=extBounds;
return dataBounds;	
}












	