// https://github.com/consbio/Leaflet.ZoomBox
// Copyright (c) 2014, Conservation Biology Institute
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
L.Control.ZoomBox=L.Control.extend({_active:false,_map:null,includes:L.Mixin.Events,options:{position:"topleft",className:"fa fa-search-plus",modal:false},onAdd:function(map){this._map=map;this._container=L.DomUtil.create("div","leaflet-zoom-box-control leaflet-bar");this._container.title="Zoom to specific area";var link=L.DomUtil.create("a",this.options.className,this._container);link.href="#";var _origMouseDown=map.boxZoom._onMouseDown;map.boxZoom._onMouseDown=function(e){_origMouseDown.call(map.boxZoom,{clientX:e.clientX,clientY:e.clientY,which:1,shiftKey:true})};map.on("zoomend",function(){if(map.getZoom()==map.getMaxZoom()){L.DomUtil.addClass(link,"leaflet-disabled")}else{L.DomUtil.removeClass(link,"leaflet-disabled")}},this);if(!this.options.modal){map.on("boxzoomend",this.deactivate,this)}L.DomEvent.on(this._container,"dblclick",L.DomEvent.stop).on(this._container,"click",L.DomEvent.stop).on(this._container,"click",function(){this._active=!this._active;if(this._active&&map.getZoom()!=map.getMaxZoom()){this.activate()}else{this.deactivate()}},this);return this._container},activate:function(){L.DomUtil.addClass(this._container,"active");this._map.dragging.disable();this._map.boxZoom.addHooks();L.DomUtil.addClass(this._map.getContainer(),"leaflet-zoom-box-crosshair")},deactivate:function(){L.DomUtil.removeClass(this._container,"active");this._map.dragging.enable();this._map.boxZoom.removeHooks();L.DomUtil.removeClass(this._map.getContainer(),"leaflet-zoom-box-crosshair");this._active=false;this._map.boxZoom._moved=false}});L.control.zoomBox=function(options){return new L.Control.ZoomBox(options)};