//  Copyright © 1999-2002 Red Hen Systems, Inc.
//              All Rights Reserved

function escapeApostrophe(theString) {
  var result = theString;
  var index = 0;
  var length = theString.length;
  var ch
  result = "";
  while (index < length) {
    ch = theString.charAt(index);
    if (ch == "'")
      result = result + "\\'";
    else
      result = result + escape(ch);
    index++;
  }
  return result;
}

function node(level, name, refer, status, allRefer, attachRefer, documentRefer, audioRefer, videoRefer, imageRefer, expanded, xPos, yPos, layerName, stockImage) {
  this.level = level;
  this.name = name;
  this.refer  = refer;
  this.status = status;
  this.statusEscaped = escapeApostrophe(status);
  this.allRefer = allRefer;
  this.attachRefer = attachRefer;
  this.documentRefer = documentRefer;
  this.audioRefer = audioRefer;
  this.videoRefer = videoRefer;
  this.imageRefer = imageRefer;
  this.expanded = expanded;
  this.xPos = xPos;
  this.yPos = yPos;
  this.layerName = layerName;
  this.stockImage = stockImage;
  this.layerNameEscaped = escapeApostrophe(layerName);
  if (window.location.href.indexOf('/') != -1)
    this.path = window.location.href.substring(0,window.location.href.lastIndexOf('/'));
  else
    this.path = window.location.href.substring(0,window.location.href.lastIndexOf('\\'));
  this.index = 0;
  this.Item = new Object();
  this.AddItem = AddItem;
}

function AddItem(newItem) {
  this.Item[this.index++] = newItem;
  node.path = this.path + '/' + newItem.name;
}

function changeNodeState(theNode) {
  theNode.expanded = !theNode.expanded;
  window.frameBrowser.location.href = window.frameBrowser.location.href;
}

function buildBrowser(theItem) {
  var nodeAnchor, nodeImage, result;
  if ((theItem.level == 'Root') && (theItem.name == 'View Object Layers')) {
    nodeAnchor = '<a href="javascript:parent.changeNodeState(parent.' + theItem.refer + ')"';
    nodeAnchor += ' onmouseover="window.status=\'' + theItem.statusEscaped + '\'; return true"';
    nodeAnchor += ' onmouseout="window.status=\'\'; return true">';
    if (theItem.expanded)
      nodeImage = '<img src="scripts/images/minus.gif" border=0>';
    else
      nodeImage = '<img src="scripts/images/plus.gif" border=0>';
    result = '<table border="0"><tr>' +
             '<td valign="top">' + nodeAnchor + nodeImage + '</td>' +
             '<td>' + nodeAnchor + '</a>' + 'View Objects';
  }
  else if (theItem.level == 'Root')
    result = '';
  else if (theItem.level == 'ViewObjectLayer')
    result = '';
  else if (theItem.level == 'FeatureLayer') {
    nodeAnchor = '<a href="javascript:parent.changeNodeState(parent.' + theItem.refer + ')"';
    nodeAnchor += ' onmouseover="window.status=\'' + theItem.statusEscaped + '\'; return true"';
    nodeAnchor += ' onmouseout="window.status=\'\'; return true">';
    if (theItem.expanded)
      nodeImage = '<img src="scripts/images/minus.gif" border=0>';
    else
      nodeImage = '<img src="scripts/images/plus.gif" border=0>';
    result = '<table border="0"><tr>' +
             '<td valign="top">' + nodeAnchor + nodeImage + '</td>' +
             '<td>' + nodeAnchor + '</a>' + theItem.name;
  }
  else if (theItem.level == 'ViewObject') {
    nodeAnchor = '<a href="javascript:parent.rememberLink(' + "'" + theItem.refer + "'" + ');"'
    nodeAnchor += ' onmouseover="parent.frameMap.HighLightFeature(' + "'" + eval('theItem.xPos') + "'" + ',' + "'" + eval('theItem.yPos') + "'" + '); window.status=\'' + theItem.statusEscaped + '\'; return true"';
    nodeAnchor += ' onmouseout="parent.frameMap.HighLightFeature(-100, -100); window.status=\'\'; return true">';
    result = '<table border="0"><td>';
    result += nodeAnchor + theItem.name + '</a></td>';
  }
  else {
    var thumbImageName;
    if (((parent.linkToId == 'audio') && (theItem.audioRefer == '')) || 
        ((parent.linkToId == 'all') && (theItem.allRefer == '')) ||
        ((parent.linkToId == 'attach') && (theItem.attachRefer == '')) ||
        ((parent.linkToId == 'document') && (theItem.documentRefer == '')) ||
        ((parent.linkToId == 'image') && (theItem.imageRefer == '')) ||
        ((parent.linkToId == 'video') && (theItem.videoRefer == '')))
      if (parent.linkToId == 'attach')
        nodeAnchor = '<a href="javascript:window.alert(\'No attachment data associated with this feature.\');"';
      else
        nodeAnchor = '<a href="javascript:window.alert(\'No ' + parent.linkToId + ' data associated with this feature.\');"';
    else if ((parent.linkToId == 'attach') && (theItem.attachRefer.match('list')))
      nodeAnchor = '<a href="javascript:parent.showList(' + "'" + theItem.attachRefer + "'" + ', ' + "'" + theItem.xPos + "'" + ', ' + "'" + theItem.yPos + "'" + ')"';
    else if ((parent.linkToId == 'all') && (theItem.allRefer.match('list')))
      nodeAnchor = '<a href="javascript:parent.showList(' + "'" + theItem.allRefer + "'" + ', ' + "'" + theItem.xPos + "'" + ', ' + "'" + theItem.yPos + "'" + ')"';
    else
      nodeAnchor = '<a href="javascript:parent.openWindow(' + "'" + eval('theItem.layerNameEscaped') + "/" + parent.linkToId + "/" + eval('theItem.'+parent.linkToId+'Refer') + "'" + ',' + "'" + theItem.statusEscaped + "'" + ')"';
    nodeAnchor += ' onmouseover="parent.frameMap.HighLightFeature(' + "'" + eval('theItem.xPos') + "'" + ',' + "'" + eval('theItem.yPos') + "'" + '); window.status=\'' + theItem.statusEscaped + '\'; return true"';
    nodeAnchor += ' onmouseout="parent.frameMap.HighLightFeature(-100, -100); window.status=\'\'; return true">';
    result = '<table border="0"><tr>';
    if (theItem.stockImage != '')
      thumbImageName = theItem.path + '/' + theItem.layerName + '/thumb/' + theItem.stockImage;
    else
    {
      var aThumbFile = theItem.imageRefer.split(".");
      var sThumbFile = "";
      for(var loop=0; loop < aThumbFile.length - 1; loop++)
      {
         sThumbFile = sThumbFile + aThumbFile[loop];
         if (loop < aThumbFile.length - 2)
            sThumbFile = sThumbFile + ".";
      }
      sThumbFile = sThumbFile + "small.jpg";
      thumbImageName = theItem.path + '/' + theItem.layerName + '/thumb/' + sThumbFile;
    }
    nodeImage = '<img src="' + eval('thumbImageName') + '" border=1 align=center>';
    result += '<td>' + nodeAnchor + nodeImage + '<br>';
    result += theItem.name + '</a></td>';
  }
  if (theItem.expanded)
    for(var i=0; i<theItem.index; i++)
      result += buildBrowser(theItem.Item[i]);
  if (theItem.level != 'ViewObjectLayer')
    result += '</td></tr></table>';
  return result;
}
