﻿/* ***** 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.ScaleLineBugFixed
*
* Inherits from:
*  - <OpenLayers.Control.ScaleLine>
*/
OpenLayers.Control.ScaleLineBugFixed =
  OpenLayers.Class(OpenLayers.Control.ScaleLine, {

      /**
      * Constructor: OpenLayers.Control.ScaleLineBugFixed
      */
      initialize: function() {
          OpenLayers.Control.LayerSwitcher.prototype.initialize.apply(this, arguments);
      },

      /**
      * Method: update
      * Update the size of the bars, and the labels they contain.
      */
      update: function() {
          var res = this.map.getResolution();
          if (!res) {
              return;
          }

          var curMapUnits = this.map.units;
          var inches = OpenLayers.INCHES_PER_UNIT;

          // convert maxWidth to map units
          var maxSizeData = this.maxWidth * res * inches[curMapUnits];

          // decide whether to use large or small scale units     
          var topUnits;
          var bottomUnits;
          if (maxSizeData > 100000) {
              topUnits = this.topOutUnits;
              bottomUnits = this.bottomOutUnits;
          } else {
              topUnits = this.topInUnits;
              bottomUnits = this.bottomInUnits;
          }

          // and to map units units
          var topMax = maxSizeData / inches[topUnits];
          var bottomMax = maxSizeData / inches[bottomUnits];

          // now trim this down to useful block length
          var topRounded = this.getBarLen(topMax);
          var bottomRounded = this.getBarLen(bottomMax);

          // and back to display units
          topMax = topRounded / inches[curMapUnits] * inches[topUnits];
          bottomMax = bottomRounded / inches[curMapUnits] * inches[bottomUnits];

          // and to pixel units
          var topPx = topMax / res;
          var bottomPx = bottomMax / res;

          if ((this.topOutUnits != "") && (this.topInUnits != "")) {
              // now set the pixel widths
              this.eTop.style.width = Math.round(topPx) + "px";
              // and the values inside them
              this.eTop.innerHTML = topRounded + " " + topUnits;
          }

          if ((this.bottomOutUnits != "") && (this.bottomInUnits != "")) {
              // now set the pixel widths
              this.eBottom.style.width = Math.round(bottomPx) + "px";
              // and the values inside them
              this.eBottom.innerHTML = bottomRounded + " " + bottomUnits;
          }
      },

      CLASS_NAME: "OpenLayers.Control.ScaleLineBugFixed"
  }); 
