/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
 * for the full text of the license. */

/**
 * @requires OpenLayers/Control.js
 */

/**
 * Class: OpenLayers.Control.ScaleLine
 * The ScaleLine displays a small line indicator representing the current 
 * map scale on the map. By default it is drawn in the lower left corner of
 * the map.
 * 
 * Class: OpenLayers.Control.LiniaEscala
 * Basat en OpenLayers.Control.ScaleLine
 *
 * Inherits from:
 *  - <OpenLayers.Control>
 *  
 * Is a very close copy of:
 *  - <OpenLayers.Control.Scale>
 */
OpenLayers.Control.LiniaEscala = OpenLayers.Class(OpenLayers.Control, {

    /**
     * Property: maxWidth
     * {Integer} Maximum width of the scale line in pixels.  Default is 100.
     */
    maxWidth: 100,

    /**
     * Property: outUnits
     * {String} Units for zoomed out on top bar.  Default is km.
     */
    outUnits: "km",
    
    /**
     * Property: inUnits
     * {String} Units for zoomed in on top bar.  Default is m.
     */
    inUnits: "m",

   
    /**
     * Property: eTop
     * {DOMElement}
     */
    eTop: null,

    /**
     * Property: eTop
     * {DOMElement}
     */
    eTop2: null,

    /**
     * Property: eBottom
     * {DOMElement}
     */
    eBottom:null,

    /**
     * Constructor: OpenLayers.Control.ScaleLine
     * Create a new scale line control.
     * 
     * Parameters:
     * options - {Object} An optional object whose properties will be used
     *     to extend the control.
     */
    initialize: function(options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);     
    },

    /**
     * Method: draw
     * 
     * Returns:
     * {DOMElement}
     */
    draw: function() {
        OpenLayers.Control.prototype.draw.apply(this, arguments);
        if (!this.eTop) {
            this.div.style.display = "block";
            this.div.style.position = "absolute";
            
            // stick in the top bar
            this.eTop = document.createElement("div");
            this.eTop.className = this.displayClass + "Top";
            var theLen = this.inUnits.length;
            this.div.appendChild(this.eTop);

            // and the bottom bar
            this.eBottom = document.createElement("div");
            this.eBottom.className = this.displayClass + "Bottom";
            this.div.appendChild(this.eBottom);

            if((this.outUnits == "") || (this.inUnits == "")) {
                this.eTop.style.visibility = "hidden";
                this.eBottom.style.visibility = "hidden";
            } else {
                this.eTop.style.visibility = "visible";
                this.eBottom.style.visibility = "visible";
            }
        }
        this.map.events.register('moveend', this, this.updateLiniaEscala);
        this.updateLiniaEscala();
        return this.div;
    },

    /** 
     * Method: getBarLen
     * Given a number, round it down to the nearest 1,2,5 times a power of 10.
     * That seems a fairly useful set of number groups to use.
     * 
     * Parameters:
     * maxLen - {float}  the number we're rounding down from
     * 
     * Returns:
     * {Float} the rounded number (less than or equal to maxLen)
     */
    getBarLen: function(maxLen) {
        // nearest power of 10 lower than maxLen
        var digits = parseInt(Math.log(maxLen) / Math.log(10));
        var pow10 = Math.pow(10, digits);
        
        // ok, find first character
        var firstChar = parseInt(maxLen / pow10);

        // right, put it into the correct bracket
        var barLen;
        if(firstChar > 5) {
            barLen = 5;
        } else if(firstChar > 2) {
            barLen = 2;
        } else {
            barLen = 1;
        }

        // scale it up the correct power of 10
        return barLen * pow10;
    },

    /**
     * Method: update
     * Update the size of the bars, and the labels they contain.
     */
    updateLiniaEscala: function() {
        var res = this.map.getResolution();
        if (!res) {
            return;
        }

        var curMapUnits = this.map.getUnits();
        var scale = this.map.getScale();
        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 > 50000) {
            topUnits = this.outUnits;
        } else {
            topUnits = this.inUnits;
        }

        // and to map units units
        var topMax = maxSizeData / inches[topUnits];

        // now trim this down to useful block length
        var topRounded = this.getBarLen(topMax);

        // and back to display units
        topMax = topRounded / inches[curMapUnits] * inches[topUnits];

        // and to pixel units
        var topPx = topMax / res;
        
        // now set the pixel widths
        // and the values inside them
        if (this.eTop.style.visibility == "visible"){
            this.eBottom.style.width = Math.round(topPx) + "px"; 
            if (!scale)
                this.eBottom.innerHTML = "&nbsp;";
            else
            {
                scale = Math.round(scale);
                this.eBottom.innerHTML = "1:" + scale;
            }
            this.eTop.style.width = Math.round(topPx) + "px";
            this.eTop.innerHTML = topRounded + " " + topUnits;
        }
        
    }, 

    CLASS_NAME: "OpenLayers.Control.LiniaEscala"
});


