// FIXME works for View2D only

goog.provide('ol.control.Zoom');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('ol.animation');
goog.require('ol.control.Control');
goog.require('ol.css');
goog.require('ol.easing');


/**
 * @define {number} Zoom duration.
 */
ol.control.ZOOM_DURATION = 250;



/**
 * Create a new control with 2 buttons, one for zoom in and one for zoom out.
 * This control is part of the default controls of a map. To style this control
 * use css selectors .ol-zoom-in and .ol-zoom-out.
 * @constructor
 * @extends {ol.control.Control}
 * @param {ol.control.ZoomOptions=} opt_options Zoom options.
 */
ol.control.Zoom = function(opt_options) {

  var options = goog.isDef(opt_options) ? opt_options : {};

  var className = goog.isDef(options.className) ? options.className : 'ol-zoom';

  var inElement = goog.dom.createDom(goog.dom.TagName.A, {
    'href': '#zoomIn',
    'class': className + '-in'
  });
  goog.events.listen(inElement, [
    goog.events.EventType.TOUCHEND,
    goog.events.EventType.CLICK
  ], this.handleIn_, false, this);

  var outElement = goog.dom.createDom(goog.dom.TagName.A, {
    'href': '#zoomOut',
    'class': className + '-out'
  });
  goog.events.listen(outElement, [
    goog.events.EventType.TOUCHEND,
    goog.events.EventType.CLICK
  ], this.handleOut_, false, this);

  var cssClasses = className + ' ' + ol.css.CLASS_UNSELECTABLE;
  var element = goog.dom.createDom(goog.dom.TagName.DIV, cssClasses, inElement,
      outElement);

  goog.base(this, {
    element: element,
    map: options.map,
    target: options.target
  });

  /**
   * @type {number}
   * @private
   */
  this.delta_ = goog.isDef(options.delta) ? options.delta : 1;

};
goog.inherits(ol.control.Zoom, ol.control.Control);


/**
 * @param {goog.events.BrowserEvent} browserEvent The browser event to handle.
 * @private
 */
ol.control.Zoom.prototype.handleIn_ = function(browserEvent) {
  // prevent #zoomIn anchor from getting appended to the url
  browserEvent.preventDefault();
  var map = this.getMap();
  map.requestRenderFrame();
  // FIXME works for View2D only
  var view = map.getView().getView2D();
  var currentResolution = view.getResolution();
  if (goog.isDef(currentResolution)) {
    map.addPreRenderFunction(ol.animation.zoom({
      resolution: currentResolution,
      duration: ol.control.ZOOM_DURATION,
      easing: ol.easing.easeOut
    }));
  }
  var resolution = view.constrainResolution(currentResolution, this.delta_);
  view.setResolution(resolution);
};


/**
 * @param {goog.events.BrowserEvent} browserEvent The browser event to handle.
 * @private
 */
ol.control.Zoom.prototype.handleOut_ = function(browserEvent) {
  // prevent #zoomOut anchor from getting appended to the url
  browserEvent.preventDefault();
  var map = this.getMap();
  // FIXME works for View2D only
  var view = map.getView().getView2D();
  var currentResolution = view.getResolution();
  if (goog.isDef(currentResolution)) {
    map.addPreRenderFunction(ol.animation.zoom({
      resolution: currentResolution,
      duration: ol.control.ZOOM_DURATION,
      easing: ol.easing.easeOut
    }));
  }
  var resolution = view.constrainResolution(currentResolution, -this.delta_);
  view.setResolution(resolution);
};

@@ -898,16 +898,78 @@
 zoom';%0A%0A
+  var delta = goog.isDef(options.delta) ? options.delta : 1;%0A%0A
   var in
@@ -1011,32 +1011,32 @@
 om.TagName.A, %7B%0A
-
     'href': '#zo
@@ -1190,22 +1190,67 @@
  %5D, 
-this.handleIn_
+goog.partial(ol.control.Zoom.prototype.zoomByDelta_, delta)
 , fa
@@ -1496,23 +1496,68 @@
  %5D, 
-this.handleOut_
+goog.partial(ol.control.Zoom.prototype.zoomByDelta_, -delta)
 , fa
@@ -1835,118 +1835,8 @@
 );%0A%0A
-  /**%0A   * @type %7Bnumber%7D%0A   * @private%0A   */%0A  this.delta_ = goog.isDef(options.delta) ? options.delta : 1;%0A%0A
 %7D;%0Ag
@@ -1884,32 +1884,69 @@
 Control);%0A%0A%0A/**%0A
+ * @param %7Bnumber%7D delta Zoom delta.%0A
  * @param %7Bgoog.
@@ -2054,16 +2054,19 @@
 ype.
-handleIn
+zoomByDelta
 _ = 
@@ -2066,32 +2066,39 @@
 lta_ = function(
+delta, 
 browserEvent) %7B%0A
@@ -2110,23 +2110,19 @@
 prevent 
-#zoomIn
+the
  anchor 
@@ -2218,36 +2218,8 @@
 ();%0A
-  map.requestRenderFrame();%0A
   //
@@ -2541,33 +2541,34 @@
 %0A    %7D));%0A  
-%7D%0A
   var 
-r
+newR
 esolution = 
@@ -2615,790 +2615,58 @@
 on, 
-this.
 delta
-_
 );%0A  
-view.setResolution(resolution);%0A%7D;%0A%0A%0A/**%0A * @param %7Bgoog.events.BrowserEvent%7D browserEvent The browser event to handle.%0A * @private%0A */%0Aol.control.Zoom.prototype.handleOut_ = function(browserEvent) %7B%0A  // prevent #zoomOut anchor from getting appended to the url%0A  browserEvent.preventDefault();%0A  var map = this.getMap();%0A  // FIXME works for View2D only%0A  var view = map.getView().getView2D();%0A  var currentResolution = view.getResolution();%0A  if (goog.isDef(currentResolution)) %7B%0A    map.addPreRenderFunction(ol.animation.zoom(%7B%0A      resolution: currentResolution,%0A      duration: ol.control.ZOOM_DURATION,%0A      easing: ol.easing.easeOut%0A    %7D));%0A  %7D%0A  var resolution = view.constrainResolution(currentResolution, -this.delta_);%0A  view.setResolution(resolution);
+  view.setResolution(newResolution);%0A  %7D
 %0A%7D;%0A

Add ol.control.Zoom#zoomByDelta function