
/*  support.js
 *
 *  Created: Dirk Townsend, TerraViva Inc.  2002.10.10
 *
 *  Changes:  
 *  
 *  13 Nov 02, DT, Added document focus command to init so that window pops-up again in IE.
 *  24 Nov 02, DT, Corrected focus command to work with IE 5
 *  22 Jan 03, TB, Make sure zoomin button is selected by default.
 *                 Enable pan and zoom during a 'zone_recherche' operation.
 *                 Call a url after the 'zone_recherche' instead of displaying a popup.
 */


  detect_browser();

  function doZoomin()
  {
    if (document.Main.action.value != null && document.Main.action.value == 'zr')
    {
      document.Main.zrstep.value     = 'select';
      document.images['zrstart'].src = 'images/zr.gif';
    }  	
    document.images['zoomin'].src     = 'images/zoomina.gif';
    document.images['zoomout'].src    = 'images/zoomout.gif';
    document.images['fullextent'].src = 'images/fullextent.gif';
    document.Main.action.value = 'zib';  
    enable_image();
  }

  function doZoomout()
  {
    if (document.Main.action.value != null && document.Main.action.value == 'zr')
    {
      document.Main.zrstep.value     = 'select';
      document.images['zrstart'].src = 'images/zr.gif';
    }  	
    document.images['zoomout'].src    = 'images/zoomouta.gif';
    document.images['zoomin'].src     = 'images/zoomin.gif';
    document.images['fullextent'].src = 'images/fullextent.gif';
    document.Main.action.value = 'zoc';  
  }

  function doFullextent()
  {
    if (document.Main.action.value != null && document.Main.action.value == 'zr')
    {
      document.Main.zrstep.value     = 'select';
      document.images['zrstart'].src = 'images/zr.gif';
    }  	
    document.images['zoomin'].src     = 'images/zoomin.gif';
    document.images['zoomout'].src    = 'images/zoomout.gif';  
    document.images['fullextent'].src = 'images/fullextent.gif';
    document.Main.action.value = 'fe';  
    document.Main.submit();  
  }

  function doZrStart()
  {
    document.images['zrstart'].src = 'images/zra.gif';
    document.images['zoomout'].src = 'images/zoomout.gif';
    document.images['zoomin'].src  = 'images/zoomin.gif';
    document.Main.action.value = 'zr';  
    document.Main.zrstep.value = 'box';
    enable_image();
  }

  function init()
  {  	
    this.focus();
    set_action();
    if (document.Main.zrstep.value != null && document.Main.zrstep.value == 'box') enable_image();
    if (document.Main.zrstep.value != null && document.Main.zrstep.value == 'send') 
    {
      if (document.Main.page_retour.value != null && document.Main.page_retour.value !='')
      {
        var targetUrl = document.Main.page_retour.value;
        targetUrl = targetUrl + '?';
        targetUrl = targetUrl +       'pos_x_min=' + document.Main.xlr.value;
        targetUrl = targetUrl + '&' + 'pos_x_max=' + document.Main.xhr.value;
        targetUrl = targetUrl + '&' + 'pos_y_min=' + document.Main.ylr.value;
        targetUrl = targetUrl + '&' + 'pos_y_max=' + document.Main.yhr.value;
        targetUrl = targetUrl + '&' + 'zrthru='    + document.Main.zrthru.value;
        window.location = targetUrl;
        document.Main.zrstep.value = null;
      } else {
         window.opener.appel_arcims(document.Main.zrthru.value, document.Main.xlr.value, document.Main.ylr.value, document.Main.xhr.value, document.Main.yhr.value);
         document.Main.zrstep.value = null;
      }
    }
  }

  function set_action()
  {
    // alert('action:zstep is ' + document.Main.action.value  + ':' + document.Main.zrstep.value);

    // Retain zoomin or zoomout mode even after map refresh.  Also, default to zoomin mode if no mode is set.   
    if (document.Main.action.value != null && document.Main.action.value == 'zoc') doZoomout();
    else 
    if (document.Main.action.value == 'none' || document.Main.action.value == null || document.Main.action.value == '' || document.Main.action.value == 'zic' || document.Main.action.value == 'zib' || document.Main.action.value == 'zr' ) doZoomin();
  }

  function enable_image() {
    if (isNav) {
    	document.captureEvents(Event.MOUSEMOVE);  // comment out to disable stretch box in NS.
    	document.captureEvents(Event.MOUSEDOWN);
    	document.captureEvents(Event.MOUSEUP);
    }
    document.onmousemove = getMouse;
    document.onmousedown = mapTool;
    document.onmouseup = chkMouseUp;
  }

  function doPan(value) {
    // value is NN, NO, NE, EE, OO, SS, SO, SE
    document.Main.action.value = value;
    document.Main.submit();
  }  

  function detect_browser() {
    if (isNav)
    {
    	// alert("isNav");
    	if (parseFloat(navigator.appVersion)<5)
    	{
    		isNav4=true;
    	        // alert("Netscape 4.x or older");
    	} else
    	{
    		is5up = true;
    		// alert("is5up");
    	}
    } else
    {
    	// alert("isIE");
    	isIE4=true;
    	isIE=true;
    	if (navigator.appVersion.indexOf("MSIE 5")>0)
    	{
    		isIE4 = false;
    		is5up = true;
    		// alert("IE5");
        }
    }
  }

  function mapTool(e) {

    // alert('Down');
    // window.status = "Down: ";
  
    getImageXY(e);  

    // window.status = "Down: initial coords   p1:" + x1 + ":" + y1 + "  p2:"+ x2 + ":" + y2;

    if ( (mouseX>iWidth+xMapOffset) || (mouseY>iHeight+yMapOffset) || (mouseX<=xMapOffset) ||(mouseY<=yMapOffset) ) return
      
    if (document.Main.action.value == 'zoc') {
    	document.Main.mapx.value = mouseX; 
        document.Main.mapy.value = mouseY;
        // alert('got ' + document.Main.mapx.value +' : ' + document.Main.mapy.value);    	
    	// document.Main.submit();  
    } 
    else if (document.Main.action.value == 'zib' || document.Main.action.value == 'zr') 
    {    	
    	zooming = true;
        x1=mouseX;
        y1=mouseY;
        x2=x1+1;
        y2=y1+1;

        boxIt(x1,y1,x2,y2);
    } 
  }

  function getMouse(e) {
  	
    if (!zooming) return;

    window.status = "x:y - " + mouseX + " : " + mouseY
    getImageXY(e);

    if ( (mouseX>iWidth+xMapOffset) || (mouseY>iHeight+yMapOffset) || (mouseX<=xMapOffset) ||(mouseY<=yMapOffset) ) {
	chkMouseUp(e);
    } else {

        x2=mouseX;
	y2=mouseY;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}

	if ((x1 != x2) && (y1 != y2)) {

	  boxIt(zleft,ztop,zright,zbottom);
        }
    }
  }

  function chkMouseUp(e) {
  	
    // alert('Up');      	
    // window.status = "Up: ";
  	
    if (!zooming) return;
    
    zooming=false;

    document.onmousemove = null;
    document.onmousedown = null;
    document.onmouseup = null;

    hideLayer("zoomBoxTop");
    hideLayer("zoomBoxLeft");
    hideLayer("zoomBoxRight");
    hideLayer("zoomBoxBottom");
    
    if ((zright <zleft+5) && (zbottom < ztop+5))
    {          	
      // alert('set to zic');
      if (document.Main.action.value != null && document.Main.action.value != 'zr') document.Main.action.value = 'zic';
    }
    else
    {
      document.Main.xlr.value = zleft-xMapOffset;
      document.Main.xhr.value = zright-xMapOffset;
      document.Main.ylr.value = ztop-yMapOffset;
      document.Main.yhr.value = zbottom-yMapOffset;    	
      if (document.Main.action.value != null && document.Main.action.value != 'zr') document.Main.action.value = "zib";
      //alert("Up. p1:" + (zleft-xMapOffset) + ":" + (ztop-yMapOffset) + "  p2:"+ (zright-xMapOffset) + ":" + (zbottom-yMapOffset));
      document.Main.submit();
    }
  }
  
  function getImageXY(e) {
    if (isNav) {
    	mouseX=e.pageX;
    	mouseY=e.pageY;
    } else {
    	mouseX=event.clientX + document.body.scrollLeft;
    	mouseY=event.clientY + document.body.scrollTop;
    }
    // subtract offsets from page left and top
    mouseX = mouseX-hspc;
    mouseY = mouseY-vspc;
  }

  
  // toggle layer to invisible
  function hideLayer(name) {
    	var layer = getLayer(name);
    	if (isNav4)
      	layer.visibility = "hide";
    	//if (document.all)
  	else
     		 layer.visibility = "hidden";
  		 //layer.display="none";
  }
    
  function createLayer(name, inleft, intop, width, height, visible, content) {
  	  var layer;
  	  // alert('in createlayer: '+isNav4);
  	  if (isNav4) {
  	    // alert('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');  	  	
  	    document.writeln('<layer name="' + name + '" left=' + inleft + ' top=' + intop + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
  	    document.writeln(content);
  	    document.writeln('</layer>');
  	  } else {
  	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + ' background-color:red; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
  	    document.writeln(content);
  	    document.writeln('</div>');
  	  }
  }
  
  function boxIt(theLeft,theTop,theRight,theBottom) {
  
  		if (!isNav4) {
  			theTop = theTop + vspc;
  			theBottom = theBottom + vspc;
  			theLeft = theLeft + hspc;
  			theRight = theRight + vspc;
  		}
  		clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+3);
  		clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+3,theBottom);
  		clipLayer("zoomBoxRight",theRight-3,theTop,theRight,theBottom);
  		clipLayer("zoomBoxBottom",theLeft,theBottom-3,theRight,theBottom);
  		showLayer("zoomBoxTop");
  		showLayer("zoomBoxLeft");
  		showLayer("zoomBoxRight");
  		showLayer("zoomBoxBottom");
  
  }
  
  function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {
  	  var layer = getLayer(name);
  	  if (isNav4) {
  		    layer.clip.left   = clipleft;
  		    layer.clip.top    = cliptop;
  		    layer.clip.right  = clipright;
  		    layer.clip.bottom = clipbottom;
  	  }	  else {
  			var newWidth  = clipright - clipleft;
  			var newHeight = clipbottom - cliptop;
  			layer.height  = newHeight;
  			layer.width	= newWidth;
  			layer.top	= cliptop  + "px";
  			layer.left	= clipleft + "px";
  		}
  
  }
  
  // toggle layer to visible
  function showLayer(name) {
    	var layer = getLayer(name);
    	if (isNav4)
      	layer.visibility = "show";
    	//if (document.all)
  	else
     	 layer.visibility = "visible";
  	 //layer.display="block";
  }
  
  // get the layer object called "name"
  function getLayer(name) {
  	  // alert('Get layer for: ' + name);
  	  if (isNav4)
  	    return(document.layers[name]);
  	  else if (isIE4) {
  	    layer = eval('document.all.' + name + '.style');
  	    return(layer);
  	  } else if (is5up) {
  		var theObj = document.getElementById(name);
  		return theObj.style
  	  }
  	  else
  	    return(null);
  }
