ZoomBar = function(newID, newParent, newXPosition, newYPosition, newZIndex, newVisibility, mapObject, ratio, newHeight,startColor,endColor,activeColor)
{
  if (arguments.length > 0)
    this.init(newID, newParent, newXPosition, newYPosition, newZIndex, newVisibility, mapObject, ratio, newHeight,startColor,endColor,activeColor);
};

ZoomBar.prototype = new GuiWidget();
ZoomBar.prototype.constructor = ZoomBar;
ZoomBar.superclass = GuiWidget.prototype;

ZoomBar.prototype.init = function(newID, newParent, newXPosition, newYPosition, newZIndex, newVisibility, newMapObject, ratio, newHeight,startColor,endColor,activeColor)
{
  this._WIDTH = 15;
  this._DIVHEIGHT = 11;
  ZoomBar.superclass.init.call(this, newID, newParent, newXPosition, newYPosition, newZIndex, this._WIDTH, newHeight, newVisibility, "zoomBar");
  this.ratio = ratio;
  this.numDivs = parseInt((newHeight-50) / this._DIVHEIGHT);
  this.startColor = startColor;
  this.endColor = endColor;
  this.mapObject = newMapObject;
  this.mapObject.zoomBar = this;
  this.divStats = null;  //will be an object containing info about each div
  this.divColors = null;
  this.activeDiv = null;
  this.activeColor = activeColor;
  this.activeDivColor = null;
  this.activeZoomTarget = null;
  this.initialized = false;
};

ZoomBar.prototype.initialize = function()
{
  //build initial zoom bar
  this.initialMapArea = Math.abs((this.mapObject.initialExtent[1]-this.mapObject.initialExtent[0])*(this.mapObject.initialExtent[3]-this.mapObject.initialExtent[2]));
  this.updateIntervals();
  this.draw();
  this.initialized = true;
};

ZoomBar.prototype.updateIntervals = function()
{
  if (this.mapObject==null)
    return;
  if ((this.mapObject.initialExtent==null)||(this.mapObject.extent==null))
    return;
  //recompute the value of each zoom bar div based on the starting extent and div count
  this.divStats = new Array();
  var Bottom = this.mapObject.extent[0];
  var Top = this.mapObject.extent[1];
  var Left = this.mapObject.extent[2];
  var Right = this.mapObject.extent[3];

  var CenterY = Bottom+((Top - Bottom)/2.0);
  var CenterX = Left+((Right - Left)/2.0);
  var FullBottom = this.mapObject.initialExtent[0];
  var FullTop = this.mapObject.initialExtent[1];
  var FullLeft = this.mapObject.initialExtent[2];
  var FullRight = this.mapObject.initialExtent[3];
  var FullHeight = FullTop - FullBottom;
  var FullWidth = FullRight - FullLeft;
  
  var NumDivisions = this.numDivs;
  var Ratio = this.ratio;
  
  var SumTheDivs = 0;
  var SomeExtentWidth = 0;
  var SomeExtentHeight = 0;
  var dx = 0;
  var dy = 0;
  var subfactor = 0;
  var ZoombarWidth = 0;
  var ZoombarHeight = 0;

  var newBottom = 0; 
  var newTop = 0;
  var newLeft = 0;
  var newRight = 0;
  
  for (var Division = 0; Division < NumDivisions; Division++)
  {
    if (Division == (NumDivisions-1)) // go to full extent direct to avoid rounding errors
    {
      newBottom = FullBottom;
      newTop = FullTop;
      newLeft = FullLeft;
      newRight = FullRight;
    }
    else
    {
      /* Why was this? */
      SumTheDivs = 0;
      for (lcv = NumDivisions-1; lcv > 0; lcv--)
        SumTheDivs = SumTheDivs + lcv;
      if (SumTheDivs <= 0)    // error here
        alert('Unknown Error in ZoomBar.updateIntervals');
        
      /* What is this?? */
      SomeExtentWidth = Ratio * FullWidth;
      SomeExtentHeight = Ratio * FullHeight;
      dx = (FullWidth - SomeExtentWidth)/(SumTheDivs);
      dy = (FullHeight - SomeExtentHeight)/(SumTheDivs);
      /* Don't know... */
      subfactor = 0;
      for (lcv = NumDivisions-1; lcv >= Division; lcv--)
        subfactor = subfactor + lcv;
      /* At least I recall variables now */
      ZoombarWidth = FullWidth - (dx * subfactor);
      ZoombarHeight = FullHeight - (dy * subfactor);
      /* Finally */
      newBottom = CenterY - (ZoombarHeight / 2.0);
      newTop = CenterY + (ZoombarHeight / 2.0);
      newLeft = CenterX - (ZoombarWidth / 2.0);
      newRight = CenterX + (ZoombarWidth / 2.0);
    }    
    this.divStats[Division] = Math.abs((newTop-newBottom)*(newRight-newLeft));
  }
};

ZoomBar.prototype.highlightNearestDiv = function()
{
  if (this.initialized)
  {
    //calculate the area of the current extent
    var currentArea = Math.abs((this.mapObject.extent[1]-this.mapObject.extent[0])*(this.mapObject.extent[3]-this.mapObject.extent[2]));
    //loop through divStats array to find closest area
    var bestGuessDiv = -1;
    var oldBestGuess = -1;
    for (var currentDiv = 0; currentDiv < this.divStats.length; currentDiv++)
    {
      if (currentArea<=this.divStats[currentDiv])
      {
        bestGuessDiv = currentDiv;
        if (currentDiv < (this.divStats.length-1))
        {
          if (Math.abs(currentArea-this.divStats[currentDiv])<Math.abs(currentArea-this.divStats[currentDiv+1]))
            bestGuessDiv++;
        }
        break;
      }
    }
    if ((bestGuessDiv==-1)||(bestGuessDiv>=this.divStats.length))
    {
      bestGuessDiv=this.divStats.length-1;
    }
    else
    {
      if ((bestGuessDiv>0)&&(bestGuessDiv<this.divStats.length))
        bestGuessDiv--;
    }
    if (this.activeDiv != null) //fix old div color
    {
      var element = document.getElementById(this.id+'.zoomBarDiv['+this.activeDiv+']');
      if (element!=null)
        element.style["backgroundColor"] = this.activeDivColor;
    }
    var element = document.getElementById(this.id+'.zoomBarDiv['+bestGuessDiv+']');
    if (element!=null)  //update div
    {
      this.activeDivColor = element.style["backgroundColor"];
      element.style["backgroundColor"] = this.activeColor;
      this.activeDiv = bestGuessDiv;
    }
    else
    {
      this.activeDiv = null;
      this.activeColor = null;
    }
  }
};

ZoomBar.prototype.draw = function()
{
  this.divColors = linearGradient(this.startColor, this.endColor,this.numDivs);
  var html = '<TABLE cellpadding="0" cellspacing="2"><TR height="15px">'+
  '<TD style="text-align: center; cursor: pointer;" width="10px" onmouseover="document.mapObject.mapImage.showZoomInTarget();" onmouseout="document.mapObject.mapImage.hideZoomInTarget();" onclick="document.mapObject.zoomIn();"><IMG src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/'+'zoomBar_InBtn.png" /></TD></TR>';
  for (var lcv=0;lcv < this.numDivs;lcv++)
  {
    html = html + '<TR>'+
    '<TD id="'+this.id+'.zoomBarDiv['+lcv+']" height="'+this._DIVHEIGHT+'" class="zoomBarCell">'+
    '<IMG src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/'+'blank.gif" class="zoomBarCell" height="5" width="10" onmouseover="document.mapObject.zoomBar.mouseOverDiv('+lcv+'); this.src = \'./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/'+'zoomBarHighlight.gif\';" onmouseout="document.mapObject.zoomBar.mouseOutDiv('+lcv+'); this.src=\'./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/blank.gif\'" onclick="document.mapObject.zoomBar.mouseClickDiv('+lcv+'); document.mapObject.zoombar('+this.ratio+','+lcv+','+this.numDivs+');"></TD></TR>';
  }
  html = html + '<TR height="15px"><TD style="text-align: center; cursor: pointer;" onmouseover="document.mapObject.mapImage.showZoomOutTarget();" onmouseout="document.mapObject.mapImage.hideZoomOutTarget();"  onclick="document.mapObject.zoomOut();"><IMG src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/'+'zoomBar_OutBtn.png" /></TD></TR></TABLE>';
  this.element.innerHTML = html;
  var element = null;
  for (var lcv=0;lcv < this.numDivs;lcv++)
  {
    element = document.getElementById(this.id+'.zoomBarDiv['+lcv+']');
    if (element!=null)
    {
      element.style.background = this.divColors[lcv];
    }
  };
  this.activeDiv = null;
  this.activeDivColor = null;
};

ZoomBar.prototype.rebuild = function(newHeight)
{
  this.height(newHeight);
  this.width(this._WIDTH);
  this.numDivs = parseInt((newHeight-50) / this._DIVHEIGHT);
  this.updateIntervals();
  this.draw();
  this.highlightNearestDiv();
};

ZoomBar.prototype.mouseOverDiv = function(divIndex)
{
  //show appropriate zoom target on the map
  if (document.extensionConfig.zoomOverlay.enabled)
  {
  if (this.activeDiv != null)
  {
    if (divIndex < this.activeDiv)
    {
      this.mapObject.mapImage.showZoomInTarget();
      this.activeZoomTarget = 'zoomIn';
    }
    else
    {
      if (divIndex == this.activeDiv)
      {
        this.activeZoomTarget = '';
      }
      else
      {
        this.mapObject.mapImage.showZoomOutTarget();
        this.activeZoomTarget = 'zoomOut';
      }
    }
  }
  }
};

ZoomBar.prototype.mouseOutDiv = function(divIndex)
{
  //hide zoom target again
  if (document.extensionConfig.zoomOverlay.enabled)
  {
    switch (this.activeZoomTarget)
    {
      case 'zoomIn':
        this.mapObject.mapImage.hideZoomInTarget();
        break;
      case 'zoomOut':
        this.mapObject.mapImage.hideZoomOutTarget();
        break;
    }
  }
};

ZoomBar.prototype.mouseClickDiv = function(divIndex)
{
  //hide zoom target again
  if (document.extensionConfig.zoomOverlay.enabled)
  {
    switch (this.activeZoomTarget)
    {
      case 'zoomIn':
        this.mapObject.mapImage.hideZoomInTarget();
        break;
      case 'zoomOut':
        this.mapObject.mapImage.hideZoomOutTarget();
        break;
    }
  }
};

