//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 unitLookup={'FT':_FT_,'YD':_YD_,'M':_M_,'MI':_MI_,'KM':_KM_};
  var unitLabels=Array('FT','YD','MI','m','km');
  var initialized = false;
  document.mapObject.scalebar = this;
  this.element = null;
  this.initialize = function()
  {
    if(document.applicationConfig.mapUnits=='LL')
      return;
    this.mapObject = document.mapObject;
    this.mapImage = this.mapObject.mapImage;
    this.baseUnits = unitLookup[document.applicationConfig.mapUnits];
    if (this.element==null)
      this.createElements();
    //attempt to load config from file
    
    
    
    this.config = [];
    if (this.mapObject.config.scalebarConfig==null)
    {
      //unit conversion populates config[2] and config[5]
      this.config = [
      [_FT_,10,null,_M_,1,null,250],
      [_FT_,100,null,_M_,10,null,500],
      [_FT_,200,null,_M_,50,null,2000],
      [_FT_,500,null,_M_,100,null,3000],
      [_FT_,1000,null,_M_,250,null,6000],
      [_MI_,1,null,_KM_,1,null,25000],
      [_MI_,2,null,_KM_,2,null,50000],
      [_MI_,2.5,null,_KM_,2.5,null,90000],
      [_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);
    }
    initialized = true;
  };
  
  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.backgroundColor = '#000000';
    this.leftBarElement.style.top = 0;
    this.leftBarElement.style.left = 0;
    this.leftBarElement.style.width = xIE4Up?4:2;
    this.leftBarElement.style.height = 30;
    this.leftBarElement.style.borderWidth = 1;
    this.leftBarElement.style.borderColor = '#FFFFFF';
    this.leftBarElement.style.borderStyle = 'solid';
    
    this.barElement = document.createElement('DIV');
    this.element.appendChild(this.barElement);
    
    this.barElement.style.position = 'absolute';
    this.barElement.style.display = 'block';
    this.barElement.style.height = xIE4Up?4:2;
    this.barElement.style.width = 100;
    this.barElement.style.backgroundColor = '#000000';
    this.barElement.style.top = 14;
    this.barElement.style.left = 3;
    this.barElement.style.borderTopWidth = 1;
    this.barElement.style.borderBottomWidth = 1;
    this.barElement.style.borderRightWidth = 1;
    this.barElement.style.borderLeftWidth = 0;
    this.barElement.style.borderColor = '#FFFFFF';
    this.barElement.style.borderStyle = 'solid';
    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 = xIE4Up?4:2;
    this.upperBarElement.style.height = xIE4Up?11:10;
    this.upperBarElement.style.top = 4;
    this.upperBarElement.style.left = 2;
    this.upperBarElement.style.backgroundColor = '#000000';
    this.upperBarElement.style.borderTopWidth = 1;
    this.upperBarElement.style.borderBottomWidth = 0;
    this.upperBarElement.style.borderRightWidth = 1;
    this.upperBarElement.style.borderLeftWidth = 1;
    this.upperBarElement.style.borderColor = '#FFFFFF';
    this.upperBarElement.style.borderStyle = 'solid';
    this.upperBarElement.style.overflow = 'hidden'; //stop IE from ignoring height
    
    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 = xIE4Up?4:2;
    this.lowerBarElement.style.height = xIE4Up?11:10;
    this.lowerBarElement.style.top = 17;
    this.lowerBarElement.style.left = 6;
    this.lowerBarElement.style.backgroundColor = '#000000';
    this.lowerBarElement.style.borderTopWidth = 0;
    this.lowerBarElement.style.borderBottomWidth = 1;
    this.lowerBarElement.style.borderRightWidth = 1;
    this.lowerBarElement.style.borderLeftWidth = 1;
    this.lowerBarElement.style.borderColor = '#FFFFFF';
    this.lowerBarElement.style.borderStyle = 'solid';
    this.lowerBarElement.style.overflow = 'hidden'; //stop IE from ignoring height
    
  };
  
  this.drawScalebar = function()
  {
    if(document.applicationConfig.mapUnits=='LL') return null;
    if (!initialized)
      this.initialize();
    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-(xIE4Up?1:0);
    this.lowerBarElement.style.left = lowerBarWidth-(xIE4Up?1:0);
    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);
  };
};


FreeanceXMLParser.prototype.parseScalebarConfig = function(mapNode)
{
  var scalebarNodes = mapNode.getElementsByTagName('scalebar');
  if(scalebarNodes.length)
  {
    var scalebarNode = scalebarNodes[0];
    var scalebarConfig = new Array();
    for (var levelIndex = this.getNextNamedChildNodeIndex(scalebarNode,0,"level");levelIndex!=-1;levelIndex=this.getNextNamedChildNodeIndex(scalebarNode,levelIndex+1,"level"))
    {
      var levelNode = scalebarNode.childNodes[levelIndex];
      var currentLevel = scalebarConfig.length;
      scalebarConfig[currentLevel] = [
        parseInt(levelNode.getAttribute('topUnit')),
        parseFloat(levelNode.getAttribute('topLength')),
        parseInt(levelNode.getAttribute('bottomUnit')),
        parseFloat(levelNode.getAttribute('bottomLength')),
        parseFloat(levelNode.getAttribute('mapWidth'))
      ];
    }
  }
  return scalebarConfig;
};

if (Freeance_BundleExtension_Manager)
  Freeance_BundleExtension_Manager.register('scalebar',function(){
    if(document.mapObject.configDocNode)
      document.mapObject.config.scalebarConfig = document.freeance_config_parser.parseScalebarConfig(document.mapObject.configDocNode);
	if(document.extensionConfig.scalebar && document.extensionConfig.scalebar.enabled){
	  document.mapObject.scalebar = new Scalebar();
	}
    if (_MAP_INITIALIZED){document.mapObject.scalebar.drawScalebar();}
    return true;
  });
