/** //////////////////////////////////////////////////////////////////////////////////////
 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 GI_ClientCommandBar("navigation");

		if (frames['iframemap'] != null)
		{
	    // Force iFrame to setParent on first page load
	    if (typeof frames['iframemap'].setParent == 'function') {
	      frames['iframemap'].setParent();
	    }
	   }

//alert("load ok");
    //window.status = 'Done';
  }
);


/** ---------------------------------------------------------------------------------------
*
* GI_ClientCommandBar class
*
*/
function GI_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' || isModeQuery) {
           this.aCheckGroup[i] = oCmd; // push, function IE 5.5+
        }    
    }

  }
}


var pcb = GI_ClientCommandBar.prototype;
pcb.onCommandMouseClick = function(sender) 
{
  var id = sender.getAttribute("id");
  if ( this.in_array(sender, this.aCheckGroup) )
  {
    // Turn off group
    for (var i=0; i<this.aCheckGroup.length; i++) {
      this.aCheckGroup[i].src = this.aCheckGroup[i].src.replace(/_on|_over/, '_off');
    }

    // Turn on button
    sender.src = sender.src.replace(/_off|_over/, '_on');
  }
  
  

  // Set GI_MapServerClient current mouse mode

  var oTarget = frames['iframemap']._mapclient;
  if (typeof oTarget == 'object') {
    oTarget.setCurrentTool(id, sender);
  }
}


// Helper func used to restore active command to iFrame
// Called by GI_ClientMapServer

pcb.restoreCurrentTool = function(oClient)
{
  // Ensure GI_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 GI_ClientMapServer
      var isModeQuery = (id.search(/INFO/g) != -1);
      if (id == 'PAN' || id == 'ZOOM_IN' || id == 'ZOOM_OUT' || isModeQuery) {
          oClient.setCurrentTool(id, null);
      }  
    }
  }
}




///////////////////////////////////////////////////////////////////////////////////////////
// Treeview helper functions
//
/**
* Uncheck all checkbox items of treeview  'LayerTree'
*/
function tvUnCheckAll()
{
  var oTree = xGetElementById('LayerTree');
  if (oTree != null)
  {
    var aInputArray = xGetElementsByTagName('input', oTree);
    if (aInputArray != null)
    {
      for (var i=0; i<aInputArray.length; i++) 
        aInputArray[i].checked = false;    
    }
  }
}


function tvCheckBranch(oInput) { // Checkbox
   objTreeMenu_1.checkBranch(oInput.parentNode.parentNode.parentNode.getAttribute('id'), oInput.checked);
  
 //francaisames['iframemap'].document.mapserver.LAYERS.value += ',' + check.value;
 // Check all child checkboxes
  //var aCheckBoxes = frames['iframemap'].xGetElementsByTagName("input", oCheck);
}


/**
* Check an item according its attribute name
* Can be called by iframe with: parent.tvCheckItemByValue
*/
function tvCheckItemByValue(szValue)
{
  var oTree = xGetElementById('LayerTree');
  if (oTree == null)
    return;

  var aInputArray = xGetElementsByTagName('input', oTree);
  if (aInputArray != null)
  {
    for (var i=0; i<aInputArray.length; i++) 
    {
      if (aInputArray[i].value == szValue)
      {
        aInputArray[i].checked = true;
        tvCheckBranch(aInputArray[i]);
      } 
    }
  }
}




//-------------------------------------------------------------------
// RECHERCHE COURS d'EAU

// Called by lsgen & zogen
// Note : popup CAN NOT be opened by iframe

function submitCoursEau(code) // Code GENELIN to zoom or Name to search...
{
  if (code == '') {
    alert('Veuillez saisir un code ou un nom de cours d\'eau.');
    return false;
  }

  // Do we have a GENELIN code (case insensitive) ?
  if ( code.search(/^([A-Z]|\-)([0-9]|-){3}([0-9]){3}([0-5]|A|B|C|D|G|H|J|K|L|M|N|P|R|S|T|U|V|W|X|Y|\-)$/gi) != -1 ) 
  {
    // This is a code Genelin, so we zoom on it
    frames['iframemap'].submitAction('LOCATE_COURS_EAU', code);
  }
  else {
    // This is not a code Genelin, so we search for name in a popup
    xWinOpen('../common/courseau.php?s='+code, 400, 600);
  }

  return false;
}
