//Scalebar.js
//Draw a scale bar on the map
//Config definition:
//Array of Array(<units_top>,<unitSize_top>,<top_in_mapUnits>,<units_bottom>,<unitSize_bottom>,<bottom_in_mapUnits>,<maximum width of map in map units>);
//units_top is the unit the top bar uses; unitSize_top is the length of the top bar in the specified units
//maxBarWidth is the maximum width of the map in map units to display this zoom bar

function Scalebar(){
  var _FT_=0;
  var _YD_=1;
  var _MI_=2;
  var _M_ =3;
  var _KM_=4;
  var _LL_=5;
  var unitLookup={'FT':_FT_,'YD':_YD_,'M':_M_,'MI':_MI_,'KM':_KM_,'LL':_LL_};
  var unitLabels=Array('FT','YD','MI','m','km');
  document.mapObject.scalebar = this;
  this.element = null;
  this.initialize = function()
  {
    if(document.applicationConfig.mapUnits=='LL') return null;
    this.mapObject = document.mapObject;
    this.mapImage = this.mapObject.mapImage;
    if (this.element==null)
      this.createElements();
    this.baseUnits = unitLookup[document.applicationConfig.mapUnits];

    this.config = new Array();
    if (this.mapObject.config.scalebarConfig==null)
    {
      //unit conversion populates config[2] and config[5]
      this.config[0] = new Array(_FT_,10,null,_M_,1,null,250);
      this.config[1] = new Array(_FT_,100,null,_M_,10,null,500);
      this.config[2] = new Array(_FT_,200,null,_M_,50,null,2000);
      this.config[3] = new Array(_FT_,500,null,_M_,100,null,3000);
      this.config[4] = new Array(_FT_,1000,null,_M_,250,null,6000);
      this.config[5] = new Array(_MI_,1,null,_KM_,1,null,25000);
      this.config[6] = new Array(_MI_,2,null,_KM_,2,null,50000);
      this.config[7] = new Array(_MI_,2.5,null,_KM_,2.5,null,90000);
      this.config[8] = new Array(_MI_,5,null,_KM_,5,null,110000);
    }
    else
    {
      for (var lcv=0;lcv<this.mapObject.config.scalebarConfig.length;lcv++)
      {
        this.config[lcv] = new Array(this.mapObject.config.scalebarConfig[lcv][0],this.mapObject.config.scalebarConfig[lcv][1],null,this.mapObject.config.scalebarConfig[lcv][2],this.mapObject.config.scalebarConfig[lcv][3],null,this.mapObject.config.scalebarConfig[lcv][4]);
      }
    }
    for (var lcv=0;lcv<this.config.length; lcv++)
    {
      this.config[lcv][2]=this.convertUnits(this.config[lcv][1],this.config[lcv][0],this.baseUnits);
      this.config[lcv][5]=this.convertUnits(this.config[lcv][4],this.config[lcv][3],this.baseUnits);
    }
  };
  
  this.createElements = function()
  {
    this.element = document.createElement('DIV');
    this.mapImage.rasterDrawPlaneNode.appendChild(this.element);
    
    this.element.id = this.mapObject.id+'.scalebar';
    this.element.style.position = 'absolute';
    this.element.style.display = 'none';
    this.element.style.height = 30;
    this.element.style.bottom = 10;
    this.element.style.left = 10;
    this.element.style.width = 100;
    this.element.style["font"] = '7pt Helvetica, sans-serif';
    
    this.leftBarElement = document.createElement('DIV');
    this.element.appendChild(this.leftBarElement);
    this.leftBarElement.style.position = 'absolute';
    this.leftBarElement.style.background = '#000000';
    this.leftBarElement.style.top = 0;
    this.leftBarElement.style.left = 0;
    this.leftBarElement.style.width = 2;
    this.leftBarElement.style.height = 30;
    
    this.barElement = document.createElement('DIV');
    this.element.appendChild(this.barElement);
    
    this.barElement.style.position = 'absolute';
    this.barElement.style.display = 'block';
    this.barElement.style.height = 2;
    this.barElement.style.width = 100;
    this.barElement.style.background = '#000000';
    this.barElement.style.top = 14;
    this.barElement.style.left = 0;
    this.barElement.style.overflow = 'hidden'; //stop IE from ignoring height
    
    this.topLabelElement = document.createElement('DIV');
    this.element.appendChild(this.topLabelElement);
    this.topLabelElement.style.position = 'absolute';
    this.topLabelElement.style.display = 'block';
    this.topLabelElement.style.height = 12;
    this.topLabelElement.style.left=4;
    this.topLabelElement.style.bottom = 16;
    
    this.upperBarElement = document.createElement('DIV');
    this.element.appendChild(this.upperBarElement);
    this.upperBarElement.style.position = 'absolute';
    this.upperBarElement.style.display = 'block';
    this.upperBarElement.style.width = 2;
    this.upperBarElement.style.height = 10;
    this.upperBarElement.style.top = 4;
    this.upperBarElement.style.left = 0;
    this.upperBarElement.style.background = '#000000';
    
    this.bottomLabelElement = document.createElement('DIV');
    this.element.appendChild(this.bottomLabelElement);
    this.bottomLabelElement.style.position = 'absolute';
    this.bottomLabelElement.style.display = 'block';
    this.bottomLabelElement.style.top=17;
    this.bottomLabelElement.style.left=4;

    this.lowerBarElement = document.createElement('DIV');
    this.element.appendChild(this.lowerBarElement);
    this.lowerBarElement.style.position = 'absolute';
    this.lowerBarElement.style.display = 'block';
    this.lowerBarElement.style.width = 2;
    this.lowerBarElement.style.height = 10;
    this.lowerBarElement.style.top = 16;
    this.lowerBarElement.style.left = 3;
    this.lowerBarElement.style.background = '#000000';
    
  };
  
  this.drawScalebar = function()
  {
    if(document.applicationConfig.mapUnits=='LL') return null;
    var bestGuess = 0;
    var mapWidth = Math.abs(this.mapObject.extent[3]-this.mapObject.extent[2]);
    for (var activeConfig=0;activeConfig<this.config.length;activeConfig++)
    {
      if (this.config[activeConfig][6]>mapWidth)
        break;
      else
        bestGuess = activeConfig;
    }
    if (bestGuess > this.config.length) return null;
    //calculate width of scalebar
    var upperBarWidth = this.mapImage.width()*(this.config[bestGuess][2]/mapWidth);
    var lowerBarWidth = this.mapImage.width()*(this.config[bestGuess][5]/mapWidth);

    //construct scalebar
    this.topLabelElement.innerHTML = this.config[bestGuess][1]+'&nbsp;'+unitLabels[this.config[bestGuess][0]];
    this.bottomLabelElement.innerHTML = this.config[bestGuess][4]+'&nbsp;'+unitLabels[this.config[bestGuess][3]];
    
    this.element.style.width = ((upperBarWidth>lowerBarWidth)?(upperBarWidth):(lowerBarWidth));
    this.barElement.style.width = ((upperBarWidth>lowerBarWidth)?(upperBarWidth):(lowerBarWidth));
    this.upperBarElement.style.left = upperBarWidth-2;
    this.lowerBarElement.style.left = lowerBarWidth-2;
    this.element.style.display = 'block';
  };
  this.clearScalebar = function()
  {
    this.element.style.display = 'none';
  };
  
  this.convertUnits = function(dist,srcUnit,dstUnit)
  {
  if (srcUnit == dstUnit)
    return(dist);
  var newdist = 0;
  /* step 1: convert to ft */
  switch (srcUnit)
  {
    case _FT_:
      newdist = dist;
      break;
    case _YD_:
      newdist = dist * 3;
      break;
    case _MI_:
      newdist = dist * 5280;
      break;
    case _M_:
      newdist = dist * 3.2808399;
      break;
    case _KM_:
      newdist = dist * 3280.8399;
      break;
  };
  /* step 2: convert from ft to requested units */
  switch (dstUnit)
  {
    case _FT_:break;
    case _YD_:
      newdist = newdist / 3.0;
      break;
    case _MI_:
      newdist = newdist / 5280.0;
      break;
    case _M_:
      newdist = newdist / 3.2808399;
      break;
    case _KM_:
      newdist = newdist / 3280.8399;
      break;
  };
  return(newdist);
  };
};
