/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 * full text of the license. */


/**
 * @requires OpenLayers/Control/PanZoom.js
 */

/**
 * Class: OpenLayers.Control.PZ
 *
 * Inherits from:
 *  - <OpenLayers.Control.PanZoom>
 *
 * Subclassed to avoid using the 'whole world' button.
 */

OpenLayers.Control.PZ = OpenLayers.Class(OpenLayers.Control.PanZoom, {

    initialize: function() {
        OpenLayers.Control.PanZoom.prototype.initialize.apply(this, arguments);
    },


    /**
     * Method: draw
     *
     * Parameters:
     * px - {<OpenLayers.Pixel>} 
     * 
     * Returns:
     * {DOMElement} A reference to the container div for the PanZoom control.
     */
    draw: function(px) {
        // initialize our internal div
        OpenLayers.Control.prototype.draw.apply(this, arguments);
        px = this.position;

        // place the controls
        this.buttons = [];

        var sz = new OpenLayers.Size(18,18);
        var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);

        this._addButton("panup", "north-mini.png", centered, sz);
        px.y = centered.y+sz.h;
        this._addButton("panleft", "west-mini.png", px, sz);
        this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz);
        this._addButton("pandown", "south-mini.png", 
                        centered.add(0, sz.h*2), sz);
        this._addButton("zoomin", "zoom-plus-mini.png", 
                        centered.add(0, sz.h*3+5), sz);
        this._addButton("zoomout", "zoom-minus-mini.png", 
                        centered.add(0, sz.h*4+5), sz);
        return this.div;
    },

    
    CLASS_NAME: "OpenLayers.Control.PZ"
});


/////////////////////////////

// In this interpretation, 'Hover' highlights a feature without selecting it, but a click does select it.
OpenLayers.Control.HoverSelectFeature = OpenLayers.Class(OpenLayers.Control.SelectFeature, {

    hover: true,

    initialize: function() {
        OpenLayers.Control.SelectFeature.prototype.initialize.apply(this, arguments);
    },


      // The methods below replace those of the parent from SelectFeature.js to support the new value for hover.

    /**
     * Method: clickFeature
     * Called on click in a feature
     * Only responds if this.hover is false.
     *
     * Parameters:
     * feature - {<OpenLayers.Feature.Vector>} 
     */
    clickFeature: function(feature) {
      var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) > -1);
      if(selected) {
	if(this.toggleSelect()) {
	  this.unselect(feature);
	} else if(!this.multipleSelect()) {
	  this.unselectAll({except: feature});
	}
      } else {
	if(!this.multipleSelect()) {
	  this.unselectAll({except: feature});
	}
	this.select(feature);
      }
    },
      
    /**
     * Method: clickoutFeature
     * Called on click outside a previously clicked (selected) feature.
     * Only responds if this.hover is false.
     *
     * Parameters:
     * feature - {<OpenLayers.Vector.Feature>} 
     */
    clickoutFeature: function(feature) {
        if(this.clickout) {
	  this.unselectAll();
        }
    },





    /**
     * Method: overFeature
     * Called on over a feature.
     * Only responds if this.hover is true.
     *
     * Parameters:
     * feature - {<OpenLayers.Feature.Vector>} 
     */
    overFeature: function(feature) {

      var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) > -1);

      if(!selected) {
	this.layer.drawFeature(feature, "hover");
      } else {
	this.layer.drawFeature(feature, "hoverSelect");
      }
    },

    /**
     * Method: outFeature
     * Called on out of a selected feature.
     * Only responds if this.hover is true.
     *
     * Parameters:
     * feature - {<OpenLayers.Feature.Vector>} 
     */
    outFeature: function(feature) {
      var selected = (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) > -1);

      if(!selected) {
	this.layer.drawFeature(feature, "default");
      }

    },



    CLASS_NAME: "OpenLayers.Control.HoverSelectFeature"
      });

/**
 * This is an 'absorbent' click handler, designed to allow a bit of tolerance when clicking.
 * The ordinary click handler goes into a drag when the mouse moves after a mouse down event.
 * This class tolerates a bit of movement. If the movement is sufficiently small, then a click is handled.
 * If the movement is large then a drag is handled by passing on the event to other handlers.
 *
 * It works as follows....
 * mousedown: Sets the 'absorb' state.
 * mousemove: In the absorb state and if the mouse is still within the tolerance zone this event is absorbed and not passed on to any other handlers.
 * 	If the mouse moves outside the tolerance zone the absorb state is cleared.
 * mouseup/mouseout also clear the absorb state.
 *
 * Note: it assumes that it gets these events before any other handlers. That has to do with the order in which controls and their handlers are activated.
 */


OpenLayers.Handler.AbsorbClick = OpenLayers.Class(OpenLayers.Handler.Click, {

    /**
     * Property: absorbEvents
     * {Boolean} When set used to stop propagaion of the event to other handlers.
     */
  absorbEvents: false,

    initialize: function() {

        OpenLayers.Handler.Click.prototype.initialize.apply(this, arguments);

	// Set the mousedown here because the parent class does this too and would be overriden if set outside, in the property list.
	this.mousedown = function(evt) {
	  
	  if(!this.absorbEvents) {
	    
	    this.absorbEvents = true;
	    
	    this.down = evt.xy;
	  }
	return true;
	};
    },
      

      /**
       * Within the tolerance zone absorb all mouse move events without passing them to other handlers.
       * This will stop going into drag straight away.
       */
    mousemove: function(evt) {

      if(this.absorbEvents && this.passesTolerance(evt)) {

	// Absorb this event.
	return false;
      }

      this.absorbEvents = false;

      // Allow other handers to receive this event.
      return true;
    },


      /**
       * Events than cancel the absorbency
       */
    mouseup: function(evt) {
      this.absorbEvents = false;
      return true;
    },
    mouseout: function(evt) {
      this.absorbEvents = false;
      return true;
    },



    CLASS_NAME: "OpenLayers.Handler.AbsorbClick"
  });

/// Ends
