﻿/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is VisKort.
*
* The Initial Developer of the Original Code is
* IT- og Telestyrelsen / Danish National IT and Telecom Agency.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):

* Lars Klindt Mogensen
* Morten Bødtkjer
* Niels Kinnerup
* Thomas Bergstedt
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

/**
* Class: OpenLayers.Control.PanZoomBar
* The PanZoomBarBugFix corrects the following IE7 bug: http://trac.openlayers.org/ticket/2272
*
* Inherits from:
*  - <OpenLayers.Control.PanZoomBar>
*/
OpenLayers.Control.PanZoomBarBugFix =
    OpenLayers.Class(OpenLayers.Control.PanZoomBar, {

/**
* Method: draw 
*
* Parameters:
* px - {<OpenLayers.Pixel>} 
*/
draw: function(px) {
    // initialize our internal div
    OpenLayers.Control.prototype.draw.apply(this, arguments);
    px = this.position.clone();

    // place the controls
    this.buttons = [];

    var sz = new OpenLayers.Size(18, 18);
    var centered = new OpenLayers.Pixel(px.x + sz.w / 2, px.y);
    var wposition = sz.w;

    if (this.zoomWorldIcon) {
        centered = new OpenLayers.Pixel(px.x + sz.w, px.y);
    }

    this._addButton("panup", "north-mini.png", centered, sz);
    px.y = centered.y + sz.h;
    this._addButton("panleft", "west-mini.png", px, sz);
    if (this.zoomWorldIcon) {
        this._addButton("zoomworld", "zoom-world-mini.png", px.add(sz.w, 0), sz);

        wposition *= 2;
    }
    this._addButton("panright", "east-mini.png", px.add(wposition, 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);
    centered = this._addZoomBar(centered.add(0, sz.h * 4 + 5));
    this._addButton("zoomout", "zoom-minus-mini.png", centered, sz);
    this.div.style.cursor = "hand";
    return this.div;
    },
        /** 
        * Method: _addZoomBar
        * 
        * Parameters:
        * location - {<OpenLayers.Pixel>} where zoombar drawing is to start.
        */
        _addZoomBar: function(centered) {
            var imgLocation = OpenLayers.Util.getImagesLocation();

            var id = this.id + "_" + this.map.id;
            var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
            var slider = OpenLayers.Util.createAlphaImageDiv(id,
                       centered.add(-1, zoomsToEnd * this.zoomStopHeight),
                       new OpenLayers.Size(20, 9),
                       imgLocation + "slider.png",
                       "absolute");
            slider.style.cursor = "hand";
            this.slider = slider;

            this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
                                            { includeXY: true });
            this.sliderEvents.on({
                "mousedown": this.zoomBarDown,
                "mousemove": this.zoomBarDrag,
                "mouseup": this.zoomBarUp,
                "dblclick": this.doubleClick,
                "click": this.doubleClick
            });

            var sz = new OpenLayers.Size();
            sz.h = this.zoomStopHeight * this.map.getNumZoomLevels();
            sz.w = this.zoomStopWidth;
            var div = null;

            if (OpenLayers.Util.alphaHack()) {
                var id = this.id + "_" + this.map.id;
                div = OpenLayers.Util.createAlphaImageDiv(id, centered,
                                      new OpenLayers.Size(sz.w,
                                              this.zoomStopHeight),
                                      imgLocation + "zoombar.png",
                                      "absolute", null, "crop");
                div.style.height = sz.h + "px";
            } else {
                div = OpenLayers.Util.createDiv(
                        'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
                        centered,
                        sz,
                        imgLocation + "zoombar.png");
            }
            div.style.cursor = "hand";
            this.zoombarDiv = div;

            this.divEvents = new OpenLayers.Events(this, div, null, true,
                                                { includeXY: true });
            this.divEvents.on({
                "mousedown": this.divClick,
                "mousemove": this.passEventToSlider,
                "dblclick": this.doubleClick,
                "click": this.doubleClick
            });

            this.div.appendChild(div);

            this.startTop = parseInt(div.style.top);
            this.div.appendChild(slider);

            this.map.events.register("zoomend", this, this.moveZoomBar);

            centered = centered.add(0,
            this.zoomStopHeight * this.map.getNumZoomLevels());
            return centered;
        },
        /**
        * Method: _removeZoomBar
        */
        _removeZoomBar: function() {
            this.sliderEvents.un({
                "mousedown": this.zoomBarDown,
                "mousemove": this.zoomBarDrag,
                "mouseup": this.zoomBarUp,
                "dblclick": this.doubleClick,
                "click": this.doubleClick
            });
            this.sliderEvents.destroy();

            this.divEvents.un({
                "mousedown": this.divClick,
                "mousemove": this.passEventToSlider,
                "dblclick": this.doubleClick,
                "click": this.doubleClick
            });
            this.divEvents.destroy();

            //            if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion == 7) {
            this.zoombarDiv.outerHTML = "";
            this.slider.outerHTML = "";
            //            }
            //            else {
            //                this.div.removeChild(this.zoombarDiv);
            //                this.div.removeChild(this.slider);
            //            }
            this.zoombarDiv = null;
            this.slider = null;

            this.map.events.unregister("zoomend", this, this.moveZoomBar);
        },

        CLASS_NAME: "OpenLayers.Control.PanZoomBarBugFix"
    });
