var map = null;
var point = null;
var _coef = null;

/* ********* */
   function EInsert(point, image, size, basezoom, zindex) {
        this.point = point;
        this.image = image;
        this.size = size;
        this.basezoom = basezoom;
        this.zindex=zindex||0;
        // Is this IE, if so we need to use AlphaImageLoader
        var agent = navigator.userAgent.toLowerCase();
        
        if ((agent.indexOf("msie") > -1) && (agent.indexOf("opera") < 1)){this.ie = true} else {this.ie = false}
        this.hidden = false;
      } 
 
      EInsert.prototype = new GOverlay();
 
      EInsert.prototype.initialize = function(map) {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.style.zIndex=this.zindex;
        if (this.zindex < 0) {
           map.getPane(G_MAP_MAP_PANE).appendChild(div);
        } else {
           map.getPane(1).appendChild(div);
        }
        this.map_ = map;
        this.div_ = div;
      }
   
      EInsert.prototype.makeDraggable = function() {
        this.dragZoom_ = map.getZoom();
        this.dragObject = new GDraggableObject(this.div_);
        
        this.dragObject.parent = this;
        
        GEvent.addListener(this.dragObject, "dragstart", function() {
          this.parent.left=this.left;
          this.parent.top=this.top;
        });

      
        GEvent.addListener(this.dragObject, "dragend", function() {
          var pixels = this.parent.map_.fromLatLngToDivPixel(this.parent.point);
          var newpixels = new GPoint(pixels.x + this.left - this.parent.left, pixels.y +this.top -this.parent.top);
          this.parent.point = this.parent.map_.fromDivPixelToLatLng(newpixels);
          this.parent.redraw(true);
          GEvent.trigger(this.parent, "dragend", this.parent.point);
        });    
      }

      EInsert.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
      }

      EInsert.prototype.copy = function() {
        return new EInsert(this.point, this.image, this.size, this.basezoom);
      }


      EInsert.prototype.redraw = function(force) {
       if (force) {
        var p = this.map_.fromLatLngToDivPixel(this.point);
        var z = this.map_.getZoom();
        var scale = Math.pow(2,(z - this.basezoom));
        var h=this.size.height * scale;
        var w=this.size.width * scale;

        this.div_.style.left = (p.x - w/2) + "px";
        this.div_.style.top = (p.y - h/2) + "px";

        if (this.ie) {
          var loader = "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.image+"', sizingMethod='scale');";
          this.div_.innerHTML = '<div style="height:' +h+ 'px; width:'+w+'px; ' +loader+ '" ></div>';
        } else {
          this.div_.innerHTML = '<img src="' +this.image+ '"  width='+w+' height='+h+' >';
        }
        
        // Only draggable if current zoom = the initial zoom
        if (this.dragObject) {
          if (z != this.dragZoom_) {this.dragObject.disable();}
        }
        
       } 
      }	 

      EInsert.prototype.show = function() {
        this.div_.style.display="";
        this.hidden = false;
      }
      
      EInsert.prototype.hide = function() {
        this.div_.style.display="none";
        this.hidden = true;
      }
      
      EInsert.prototype.getPoint = function() {
        return this.point;
      }

      EInsert.prototype.supportsHide = function() {
        return true;
      }

      EInsert.prototype.isHidden = function() {
        return this.hidden;
      }
      
      EInsert.prototype.setPoint = function(a) {
        this.point = a;
        this.redraw(true);
      }

      EInsert.prototype.setImage = function(a) {
        this.image = a;
        this.redraw(true);
      }
      
      EInsert.prototype.setZindex = function(a) {
        this.div_.style.zIndex=a;
      }

      EInsert.prototype.setSize = function(a) {
        this.size = a;
        this.redraw(true);
      }
   


/* ********** */
function load() {

 if (GBrowserIsCompatible()) {

  map = new GMap2(document.getElementById("map"));
  //map.addControl(new GSmallMapControl());
  map.addControl(new GSmallZoomControl ());
  map.addControl(new GMapTypeControl());

  // optionnel pour verifier la taille du cercle
  map.addControl(new GScaleControl());

  /* coordonnees a modifier en fonction de la recherche */
  point = new GLatLng(47.317414, 5.046158);

  map.setCenter(point, 8);

  // initialisation du cercle a 0 km
  init(0);



  GEvent.addListener(map, "moveover", function() {

    //_coef = document.getElementById('horizontal-slider').value;
    drawCircle(10);
    document.getElementById("souris").innerHTML = 'dessus';

  });



  GEvent.addListener(map, "moveout", function() {

    //_coef = document.getElementById('horizontal-slider').value;
    drawCircle(10);
    document.getElementById("souris").innerHTML = 'dehors';

  });





  GEvent.addListener(map, "move", function() {

    _coef = document.getElementById('horizontal-slider').value;
    drawCircle(_coef);
    //document.getElementById("zoom").innerHTML = "Zoom : "+map.getZoom();

  });


  GEvent.addListener(map, "moveend", function() {

    _coef = document.getElementById('horizontal-slider').value;
	document.getElementById("souris").innerHTML='dessus';
    
    


  });


 }

}

function metresToPixels(lat,zoom) {

  var c = 256 * Math.pow(2,zoom);        // taille du monde en pixels
  var d = 12756000 * Math.PI;            // circonference de l'equateur
  var d = d * Math.cos(lat*Math.PI/180); // circonference au niveau du point
  return c/d;                            // pixels par metre en fonction du zoom

}

function init(k) {

  // One kilometre at this point in pixels
  var r = 1000 * metresToPixels(point.lat(), map.getZoom());

  // cercle de 50 km par defaut (x2 car rayon)
  r = r*(k*2);

  var circle = new EInsert(map.getCenter(), "/img/g3/js/cercle-carto-gris.png", new GSize(r,r), map.getZoom());

  map.addOverlay(circle);

}


function drawCircle(k) {

  var zoom = map.getZoom();

  point = map.getCenter();

  // One kilometre at this point in pixels
  var r = 1000 * metresToPixels(point.lat(), map.getZoom());

  r = r*(k*2);

  var lat_change = (180/Math.PI)*(k/6335);


  map.clearOverlays();

  var circle = new EInsert(map.getCenter(), "/img/g3/js/cercle-carto-gris.png", new GSize(r,r), zoom);

  map.addOverlay(circle);
}

function zoom(k) {

  var zoom;
  if ( k == 5 ) { zoom = 12; }
  else if ( k == 10 ) { zoom = 11; }
  else if ( k == 20 ) { zoom = 10; }
  else if ( k == 30 ) { zoom = 9; }
  else if ( k == 40 || k == 50 ) { zoom = 8; }
  else { zoom = 8;}

  map.setZoom(zoom);

}

