/** //////////////////////////////////////////////////////////////////////////////////////
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
 File : Javascript / client.js

 Cross-Browser X DHTML Library v3.15.1, from Cross-Browser.com
 Copyright İ 2002,2003,2004 Michael Foster (mike@cross-browser.com)
 
 Using iFrame :

- iframes are not supported by Netscape 4.
- iframes inside layers are buggy in Opera 6 (solved in 7). 
Even outside layers, they can be very buggy in Opera 5 on Mac.
- WebTV reloads the entire page when reloading an iframe.
- Using location.href in an iframe to change the page in the iframe does not work 
in Explorer 4 and 5.0 on Windows and Opera 5 and 6.

  XHTML DOM elements :

- Object Map. Must be an <input type="image" />
- Object ZoomBox. 
  Must be an <div id="zoomrect"></div> 
  with CSS definition : #zoomrect { position: absolute; top: 0; left: 0; width: 1; height: 1; z-index: 105; border: 1px solid blue; visibility: visible; }

_________________________________________________________________________________________
*/

var _mapclient = null;

//---------------------------------------------------------------------------------------
// Stack up wondow.onload event using Simon Willison solution 
// loadEvent( function() { ... } );
//
function loadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } 
  else {
    window.onload = function() { 
      oldonload(); 
      func();
    }
  }
  
  // Hide spinner when page is loaded
  xHide('workprogress');  
}


/** ---------------------------------------------------------------------------------------
*
* GI_ClientMapServer class (compat. IE5+)
*
*/
function GI_ClientMapServer(id)
{
  //// Private Objects [MODIFY TARGETS IF REQUIRED]
  this.oMap     = xGetElementById('mainmap');
//  this.oCoord   = xGetParentFrameElementById('coordinates');// Location: parent.document
  this.oZoomBox = xGetElementById('zoomrect');
  this.oSpinner = xGetElementById('workprogess');
  this.currentTool = 'ZOOM_IN';
  this.currentTool = 'INFO';

  //// Public Methods
  this.setMapSize = function(w, h) 
  { 
    this.nMapWidth = w; this.nMapHeight = h; 
    // Restore current tool
    var oTargetCmdBar = parent._cmdBar; 
    if (typeof oTargetCmdBar == 'object') { oTargetCmdBar.restoreCurrentTool(this); }  
  }

  this.setMapGeoExtent = function(minx, miny, maxx, maxy) { this.fGeoMinX = minx; this.fGeoMinY = miny; this.fGeoMaxX = maxx; this.fGeoMaxY = maxy; }      
//  this.setCurrentTool  = function(t, oSrc) { if (xStr(t)) { this.currentTool = t; xGetElementById('ACTION').value = t; this.setCursor(); } }
  this.setCurrentTool  = function(t, oSrc) { if (xStr(t)) { this.currentTool = t; xGetElementById('cmd').value = t; this.setCursor(); 
		if (this.currentTool == 'INFO')
		{
			xGetElementById('mapzoom').style.visibility="visible";
	  }
		else
		{
			xGetElementById('mapzoom').style.visibility="hidden";
	  }
  
  } }
  this.setCursor       = function()
  {
    // Note: [Gecko] take effect only on first pixel move of mouse
    if (this.currentTool == 'ZOOM_IN' || this.currentTool == 'ZOOM_OUT') { 
      if (this.oMap.style) this.oMap.style.cursor = 'crosshair'; 
      return;
    }
    else if (this.currentTool == 'MOVE') { 
      if (this.oMap.style) this.oMap.style.cursor = 'move';
    }
    else if (this.currentTool.search(/INFO/g) != -1) { 
      if (this.oMap.style) this.oMap.style.cursor = 'help'; 
    }
  }

  //// Private Properties
  this.fGeoMinX = this.fGeoMinY = this.fGeoMaxX = this.fGeoMaxY = 0; //this.fGeoMinW = xGetElementById('MAP_EXTENT_MINX').value;   
  
  // Pt screen onMouseDown (x1/y1), onMouseMove/Up (x2/y2)
  this.x1 = this.y1 = this.x2 = this.y2 = -1;
  this.drag = false;
  
  // Map position
  this.nMapPosLeft = xPageX(this.oMap); // xLeft
  this.nMapPosTop  = xPageY(this.oMap); // xTop
  this.nMapWidth   = 0;
  this.nMapHeight  = 0;

  this.oMap.style.position = 'absolute';
  this.oMap.style.top  = this.nMapPosTop;
  this.oMap.style.left = this.nMapPosLeft;

  // Initialize wait spinner...
  xHide(this.oSpinner);
  this.oSpinner.style.position = 'absolute';

  
  //// Private Events
  // Reference 'this' by 'o' since inside function 'this' refers oMap
  var o = this;
  
  this.oMap.onmouseover = function(e) {o._handleMouseEnter(e)};    
  this.oMap.onmousedown = function(e) {o._handleMouseDown(e); /* Turn off default mouse selection */ return false; };
  this.oMap.onmousemove = function(e) {o._handleMouseMove(e)};
  this.oMap.onmouseup   = function(e) {o._handleMouseUp(e)};
  this.oMap.onmouseout  = function(e) {o._handleMouseOut(e)};  
  
  this.oZoomBox.onmousemove = function(e) {o._handleMouseMove(e)};    
  this.oZoomBox.onmouseup = function(e) {o._handleMouseUp(e)};  
}



var p = GI_ClientMapServer.prototype;

p._handleMouseEnter = function(e) {
  if (this.currentTool == 'PAN')
    this.oMap.style.cursor = 'move';
}


//--------------------------------------------------------------
// Purpose  : Directly submit an Action to server
// Return   : None.
//
// Used by actions :
// - ZOOM_FULL, 
// - NAV_PREV, 
// - NAV_NEXT

p.submitAction = function(sender) 
{
   xGetElementById('cmd').value = sender.getAttribute('id');
   xShow(this.oSpinner);
   document.formMap.submit();
}
  

p.transformPix2Geo = function(nPixPos, dfPixMin, dfPixMax, dfGeoMin, dfGeoMax, nInversePix) 
{
    dfWidthGeo = dfGeoMax - dfGeoMin;
    dfWidthPix = dfPixMax - dfPixMin;

    dfPixToGeo = dfWidthGeo / dfWidthPix;

    if (Math.abs(dfPixToGeo) < 1)
        dNbDecimal = Math.abs(Math.log(dfPixToGeo))/Math.log(10)+1;
    else
        dNbDecimal = 0;

    if (nInversePix == 0) {
        dfDeltaPix = nPixPos - dfPixMin;
    }
    else {
        dfDeltaPix = dfPixMax - nPixPos;
    }

    dfDeltaGeo = dfDeltaPix * dfPixToGeo;

    dfPosGeo = new String(new Number(dfGeoMin) + new Number(dfDeltaGeo));
    aPosGeo = dfPosGeo.split(".");

    if (dNbDecimal > 0 && aPosGeo.length == 2) {
        dfPosGeo = aPosGeo[0]+"."+aPosGeo[1].substr(0, dNbDecimal);
    }
    else {
        dfPosGeo = aPosGeo[0];
    }

    szttt =  ""+"pixelpos = "+nPixPos+" result ="+dfPosGeo;
    //alert(szttt);
    return dfPosGeo;
}



/**
 * This event occurs when user moves the mouse over the map
 **/

p._handleMouseMove = function(e)
{
  if (typeof xEvent != 'function')
    return;
    
  var evt = new xEvent(e);
  this.x2 = evt.pageX; // cross-browser X-screen coordinate
  this.y2 = evt.pageY; // cross-browser Y-screen coordinate

  // Set cursor according current tool
  this.setCursor();
  
  // Set X, Y
  var fGeoPosX = this.transformPix2Geo((this.x2 - this.nMapPosLeft), 0, this.nMapWidth, this.fGeoMinX, this.fGeoMaxX, 0);
  var fGeoPosY = this.transformPix2Geo((this.y2 - this.nMapPosTop),  0, this.nMapHeight, this.fGeoMinY, this.fGeoMaxY, 1);
//  this.oCoord.innerHTML =  'X,Y : ' + fGeoPosX + ' / ' + fGeoPosY + '<br />Lambert II étendu' ; //this.x2;

  //// MODE PAN
  if (this.currentTool == 'PAN' && this.drag)
  {
    var left = this.x2 - this.x1 + this.nMapPosLeft;
    var top  = this.y2 - this.y1  + this.nMapPosTop;
    xMoveTo(this.oMap, left, top);

    //window.status = left + ' -/- ' + top; 
    
    var t = this.y1 - this.y2;
    var r = this.x1 - this.x2 + this.nMapWidth;
    var b = this.y1 - this.y2 + this.nMapHeight;
    var l = this.x1 - this.x2;

    xClip(this.oMap, t, r, b, l);
    
    return false; // Fix for IE5
  }

  //// MODE ZOOM_IN
  else if (this.currentTool == 'ZOOM_IN' && this.drag)
  {
    if(this.x1==this.x2 && this.y1==this.y2) {    
      xHide(this.oZoomBox); // defer to the browser cursor  
    }
    else 
    {
        var x = Math.min(this.x1,  this.x2);// - this.offsetx; // UL corner of box
        var y = Math.min(this.y1,  this.y2);// - this.offsety;  
        var w = Math.abs(this.x1 - this.x2) + 1; // + 2; //this.thickness;
        var h = Math.abs(this.y1 - this.y2) + 1; // + 2; //this.thickness;

        xMoveTo(this.oZoomBox, x, y);
        xResizeTo(this.oZoomBox, w, h);
        xShow(this.oZoomBox);
        
        // Only for debug
        //window.status = 'h:' + h + ' w:' + w + ' x:' + x + ' y:' + y + ' {' + this.x1 + '/' + this.y1 + ',' + this.x2 + '/' + this.y2 + '}';
        
        return false; // Fix for IE5
    }
  }
}


/**
 * This event occurs when the mouse leaves the map
 **/

p._handleMouseOut = function(e) 
{
//  this.oCoord.innerHTML  = '';
  this.oMap.style.cursor = this.oZoomBox.style.cursor = "default";
}

p._handleMouseDown = function(e) 
{
  var evt = new xEvent(e);

  this.drag = true;
  this.x1 = this.x2 = evt.pageX;
  this.y1 = this.y2 = evt.pageY;
  
  if (this.currentTool == 'ZOOM_IN')
  {
    //jg.paint();
  }
}

p._handleMouseUp = function(e) 
{
  var evt = new xEvent(e);
  
  this.drag = false;
  
  // Skip right mouse down button
  var rightclick = false;
  if (!e) var e = window.event;
  if (e.which) rightclick = (e.which == 3 || evt.keyCode == 0);
  else if (e.button) rightclick = (e.button == 2);
  if (rightclick) return true;
  
  // submit the form with the values

//  xGetElementById('ACTION').value = this.currentTool;
  xGetElementById('cmd').value = this.currentTool;

  this.x1 -= this.nMapPosLeft;
  this.x2 -= this.nMapPosLeft;
  this.y1 -= this.nMapPosTop;
  this.y2 -= this.nMapPosTop;

  if (this.currentTool == 'ZOOM_IN'  || 
      this.currentTool == 'ZOOM_OUT' || 
      this.currentTool == 'INFO')
  {    //xHide(this.oZoomBox);
    xGetElementById('NAV_INPUT_TYPE').value = 'auto_rect';
    xGetElementById('NAV_INPUT_COORDINATES').value = this.x1+','+this.y1+';'+this.x2+','+this.y2;
    
   if (this.currentTool == 'ZOOM_OUT')
   {
     xGetElementById('NAV_INPUT_COORDINATES').value = this.x2+','+this.y2;
     xGetElementById('NAV_INPUT_TYPE').value = 'POINT';
   }
   
   //xHide(this.oMap);
   xShow(this.oSpinner);
   document.formMap.submit();
  }
  else if (this.currentTool == 'PAN')
  {
    xGetElementById('NAV_INPUT_COORDINATES').value = this.x1+','+this.y1+';'+this.x2+','+this.y2;
    
    //xHide(this.oMap);
   xShow(this.oSpinner);
   document.formMap.submit();
  }
  else if (this.currentTool.search(/INFO/g) != -1)
  {
    if (this.currentTool.search(/INFO/g) != -1)
    {
      // We want redirect query result to a popup window
      // (only parent iframe can open window) parent.xWinOpen
      window.open(xGetElementById(this.currentTool).value + '?X=' + this.x2 + '&Y=' + this.y2, "Résultats", "left=300,top=200,width=800,height=400,location=1,menubar=1,resizable=1,scrollbars=1,status=1,toolbar=1"); 
      return false;
    }
    else
    {
      xGetElementById('NAV_INPUT_COORDINATES').value = this.x2+','+this.y2;
      xShow(this.oSpinner);
      document.formMap.submit();    
    }
    

  }
}

// Called by the result popup window
function submitAction(szAction, szParams)
{
  xShow('workprogess');
  xGetElementById('ACTION').value = szAction;
  xGetElementById('PARAMS').value = szParams;
  document.formMap.submit();
}

// This function is can be called by parent frame 
// while reloading map

function Wait() { // Show spinner
  xShow('workprogess');
}

