var map = null;
var point = null;
var marker = null;
var geocoder = null;
/* ornox variable for map display */
var ornox_timer = null;
var ornox_address = null;
var ornox_map_lat = null;
var ornox_map_lng = null;
var ornox_map_zoom = null;
var ornox_marker_lat = null;
var ornox_marker_lng = null;
var ornox_enable_dragging = false;

    //<![CDATA[

    function GMAPinitialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        /* coordonnees pour la france */
        point = new GLatLng(46.227638, 2.213749);
        map.setCenter(point, 5);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setMapType(G_NORMAL_MAP);

		if( ornox_enable_dragging == true ) {
	        marker = new GMarker(point);
	        map.addOverlay(marker);
        }
        geocoder = new GClientGeocoder();
      }
    }

    //]]>
function callDoShowAddress() {
	doShowAddress();
}

function doShowAddress() {
  if( !ornox_address ) {
  	if( ornox_timer )
  		clearTimeout(ornox_timer);
  	return;
  }
  if (geocoder) {
  	 if( ornox_timer )
  	 	clearTimeout(ornox_timer);
     geocoder.getLatLng(
       ornox_address,
       function(point) {
         if (point) {
           map.setCenter(point, 12);
           map.clearOverlays();
		   if( ornox_enable_dragging == true || ornox_enable_dragging == false ) {
	           marker = new GMarker(point, {draggable: true});
	           marker.value = 1;
	
			  if( ornox_enable_dragging == true) {
	            marker.enableDragging();
				if( updMarkerCoordonate ) {
				latlng = marker.getLatLng();
				updMarkerCoordonate(latlng.lat(),latlng.lng());
				}
				if( 'function' == typeof(updMapCoordonate) ) {
				latlng = map.getCenter();
				updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
				}
				
				GEvent.addListener(marker, "dragstart", function() {
				map.closeInfoWindow();
				});
				
				GEvent.addListener(marker, "dragend", function() {
				if( updMarkerCoordonate ) {
				latlng = marker.getLatLng();
				updMarkerCoordonate(latlng.lat(),latlng.lng());
				}
				});
			  }
			  else
	            marker.disableDragging();
        	}
			
			GEvent.addListener(map, "moveend", function() {
			if( 'function' == typeof(updMapCoordonate) ) {
			latlng = map.getCenter();
			updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
			}
			});
			
			GEvent.addListener(map, "zooming", function() {
			if( 'function' == typeof(updMapCoordonate) ) {
			latlng = map.getCenter();
			updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
			}
			});

			if( ornox_enable_dragging == true || ornox_enable_dragging == false )
           		map.addOverlay(marker);
       
         }
       }
     );
   }
   else {
	 ornox_timer = setTimeout("callDoShowAddress()", 500);
   }
 }

function showAddress(address,dragflg) {
  ornox_address = address;
  ornox_enable_dragging = dragflg;
  if (geocoder ) {
  	doShowAddress();
   }
   else {
	 ornox_timer = setTimeout("callDoShowAddress()", 1000);
   }
 }
 
function callDoShowLatLng() {
	doShowLatLng();
}

function doShowLatLng() {
  if( !ornox_map_lat || !ornox_map_lng || !ornox_map_zoom || !ornox_marker_lat || !ornox_marker_lng) {
  	if( ornox_timer )
  		clearTimeout(ornox_timer);
  	return;
  }
  if (geocoder) {
  	if( ornox_timer )
  		clearTimeout(ornox_timer);
    point = new GLatLng(parseFloat(ornox_map_lat), parseFloat(ornox_map_lng));
    map.setCenter(point, parseInt(ornox_map_zoom));
       map.clearOverlays();
       if( ornox_enable_dragging == true || ornox_enable_dragging == false ) {
	       marker_point = new GLatLng(parseFloat(ornox_marker_lat), parseFloat(ornox_marker_lng));
	       marker = new GMarker(marker_point, {draggable: ornox_enable_dragging});
	       if( ornox_enable_dragging == true )
	       	marker.enableDragging();
	       marker.value = 1;
	
		   /* if( ornox_enable_dragging ) { */
	       	if( 'function' == typeof(updMapCoordonate) ) {
	       	latlng = marker.getLatLng();
	       	updMarkerCoordonate(ornox_marker_lat, ornox_marker_lng);
	       	}
	
	       	GEvent.addListener(marker, "dragstart", function() {
	         map.closeInfoWindow();
	       	});
	
	       	GEvent.addListener(marker, "dragend", function() {
	         if( 'function' == typeof(updMapCoordonate) ) {
	       	   latlng = marker.getLatLng();
	       	   updMarkerCoordonate(latlng.lat(),latlng.lng());
	         }
	       	});
	       /*}*/
       }

       GEvent.addListener(map, "moveend", function() {
         if( 'function' == typeof(updMapCoordonate) ) {
         	latlng = map.getCenter();
         	updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
          }
       });

       GEvent.addListener(map, "zooming", function() {
         if( 'function' == typeof(updMapCoordonate) ) {
       	   latlng = map.getCenter();
       	   updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
         }
       });
       
		if( ornox_enable_dragging == true || ornox_enable_dragging == false )
       		map.addOverlay(marker);       
    }
   else {
	 ornox_timer = setTimeout("callDoShowLatLng()", 500);
   }
}
 
function showLatLng(lat,lng,zoom,marker_lat,marker_lng,dragflg) {
  // alert("in showAdress..., address="+address);
  ornox_map_lat = lat;
  ornox_map_lng = lng;
  ornox_map_zoom = zoom;
  ornox_marker_lat = marker_lat;
  ornox_marker_lng = marker_lng;
  ornox_enable_dragging = dragflg;
  if (geocoder ) {
  	doShowLatLng();
   }
   else {
	 ornox_timer = setTimeout("callDoShowLatLng()", 1000);
   }
 }
 
 function updAddress(address,fname,updCoord) {
 	  ornox_address = address;
      geocoder.getLatLng(
       ornox_address,
       function(point) {
         if (!point) {
           // alert(address + " non trouvée!!");
         } else {
         	if( ornox_address.indexOf('France') > 0 || ornox_address.indexOf('france') > 0  )
	           map.setCenter(point, 12);
			else
	           map.setCenter(point, 9);
           map.clearOverlays();
           marker = new GMarker(point, {draggable: true});
           marker.value = 1;

           if( 'function' == typeof(updMapCoordonate) ) {
           	latlng = marker.getLatLng();
           	updMarkerCoordonate(latlng.lat(),latlng.lng());
           }
           if( 'function' == typeof(updMapCoordonate) ) {
           	   latlng = map.getCenter();
           	   updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
           }

           GEvent.addListener(marker, "dragstart", function() {
             map.closeInfoWindow();
           });

           GEvent.addListener(marker, "dragend", function() {
             if( 'function' == typeof(updMapCoordonate) ) {
           	   latlng = marker.getLatLng();
           	   updMarkerCoordonate(latlng.lat(),latlng.lng());
             }
           });
           
           GEvent.addListener(map, "zooming", function() {
             if( 'function' == typeof(updMapCoordonate) ) {
           	   latlng = map.getCenter();
           	   updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
             }
           });

           
           GEvent.addListener(map, "moveend", function() {
             if( 'function' == typeof(updMapCoordonate) ) {
             	latlng = map.getCenter()
             	updMapCoordonate(latlng.lat(),latlng.lng(),map.getZoom());
              }
           });

           map.addOverlay(marker);
         }
       }
     );
 }

