/** //////////////////////////////////////////////////////////////////////////////////////
 File : Javascript / navigation.js

 Cross-Browser X DHTML Library v3.15.1, from Cross-Browser.com
 Copyright © 2002,2003,2004 Michael Foster (mike@cross-browser.com)
//////////////////////////////////////////////////////////////////////////////////////**/

//---------------------------------------------------------------------------------------
// Stack up wondow.onload event using Simon Willison solution 
// loadEvent( function() { ... } );
//
function loadNav(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } 
  else {
    window.onload = function() { oldonload(); func(); }
  }
}

// Instanciate _cmdBar when page has finished loading
loadNav ( 
  function() {
    _cmdBar = new ClientCommandBar("navigation");

    // Force iFrame to setParent on first page load
    if (typeof frames['iframemap'].setParent == 'function') {
      frames['iframemap'].setParent();
    }
//alert("load ok");    
    //window.status = 'Done';
  } );


/** ---------------------------------------------------------------------------------------
*
* ClientCommandBar class
*
*/
function ClientCommandBar(targetID)
{
  this.aCommandArray = xGetElementsByTagName("img",  xGetElementById(targetID));
  this.aCheckGroup   = new Array();

  this.in_array = function(item, arr) { for (var i=0; i<arr.length; i++) {if (arr[i] == item) return true;} return false; }
  this.button = { 
    name  : null, 
    group : null 
  }

  var o=this;
  for ( var i=0; i<this.aCommandArray.length; i++ ) 
  {	var oCmd = this.aCommandArray[i];
    if ( !oCmd.getAttribute("onclick") ) {
      oCmd.onclick = function() {o.onCommandMouseClick(this)};
    }

    oCmd.onmouseover = function() { 
      this.src = this.src.replace(/_off/, '_over');
//      xGetElementById('coordinates').innerHTML = this.getAttribute('title');
    };

    oCmd.onmouseout  = function() { 
      this.src = this.src.replace(/_over/, '_off');
//      xGetElementById('coordinates').innerHTML = '';
    };

    var id = oCmd.getAttribute("id"); // returns null if not presents
    if (id != null)
    {	var isModeQuery = (id.search(/INFO/g) != -1);
      if (id == 'PAN' || id == 'ZOOM_IN' || id == 'ZOOM_OUT' || id == 'SURFACE' || id == 'DISTANCE' || isModeQuery)
      {	this.aCheckGroup[i] = oCmd;
      }   
    }
  }
}


var pcb = ClientCommandBar.prototype;
pcb.onCommandMouseClick = function(sender) 
{
  var id = sender.getAttribute("id");
  if ( this.in_array(sender, this.aCheckGroup) )
  {
    // Turn off group
    exp=/_on|_over/;
    for (var i=0; i<this.aCheckGroup.length; i++)
    {	if (exp.test(this.aCheckGroup[i].src))
			{	this.aCheckGroup[i].src = this.aCheckGroup[i].src.replace(/_on|_over/, '_off');
			}
    }

    // Turn on button
    sender.src = sender.src.replace(/_off|_over/, '_on');
  }

  // Set MapServerClient current mouse mode
  if (frames['iframemap'] != null)
  {	var oTarget = frames['iframemap']._mapclient;
	  if ((typeof oTarget == 'object') && (oTarget != null))
	  {	if ((oTarget.currentTool == "SURFACE") && (id != "SURFACE"))
			{	// saisie de zone en cours : on cache les boutons de validation/annulation de la saisie
	    	xHide(xGetElementById("zone_cmds"));
	    	oTarget.annulerZone();
			}
	  	else if ((oTarget.currentTool == "DISTANCE") && (id != "DISTANCE"))
			{	// distance
	    	xHide(xGetElementById("divdist"));
	    	oTarget.annulerDist();
			}

	    oTarget.setCurrentTool(id, sender);
	    if (id == "SURFACE")
	    {	// Ajouter les boutons Valider et Annuler la création d'une zone
	    	xShow(xGetElementById("zone_cmds"));
	    }
	  	else if (id == "DISTANCE")
	    {	// Afichage de la distance sous la carte
	    	xShow(xGetElementById("divdist"));
	    }
	  }
	}
}


// Helper func used to restore active command to iFrame
// Called by ClientMapServer

pcb.restoreCurrentTool = function(oClient)
{
  // Ensure ClientMapServer object reference is valid
  if (typeof oClient != 'object')
    return;

  for ( var i=0; i<this.aCommandArray.length; i++ ) 
  {
    // Do we have an active command on ?
    var oCmd = this.aCommandArray[i];
    if (oCmd.src.search(/_on/g) != -1)
    {
      var id = oCmd.getAttribute("id");

      // If this is a mouse mode, restore this mode to ClientMapServer
      var isModeQuery = (id.search(/INFO/g) != -1);
      if (id == 'PAN' || id == 'ZOOM_IN' || id == 'ZOOM_OUT' || id == 'SURFACE' || id == 'DISTANCE' || isModeQuery) {
          oClient.setCurrentTool(id, null);
      }  
    }
  }
}

function validerZone()
{ var oTarget = frames['iframemap']._mapclient;
	if (typeof oTarget == 'object') {
		if (oTarget.currentTool == "SURFACE")
		{	// saisie de zone en cours : on cache les boutons de validation/annulation de la saisie
	  	oTarget.validerZone(0);
	  }
	}
}

function annulerZone()
{ var oTarget = frames['iframemap']._mapclient;
	if (typeof oTarget == 'object') {
		if (oTarget.currentTool == "SURFACE")
		{	// saisie de zone en cours : on cache les boutons de validation/annulation de la saisie
	  	oTarget.annulerZone();
	  }
	}
}

