//---------------------------------------------------------------
// GESTIONE ALBERATURA DELLA TABLE OF CONTENTS
//---------------------------------------------------------------


//-----------------
// RESOURCE
//-----------------

function RESOURCE(name) {
  this.name = name;
}


//-----------------
// TOC
//-----------------

function TOC(name) {
  this.name = name;
  this.groups = new Array();
  this.isInited = false;
  this.init = TOC_init;
  this.refresh = TOC_refresh;
  this.write = TOC_write;
  this.writeHelp = TOC_writeHelp;
  this.addGroup = TOC_addGroup;
  this.groupToggleOpened = TOC_groupToggleOpened;
  this.groupSetVisible = TOC_groupSetVisible;
  this.layerToggleVisible = TOC_layerToggleVisible;
  this.layerSetActive = TOC_layerSetActive;
}

function TOC_init() {	
	
  for (var g=0; g<this.groups.length; g++) {
    for (var l=0; l<this.groups[g].layers.length; l++) {
      var lyr = this.groups[g].layers[l];
      if (lyr.index < 0) {
        lyr.index = getLayerIndexByName(lyr.name);
      }
    }
  }
  this.isInited = true;
}

function getLayerIndexByName(name) {
  for (var i=0; i<LayerName.length; i++) {
    if (LayerName[i] == name)
      return i;
  }
  if (debugOn > 0)
    alert("Errore in aimsToc.js getLayerIndexByName().\nNon è possibile acquisire l\'id del layer '" + name + "'.  Verificare come sono definiti i layer.");
  return -1;
}

function TOC_refresh() {
  parent.TOCFrame.document.location.reload();
}

function TOC_write(doc) {
  if (!this.isInited)
    this.init();
  //doc.writeln("<br><br><CENTER><P CLASS='LayerListTitle'>" + tocResource.stringTitle + "<BR><BR>");
 
 //INIZIO - Baco ResizeToc : GeDeONE 1.2.0.
  //doc.writeln("<TABLE WIDTH='100%'  border=0 CELLSPACING='0' CELLPADDING='0'>");
  doc.writeln("<TABLE WIDTH='400'  border=0 CELLSPACING='0' CELLPADDING='0'nowrap>");
 //FINE - Baco ResizeToc : GeDeONE 1.2.0.
  
  for (var g=0; g<this.groups.length; g++) {
    this.groups[g].write(doc);
  }
  doc.writeln("</TABLE><BR></CENTER>");
}

function TOC_writeHelp(doc) {
  doc.writeln("<BR><BR><CENTER>Help:<BR><DIV  CLASS='LayerListHelp' ALIGN='left' NOWRAP>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconClosed + "'> A closed group, click to open.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconOpened + "'> An open group, click to close.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconChild + "'> A layer contained within a group.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconHidden + "'> A hidden group/layer, click to make visible.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconVisible + "'> A visible group/layer, click to hide.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconVisscale + "'> A visible layer, but not at this scale.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconTristate + "'> A partially visible group, click to make visible.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconInactive + "'> An inactive layer, click to make active.<BR>");
  doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconActive + "'> The active layer.<BR>");
  if (displayLayerInfoButton)
    doc.writeln("<IMG SRC='" + tocResource.iconPath + tocResource.iconInfo + "'> Display information about a layer.<BR>");
  doc.writeln("</DIV></CENTER>");
}

function TOC_addGroup(group) {
  group.parent = this;
  group.index = this.groups.length;
  this.groups[group.index] = group;
  return group;
}

function TOC_groupToggleOpened(index) {
  this.groups[index].opened = !this.groups[index].opened;
  this.refresh();
}

function TOC_groupSetVisible(index,value) {
  for (var l=0; l<this.groups[index].layers.length; l++)
    LayerVisible[this.groups[index].layers[l].index] = value;
  this.refresh();
}

function TOC_layerToggleVisible(index) {
  LayerVisible[index] = 1 - LayerVisible[index];
  this.refresh();
}

function TOC_layerSetActive(index) {
  setActiveLayer(index);

  //INIZIO LAYER QUERY: laq
  //Vengono eliminati gli "oggetti evidenziati" presenti
  //in aimsSelect quando viene cambiato nella TOC il layer
  //attivo. Non viene fatto quando si effettua la ricerca
  //dell'ID passato via URL con GRP all'interno dei layer
  if (laq_csi_useZoomGRP == false)
  	{
  top.MapFrame.highlightedOne = "";
  	}
  //FINE LAYER QUERY
  
  var isOk = checkHyperLinkLayer(index);
  if (toolMode==15) {
    if (!isOk) {
      currentHyperLinkLayer="";
      currentHyperLinkField="";
      currentHyperLinkPrefix="";
      currentHyperLinkSuffix="";
      alert("This layer does not have any Hyperlinks.");
    }
  }

  this.refresh();
}


//-----------------
// GROUP
//-----------------

function GROUP(name,opened) {
  this.parent = null;
  this.index = -1;
  this.name = name;
  this.opened = opened;
  this.layers = new Array();
  this.write = GROUP_write;
  this.addLayer = GROUP_addLayer;
}

function GROUP_write(doc) {
  if (this.layers.length > 1) {
    doc.write("<TR HEIGHT='" + tocResource.imageHeight + "'>");
    
    if (this.opened)
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconOpened + "' onclick='t.toc.groupToggleOpened(" + this.index + ");'></TD>");
    else
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconClosed + "' onclick='t.toc.groupToggleOpened(" + this.index + ");'></TD>");
    
    var count = 0;
    for (var l=0; l<this.layers.length; l++)
      if (LayerVisible[this.layers[l].index])
        count = count + 1;
    
    if (count == 0)
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconHidden + "' onclick='t.toc.groupSetVisible(" + this.index + ",1);'></TD>");
    else if (count == this.layers.length)
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconVisible + "' onclick='t.toc.groupSetVisible(" + this.index + ",0);'></TD>");
    else
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconTristate + "' onclick='t.toc.groupSetVisible(" + this.index + ",1);'></TD>");

    doc.write("<TD class=toc_title COLSPAN='3'>" + this.name + "  ("+count+"/"+this.layers.length+")</TD>");
    
    doc.writeln("</TR>");
  }
  if ((this.opened) || (this.layers.length==1))
    for (var l=0; l<this.layers.length; l++){
      var ultimo = false;
      if(l==(this.layers.length - 1) && l>0)
         ultimo=true;
      this.layers[l].write(doc,ultimo);
    }

  if (tocResource.groupSpacing > 0) {
    doc.write("<TR HEIGHT='" + tocResource.groupSpacing + "'><TD>");
    doc.write("<IMG SRC='" + tocResource.iconPath + tocResource.iconPixel + "' WIDTH='" + tocResource.groupSpacing + "'>");
    doc.writeln("</TD></TR>");
  }
}


function GROUP_addLayer(layer) {
  layer.parent = this;
  this.layers[this.layers.length] = layer;
  return layer;
}


//-----------------
// LAYER
//-----------------

function LAYER(name) {
  this.parent = null;
  this.name = name;
  this.index = -1;
  this.write = LAYER_write;
}

function LAYER_write(doc,ultimo_layer) {
  doc.write("<TR HEIGHT='" + tocResource.imageHeight + "'>");

  if (this.parent.layers.length==1) {
    //doc.write("<TD>&nbsp;</TD>");    
    doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconOrphan + "'></TD>");
  } else {
   if(ultimo_layer)
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconChildLast + "'></TD>");
   else 
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconChild + "'></TD>");
  }
  if (LayerVisible[this.index])
    if ((mapScaleFactor>=LayerMinScale[this.index]) && (mapScaleFactor<=LayerMaxScale[this.index]))
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconVisible + "' onclick='t.toc.layerToggleVisible(" + this.index + ");'></TD>");
    else
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconVisscale + "' onclick='t.toc.layerToggleVisible(" + this.index + ");'></TD>");
  else
    doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconHidden + "' onclick='t.toc.layerToggleVisible(" + this.index + ");'></TD>");

  if (LayerIsFeature[this.index])
    if (ActiveLayerIndex == this.index)
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconActive + "'></TD>");
    else
      doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconInactive + "' onclick='t.toc.layerSetActive(" + this.index + ");'></TD>");
  else
    doc.write("<TD WIDTH='" + tocResource.imageWidth + "'>&nbsp;</TD>");
  
//INIZIO - Baco ResizeToc : GeDeONE 1.2.0.
  //doc.write("<TD class=toc>" + this.name + "</TD>");
  doc.write("<TD align='left' width='336' class=toc >" + this.name + "</TD>");
//FINE - Baco ResizeToc : GeDeONE 1.2.0.

  if (displayLayerInfoButton)
    doc.write("<TD WIDTH='" + tocResource.imageWidth + "'><IMG SRC='" + tocResource.iconPath + tocResource.iconInfo + "' onClick='showLayerInfo(" + this.index + ")'></TD>");
  else
    doc.write("<TD WIDTH='" + tocResource.imageWidth + "'>&nbsp;</TD>");

  doc.writeln("</TR>");
}

//-----------------
// eof
//-----------------
