//Todo lo necesario para el cuadro para la busque por zona
var cuadro=false;
var PrimerLat;
var PrimerLng;
var PrimerClick;

var overlay=null;
var mostrartooltip=false;
var mensaje;
var rectangulo;
var eventoMouseUp;
var eventoMouseMove;
///////////////////////////////////////////////// Objeto Cuadro /////////////////////////////////

objCuadro.prototype = new google.maps.OverlayView();
	
function objCuadro(pNorte,pEste,pSur,pOeste, map)
		{
		var neBound = new google.maps.LatLng(pNorte, pEste);
		var swBound = new google.maps.LatLng(pSur, pOeste);
		var bounds = new google.maps.LatLngBounds(swBound, neBound);
		this.Norte=pNorte;
		this.Este=pEste;
		this.Sur=pSur;
		this.Oeste=pOeste;
		this.bounds_ = bounds;
        this.map_ = map;
    	this.div_ = null;
    	this.setMap(map);
		this.Limpiar=function(){ this.setMap(null);}
		this.Repitar=function(pNorte,pEste,pSur,pOeste){
						var neBound = new google.maps.LatLng(pNorte, pEste);
						var swBound = new google.maps.LatLng(pSur, pOeste);
						var bounds = new google.maps.LatLngBounds(swBound, neBound);
						this.Norte=pNorte;
						this.Este=pEste;
						this.Sur=pSur;
						this.Oeste=pOeste;
						this.bounds_ = bounds;
						var overlayProjection = this.getProjection();
						var sw = overlayProjection.fromLatLngToDivPixel(bounds.getSouthWest());
						var ne = overlayProjection.fromLatLngToDivPixel(bounds.getNorthEast());
						var div = this.div_;
						if (ne.x>sw.x) {
							div.style.left = sw.x + 'px';
						} else {
							div.style.left = ne.x + 'px'
						}
						if (ne.y>sw.y) {
							div.style.top = sw.y + 'px';
						} else {
							div.style.top = ne.y + 'px'
						}
						div.style.width = Math.abs(ne.x - sw.x) + 'px';
						div.style.height = Math.abs(sw.y - ne.y) + 'px';
						}
		}
		
objCuadro.prototype.onAdd = function() {
    var div = document.createElement('DIV');
    div.id="Recuadro";
	/*div.style.borderStyle = "double";
    div.style.borderWidth = "3px";
    div.style.position = "absolute";
	div.style.backgroundColor="#6CF";*/
	
    this.div_ = div;
    var panes = this.getPanes();
    panes.overlayImage.appendChild(div);
  }
objCuadro.prototype.draw = function() {
 
    var overlayProjection = this.getProjection();
 
    var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
    var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());
  
  
    var div = this.div_;
	if (ne.x>sw.x) {
		div.style.left = sw.x + 'px';
	} else {
		div.style.left = ne.x + 'px'
	}
	if (ne.y>sw.y) {
		div.style.top = sw.y + 'px';
	} else {
		div.style.top = ne.y + 'px'
	}
    div.style.width = Math.abs(ne.x - sw.x) + 'px';
    div.style.height = Math.abs(sw.y - ne.y) + 'px';
  }
 
objCuadro.prototype.onRemove = function() {
    this.div_.parentNode.removeChild(this.div_);
    this.div_ = null;
  }  
///////////////////////////////////////////////// Fin Objeto Cuadro ///////////////////////////// 
 
function vCuadro(){
				
				if (cuadro==true)
					{
					mostrartooltip=false;
					cuadro=false; 
					var myOptions = {
							  draggable: true,
							  disableDoubleClickZoom: false,
							  navigationControl: true
							};
					//Se puede mover el mapa
					map.setOptions(myOptions);
					//Cambiamos color boton 
					if (document.getElementById("BtnMarcaZona")!= null)
						{
						document.getElementById("BtnMarcaZona").style.backgroundColor="#CCC";
						}
					if (rectangulo!=null)
						{rectangulo.setMap(null);}
					
					for (i=0;i<coleccionMarcas.length;i++)
							{
							coleccionMarcas.Items[i].Objeto.MenuDerecho(0);
							}	
					}
				else
					{//Opcion crear cuadro
					mostrartooltip=true;
					mensaje='Haga un click sobre un punto del mapa para poner un limite de la zona';
					if (overlay!=null)
							{
							overlay.Limpiar();
							overlay=null;
							}
					cuadro=true; 
					PrimerClick=true;
					var myOptions = {
							  draggable: false,
							  disableDoubleClickZoom: true,
							  navigationControl: false
							};
					//No se puede mover el mapa
					map.setOptions(myOptions);
					if (document.getElementById("BtnMarcaZona")!= null)
						{
						document.getElementById("BtnMarcaZona").style.backgroundColor="#F00";
						}
						
					for (i=0;i<coleccionMarcas.length;i++)
							{
							coleccionMarcas.Items[i].Objeto.CerrarVentanas();
							coleccionMarcas.Items[i].Objeto.MenuDerecho(3);
							}	
					nuevoMenu.cerrarPrincipal();
					
					var limite= map.getBounds();
				 	rectangulo= new google.maps.Rectangle({
          			map: map,
		  			bounds: limite
        			}); 
					google.maps.event.addListener(rectangulo, 'click', function(event) {
							if (PrimerClick)
							{
							PrimerClick=false;
							mensaje='Doble click en otro punto del mapa para cerrar la zona';
							var PuntoUno=event.latLng;
							PrimerLat=PuntoUno.lat();
							PrimerLng=PuntoUno.lng();
							//alert ("PrimerLat: "+PrimerLat);
							eventoMouseMove=google.maps.event.addListener(rectangulo, 'mousemove', function(event) {
							
							var PuntoDos=event.latLng;	
							var SegundoLat=PuntoDos.lat();
							var SegundoLng=PuntoDos.lng();
							//alert ("SegundoLat: "+SegundoLat);
								if (SegundoLat>PrimerLat)
									{
									Norte=SegundoLat;
									Sur=PrimerLat;
									}
								else
									{
									Norte=PrimerLat;
									Sur=SegundoLat;
									}
								
								if (SegundoLng>PrimerLng)
									{
									Este=SegundoLng;
									Oeste=PrimerLng;
									}
								else
									{
									Este=PrimerLng;
									Oeste=SegundoLng;
									}
							
							// Para que no se monte el puntero con la capa que estamos creando.
							// Se utiliza los limites del mapa para calcular el coeficiente
							var bounds = map.getBounds();
							var sw=bounds.getNorthEast();
							var ne=bounds.getSouthWest();
							var s=sw.lat();
							var n=ne.lat();
							var coeficiente= (s - n)/60;
							
							Norte-=coeficiente;
							Sur+=coeficiente;
							Este-=coeficiente;
							Oeste+=coeficiente;			
							
							if (overlay!=null)
								{
								if (overlay.div_!=null)
									overlay.Repitar(Norte,Este,Sur,Oeste,map);
								}
							else
								{
								
								overlay = new objCuadro(Norte,Este,Sur,Oeste,map);
								}
								
							/*
							google.maps.event.removeListener(eventoMouseMove);
							rectangulo.setMap(null);
							cuadro=true;
							vCuadro();		
							*/	
							});
							
							}
							
						eventoMouseUp=google.maps.event.addListener(rectangulo, 'dblclick', function(event) {
									google.maps.event.removeListener(eventoMouseMove);
									google.maps.event.removeListener(eventoMouseUp);
									rectangulo.setMap(null);
									cuadro=true;
									vCuadro();
									});
						
					});
					}
					
				}

