﻿/* ***** 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.StyledOverviewMap
*
* Inherits from:
*  - <OpenLayers.Control.OverviewMap>
*/
OpenLayers.Control.StyledOverviewMap =
    OpenLayers.Class(OpenLayers.Control.OverviewMap, {

    /**
    * Property: minRectSize
    * Overrides the default 16 pixel 
    */
    minRectSize: 4,

        initialize: function() {
            OpenLayers.Control.OverviewMap.prototype.initialize.apply(this, arguments);
        },

        /**
        * Method: draw
        * Render the control in the browser.
        */
        draw: function() {
            OpenLayers.Control.prototype.draw.apply(this, arguments);
            if (!(this.layers.length > 0)) {
                if (this.map.baseLayer) {
                    var layer = this.map.baseLayer.clone();
                    this.layers = [layer];
                } else {
                    this.map.events.register("changebaselayer", this, this.baseLayerDraw);
                    return this.div;
                }
            }

            // create overview map DOM elements
            this.element = document.createElement('div');
            this.element.className = this.displayClass + 'Element';
            this.element.style.display = 'none';

            this.mapDiv = document.createElement('div');
            this.mapDiv.style.width = this.size.w + 'px';
            this.mapDiv.style.height = this.size.h + 'px';
            this.mapDiv.style.position = 'relative';
            this.mapDiv.style.overflow = 'hidden';
            //this.mapDiv.style.display = 'block';
            this.mapDiv.id = OpenLayers.Util.createUniqueID('overviewMap');

            this.extentRectangle = document.createElement('div');
            this.extentRectangle.style.position = 'absolute';
            this.extentRectangle.style.zIndex = 1000;  //HACK
            this.extentRectangle.className = this.displayClass + 'ExtentRectangle';
            this.mapDiv.appendChild(this.extentRectangle);

            this.element.appendChild(this.mapDiv);

            this.div.appendChild(this.element);

            // Optionally add min/max buttons if the control will go in the
            // map viewport.
            if (!this.outsideViewport) {
                this.div.className += " " + this.displayClass + 'Container';
                var imgLocation = OpenLayers.Util.getImagesLocation();
                // maximize button div
                var img = imgLocation + 'overview-map-maximize.png';
                this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv(
                                        this.displayClass + 'MaximizeButton',
                                        null,
                                        new OpenLayers.Size(207, 18),
                                        img,
                                        'absolute');
                this.maximizeDiv.style.display = 'none';
                this.maximizeDiv.className = this.displayClass + 'MaximizeButton';
                OpenLayers.Event.observe(this.maximizeDiv, 'click',
                    OpenLayers.Function.bindAsEventListener(this.maximizeControl,
                                                        this));

                this.div.appendChild(this.maximizeDiv);

                // minimize button div
                var img = imgLocation + 'overview-map-minimize.png';
                this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv(
                                        //'OpenLayers_Control_minimizeDiv',
                                        this.displayClass +  'MinimizeButton',
                                        null,
                                        new OpenLayers.Size(207, 18),
                                        img,
                                        'absolute');
                this.minimizeDiv.style.display = 'none';
                this.minimizeDiv.className = this.displayClass + 'MinimizeButton';
                OpenLayers.Event.observe(this.minimizeDiv, 'click',
                    OpenLayers.Function.bindAsEventListener(this.minimizeControl,
                                                        this));

                this.div.appendChild(this.minimizeDiv);

                var eventsToStop = ['dblclick', 'mousedown'];

                for (var i = 0; i < eventsToStop.length; i++) {

                    OpenLayers.Event.observe(this.maximizeDiv,
                                         eventsToStop[i],
                                         OpenLayers.Event.stop);

                    OpenLayers.Event.observe(this.minimizeDiv,
                                         eventsToStop[i],
                                         OpenLayers.Event.stop);
                }

                this.minimizeControl();
            } else {
                // show the overview map
                this.element.style.display = '';
            }
            if (this.map.getExtent()) {
                this.update();
            }

            this.map.events.register('moveend', this, this.update);

            return this.div;
        },

        createMap: function() {
      
            OpenLayers.Control.OverviewMap.prototype.createMap.apply(this, arguments);        

            // check extent rectangle border width (overwrite, default to border-width=4)
            this.wComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
                                               'border-left-width')) +
                     parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
                                               'border-right-width'));
            this.wComp = (this.wComp) ? this.wComp : 4;
            this.hComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
                                               'border-top-width')) +
                     parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
                                               'border-bottom-width'));
            this.hComp = (this.hComp) ? this.hComp : 4;
            
        },

        
        CLASS_NAME: "OpenLayers.Control.StyledOverviewMap"
    });       
