var PixelsPerInch = 72; // these can be overridden, defaults are for meters
var InchesPerMapUnit = 39.3701;


function Mapserv(name, mapfile, queryfile, minx, miny, maxx, maxy, width, height)
{
  this.mode = 'map';
  this.name = name; // name of applet or image
  this.url = '';

  this.layers = new Array();
  this.layerlist = '';

  this.mapfile = mapfile;
  this.queryfile = queryfile;

  this.extent = new Array(minx, miny, maxx, maxy);
  
  this.queryextent = new Array(-1, -1, -1, -1);
  this.querypoint = new Array(-1, -1);

  this.width = width;
  this.height = height;

  this.options = '';
  this.queryoptions = '';

  //this.referencemap = null;
  
  this.cellsize = AdjustExtent(this.extent, this.width, this.height);
  this.defaultextent = this.extent;

  this.zoomsize = 2;
  this.zoomdir = 0; // pan to start

  this.minscale = -1;
  this.maxscale = -1;

  this.pansize = .8;

  this.box = true; // allow box drawing (or not)
  
  this.x = -1;
  this.y = -1;
  
  
  this.draw = Mapserv_draw;
  this.applybox = Mapserv_applybox;
  this.applyzoom = Mapserv_applyzoom;
  this.setextentfromscale = Mapserv_setextentfromscale;
  this.getscale = Mapserv_getscale;
  this.boxon = Mapserv_boxon;
  this.boxoff = Mapserv_boxoff;
  this.setextent = Mapserv_setextent;
}

function Mapserv_boxon(){
	eval("document.jBox.boxon()");
}

function Mapserv_boxoff(){
	eval("document.jBox.boxoff()");
}

function Mapserv_draw()
{
  this.url = MapServer +
       	     '?mode=' + this.mode + 
             '&map=' + this.mapfile +
             '&mapext=' + this.extent.join('+') +
             '&mapsize=' + this.width + '+' + this.height +
             '&reg_av='+args['reg_av'];
				
  //alert(this.url);
  listeAdd(this);
  //alert(this.url);
  eval("document." + this.name + ".setimage(this.url)");
}

function Mapserv_applybox(minx, miny, maxx, maxy) 
{
  var temp = new Array(4);
  temp[0] = this.extent[0] + this.cellsize*minx;
  temp[1] = this.extent[3] - this.cellsize*maxy;
  temp[2] = this.extent[0] + this.cellsize*maxx;	
  temp[3] = this.extent[3] - this.cellsize*miny;

  this.extent = temp;
  this.cellsize = AdjustExtent(this.extent, this.width, this.height);

  if(this.minscale != -1 && this.getscale() < this.minscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.minscale);
  }
  if(this.maxscale != -1 && this.getscale() > this.maxscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.maxscale);
  }
}

function Mapserv_applyzoom(x,y)
{
  var dx, dy;
  var mx, my;
  var x, y;
  var zoom;

  if(this.zoomdir == 1 && this.zoomsize != 0)
    zoom = this.zoomsize;
  else if(this.zoomdir == -1 && this.zoomsize != 0)
    zoom = 1/this.zoomsize;
  else
    zoom = 1;

  dx = this.extent[2] - this.extent[0];
  dy = this.extent[3] - this.extent[1];
  mx = this.extent[0] + this.cellsize*x; // convert *click* to map coordinates
  my = this.extent[3] - this.cellsize*y;

  this.extent[0] = mx - .5*(dx/zoom);
  this.extent[1] = my - .5*(dy/zoom);
  this.extent[2] = mx + .5*(dx/zoom);
  this.extent[3] = my + .5*(dy/zoom);

  this.cellsize = AdjustExtent(this.extent, this.width, this.height);

  if(this.minscale != -1 && this.getscale() < this.minscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.minscale);
  }
  if(this.maxscale != -1 && this.getscale() > this.maxscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.maxscale);
  }
}

function Mapserv_setextentfromscale(x, y, scale)
{
  if((this.minscale != -1) && (scale < this.minscale))
    scale = this.minscale;

  if((this.maxscale != -1) && (scale > this.maxscale))
    scale = this.maxscale;

  this.cellsize = (scale/PixelsPerInch)/InchesPerMapUnit;

  this.extent[0] = x - this.cellsize*this.width/2.0;
  this.extent[1] = y - this.cellsize*this.height/2.0;
  this.extent[2] = x + this.cellsize*this.width/2.0;
  this.extent[3] = y + this.cellsize*this.height/2.0;

  this.cellsize = AdjustExtent(this.extent, this.width, this.height);
}

function Mapserv_getscale()
{
  var gd, md;

  md = (this.width-1)/(PixelsPerInch*InchesPerMapUnit);
  gd = this.extent[2] - this.extent[0];

  return(gd/md);
}

function Mapserv_setextent(minx, miny, maxx, maxy)
{
  this.extent[0] = minx;
  this.extent[1] = miny;
  this.extent[2] = maxx;
  this.extent[3] = maxy;

  this.cellsize = AdjustExtent(this.extent, this.width, this.height);

  if(this.minscale != -1 && this.getscale() < this.minscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.minscale);    
  }
  if(this.maxscale != -1 && this.getscale() > this.maxscale) {
    x = (this.extent[2] + this.extent[0])/2;
    y = (this.extent[3] + this.extent[1])/2;
    this.setextentfromscale(x, y, this.maxscale);
  }
}
