// treeview with dynamic nodes

var lastNode = "";
var nodeBuilding = false;
var resetTimer = -1;
           
function toggleNode(nodeId) {
  if (document.getElementById("td_"+nodeId).innerHTML=="") buildNode(nodeId); else toggle(nodeId);
}   

function toggle(nodeId) { 
  var nodeDiv = document.getElementById("td_"+nodeId);
  var nodeButton = document.getElementById("tb_"+nodeId);
  var nodeContainer = document.getElementById("tc_"+nodeId);
  nodeDiv.className = (nodeDiv.className == "expanded") ? "collapsed" : "expanded";
  nodeContainer.className = (nodeContainer.className == "nodecontainerconnect") ? "nodecontainer" : "nodecontainerconnect";
  nodeButton.src = (nodeDiv.className == "collapsed") ? view_folder + "/images/buttons/tree_plus.gif" : view_folder + "/images/buttons/tree_min.gif";
}   
  
function buildNode(nodeId) {
  if (nodeBuilding) return;
  var nodeDiv = document.getElementById("td_"+nodeId);
  if (lastNode=="" && nodeDiv) {
    if (nodeDiv.innerHTML=="") {
      nodeDiv.innerHTML = '<marquee direction="right" loop="-1" style="width:64px; line-height:14px;">...</marquee>';
      toggle(nodeId);
      lastNode = nodeId;
      nodeBuilding = true;
      callToServer(asp_link + "?alias=" + site_alias + "&id=" + page_id + "&cid=" + nodeId + "&treefragment=true&browsemode=tree");
      resetTimer = setTimeout(resetTreeUpdate, 15*1000)
    } 
    else {
      toggle(nodeId);
    }  
  }  
}   

function updateTree() {    
  clearTimeout(resetTimer);
  var nodeDiv = document.getElementById("td_"+lastNode);
  treeFrameDocument = getFrameDoc();
  if (lastNode!="" && nodeDiv && treeFrameDocument.getElementById("subtree")) {
    nodeDiv.innerHTML = treeFrameDocument.getElementById("subtree").innerHTML;
  }
  lastNode = "";
  nodeBuilding = false; 
}   

function resetTreeUpdate() {
  lastNode = "";
  nodeBuilding = false;
}

function showNode(pageId, nodeId) {                          
  /*
  if (lastNode=="") {
    document.all("divNode").style.display = "none";               
    nodeframe.location.replace("page.asp?alias=" + site_alias + "&id=" + pageId + "&referrerpage=" + page_id + "&node=" + nodeId + "&nodeonly=true");
    lastNode = nodeId;
    document.all("divNode").style.top = event.clientY + body.scrollTop*1;
  } 
  */
  var _url = asp_link + "?alias=" + site_alias + "&id=" + pageId + "&cid=" + nodeId;
  document.location = _url;
}   



function updateNode() {    
  if (lastNode!="" && nodeframe.document.all("nodepage")) {
    document.all("divNode").innerHTML = nodeframe.document.all("nodepage").innerHTML;  
  }
  lastNode="";                             
  //document.all("divNode").style.top = body.scrollTop*1 + 200;
  document.all("divNode").style.left = (body.clientWidth / 2) + 150;
  document.all("divNode").style.display = "block";
}   
  
  
var IFrameObj; // our IFrame object

function getFrameDoc() {
  if (IFrameObj.contentDocument) {
    // For NS6
    return IFrameObj.contentDocument; 
  } 
  else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    return IFrameObj.contentWindow.document;
  } 
  else if (IFrameObj.document) {
    // For IE5
    return IFrameObj.document;
  } 
  else {
    return null;
  }
}

function callToServer(URL) {
  if (!document.createElement) {return true};
  var IFrameDoc; // our IFrame document
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','RSIFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['RSIFrame'];
      }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\&lt;iframe id="RSIFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"&gt;&lt;\/iframe&gt;';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout("callToServer('" + URL + "')",10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
  
  IFrameDoc.location.replace(URL);
  return false;
}