/* 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.js
 */

/**
 * Class: OpenLayers.Control.PZBGBAV
 * 
 * Inherits from:
 *  - <OpenLayers.Control>
 */
OpenLayers.Control.PZBGBAV = OpenLayers.Class(OpenLayers.Control, {

    /** 
     * APIProperty: slideFactor
     * {Integer} Number of pixels by which we'll pan the map in any direction 
     *     on clicking the arrow buttons. 
     */
    slideFactor: 50,

    /** 
     * Property: buttons
     * {Array(DOMElement)} Array of Button Divs 
     */
    buttons: null,

    /** 
     * Property: position
     * {<OpenLayers.Pixel>} 
     */
    position: null,

    /**
     * Constructor: OpenLayers.Control.PZBGBAV
     * 
     * Parameters:
     * options - {Object}
     */
    initialize: function(options) {
        this.position = new OpenLayers.Pixel(OpenLayers.Control.PZBGBAV.X,
                                             OpenLayers.Control.PZBGBAV.Y+140);
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
    },

    /**
     * APIMethod: destroy
     */
    destroy: function() {
        OpenLayers.Control.prototype.destroy.apply(this, arguments);
        while(this.buttons.length) {
            var btn = this.buttons.shift();
            btn.map = null;
            OpenLayers.Event.stopObservingElement(btn);
        }
        this.buttons = null;
        this.position = null;
    },

    /**
     * Method: draw
     *
     * Parameters:
     * px - {<OpenLayers.Pixel>} 
     * 
     * Returns:
     * {DOMElement} A reference to the container div for the PZBGBAV control.
     */
    draw: function(px) {
        // initialize our internal div
        OpenLayers.Control.prototype.draw.apply(this, arguments);
        px = this.position;

        // place the controls
        this.buttons = [];

		var szin = new OpenLayers.Size(56,28);
		var szou = new OpenLayers.Size(26,20);
		var szlf = new OpenLayers.Size(26,46);
        var szup = new OpenLayers.Size(30,20);				
		var szwr = new OpenLayers.Size(30,26);
        var centeredin = new OpenLayers.Pixel(2+px.x+szin.w/2, (px.y+szin.h/2)-1);
		/*var centeredup = new OpenLayers.Pixel(px.x+szup.w/2, px.y+szup.h/2);
		var centeredlf = new OpenLayers.Pixel(px.x+szlf.w/2, px.y+szlf.h/2);*/
		
		/*var centeredwr = new OpenLayers.Pixel(px.x+szwr.w/2, px.y+szwr.h/2);*/
		
		this._addButton("zoomin", "../../img/zoom-plus-mini.png", centeredin, szin);
		px.y = centeredin.y+szin.h;
		
		this._addButton("zoomout", "../../img/zoom-minus-mini.png", px.add(szou.w+4, 0), szou);
		
		this._addButton("panleft", "../../img/west-mini.png", px.add(szou.w+4, szou.h), szlf);
		
		this._addButton("panup", "../../img/north-mini.png", px.add(szlf.w+szup.w, 0), szup);
        
		this._addButton("zoomworld", "../../img/zoom-world-mini.png", px.add(szlf.w+szup.w, szup.h), szwr);
		
        this._addButton("panright", "../../img/east-mini.png", px.add(szlf.w+szup.w+szwr.w, 0), szlf);
		
        this._addButton("pandown", "../../img/south-mini.png", px.add(szlf.w+szup.w, szup.h+szwr.h), szup);
		
        return this.div;
    },
    
    /**
     * Method: _addButton
     * 
     * Parameters:
     * id - {String} 
     * img - {String} 
     * xy - {<OpenLayers.Pixel>} 
     * sz - {<OpenLayers.Size>} 
     * 
     * Returns:
     * {DOMElement} A Div (an alphaImageDiv, to be precise) that contains the
     *     image of the button, and has all the proper event handlers set.
     */
    _addButton:function(id, img, xy, sz) {
        var imgLocation = OpenLayers.Util.getImagesLocation() + img;
        var btn = OpenLayers.Util.createAlphaImageDiv(
                                    this.id + "_" + id, 
                                    xy, sz, imgLocation, "absolute");

        //we want to add the outer div
        this.div.appendChild(btn);

        OpenLayers.Event.observe(btn, "mousedown", 
            OpenLayers.Function.bindAsEventListener(this.buttonDown, btn));
        OpenLayers.Event.observe(btn, "dblclick", 
            OpenLayers.Function.bindAsEventListener(this.doubleClick, btn));
        OpenLayers.Event.observe(btn, "click", 
            OpenLayers.Function.bindAsEventListener(this.doubleClick, btn));
        btn.action = id;
        btn.map = this.map;
        btn.slideFactor = this.slideFactor;

        //we want to remember/reference the outer div
        this.buttons.push(btn);
        return btn;
    },
    
    /**
     * Method: doubleClick
     *
     * Parameters:
     * evt - {Event} 
     *
     * Returns:
     * {Boolean}
     */
    doubleClick: function (evt) {
        OpenLayers.Event.stop(evt);
        return false;
    },
    
    /**
     * Method: buttonDown
     *
     * Parameters:
     * evt - {Event} 
     */
    buttonDown: function (evt) {
        if (!OpenLayers.Event.isLeftClick(evt)) {
            return;
        }
	var extent = new OpenLayers.Bounds(-181, -58, 181, 88);
        switch (this.action) {
            case "panup": 
                this.map.pan(0, -this.slideFactor);
                break;
            case "pandown": 
                this.map.pan(0, this.slideFactor);
                break;
            case "panleft": 
                this.map.pan(-this.slideFactor, 0);
                break;
            case "panright": 
                this.map.pan(this.slideFactor, 0);
                break;
            case "zoomin": 
                this.map.zoomIn(); 
                break;
            case "zoomout": 
                this.map.zoomOut(); 
                break;
            case "zoomworld": 
                this.map.zoomToMaxExtent(extent); 
                break;
        }

        OpenLayers.Event.stop(evt);
    },

    CLASS_NAME: "OpenLayers.Control.PZBGBAV"
});

/**
 * Constant: X
 * {Integer}
 */
OpenLayers.Control.PZBGBAV.X = 4;

/**
 * Constant: Y
 * {Integer}
 */
OpenLayers.Control.PZBGBAV.Y = 4;

