// © 2011 INSER SA

// arcims.js
var request;
var requestOver;
var preloadImage;

function ArcIMS(
    imageService, featureService,
    mapDiv, mapImg, mapBorderDiv, mapLoadingDiv, mapZoomDiv, envelope, imageSize, mapDivPosition, 
    overDiv, overImg, overZoomImg, overBorderDiv, overLoadingDiv, overZoomDiv, overSize, overDivPosition,
    legendLayer1, legendLayer2, legendFields, legendDiv, legendLoadingDiv, 
    legendOffset, legendDivPosition, legendRetired,
    histoDiv, infoHisto, histoDivPosition, start, stop,
    meteoLayer, meteoField, meteoWhere, meteoDiv, dateMeteo, buoyLayer, copyright, minimum, autoTime,
    listRT, listHisto, infoUrl, scrollEnabled, codeId) {
  this.service = "rt";
  this.imageService = imageService;
  this.featureService = featureService;
  this.mode = "zoomin";
  this.setAuto('autoOff');
  this.listRT = listRT;
  this.listHisto = listHisto;
  this.scrollEnabled = scrollEnabled;
  this.codeId = codeId;
  
  this.mapDiv = xGetElementById(mapDiv);
  this.mapImg = xGetElementById(mapImg);
  this.mapBorderDiv = xGetElementById(mapBorderDiv);
  this.mapLoadingDiv = xGetElementById(mapLoadingDiv);
  this.mapZoomDiv = xGetElementById(mapZoomDiv);
  
  this.overDiv = xGetElementById(overDiv);
  this.overImg = xGetElementById(overImg);
  this.overZoomImg = xGetElementById(overZoomImg);
  this.overBorderDiv = xGetElementById(overBorderDiv);
  this.overLoadingDiv = xGetElementById(overLoadingDiv);
  this.overZoomDiv = xGetElementById(overZoomDiv);
  
  this.initEnvelope = envelope;
  this.envelope = envelope.clone();
  
  this.imageSize = imageSize;
  this.mapDivPosition = mapDivPosition;
  
  this.overSize = overSize;
  this.overDivPosition = overDivPosition;
  
  this.histoDiv = xGetElementById(histoDiv);
  this.infoHisto = xGetElementById(infoHisto);
  this.boats = new Array();
  this.boats[1]= null;
  this.boats[2]= null;
  this.boats[3]= null;
  this.boats[4]= null;
  this.boats[5]= null;
  this.start = xGetElementById(start);
  this.stop = xGetElementById(stop);
  
  this.legendLayer1 = legendLayer1;
  this.legendLayer2 = legendLayer2;
  
  this.delta = new Point(0, 0);
  if (is_opera) {
    this.delta = new Point(0, 0);
  }
  if (is_ie) {
    this.delta.y = 0;
  }
  
  this.pan = new Pan(this, this.mapDiv, this.mapDivPosition);
  this.zoomIn = new ZoomIn(this, this.mapDiv, this.mapDivPosition, this.mapZoomDiv);
  this.over = new Over(this, this.overDiv, this.overDivPosition, this.overZoomDiv);
  
  this.legend = new Legend(this, legendLayer1, legendFields, 
                           xGetElementById(legendDiv), xGetElementById(legendLoadingDiv),
                           legendOffset, legendDivPosition, legendRetired);
  this.meteo = new Meteo(this, meteoLayer, meteoField, meteoWhere, xGetElementById(meteoDiv), histoDivPosition, xGetElementById(dateMeteo));
  this.label = new Label(this, legendLayer2, infoUrl);
  
  this.buoyLayer = buoyLayer;
  
  this.copyright = copyright;
  this.minimum = minimum;
  this.autoTime = autoTime;
  
  this.catURL = "http://" + document.location.host + "/servlet/com.esri.esrimap.Esrimap?ServiceName=";
  
  this.mapOffset = new Point(0, 0);
  
  this.resetMap();
  
  xEnableDrag(this.mapDiv, onDragStart, onDrag, onDragStop);
  xAddEventListener(this.mapDiv, "mouseover", onMouseOver);
  xAddEventListener(this.mapDiv, "mouseout", onMouseOut);
  xAddEventListener(this.mapDiv, "mousemove", onMouseMove);
  xAddEventListener(this.mapDiv, "click", onMapClick);
  
  xVisibility(this.overZoomDiv, false);
  
  xMoveTo(this.mapDiv, mapDivPosition.x + this.delta.x, mapDivPosition.y + this.delta.y);
  xMoveTo(this.mapBorderDiv, mapDivPosition.x + this.delta.x, mapDivPosition.y + this.delta.y);
  xMoveTo(this.overDiv, overDivPosition.x + this.delta.x, overDivPosition.y + this.delta.y);
  xMoveTo(this.overZoomDiv, overDivPosition.x + this.delta.x, overDivPosition.y + this.delta.y);
  
  xClip(this.overDiv, 0, xWidth(this.overDiv) - this.mapOffset.x, xHeight(this.overDiv) - this.mapOffset.y, 0);
  xClip(this.overZoomDiv, 0, xWidth(this.overDiv) - this.mapOffset.x, xHeight(this.overDiv) - this.mapOffset.y, 0);
}

function ArcIMS_swapService(aService) {
  var newService = null;
  if (aService == 'rt' && this.service != aService) {
    this.boats[1]= null;
    this.boats[2]= null;
    this.boats[3]= null;
    this.boats[4]= null;
    this.boats[5]= null;
    if (this.listRT) {
      xGetElementById("histo").className = "menu";
      xGetElementById("rt").className = "menuSelected";
    } else {
      xBackground("histo", "");
      xBackground("rt", "#77C3D3");
    }
    if (this.listRT && xGetElementById("listFrame").src.indexOf(this.listRT) == -1) {
      xGetElementById("listFrame").src = this.listRT;
    }
    newService = 'rt';
    this.legend.visibility(true);
    this.meteo.visibility(true);
    xHide(this.histoDiv);
    xDisplay(this.infoHisto, "none");
  } else if (aService == 'histo' && this.service != aService) {
    if (this.listRT) {
      xGetElementById("rt").className = "menu";
      xGetElementById("histo").className = "menuSelected";
    } else {
      xBackground("rt", "");
      xBackground("histo", "#77C3D3");
    }
    if (this.listHisto && xGetElementById("listFrame").src.indexOf(this.listHisto) == -1) {
      xGetElementById("listFrame").src = this.listHisto;
    }
    newService = 'histo';
    this.legend.visibility(false);
    this.meteo.visibility(false);
    xShow(this.histoDiv);
    xDisplay(this.infoHisto, "block");
  }
  if (newService != null) {
    this.service = newService;
    request = null;
    this.updateMap();
    this.updateOver();
  }
}

function ArcIMS_autoUpdate() {
  if (this.auto == 'autoOn') {
    if (this.service == "rt") {
      this.updateMap();
     }
    window.setTimeout('autoUpdate()', this.autoTime);
  }
}

function ArcIMS_setAuto(aMode) {
  if (aMode != this.auto) {
    this.auto = aMode;
    if (this.auto == 'autoOn') {
      this.autoUpdate();
    }
  }
}

function ArcIMS_resetMap() {
  xMoveTo(this.mapDiv, this.mapDivPosition.x + this.delta.x, this.mapDivPosition.y + this.delta.y);
  xClip(this.mapDiv, 0, xWidth(this.mapDiv) - this.mapOffset.x, xHeight(this.mapDiv) - this.mapOffset.y, 0);
}

function ArcIMS_updateMap() {
  xVisibility(this.mapLoadingDiv, true);
  this.mapDiv.style.opacity = "0.6";
  
  this.envelope.limit(this.initEnvelope, this.minimum);
  
  if (this.service == "rt") {
    this.meteo.update();
    this.label.update();
  }
  
  if (this.auto == 'autoOff' && request != null && request.readyState != 4) {
    alert("Carte en chargement, attendez la fin du chargement");
  } else {
    request = getXMLRequest();
    this.getImageXML("map");
  }
}

function ArcIMS_updateOver(notUpdateImage) {
  xVisibility(this.overZoomDiv, false);
  var sizeX = this.overSize.width * this.envelope.width() / this.initEnvelope.width();
  var sizeY = this.overSize.height * this.envelope.height() / this.initEnvelope.height();
  var deltaX = this.overSize.width * (this.envelope.minx - this.initEnvelope.minx) / this.initEnvelope.width();
  var deltaY = this.overSize.height * (this.initEnvelope.maxy - this.envelope.maxy) / this.initEnvelope.height();
  if (sizeX < 6) {
    deltaX = deltaX + sizeX / 2 - 3;
    sizeX = 6;
  }
  if (sizeY < 4) {
    deltaY = deltaY + sizeY / 2 - 2;
    sizeY = 4;
  }
  var offset = 0;
  if (is_ie) {
    offset = 1;
  }
  xMoveTo(this.overZoomDiv, xLeft(this.overDiv) + deltaX + offset, xTop(this.overDiv) + deltaY);
  xResizeTo(this.overZoomDiv, sizeX - offset, sizeY);
  xClip(this.overZoomDiv, - deltaY, xWidth("overDiv") - this.mapOffset.x - deltaX, xHeight("overDiv") - this.mapOffset.y - deltaY + offset, - deltaX);
  xResizeTo(this.overZoomImg, sizeX - offset, sizeY);
  xVisibility(this.overZoomDiv, true);
  
  if (!notUpdateImage) {
    requestOver = getXMLRequest();
    this.getImageXML("over");
  }
}

function ArcIMS_getImageXML(aMode, aCallback) {
  var req = (aMode == "map") ? request : requestOver;
  req.open("POST", this.catURL + this.imageService, true);
  req.onreadystatechange = (aMode == "map") ? callback : callbackOver;
  if (!is_opera) {
    req.setRequestHeader("Content-Type", "text/xml");
  }
  var textXML = 
    '<ARCXML version="1.1">' +
    '<REQUEST>' +
    '<GET_IMAGE>' +
    '<PROPERTIES>' + 
    ((aMode == "map") ? this.envelope : this.initEnvelope).toXML()  + 
    ((aMode == "map") ? this.imageSize : this.overSize).toXML() + 
    '<LAYERLIST>';
  if (this.service == "histo") {
    if (!this.listRT) {
      this.boats[1] = xGetElementById('boat1').value;
      this.boats[2] = xGetElementById('boat2').value;
    }
    for (var i = 0; i < this.boats.length; i++) {
      if (this.boats[i]!= null && this.boats[i].length > 0) {
        textXML += 
          '<LAYERDEF id="' + i + '">' +
          '<QUERY where="CELLNUMBER = \'' + this.boats[i] + 
            '\' AND MTIME &gt;= ' + getSeconds(this.start) + 
            ' AND MTIME &lt;= ' + getSeconds(this.stop) + '" />' +
          '</LAYERDEF>';
      }
    }
  } else {
    textXML += '<LAYERDEF id="wind" visible="true" />';
    
    var query = "";;
    if (this.codeId || this.filtred) {
      query = '<QUERY where="';
      if (this.codeId) {
        query += 'CODE_ID = \'' + this.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.start);
      }
      if (this.filtred) {
        if (this.codeId) {
          query += ' AND ';
        }
        query += 'CELLNUMBER IN (' + this.filtred + ')';
      }
      query += '" />';
    }

    textXML += '<LAYERDEF id="trace" visible="true">' + query + '</LAYERDEF>';

    textXML += '<LAYERDEF id="blue" visible="true">' + query + '</LAYERDEF>';
    textXML += '<LAYERDEF id="green" visible="true">' + query + '</LAYERDEF>';

    textXML += '<LAYERDEF id="red" visible="true">' + query + '</LAYERDEF>';

    if (this.selected) {
      textXML +=
        '<LAYERDEF id="' + this.legendLayer1 + '" visible="true">' +
        '<QUERY where="CELLNUMBER IN (' + this.selected + ')';
      if (this.codeId) {
        textXML += ' AND CODE_ID = \'' + this.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.start) + '';
      }
      textXML += '" />' +
        '</LAYERDEF>' +
        '<LAYERDEF id="' + this.legendLayer2 + '" visible="true">' +
        '<QUERY where="CELLNUMBER IN (' + this.selected + ')';
      if (this.codeId) {
        textXML += ' AND CODE_ID = \'' + this.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.start) + '';
      }
      textXML += '" />' +
        '</LAYERDEF>';
    }
  }
  if (this.buoyLayer) {
    if (this.buoyLayer == "bom07") {
      textXML += '<LAYERDEF id="ligne_arrivee" visible="true" />';
      textXML += '<LAYERDEF id="ligne_depart_07" visible="true" />';
      textXML += '<LAYERDEF id="degagement_07" visible="true" />';
      textXML += '<LAYERDEF id="chaland" visible="true" />';
      textXML += '<LAYERDEF id="arrivee" visible="true" />';
    }
    textXML += '<LAYERDEF id="' + this.buoyLayer + '" visible="true" />';
  } else {
    textXML += '<LAYERDEF id="ligne_arrivee" visible="true" />';
    textXML += '<LAYERDEF id="ligne_depart" visible="true" />';
    textXML += '<LAYERDEF id="chaland" visible="true" />';
    textXML += '<LAYERDEF id="arrivee" visible="true" />';
  }
  textXML += 
    '</LAYERLIST>' +
    '</PROPERTIES>' + 
    '</GET_IMAGE>' +
    '</REQUEST>' +
    '</ARCXML>';
  req.send(textXML);
}

function ArcIMS_callback() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var xmlDocument = stringToDocument(request.responseText);
      if (contain(xmlDocument, "ERROR")) {
        error(xmlDocument);
      } else if (contain(xmlDocument, "IMAGE")) {
        this.envelope.parseXML(xmlDocument);
        this.updateOver(true);
        if (contain(xmlDocument, "OUTPUT")) {
          xVisibility(this.mapDiv, false);
          preloadImage = new Image();
          preloadImage.onload = onMapLoaded;
          preloadImage.src = xmlDocument.getElementsByTagName("OUTPUT")[0].attributes.getNamedItem("url").nodeValue;
        }
      }
    }
  }
}

function ArcIMS_onMapLoaded() {
  this.resetMap();
  xGetElementById(this.mapImg).src = preloadImage.src;
  this.mapDiv.style.opacity = "1.0";
  xVisibility(this.mapDiv, true);
  xVisibility(this.mapLoadingDiv, false);
}

function ArcIMS_callbackOver() {
  if (requestOver.readyState == 4) {
    if (requestOver.status == 200) {
      var xmlDocument = stringToDocument(requestOver.responseText);
      if (contain(xmlDocument, "ERROR")) {
        error(xmlDocument);
      } else if (contain(xmlDocument, "IMAGE")) {
        if (contain(xmlDocument, "OUTPUT")) {
          xVisibility(this.overDiv, false);
          xGetElementById(this.overImg).src = xmlDocument.getElementsByTagName("OUTPUT")[0].attributes.getNamedItem("url").nodeValue;
          xVisibility(this.overDiv, true);
          xVisibility(this.overLoadingDiv, false);
        }
      }
    }
  }
}

function ArcIMS_mapPan(x, y) {
  this.scroll();
  
  var deltaX = this.envelope.width() / this.imageSize.width * x;
  var deltaY = this.envelope.height() / this.imageSize.height * y;
  this.envelope.translate(-deltaX, deltaY);
  
  this.updateMap();
}

function ArcIMS_mapZoomIn(x, y, width, height) {
  if (width < 10 && height < 10) {
    this.zoom(x, y, 0.25);
  } else {
    this.scroll();
    
    var deltaX = this.envelope.width() / this.imageSize.width * x;
    var deltaY = this.envelope.height() / this.imageSize.height * y;
    this.envelope.translate(deltaX, -deltaY);
    
    var newWidth = this.envelope.width() / this.imageSize.width * width;
    var newHeight = this.envelope.height() / this.imageSize.height * height;
    this.envelope.maxx = parseFloat(this.envelope.minx) + newWidth;
    this.envelope.miny = parseFloat(this.envelope.maxy) - newHeight;v
  
    this.updateMap();
  }
}

function ArcIMS_zoomOut(x, y) {
  this.zoom(x, y, 4);
}

function ArcIMS_zoom(x, y, ratio) {
  this.scroll();
  
  var deltaX = this.envelope.width() / this.imageSize.width * (this.imageSize.width / 2 - x);
  var deltaY = this.envelope.height() / this.imageSize.height * (this.imageSize.height / 2 - y);
  this.envelope.translate(-deltaX, deltaY);
  
  var newWidth = this.envelope.width() * ratio;
  var newHeight = this.envelope.height() * ratio;
  this.envelope.resize(newWidth, newHeight);
  
  this.updateMap();
}

function ArcIMS_setMode(aMode) {
  if (this.listRT) {
    xGetElementById(this.mode).className = "menu";
    this.mode = aMode;
    xGetElementById(this.mode).className = "menuSelected";
  } else {
    xBackground(this.mode, "");
    xBackground(aMode, "#77C3D3");
    this.mode = aMode;
  }
}

function ArcIMS_reset() {
  this.scroll();
  
  this.envelope = this.initEnvelope.clone();
  this.updateMap();
}

function ArcIMS_onMapClick(e) {
  var evt = new xEvent(e);
  if (this.mode == "zoomout") {
    this.zoomOut(evt.offsetX, evt.offsetY);
  }
  if (this.mode == "info") {
    this.label.info();
  }
}

function ArcIMS_onMouseOver() {
  if (this.mode == "pan") {
    this.mapDiv.style.cursor = "move";
  }
  if (this.mode == "zoomin") {
    this.mapDiv.style.cursor = "crosshair";
  }
  if (this.mode == "zoomout") {
    this.mapDiv.style.cursor = "crosshair";
  }
  if (this.mode == "info") {
    if (!is_ie || is_ie6 || is_ie6up) {
      this.mapDiv.style.cursor = "pointer";
    }
  }
}

function ArcIMS_onMouseOut() {
  this.mapDiv.style.cursor = "default";
}

function ArcIMS_onMouseMove(e) {
  this.label.show(new xEvent(e));
}

function ArcIMS_onDragStart(ele, mx, my)
{
  if (this.mode == "pan") {
    this.pan.onDragStart(ele, mx, my);
  }
  if (this.mode == "zoomin") {
    this.zoomIn.onDragStart(ele, mx, my);
  }
}

function ArcIMS_onDrag(ele, mdx, mdy)
{
  if (this.mode == "pan") {
    this.pan.onDrag(ele, mdx, mdy);
  }
  if (this.mode == "zoomin") {
    this.zoomIn.onDrag(ele, mdx, mdy);
  }
}

function ArcIMS_onDragStop(ele, mx, my)
{
  if (this.mode == "pan") {
    this.pan.onDragStop(ele, mx, my);
  }
  if (this.mode == "zoomin") {
    this.zoomIn.onDragStop(ele, mx, my);
  }
}

function ArcIMS_onOverDragStart(ele, mx, my)
{
  this.over.onDragStart(ele, mx, my);
}

function ArcIMS_onOverDrag(ele, mdx, mdy)
{
  this.over.onDrag(ele, mdx, mdy);
}

function ArcIMS_onOverDragStop(ele, mx, my)
{
  this.over.onDragStop(ele, mx, my);
}

function ArcIMS_onOverClick(e) {
  this.over.onClick(e);
}

function ArcIMS_zoomTo(id) {
  xShow(this.mapLoadingDiv);
  xGetElementById(this.mapDiv).style.opacity = "0.6";
  this.legend.zoomTo(id);
}

function ArcIMS_setHisto(index, cellnumber) {
  if (cellnumber != null && cellnumber.length > 0) {
    this.boats[index] = cellnumber;
  } else {
    this.boats[index] = null;
  }
}

function ArcIMS_scroll() {
  if (this.scrollEnabled) {
    window.scrollTo(0, 100);
  }
}

ArcIMS.prototype.updateMap = ArcIMS_updateMap;
ArcIMS.prototype.updateOver = ArcIMS_updateOver;
ArcIMS.prototype.callback = ArcIMS_callback;
ArcIMS.prototype.callbackOver = ArcIMS_callbackOver;
ArcIMS.prototype.mapPan = ArcIMS_mapPan;
ArcIMS.prototype.mapZoomIn = ArcIMS_mapZoomIn;
ArcIMS.prototype.zoomOut = ArcIMS_zoomOut;
ArcIMS.prototype.zoom = ArcIMS_zoom;
ArcIMS.prototype.resetMap = ArcIMS_resetMap;
ArcIMS.prototype.setMode = ArcIMS_setMode;
ArcIMS.prototype.reset = ArcIMS_reset;
ArcIMS.prototype.onMapClick = ArcIMS_onMapClick;
ArcIMS.prototype.onMouseOver = ArcIMS_onMouseOver;
ArcIMS.prototype.onMouseOut = ArcIMS_onMouseOut;
ArcIMS.prototype.onMouseMove = ArcIMS_onMouseMove;
ArcIMS.prototype.onDragStart = ArcIMS_onDragStart;
ArcIMS.prototype.onDrag = ArcIMS_onDrag;
ArcIMS.prototype.onDragStop = ArcIMS_onDragStop;
ArcIMS.prototype.onOverDragStart = ArcIMS_onOverDragStart;
ArcIMS.prototype.onOverDrag = ArcIMS_onOverDrag;
ArcIMS.prototype.onOverDragStop = ArcIMS_onOverDragStop;
ArcIMS.prototype.onOverClick = ArcIMS_onOverClick;
ArcIMS.prototype.zoomTo = ArcIMS_zoomTo;
ArcIMS.prototype.swapService = ArcIMS_swapService;
ArcIMS.prototype.getImageXML = ArcIMS_getImageXML;
ArcIMS.prototype.onMapLoaded = ArcIMS_onMapLoaded;
ArcIMS.prototype.setAuto = ArcIMS_setAuto;
ArcIMS.prototype.autoUpdate = ArcIMS_autoUpdate;
ArcIMS.prototype.setHisto = ArcIMS_setHisto;
ArcIMS.prototype.scroll = ArcIMS_scroll;

// arcims-setup.js
/**
 *  The "params" is a single object that can have the following properties:
 *
 *    prop. name   | description
 *  -------------------------------------------------------------------------------------------------
 *   catURL           |
 *   imageService     | the name of the image ArcIMS service
 *   featureService   | the name of the feature ArcIMS service
 *   mapDiv           | 
 *   mapImg           | 
 *   mapBorderDiv     | 
 *   mapLoadingDiv    | 
 *   mapZoomDiv       | 
 *   envelopeMinX     | 
 *   envelopeMinY     | 
 *   envelopeMaxX     | 
 *   envelopeMaxY     | 
 *   imageSizeWidth   | 
 *   imageSizeHeight  | 
 *   mapDivPositionX  |  
 *   mapDivPositionY  | 
 *   overDiv          | 
 *   overImg          | 
 *   overZoomImg      | 
 *   overBorderDiv    | 
 *   overLoadingDiv   | 
 *   overZoomDiv      | 
 *   overSizeWidth    | 
 *   overSizeHeight   | 
 *   overDivPositionX | 
 *   overDivPositionY | 
 *   legendLayer1     | 
 *   legendLayer2     | 
 *   legendFields     | 
 *   legendWhere      | 
 *   legendDiv        | 
 *   legendLoadingDiv | 
 *   legendTitle      |
 *   legendOffset     | 
 *   legendDivPositionX | 
 *   legendDivPositionY |
 *   legendRetired      | 
 *   histoDiv           | 
 *   histoDivPositionX  | 
 *   histoDivPositionY  |
 *   start            | 
 *   stop             | 
 *   meteoLayer       | 
 *   meteoField       | 
 *   meteoWhere       | 
 *   meteoDiv         | 
 *   dateMeteo        |
 *   copyright        |
 *   minimum          |
 *   autoTime         |
 *   listRT           |
 *   listHisto        |
 *   infoUrl          |
 *   scrollEnabled    |
 *   codeId           |
 */
ArcIMS.setup = function (params) {
  function param_default(pname, def) { 
    if (typeof params[pname] == "undefined") { 
      params[pname] = def;
    }
  };
  
  param_default("mapDiv", "mapDiv");
  param_default("mapImg", "mapImg");
  param_default("mapBorderDiv", "mapBorderDiv");
  param_default("mapLoadingDiv", "mapLoadingDiv");
  param_default("mapZoomDiv", "mapZoomDiv");
  param_default("envelopeMinX", 490000);
  param_default("envelopeMinY", 112500);
  param_default("envelopeMaxX", 565000);
  param_default("envelopeMaxY", 162500);
  param_default("overDiv", "overDiv");
  param_default("overImg", "overImg");
  param_default("overZoomImg", "overZoomImg");
  param_default("overBorderDiv", "overBorderDiv");
  param_default("overLoadingDiv", "overLoadingDiv");
  param_default("overZoomDiv", "overZoomDiv");
  
  param_default("legendLayer1", "select_trace");
  param_default("legendLayer2", "select_point");
  
  param_default("legendDiv", "legendDiv");
  param_default("legendLoadingDiv", "legendLoadingDiv");
  param_default("legendFields", "CELLNUMBER ID NAME");
  param_default("legendRetired", new Array());
  
  param_default("histoDiv", "histoDiv");
  param_default("start", "start");
  param_default("stop", "stop");
  
  param_default("infoRT", "infoRT");
  param_default("infoHisto", "infoHisto");
  
  param_default("dateMeteo", "dateMeteo");
  
  param_default("buoyLayer", null);
    
  param_default("copyright", "Powered by ESRI - Copyright CARTOSPHERE");
  
  param_default("minimum", 100);
  param_default("autoTime", 120000);
  
  param_default("scrollEnabled", true);
  
  param_default("codeId", null);
  
  arcIMS = new ArcIMS(
    params.imageService, params.featureService,
    params.mapDiv, params.mapImg, params.mapBorderDiv, params.mapLoadingDiv, params.mapZoomDiv, 
    new Envelope(params.envelopeMinX, params.envelopeMinY, params.envelopeMaxX, params.envelopeMaxY), 
    new ImageSize(params.imageSizeWidth, params.imageSizeHeight), new Point(params.mapDivPositionX, params.mapDivPositionY),
    params.overDiv, params.overImg, params.overZoomImg, params.overBorderDiv, params.overLoadingDiv, params.overZoomDiv, 
    new ImageSize(params.overSizeWidth, params.overSizeHeight), new Point(params.overDivPositionX, params.overDivPositionY), 
    params.legendLayer1, params.legendLayer2, params.legendFields, params.legendDiv, params.legendLoadingDiv,
    params.legendOffset, new Point(params.legendDivPositionX, params.legendDivPositionY), params.legendRetired,
    params.histoDiv, params.infoHisto, new Point(params.histoDivPositionX, params.histoDivPositionY), 
    params.start, params.stop,
    params.meteoLayer, params.meteoField, params.meteoWhere, params.infoRT, params.dateMeteo, params.buoyLayer, params.copyright, params.minimum, params.autoTime,
    params.listRT, params.listHisto, params.infoUrl, params.scrollEnabled, params.codeId);
  
  arcIMS.setMode("zoomin");
  xGetElementById("rt").className = "menuSelected";
  
  // For real-time
  if (!arcIMS.listRT) {
    xBackground("rt", "#77C3D3");
    arcIMS.legend.update();
  }
  
  if (getArgs().id) {
    arcIMS.swapService('histo');
  } else {
    if (arcIMS.listRT) {
      xGetElementById("listFrame").src = arcIMS.listRT;
    }
    arcIMS.updateMap();
    arcIMS.updateOver();
  }
  
  // For history only (remove comment)
  arcIMS.swapService('histo');
}

// envelope.js
function Envelope(minx, miny, maxx, maxy) {
  this.minx = minx;
  this.miny = miny;
  this.maxx = maxx;
  this.maxy = maxy;
}

function Envelope_toXML() {
  return '<ENVELOPE minx="' + this.minx + '" miny="' + this.miny + '" maxx="' + this.maxx + '" maxy="' + this.maxy + '" />';
}

function Envelope_toString() {
  return 'minx="' + this.minx + '" miny="' + this.miny + '" maxx="' + this.maxx + '" maxy="' + this.maxy;
}

function Envelope_parseXML(xmlDocument) {
  var elements = xmlDocument.getElementsByTagName("ENVELOPE");
  if (elements.length > 0) {
    this.minx = getAttribute(elements[0], "minx");
    this.miny = getAttribute(elements[0], "miny");
    this.maxx = getAttribute(elements[0], "maxx");
    this.maxy = getAttribute(elements[0], "maxy");
  }
  for (var i = 1; i < elements.length; i++) {
    if (getAttribute(elements[i], "minx") < this.minx) {
      this.minx = getAttribute(elements[i], "minx");
    }
    if (getAttribute(elements[i], "miny") < this.miny) {
      this.miny = getAttribute(elements[i], "miny");
    }
    if (getAttribute(elements[i], "maxx") > this.maxx) {
      this.maxx = getAttribute(elements[i], "maxx");
    }
    if (getAttribute(elements[i], "maxy") > this.maxy) {
      this.maxy = getAttribute(elements[i], "maxy");
    }
  }
}

function Envelope_width() {
	return this.maxx - this.minx;
}

function Envelope_height() {
	return this.maxy - this.miny;
}

function Envelope_translate(x, y) {
	this.minx = parseFloat(this.minx) + x;
	this.miny = parseFloat(this.miny) + y;
	this.maxx = parseFloat(this.maxx) + x;
	this.maxy = parseFloat(this.maxy) + y;
}

function Envelope_resize(width, height) {
  var x = this.centerX();
  var y = this.centerY();
	this.minx = x - width / 2;
	this.miny = y - height / 2;
	this.maxx = x + width / 2;
	this.maxy = y + height / 2;
}

function Envelope_center(x, y) {
  this.translate(x - (parseFloat(this.minx) + parseFloat(this.maxx)) / 2, 
                 y - (parseFloat(this.miny) + parseFloat(this.maxy)) / 2);
}

function Envelope_expand(offset) {
	this.minx = parseFloat(this.minx) - offset;
	this.miny = parseFloat(this.miny) - offset;
	this.maxx = parseFloat(this.maxx) + offset;
	this.maxy = parseFloat(this.maxy) + offset;
}

function Envelope_clone() {
  return new Envelope(this.minx, this.miny, this.maxx, this.maxy);
}

function Envelope_toRectangle() {
  return this.minx + ' ' + this.miny + ';' +  
         this.minx + ' ' + this.maxy + ';' +  
         this.maxx + ' ' + this.maxy + ';' + 
         this.maxx + ' ' + this.miny + ';' + 
         this.minx + ' ' + this.miny;
}

function Envelope_toCenter() {
  return this.centerX() + ' ' + this.centerY();
}

function Envelope_centerX() {
  return (parseFloat(this.minx) + parseFloat(this.maxx)) / 2;
}

function Envelope_centerY() {
  return (parseFloat(this.miny) + parseFloat(this.maxy)) / 2;
}

function Envelope_limit(limitEnv, minimum) {
  if (this.width() > limitEnv.width()) {
    var deltaX = (this.width() - limitEnv.width()) / 2;
    this.minx += deltaX;
    this.maxx -= deltaX;
  }
  if (this.height() > limitEnv.height()) {
    var deltaY = (this.height() - limitEnv.height()) / 2;
    this.miny += deltaY;
    this.maxy -= deltaY;
  }
  if (this.width() < minimum) {
    var deltaX = (this.width() - minimum) / 2;
    this.minx += deltaX;
    this.maxx -= deltaX;
  }
  if (this.height() < minimum) {
    var deltaY = (this.height() - minimum) / 2;
    this.miny += deltaY;
    this.maxy -= deltaY;
  }
  
  var x = this.centerX();
  if (x < limitEnv.minx) {
    x = limitEnv.minx;
  }
  if (x > limitEnv.maxx) {
    x = limitEnv.maxx;
  }  
  var y = this.centerY();
  if (y < limitEnv.miny) {
    y = limitEnv.miny;
  }
  if (y > limitEnv.maxy) {
    y = limitEnv.maxy;
  }  
  this.center(x, y);
}

Envelope.prototype.toXML = Envelope_toXML;
Envelope.prototype.toString = Envelope_toString;
Envelope.prototype.parseXML = Envelope_parseXML;
Envelope.prototype.width = Envelope_width;
Envelope.prototype.height = Envelope_height;
Envelope.prototype.center = Envelope_center;
Envelope.prototype.translate = Envelope_translate;
Envelope.prototype.resize = Envelope_resize;
Envelope.prototype.expand = Envelope_expand;
Envelope.prototype.clone = Envelope_clone;
Envelope.prototype.toRectangle = Envelope_toRectangle;
Envelope.prototype.toCenter = Envelope_toCenter;
Envelope.prototype.centerX = Envelope_centerX;
Envelope.prototype.centerY = Envelope_centerY;
Envelope.prototype.limit = Envelope_limit;

// functions.js
// © 2011 INSER SA
var arcIMS;

function setMode(aMode) {
  arcIMS.setMode(aMode);
}

function swapService(aService) {
  arcIMS.swapService(aService);
}

function callback() {
  arcIMS.callback();
}

function callbackOver() {
	arcIMS.callbackOver();
}

function legendCallback() {
  arcIMS.legend.callback();
}

function labelCallback() {
  arcIMS.label.callback();
}

function meteoCallback() {
  arcIMS.meteo.callback();
}

function onMapClick(e) {
  arcIMS.onMapClick(e);
}

function onMouseOver() {
  arcIMS.onMouseOver();
}

function onMouseOut() {
  arcIMS.onMouseOut();
}

function onMouseMove(e) {
  arcIMS.onMouseMove(e);
}

function onDragStart(ele, mx, my)
{
  arcIMS.onDragStart(ele, mx, my);
}

function onDrag(ele, mdx, mdy)
{
  arcIMS.onDrag(ele, mdx, mdy);
}

function onDragStop(ele, mx, my)
{
  arcIMS.onDragStop(ele, mx, my);
}

function onOverDragStart(ele, mx, my)
{
  arcIMS.onOverDragStart(ele, mx, my);
}

function onOverDrag(ele, mdx, mdy)
{
  arcIMS.onOverDrag(ele, mdx, mdy);
}

function onOverDragStop(ele, mx, my)
{
  arcIMS.onOverDragStop(ele, mx, my);
}

function onOverClick(e) {
  arcIMS.onOverClick(e);
}

function zoomTo(id) {
  arcIMS.zoomTo(id);
}

function zoomOut(x, y) {
  arcIMS.zoomOut(x, y);
}

function refresh() {
  arcIMS.scroll();
  
  arcIMS.updateMap();
  arcIMS.updateOver();
}

function reset() {
  arcIMS.reset();
}

function onMapLoaded() {
  arcIMS.onMapLoaded();
}

function setAuto(aMode) {
  arcIMS.setAuto(aMode);
}

function autoUpdate() {
  arcIMS.autoUpdate();
}

function setHisto(index, cellnumber) {
  arcIMS.setHisto(index, cellnumber);
}

function setCode(codeId) {
  arcIMS.codeId = codeId;
}

// imagesize.js
// © 2011 INSER SA
function ImageSize(width, height) {
  this.width = width;
  this.height = height;
}

function ImageSize_toXML() {
  return '<IMAGESIZE width="' + this.width + '" height="' + this.height + '" />';
}

function ImageSize_toString() {
  return 'width="' + this.width + '" height="' + this.height;
}

function ImageSize_parseXML(xmlDocument) {
  var element = xmlDocument.getElementsByTagName("IMAGESIZE")[0];
  this.width = getAttribute(element, "width");
  this.height = getAttribute(element, "height");
}

ImageSize.prototype.toXML = ImageSize_toXML;
ImageSize.prototype.toString = ImageSize_toString;
ImageSize.prototype.parseXML = ImageSize_parseXML;

// label.js
// © 2005-2010 LogiFleet SA / INSER SA
var labelRequest;

function Label(arcIMS, layer, infoUrl) {
  this.arcIMS = arcIMS;
  this.layer = layer;
  this.infoUrl = infoUrl;
  this.labelDiv = xGetElementById("labelDiv");
  this.labelDiv.style.opacity = "0.8";
  if (!this.arcIMS.listRT) {
    this.idValue = xGetElementById("idValue");
  }
  this.nameValue = xGetElementById("nameValue");
  this.speedValue = xGetElementById("speedValue");
  this.mtimeValue = xGetElementById("mtimeValue");
  this.codeTypeValue = xGetElementById("codeTypeValue");
  
  this.boats = new Array();
  this.selected = null;
}

function Label_update() {
  labelRequest = getXMLRequest();
  labelRequest.open("POST", this.arcIMS.catURL + this.arcIMS.featureService, true);
  if (!is_opera) {
    labelRequest.setRequestHeader("Content-Type", "text/xml");
  }
  labelRequest.onreadystatechange = labelCallback;
  var textXML = 
    '<ARCXML version="1.1">' +
    '<REQUEST>' +
    '<GET_FEATURES outputmode="newxml" geometry="true" envelope="false" >' +
    '<LAYER id="' + this.layer + '" />' +
    '<QUERY subfields="CELLNUMBER NAME STATUS SPEED MTIME CLASS CODE_TYPE #SHAPE#"';
    
  if (this.arcIMS.codeId || this.arcIMS.filtred) {
    textXML += ' where="';
    if (this.arcIMS.codeId) {
      textXML += 'CODE_ID = \'' + this.arcIMS.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.arcIMS.start);
    }
    if (this.arcIMS.filtred) {
      if (this.arcIMS.codeId) {
        textXML += ' AND ';
      }
      textXML += 'CELLNUMBER IN (' + this.arcIMS.filtred + ')';
    }
    textXML += '" ';
  }
  
  textXML += 
    '>' +
    '</QUERY>' +
    '</GET_FEATURES>' +
    '</REQUEST>' +
    '</ARCXML>';
  labelRequest.send(textXML);
}

function Label_callback() {
  if (labelRequest.readyState == 4) {
    if (labelRequest.status == 200) {
      var xmlDocument = stringToDocument(labelRequest.responseText);
      if (contain(xmlDocument, "ERROR")) {
        error(xmlDocument);
      } else if (contain(xmlDocument, "FEATURES") && !contain(xmlDocument, "ENVELOPE")) {
        var features = xmlDocument.getElementsByTagName("FEATURE");
        this.boats = new Array();
        for (var i = 0; i < features.length; i++) {
          var fields = features[i].getElementsByTagName("FIELD");
          var cellnumber;
          var nameLabel;
          var status;
          var speed;
          var mtime;
          var classId;
          var codeType;
          for (var j = 0; j < fields.length; j++) {
            var name = fields[j].attributes.getNamedItem("name").nodeValue;
            if (name.indexOf("CELLNUMBER") != -1) {
              cellnumber = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("NAME") != -1) {
              nameLabel = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("STATUS") != -1) {
              status = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("SPEED") != -1) {
              speed = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("MTIME") != -1) {
              mtime = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("CLASS") != -1) {
              classId = fields[j].attributes.getNamedItem("value").nodeValue;
            }
            if (name.indexOf("CODE_TYPE") != -1) {
              codeType = fields[j].attributes.getNamedItem("value").nodeValue;
            }
          }
          fields = features[i].getElementsByTagName("MULTIPOINT")[0].getElementsByTagName("POINT")[0].attributes;
          var x = fields.getNamedItem("x").nodeValue;
          var y = fields.getNamedItem("y").nodeValue;
          this.boats.push([x, y, cellnumber, nameLabel, status, speed, formatDate(mtime * 1000), classId, codeType]);
        }
      }
    }
  }
}

function Label_show(evt) {
  if (this.arcIMS.service == "histo") {
    this.selected = null;
    xHide(this.labelDiv);
    return;
  }
  var boatX;
  var boatY;
  var boat;
  for (var i = 0; i < this.boats.length; i++) {
    boatX = (this.boats[i][0] - this.arcIMS.envelope.minx) * this.arcIMS.imageSize.width / this.arcIMS.envelope.width();
    boatY = this.arcIMS.imageSize.height * (1 - (this.boats[i][1] - this.arcIMS.envelope.miny) / this.arcIMS.envelope.height());
    if (Math.abs(evt.offsetX - boatX) < 4 && Math.abs(evt.offsetY - boatY) < 4) {
      boat = this.boats[i];
      break;
    }
  }
  if (boat != null) {
    if (this.idValue) {
      this.idValue.innerHTML = "";
      for (var i = 0; i < this.arcIMS.legend.boats.length; i++) {
        if (this.arcIMS.legend.boats[i][0] == boat[2]) {
          this.idValue.innerHTML = this.arcIMS.legend.boats[i][1];
        }
      }
    }
    this.nameValue.innerHTML = boat[3];
    this.speedValue.innerHTML = Math.round(boat[5] / 0.1852, 1) / 10 + " noeuds";
    this.mtimeValue.innerHTML = boat[6];
    var status = new Number(boat[4]);
    //alert(status > 128);
    var color = "#2ECB00";
    if (status == 14 || status > 128) {
      color = "#0000FF";
    } else if (status == 13) {
      color = "#E11A1A";
    }
    if (boat[7] != "M1" && boat[7] != "M2") {
      if (boat[5] >= 15) {
        color = "#E11A1A";
      }
    } else {
      if (boat[5] >= 23) {
        color = "#E11A1A";
      }
    }
    if (boat[8] == "MMT") {
      this.codeTypeValue.innerHTML = "balise personnelle";
      this.speedValue.innerHTML = "";
      color = "#FF9000";
    } else {
      this.codeTypeValue.innerHTML = "balise officielle";
    }
    xBackground(this.labelDiv, color);
    xMoveTo(this.labelDiv, evt.pageX + 3, evt.pageY - 3 - xHeight(this.labelDiv));
    this.selected = boat[2];
    xShow(this.labelDiv);
  } else {
    this.selected = null;
    xHide(this.labelDiv);
  }
}

function Label_info() {
  if (this.selected != null) {
    launch(this.infoUrl + this.selected.substr(1));
  }
}

Label.prototype.update = Label_update;
Label.prototype.callback = Label_callback;
Label.prototype.show = Label_show;
Label.prototype.info = Label_info;

// legen.js
// © 2011 INSER SA
var legendRequest;

function Legend(arcIMS, layer, fields, legendDiv, legendLoadingDiv,
                offset, legendDivPosition, legendRetired) {
  this.arcIMS = arcIMS;
  this.layer = layer;
  this.fields = fields;
  this.legendDiv = legendDiv;
  this.offset = offset;
  if (!this.arcIMS.listRT) {
    this.legendLoadingDiv = legendLoadingDiv;
    this.boat1 = xGetElementById("boat1");
    this.boat2 = xGetElementById("boat2");
  }
  this.legendRetired = legendRetired;
  this.boats = new Array();
}

function Legend_update() {
  legendRequest = getXMLRequest();
  legendRequest.open("POST", this.arcIMS.catURL + this.arcIMS.featureService, true);
  if (!is_opera) {
    legendRequest.setRequestHeader("Content-Type", "text/xml");
  }
  legendRequest.onreadystatechange = legendCallback;
  var textXML = 
    '<ARCXML version="1.1">' +
    '<REQUEST>' +
    '<GET_FEATURES outputmode="newxml" geometry="false" envelope="false" >' +
    '<LAYER id="' + this.layer + '" />' +
    '<QUERY subfields="OBJECTID ' + this.fields + '"';
  
  if (this.arcIMS.codeId || this.arcIMS.filtred) {
    textXML += ' where="';
    if (this.arcIMS.codeId) {
      textXML += 'CODE_ID = \'' + this.arcIMS.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.arcIMS.start);
    }
    if (this.arcIMS.filtred) {
      if (this.arcIMS.codeId) {
        textXML += ' AND ';
      }
      textXML += 'CELLNUMBER IN (' + this.arcIMS.filtred + ')';
    }
    textXML += '" ';
  }
  
  textXML += 
    '>' +
    '</QUERY>' +
    '</GET_FEATURES>' +
    '</REQUEST>' +
    '</ARCXML>';
  legendRequest.send(textXML);
}

function Legend_callback() {
  if (legendRequest.readyState == 4) {
    if (legendRequest.status == 200) {
      var xmlDocument = stringToDocument(legendRequest.responseText);
      var fieldArray = this.fields.split(" ");
      if (contain(xmlDocument, "ERROR")) {
        error(xmlDocument);
      } else if (contain(xmlDocument, "FEATURES") && contain(xmlDocument, "ENVELOPE")) {
        this.arcIMS.envelope.parseXML(xmlDocument);
        this.arcIMS.envelope.expand(this.offset);
        this.arcIMS.updateMap();
      } else {
        this.arcIMS.updateMap();
      }
    }
  }
}

function Legend_zoomTo(id) {
  if (id.length == 0) {
    this.arcIMS.filtred = null;
    this.arcIMS.updateMap();
    return;
  }
  
  var splitString = id.toString().split(",");
  var queryString = "";
  for (var i = 0; i < splitString.length; i++) {
    if (i > 0) {
      queryString += ",";
    }
    queryString += "\'" + splitString[i] + "\'";
  }
  
  if (splitString.length > 1) {
    this.arcIMS.selected = null;
    this.arcIMS.filtred = queryString;
    this.arcIMS.updateMap();
    return;
  }
  
  if (this.arcIMS.selected == queryString) {
    this.arcIMS.selected = null;
    this.arcIMS.updateMap();
    return;
  }
  
  this.arcIMS.selected = queryString;
  
	legendRequest = getXMLRequest();
  legendRequest.open("POST", this.arcIMS.catURL + this.arcIMS.featureService, true);
  if (!is_opera) {
    legendRequest.setRequestHeader("Content-Type", "text/xml");
  }
  legendRequest.onreadystatechange = legendCallback;
  var textXML = 
    '<ARCXML version="1.1">' +
    '<REQUEST>' +
    '<GET_FEATURES outputmode="newxml" geometry="false" envelope="true" >' +
    '<LAYER id="' + this.layer + '" />' +
    '<QUERY subfields="#SHAPE#" where="CELLNUMBER IN (' + this.arcIMS.selected + ')';
  if (this.arcIMS.codeId) {
    textXML += ' AND CODE_ID = \'' + this.arcIMS.codeId + '\' AND MTIME &gt;= ' + getSeconds(this.arcIMS.start) + '';
  }
  textXML += '">' +
    '</QUERY>' +
    '</GET_FEATURES>' +
    '</REQUEST>' +
    '</ARCXML>';
  legendRequest.send(textXML);
}

function Legend_visibility(bShow) {
  xVisibility(this.legendDiv, bShow);
  if (!this.arcIMS.listRT) {
    xVisibility("legendBorderDiv", bShow);
    xVisibility("legendTitle", bShow);
  }
}

Legend.prototype.update = Legend_update;
Legend.prototype.callback = Legend_callback;
Legend.prototype.zoomTo = Legend_zoomTo;
Legend.prototype.visibility = Legend_visibility;

// meteo.js
// © 2011 INSER SA
var meteoRequest;

function Meteo(arcIMS, layer, field, where, meteoDiv, meteoDivPosition, dateMeteo) {
  this.arcIMS = arcIMS;
  this.layer = layer;
  this.field = field;
  this.where = where;
  this.meteoDiv = meteoDiv;
  this.dateMeteo = dateMeteo;
  
  xMoveTo(meteoDiv, meteoDivPosition.x + this.arcIMS.delta, meteoDivPosition.y + this.arcIMS.delta);
}

function Meteo_update() {
  meteoRequest = getXMLRequest();
  meteoRequest.open("POST", this.arcIMS.catURL + this.arcIMS.featureService, true);
  if (!is_opera) {
    meteoRequest.setRequestHeader("Content-Type", "text/xml");
  }
  meteoRequest.onreadystatechange = meteoCallback;
  var textXML = 
    '<ARCXML version="1.1">' +
    '<REQUEST>' +
    '<GET_FEATURES outputmode="newxml" geometry="false" envelope="false" >' +
    '<LAYER id="' + this.layer + '" />' +
    '<QUERY subfields="OBJECTID ' + this.field + '"';
  if (this.where != null) {
    textXML += ' where="' + this.where + '" ';
  }
  textXML += 
    '>' +
    '</QUERY>' +
    '</GET_FEATURES>' +
    '</REQUEST>' +
    '</ARCXML>';
  meteoRequest.send(textXML);
}

function Meteo_callback() {
  if (meteoRequest.readyState == 4) {
    if (meteoRequest.status == 200) {
      var xmlDocument = stringToDocument(meteoRequest.responseText);
      if (contain(xmlDocument, "ERROR")) {
        error(xmlDocument);
      } else if (contain(xmlDocument, "FEATURES") && !contain(xmlDocument, "ENVELOPE")) {
        var features = xmlDocument.getElementsByTagName("FEATURE");
        var newDate = "";
        for (var i = 0; i < features.length; i++) {
          var fields = features[i].getElementsByTagName("FIELD");
          for (var j = 0; j < fields.length; j++) {
            var name = fields[j].attributes.getNamedItem("name").nodeValue;
            if (name.indexOf(this.field) != -1) {
              newDate = fields[j].attributes.getNamedItem("value").nodeValue;
            }
          }
        }
        
        if (newDate.length > 0) {
          newDate = formatDate(newDate);
          if (this.dateMeteo.innerHTML != newDate) {
            if (this.dateMeteo.innerHTML.length > 0) {
              this.arcIMS.updateOver();
            }
            this.dateMeteo.innerHTML = newDate;
          }
        } else {
          this.dateMeteo.innerHTML = "";
        }
      }
    }
  }
}

function Meteo_visibility(bShow) {
  if (bShow) {
    xDisplay(this.meteoDiv, "block");
  } else {
    xDisplay(this.meteoDiv, "none");
  }
}

Meteo.prototype.update = Meteo_update;
Meteo.prototype.callback = Meteo_callback;
Meteo.prototype.visibility = Meteo_visibility;

// over.js
// © 2011 INSER SA
function Over(arcIMS, overDiv, overDivPosition, overZoomDiv) {
  this.arcIMS = arcIMS;
  this.overDiv = overDiv;
  this.overDivPosition = overDivPosition;
  this.overZoomDiv = overZoomDiv;
  
  this.mapOffset = new Point(0, 0);
	if (is_ie) {
	  this.mapOffset = new Point(0, 4);
	}
	
	xEnableDrag(this.overZoomDiv, onOverDragStart, onOverDrag, onOverDragStop);
  xAddEventListener(this.overDiv, "click", onOverClick);
  this.overDiv.style.cursor = "crosshair";
  this.overZoomDiv.style.cursor = "move";
}

function Over_onDragStart(ele, mx, my)
{
  if (xWidth(this.overZoomDiv) > 25) {
  	this.maxx = this.overDivPosition.x + xWidth(this.overDiv) - 25 + this.arcIMS.delta.x;
  	this.minx = this.overDivPosition.x - xWidth(this.overZoomDiv) + 25 + this.arcIMS.delta.x;
  } else {
  	this.maxx = this.overDivPosition.x + xWidth(this.overDiv) - xWidth(this.overZoomDiv) + this.arcIMS.delta.x;
  	this.minx = this.overDivPosition.x + this.arcIMS.delta.x;
  }
  if (xHeight(this.overZoomDiv) > 25) {
  	this.maxy = this.overDivPosition.y + xHeight(this.overDiv) - 25 + this.arcIMS.delta.y;
  	this.miny = this.overDivPosition.y - xHeight(this.overZoomDiv) + 25 + this.arcIMS.delta.y;
  } else {
  	this.maxy = this.overDivPosition.y + xHeight(this.overDiv) - xHeight(this.overZoomDiv) + this.arcIMS.delta.y;
  	this.miny = this.overDivPosition.y + this.arcIMS.delta.y;
  }
  this.overZoomDiv.tx = xLeft(this.overZoomDiv);
  this.overZoomDiv.ty = xTop(this.overZoomDiv);
}

function Over_onDrag(ele, mdx, mdy)
{
  this.overZoomDiv.tx += mdx;
  this.overZoomDiv.ty += mdy;
  xMoveTo(this.overZoomDiv, 
          Math.max(Math.min(this.overZoomDiv.tx, this.maxx), this.minx), 
          Math.max(Math.min(this.overZoomDiv.ty, this.maxy), this.miny));
   xClip(this.overZoomDiv, 
        this.overDivPosition.y - xTop(this.overZoomDiv) + this.arcIMS.delta.y, 
        this.overDivPosition.x + xWidth(this.overDiv) - xLeft(this.overZoomDiv) - this.mapOffset.x + this.arcIMS.delta.x, 
        this.overDivPosition.y + xHeight(this.overDiv) - xTop(this.overZoomDiv) - this.mapOffset.y + this.arcIMS.delta.y, 
        this.overDivPosition.x - xLeft(this.overZoomDiv) + this.arcIMS.delta.x);
}

function Over_onDragStop(ele, mx, my)
{
  var x = this.arcIMS.initEnvelope.minx + this.arcIMS.initEnvelope.width() / xWidth(this.overDiv) * 
          (xLeft(this.overZoomDiv) - this.overDivPosition.x + xWidth(this.overZoomDiv) / 2 - this.arcIMS.delta.x);
  var y = this.arcIMS.initEnvelope.maxy - this.arcIMS.initEnvelope.height() / xHeight(this.overDiv) * 
          (xTop(this.overZoomDiv) - this.overDivPosition.y + xHeight(this.overZoomDiv) / 2 - this.arcIMS.delta.y);
  this.arcIMS.envelope.center(x, y);
  
  this.arcIMS.updateMap();
}

function Over_onClick(e) {
  var e = new xEvent(e);
  
  var x = this.arcIMS.initEnvelope.minx + this.arcIMS.initEnvelope.width() / xWidth(this.overDiv) * e.offsetX;
  var y = this.arcIMS.initEnvelope.maxy - this.arcIMS.initEnvelope.height() / xHeight(this.overDiv) * e.offsetY;
  this.arcIMS.envelope.center(x, y);
  
  this.arcIMS.updateMap();
}

Over.prototype.onDragStart = Over_onDragStart;
Over.prototype.onDrag = Over_onDrag;
Over.prototype.onDragStop = Over_onDragStop;
Over.prototype.onClick = Over_onClick;

// pan.js
// © 2011 INSER SA
function Pan(arcIMS, mapDiv, mapDivPosition) {
  this.arcIMS = arcIMS;
  this.mapDiv = mapDiv;
  this.mapDivPosition = mapDivPosition;
  
  this.mapOffset = new Point(0, 0);
	if (is_ie) {
	  this.mapOffset = new Point(0, 4);
	}
	this.maxx = xWidth(this.mapDiv) + this.mapDivPosition.x - 100 + this.arcIMS.delta.x;
	this.minx = 100 - xWidth(this.mapDiv) - this.mapDivPosition.x + this.arcIMS.delta.x;
	this.maxy = xHeight(this.mapDiv) + this.mapDivPosition.y - 100 + this.arcIMS.delta.y;
	this.miny = 100 - xHeight(this.mapDiv) - this.mapDivPosition.y + this.arcIMS.delta.y;
}

function Pan_onDragStart(ele, mx, my)
{
  this.mapDiv.tx = xLeft(this.mapDiv);
  this.mapDiv.ty = xTop(this.mapDiv);
}

function Pan_onDrag(ele, mdx, mdy)
{
  this.mapDiv.tx += mdx;
  this.mapDiv.ty += mdy;
  xMoveTo(ele, 
          Math.max(Math.min(this.mapDiv.tx, this.maxx), this.minx), 
          Math.max(Math.min(this.mapDiv.ty, this.maxy), this.miny));
  xClip(this.mapDiv, 
        this.mapDivPosition.y - xTop(this.mapDiv) + this.arcIMS.delta.y, 
        xWidth(this.mapDiv) - xLeft(this.mapDiv) + this.mapDivPosition.x - this.mapOffset.x + this.arcIMS.delta.x, 
        xHeight(this.mapDiv) - xTop(this.mapDiv) + this.mapDivPosition.y - this.mapOffset.y + this.arcIMS.delta.y, 
        this.mapDivPosition.x - xLeft(this.mapDiv) + this.arcIMS.delta.x);
}

function Pan_onDragStop(ele, mx, my)
{
  this.arcIMS.mapPan(this.mapDiv.tx - this.mapDivPosition.x, this.mapDiv.ty - this.mapDivPosition.y);
}

Pan.prototype.onDragStart = Pan_onDragStart;
Pan.prototype.onDrag = Pan_onDrag;
Pan.prototype.onDragStop = Pan_onDragStop;

// point.js
// © 2011 INSER SA
function Point(x, y) {
  this.x = x;
  this.y = y;
}

// util.js
// © 2011 INSER SA
function getAttribute(element, attributeName) { 
  return element.attributes.getNamedItem(attributeName).nodeValue;
}

function stringToDocument(xmlText) {
  xmlText = xmlText.replace(/&/g, "&amp;");
  if (window.ActiveXObject && !window.DOMParser) { //IE
    var doc = new ActiveXObject("Microsoft.XMLDOM");
    doc.async="false";
    doc.loadXML(xmlText);
    return doc
  }
  if (window.DOMParser) { //FF,OP
    return new DOMParser().parseFromString(xmlText, "text/xml");
  } 
  if (document.implementation && document.implementation.createDocument) { //KQ
    var doc = document.implementation.createDocument("", "", null);
    doc.loadXML(xmlText);
    return doc;
  }
  if (navigator.userAgent && navigator.vendor && 
      (navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1 || 
       navigator.vendor.indexOf("Apple") != -1)) { //SF
      var xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET", "data:text/xml;charset=utf-8," + encodeURIComponent(str), false);
      xmlhttp.send(null);
      return xmlhttp.responseXML;
  }
  alert("Sorry but your browser is not supported!");
}

function UTF2HTML() {
}

function contain(xmlDocument, elementName) {
  return count(xmlDocument, elementName) > 0;
}

function count(xmlDocument, elementName) {
  return xmlDocument.getElementsByTagName(elementName).length;
}

function getXMLRequest() {
  if (window.ActiveXObject && !window.XMLHttpRequest) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
}

function error(xmlDocument) {
  alert(xmlDocument.getElementsByTagName("ERROR")[0].childNodes[0].nodeValue);
}

function getSeconds(element) {
  var dateTime = element.value.split(" ");
  var dateOnly = dateTime[0].split(".");
  var timeOnly = dateTime[1].split(":");
  var dateObject = new Date(dateOnly[2], dateOnly[1] - 1, dateOnly[0], timeOnly[0], timeOnly[1]);
  return Math.round(Date.UTC(dateObject.getUTCFullYear(), dateObject.getUTCMonth(), dateObject.getUTCDate(), 
                             dateObject.getUTCHours(), dateObject.getUTCMinutes()) / 1000);
}

function formatDate(milliseconds) {
  var newDate = new Date(Number(milliseconds));
  var day = newDate.getDate().toString();
  if (day.length == 1) {
    day = "0" + day;
  }
  var month = (newDate.getMonth() + 1).toString();
  if (month.length == 1) {
    month = "0" + month;
  }
  var hours = newDate.getHours().toString();
  if (hours.length == 1) {
    hours = "0" + hours;
  }
  var minutes = newDate.getMinutes().toString();
  if (minutes.length == 1) {
    minutes = "0" + minutes;
  }
  return day + "." + month + "." + newDate.getFullYear() + " " + hours + ":" + minutes;
}

function getArgs() {
  var args = new Object();
  var query = location.search.substring(1);
  var pairs = query.split("&");
  for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0,pos);
    var value = pairs[i].substring(pos+1);
    args[argname] = unescape(value.replace(/\+/g, " "));
  }
  return args;
}

function launch(url) {
	if (window.top.opener) {
		if ((typeof window.top.opener != 'undefined') && !(window.top.opener.closed)) {
			window.top.opener.location.href=url;
		} else {
			window.open(url);
		}					
	} else {
		if (window.top.opener != null) {
			window.top.opener.location.href=url;
		} else {
			window.open(url);
		}
	}
}

//zoomin.js
// © 2011 INSER SA
function ZoomIn(arcIMS, mapDiv, mapDivPosition, zoomDiv) {
  this.arcIMS = arcIMS;
  this.mapDiv = mapDiv;
  this.mapDivPosition = mapDivPosition;
  this.zoomDiv = zoomDiv;
  
  this.mapOffset = new Point(0, 0);
	if (is_ie) {
	  this.mapOffset = new Point(4, 4);
	}
}

function ZoomIn_onDragStart(ele, mx, my)
{
  this.zoomDiv.tx = mx;
  this.zoomDiv.ty = my;
  this.zoomDiv.tw = 0;
  this.zoomDiv.th = 0;
  xMoveTo(this.zoomDiv, mx, my);
  xResizeTo(this.zoomDiv, 2, 2);
  xVisibility(this.zoomDiv, true);
}

function ZoomIn_onDrag(ele, mdx, mdy)
{
  this.zoomDiv.tw += mdx;
  this.zoomDiv.th += mdy;
  var width = Math.max(Math.abs(this.zoomDiv.tw), 2);
  var height = Math.max(Math.abs(this.zoomDiv.th), 2);
  if (this.zoomDiv.tw < 0) {
    xLeft(this.zoomDiv, Math.max(this.zoomDiv.tx + this.zoomDiv.tw, xLeft(this.mapDiv)));
	  width = Math.min(width, this.zoomDiv.tx - xLeft(this.zoomDiv));
  } else {
    width = Math.min(width, xLeft(this.mapDiv) + xWidth(this.mapDiv) - this.zoomDiv.tx - this.mapOffset.x);
  }
  if (this.zoomDiv.th < 0) {
    xTop(this.zoomDiv, Math.max(this.zoomDiv.ty + this.zoomDiv.th, xTop(this.mapDiv)));
	  height = Math.min(height, this.zoomDiv.ty - xTop(this.zoomDiv));
  } else {
    height = Math.min(height, xTop(this.mapDiv) + xHeight(this.mapDiv) - this.zoomDiv.ty - this.mapOffset.y);
  }
  xResizeTo(this.zoomDiv, width, height);
}

function ZoomIn_onDragStop(ele, mx, my)
{
  xVisibility(this.zoomDiv, false);
  this.arcIMS.mapZoomIn(xLeft(this.zoomDiv) - xLeft(this.mapDiv), xTop(this.zoomDiv) - xTop(this.mapDiv), 
                     xWidth(this.zoomDiv), xHeight(this.zoomDiv));
}

ZoomIn.prototype.onDragStart = ZoomIn_onDragStart;
ZoomIn.prototype.onDrag = ZoomIn_onDrag;
ZoomIn.prototype.onDragStop = ZoomIn_onDragStop;
