COORDINATEROUND=10000000;

function roundCoordinate(x){
  return Math.round(x*COORDINATEROUND)/COORDINATEROUND;
}

function showImg(url,w,h){
  var photo = window.document.getElementById('photo');
  var divPhoto = window.document.getElementById('div-photo');
  photo.width = w;
  photo.height = h;
  photo.src = url;
  photo.style.marginTop = Math.round((660 - h)/2)+"px";
  divPhoto.style.zIndex = 3;
}	

function closeImg(){
  var photo = window.document.getElementById('photo');
  var divPhoto = window.document.getElementById('div-photo');
  photo.width = 1;
  photo.height = 1;
  divPhoto.style.zIndex = 1;
  photo.src = "vide.jpg";
}

function createMarker(point,id) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    process_it = function(doc) {
      var tabs = [];
      var labels = ["Infos", "Photos"];
      htmls = doc.split("--part--")
      for (var i=0; i<htmls.length; i++) {
        tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
      }
      marker.openInfoWindowTabsHtml(tabs);
    } 
    GDownloadUrl('index.php?getdata&id='+id, process_it);
  });
  return marker;
}
	
function go() {
  lng = parseFloat(document.getElementById("preset").value.split(",")[0]);
  lat = parseFloat(document.getElementById("preset").value.split(",")[1]);
  scale = parseInt(document.getElementById("preset").value.split(",")[2]);
  map.setCenter(new GLatLng(lat,lng),scale);
}	
		
GMap.prototype.centerAndZoomOnBounds = function(bounds) {
    var span = new GSize(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY);
    var center = new GPoint(bounds.minX + span.width / 2., bounds.minY + span.height / 2.);
    var newZoom = this.spec.getLowestZoomLevel(center, span, this.viewSize);
    if (this.getZoomLevel() != newZoom) {
        this.centerAndZoom(center, newZoom);
    } else {
        this.recenterOrPanToLatLng(center);
    }
} 
//// exemple :
//// create a set of points
//var points = [new GPoint(...), new GPoint(...), ...];
//
//var bounds = new GBounds(Number.MAX_VALUE, Number.MAX_VALUE, -
//Number.MAX_VALUE, -Number.MAX_VALUE);
//
//// extend the bounds to include all the points.
//for (var i = 0; i < points.length; i++) {
//bounds.extend(points[i]);
//
//}
//
//// center and zoom on the bounds.
//map.centerAndZoomOnBounds(bounds);

function eopEvent(e){
    if (!e) {e = window.event;}
    this.event = e;

    if   (e.wheelDelta)             {this.wheelDelta = e.wheelDelta;}
    if   (e.detail)                 {this.wheelDelta = e.detail * -40;}
    if   (e.target)                 {this.source = e.target;}
    else                            {this.source = e.srcElement;}
    if   (e.pageX || e.pageY)       {this.mouseX = e.pageX; this.mouseY = e.pageY;}
    else if(e.clientX || e.clientY) {this.mouseX = e.clientX + document.body.scrollLeft;
                                     this.mouseY = e.clientY + document.body.scrollTop;}
    this.stuck = function(){if (this.event.stopPropagation){this.event.preventDefault(); this.event.stopPropagation();} else {this.event.cancelBubble = true; this.event.returnValue = false;}}
}

function doMouseWheel(e){
    var mp, zoomlevel, newCenter, newZoom;

    e         = new eopEvent(e);
    mp        = new GLatLng(mouseLat, mouseLon);
    zoomlevel = map.getZoom();

    if  (e.wheelDelta >= 120) 
          {newCenter = mp.scaleRelative(map.getCenter(),  2); newZoom = zoomlevel + 1;}
    else  {newCenter = mp.scaleRelative(map.getCenter(), -1); newZoom = zoomlevel - 1;}

    if (newZoom <  0) {newZoom = 0;} if (newZoom > 19) {newZoom = 19;}

    map.setCenter (newCenter, newZoom);
    e.stuck();
}

var mapDiv = document.getElementById("map");
var map = new GMap2(mapDiv);

if (mapDiv.addEventListener){ 
    GEvent.addDomListener(mapDiv, 'DOMMouseScroll', doMouseWheel);
} else {
    mapDiv.onmousewheel = doMouseWheel;
}
GEvent.addListener(map, "mousemove", function(p){mouseLat = p.lat(); mouseLon = p.lng();});
//GEvent.addListener(map, 'movestart', function() {});
//GEvent.addListener(map, 'moveend',   function() {});
GLatLng.prototype.scaleRelative = function(a, z) {
    var nx = ((this.lng() - a.lng()) / z) + a.lng();
    var ny = ((this.lat() - a.lat()) / z) + a.lat();
    return new GLatLng( ny, nx );
};

var clusterer = new Clusterer( map );
clusterer.maxVisibleMarkers = 10;
clusterer.gridSize = 5;
clusterer.minMarkersPerClusterer = 5;
clusterer.maxLinesPerInfoBox = 6;
  	    
//// ====== Restricting the range of Zoom Levels =====
//// Get the list of map types      
//var mt = map.getMapTypes();
//// Overwrite the getMinimumResolution() and getMaximumResolution() methods
//for (var i=0; i<mt.length; i++) {
//	mt[i].getMinimumResolution = function() {return 2;}	
//	mt[i].getMaximumResolution = function() {return 19;}	
//}	
	
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

var EditIcon = new GIcon();
EditIcon.image = "pin_yellow.png";
EditIcon.shadow = "pin_shadow.png";
EditIcon.iconSize = new GSize(12, 20);
EditIcon.shadowSize = new GSize(22, 20);
EditIcon.iconAnchor = new GPoint(6, 20);
EditIcon.infoWindowAnchor = new GPoint(5, 1);

GEvent.addListener(map, "dblclick", function(marker, point) {
  if (!marker) {
     var temp = point.toString().split(',');
     var lat = roundCoordinate(temp[0].replace(/\(/,''));
     var lon = roundCoordinate(temp[1].replace(/\)/,'').replace(/^ /,''));
     marker = new GMarker(point,EditIcon);

     var $code = "<center><table>";
     $code += "<tr><td class='infowHead'>Latitude</td><td>"+lat+" °</td></tr>";
     $code += "<tr><td class='infowHead'>Longitude</td><td>"+lon+" °</td></tr>";
     $code += "<tr><td align='center'><a href='http://bleuazur.com/gps/blog/?lat="+lat+"&lon="+lon+"&zoom="+map.getZoom()+"&mark'>URL</a></td>";
     $code += "<td align='center'><a href='http://bleuazur.com/gps/blog/?kmzLat="+lat+"&kmzLon="+lon+"'>GoogleEarth</a></td></tr>";
     $code += "</table></center>";

     GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml($code);
     });

     map.addOverlay(marker);
     marker.openInfoWindowHtml($code);
  }
  
});

