	//Activate metric valor
	var metric = true;
	//Initialize singleClick
	var singleClick = false;
	//Create new object icon move
	var queryCenterOptions = new Object();
	//Create new object icon resize
	var queryLineOptions = new Object();

	var Maximum_Cicle = 1;
	var Current_Nbr_Circle= 0;
	var Nbr_Marker=0;
	
	//Create icon move
	queryCenterOptions.icon = new GIcon();
	queryCenterOptions.icon.image = "http://www.hotelenia.com/S@Framework/Theme/Classic/Img/Google_Map/move_zone.png";
	queryCenterOptions.icon.iconSize = new GSize(28,27);
	queryCenterOptions.icon.shadowSize = new GSize(0, 0);
	queryCenterOptions.icon.iconAnchor = new GPoint(14, 14);
	queryCenterOptions.draggable = true;
	queryCenterOptions.zIndexProcess=importanceOrder;
	queryCenterOptions.bouncy = false;

	//Create icon resize
	queryLineOptions.icon = new GIcon();
	queryLineOptions.icon.image = "http://www.hotelenia.com/S@Framework/Theme/Classic/Img/Google_Map/resize_zone.png";
	queryLineOptions.icon.iconSize = new GSize(24,24);
	queryLineOptions.icon.shadowSize = new GSize(0, 0);
	queryLineOptions.icon.iconAnchor = new GPoint(12, 10);
	queryLineOptions.draggable = true;
	queryLineOptions.zIndexProcess=importanceOrder;
	queryLineOptions.bouncy = false;
	
	
	function createCircle(point, radius) {
		if(Current_Nbr_Circle<Maximum_Cicle){
			singleClick = false;
			geoQuery = new GeoQuery();
			geoQuery.initializeCircle(radius, point, map);
			myQueryControl.addGeoQuery(geoQuery);
			geoQuery.render();
			Current_Nbr_Circle=(Current_Nbr_Circle+1);
			
		}
		
	}

	function destination(orig, hdng, dist) {
	  var R = 6371; // earth's mean radius in km
	  var oX, oY;
	  var x, y;
	  var d = dist/R;  // d = angular distance covered on earth's surface
	  hdng = hdng * Math.PI / 180; // degrees to radians
	  oX = orig.x * Math.PI / 180;
	  oY = orig.y * Math.PI / 180;
	
	  y = Math.asin( Math.sin(oY)*Math.cos(d) + Math.cos(oY)*Math.sin(d)*Math.cos(hdng) );
	  x = oX + Math.atan2(Math.sin(hdng)*Math.sin(d)*Math.cos(oY), Math.cos(d)-Math.sin(oY)*Math.sin(y));
	
	  y = y * 180 / Math.PI;
	  x = x * 180 / Math.PI;
	  return (new GLatLng(y, x));
	}

	function distance(point1, point2) {
	  var R = 6371; // earth's mean radius in km
	  var lon1 = point1.lng()* Math.PI / 180;
	  var lat1 = point1.lat() * Math.PI / 180;
	  var lon2 = point2.lng() * Math.PI / 180;
	  var lat2 = point2.lat() * Math.PI / 180;
	
	  var deltaLat = (lat1 - lat2);
	  var deltaLon = (lon1 - lon2);
	
	  var step1 = Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(lat2) * Math.cos(lat1) * Math.pow(Math.sin(deltaLon/2), 2);
	  var step2 = (2 * Math.atan2(Math.sqrt(step1), Math.sqrt(1 - step1)));
	  return (step2 * R);
	}

function GeoQuery() {

}

function importanceOrder (marker_z,b) {
    return 1000;
}


GeoQuery.prototype.CIRCLE='circle';
GeoQuery.prototype.COLORS=["#0000ff", "#00ff00", "#ff0000"];
var COLORI=0;

GeoQuery.prototype._map;
GeoQuery.prototype._type;
GeoQuery.prototype._radius;
GeoQuery.prototype._dragHandle;
GeoQuery.prototype._centerHandle;
GeoQuery.prototype._polyline;
GeoQuery.prototype._color ;
GeoQuery.prototype._control;
GeoQuery.prototype._points;
GeoQuery.prototype._dragHandlePosition;
GeoQuery.prototype._centerHandlePosition;


GeoQuery.prototype.initializeCircle = function(radius, point, map) {

	//Initialize variable
		//Initialize type object
	    this._type = this.CIRCLE;
	    //Set Radius
	    this._radius = radius;
	    
	    //Set Map
	    this._map = map;
		//Initialize color
	    this._color = this.COLORS[COLORI++ % 3];
	    
	    this._control="";
	//Create marker to control circle
		    
	    //Set position to the marker "resize"
	    this._dragHandlePosition = destination(point, 90, this._radius/1000);
	    //Create marker "resize" 
	    this._dragHandle = new GMarker(this._dragHandlePosition, queryLineOptions);
		//Add marker on the map
		map.addOverlay(this._dragHandle);

	    
		//Set position to the marker "move"  
	    this._centerHandlePosition = point;
	    //Create marker "move"
	    this._centerHandle = new GMarker(this._centerHandlePosition,queryCenterOptions );


	    
		//Add marker on the map
	    map.addOverlay(this._centerHandle);
	    
	    
	//Set the object in one variable
    var myObject = this;

	//Create event for the markers
		//Add event "drag" on "resize" marker
		GEvent.addListener (this._dragHandle, "drag", function() {myObject.updateCircle(1);});
		//Add event "dragend" on "resize" marker
	    GEvent.addListener (this._dragHandle, "dragend", function() {myObject.updateCircle(1);});
	    
	    //Add event "drag" on "move" marker 
	    GEvent.addListener(this._centerHandle, "drag", function() {myObject.updateCircle(2);});
		//Add event "dragend" on "move" marker
	    GEvent.addListener(this._centerHandle, "dragend", function() {myObject.updateCircle(2);});
	    
	    
};


GeoQuery.prototype.updateCircle = function (type) {

	//Remove current circle
    this._map.removeOverlay(this._polyline);

    //If typt = 1 : Resize
    if (type==1) {
		// Update marker position
		this._dragHandlePosition = this._dragHandle.getPoint();
		// Update radius circle
		this._radius = distance(this._centerHandlePosition, this._dragHandlePosition) * 1000;
		//Excute render
		this.render();
    } else {
    	// Update move marker position
		this._centerHandlePosition = this._centerHandle.getPoint();
		//Execute render
		this.render();
		//update resize marker
		this._dragHandle.setPoint(this.getEast());
    }
};


GeoQuery.prototype.render = function() {
  if (this._type == this.CIRCLE) {
    this._points = [];
    var distance = this._radius/1000;
    for (i = 0; i < 72; i++) {
      this._points.push(destination(this._centerHandlePosition, i * 360/72, distance) );
    }
    this._points.push(destination(this._centerHandlePosition, 0, distance) );
    //this._polyline = new GPolyline(this._points, this._color, 6);
    this._polyline = new GPolygon(this._points, this._color, 1, 1, this._color, 0.2);
    this._map.addOverlay(this._polyline);
    this._control.render();
    
  }
};

GeoQuery.prototype.remove = function() {
  this._map.removeOverlay(this._polyline);
  this._map.removeOverlay(this._dragHandle);
  this._map.removeOverlay(this._centerHandle);
};

GeoQuery.prototype.getRadius = function() {
    return this._radius;
};

GeoQuery.prototype.getHTML = function() {
  return "<span>" + this.getDistHtml() + "</span>";
};

GeoQuery.prototype.getDistHtml = function() {
	/*<img src='http://jfno.net/images/close.gif' onClick='myQueryControl.remove(" + this._control.getIndex(this) + ");'/> */
  result = "Hotels in Barcelona, in a radius of  ";
  
  if (metric) {
    if (this._radius < 1000) {
      result +=  this._radius.toFixed(1)+" meters of the selected zone ";
    } else {
      result += (this._radius / 1000).toFixed(1)+" kilometers of the selected zone ";
    }
  } else {
    var radius = this._radius * 3.2808399;
    if (radius < 5280) {
      result += "in feet : " + radius.toFixed(1);
    } else {
      result += "in miles : " + (radius / 5280).toFixed(1);
    }
  }
  return result;   
};

GeoQuery.prototype.getNorth = function() {
  return this._points[0];
};

GeoQuery.prototype.getSouth = function() {
  return this._points[(72/2)];
};

GeoQuery.prototype.getEast = function() {
  return this._points[(72/4)];
};

GeoQuery.prototype.getWest = function() {
  return this._points[(72/4*3)];
};

function QueryControl (localSearch) {
  this._localSearch = localSearch;
};

QueryControl.prototype = new GControl();
QueryControl.prototype._geoQueries ;
QueryControl.prototype._mainDiv;
QueryControl.prototype._queriesDiv;
QueryControl.prototype._timeout;
QueryControl.prototype._localSearch;

QueryControl.prototype.initialize = function(map) {
  this._mainDiv = document.createElement("div");
  this._mainDiv.id = "GQueryControl";
  titleDiv = document.createElement("div");
  titleDiv.id = "GQueryControlTitle";
  titleDiv.appendChild(document.createTextNode(""));
  this._mainDiv.appendChild(titleDiv);
  this._queriesDiv = document.createElement("div");
  this._queriesDiv.id = "queriesDiv";
  this._mainDiv.appendChild(this._queriesDiv);

  map.getContainer().appendChild(this._mainDiv);
  this._geoQueries = new Array();
  return this._mainDiv;
};

QueryControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10));
};

QueryControl.prototype.addGeoQuery = function(geoQuery) {
  this._geoQueries.push(geoQuery);
  geoQuery._control = this;
  newDiv = document.createElement("div");
  newDiv.innerHTML = geoQuery.getHTML();
  this._queriesDiv.appendChild(newDiv);
 
};

QueryControl.prototype.render = function() {
  for (i = 0; i < this._geoQueries.length; i++) {
    geoQuery = this._geoQueries[i];
    this._queriesDiv.childNodes[i].innerHTML = geoQuery.getHTML();
  }
  if (this._timeout == null) {
    this._timeout = setTimeout(myQueryControl.query, 1000);
  } else {
    clearTimeout(this._timeout);
    this._timeout = setTimeout(myQueryControl.query, 1000);
  }
  
  
};

QueryControl.prototype.query = function() {
	
	Nbr_Marker=0;
	
	for (i = 1; i <= marker.length; i++) {
	    if(marker[i]){
		    var selected_marker = marker[i];

		    inCircle = true;
		    for (j = 0; j < myQueryControl._geoQueries.length; j++) {
		      geoQuery = myQueryControl._geoQueries[j];
		      dist = distance(selected_marker.getLatLng(), geoQuery._centerHandlePosition); 
		      if (dist > geoQuery._radius / 1000) {
				inCircle = false;
				break;
		      }
		    }
		    if (inCircle) {
			if((document.getElementById("Block_Info_"+i).getAttribute("stars_selected")=="true")&&(document.getElementById("Block_Info_"+i).getAttribute("price_selected")=="true")){
			    	selected_marker.show();
			    	document.getElementById("Block_Info_"+i).style.display="block";
			    	document.getElementById("Block_Info_"+i).setAttribute("inzone","in");
			    	Nbr_Marker++;
			}
		    } else {
			if((document.getElementById("Block_Info_"+i).getAttribute("stars_selected")=="true")&&(document.getElementById("Block_Info_"+i).getAttribute("price_selected")=="true")){
			      	selected_marker.hide();
			      	document.getElementById("Block_Info_"+i).setAttribute("inzone","out");
			      	document.getElementById("Block_Info_"+i).style.display="none";
			}
		    }
	   }
	}
};

QueryControl.prototype.remove = function(index) {
  this._geoQueries[index].remove();
  this._queriesDiv.removeChild(this._queriesDiv.childNodes[index]);
  delete this._geoQueries[index];
  this._geoQueries.splice(index,1);
  this.render();
  Current_Nbr_Circle=0;
};

QueryControl.prototype.getIndex = function(geoQuery) {
  for (i = 0; i < this._geoQueries.length; i++) {
    if (geoQuery == this._geoQueries[i]) {
      return i;
    }
  }
  return -1;
};
