﻿/**
* 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"
  }); 