var nodes	= new Array();
var openNodes	= new Array();
var icons	= new Array(6);

var nameFile = "";

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "img/folder.gif";      
	icons[1] = new Image();
	icons[1].src = "img/folder.gif";      

	icons[2] = new Image();
	icons[2].src = "img/folderopen.gif";  
	icons[3] = new Image();
	icons[3].src = "img/folderopen.gif";  
}

// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}

// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++){
	  if (openNodes[i]==node){ 
            return true;
          }
        }
	return false;
}

// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}

// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}

// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}

function setOpenNodesName(nameFile) {
	for (i=0; i<nodes.length; i++) {
	  var nodeValues = nodes[i].split("|");
          if (nameFile == nodeValues[3]){
	    openNodes.push(nodeValues[0]);
	    setOpenNodes(nodeValues[1]);
          }
	} 
}

// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);
                        var bname_file = 0;

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
			  if (recursedNodes[g] == 1) 
                            document.write("<img src=\"img/line.gif\" align=\"absbottom\" alt=\"\" />");
			  else  
                            document.write("<img src=\"img/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) 
                          recursedNodes.push(0);
			else 
                          recursedNodes.push(1);

			// Write out join icons
			if (hcn) {
			  if (ls) {
			    document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
			    if (ino) 
                              document.write("folderopen");
			    else 
                              document.write("folder");
			  } 
                          else {
			    document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"img/");
			    if (ino) 
                              document.write("folderopen");
			    else 
                              document.write("folder");
			  }
 			  document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
                          document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0,'" + nodeValues[3] + "');\"><align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a>");
			} 
                        else {
			  if (ls) 
                            document.write("<img src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
			  else 
                            document.write("<img src=\"img/join.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Start link
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			// Write out folder & page icons
			if (!hcn) {
                          document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />");
                          if (nameFile != ""){
                            if (nameFile == nodeValues[3]){
			      bname_file = 1;
                            }  
                          }
                          if(bname_file==1){ 
			    document.write("<font color=\"#99CCFF\">" + nodeValues[2] + "</font>");
                          }else{
			    document.write(nodeValues[2]);
                          }
                        }

			// End link
			document.write("</a><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
			  document.write("<div id=\"div" + nodeValues[0] + "\"");
			  if (!ino) 
                            document.write(" style=\"display: none;\"");
			  document.write(">");
			  addNode(nodeValues[0], recursedNodes);
			  document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

// Opens or closes a node
function oc(node, bottom, a_href) {
	var theDiv = document.getElementById("div" + node);
	var theJoin	= document.getElementById("join" + node);
	//var theIcon = document.getElementById("icon" + node);
	if (theDiv.style.display == 'none') {
		if (bottom==1) 
                  theJoin.src = icons[3].src;
		else 
                  theJoin.src = icons[2].src;
		theDiv.style.display = '';
	} 
        else {
		if (bottom==1) 
                  theJoin.src = icons[1].src;
		else 
                  theJoin.src = icons[0].src;
		theDiv.style.display = 'none';
	}

        if(a_href != null){
          open(a_href,"_self");
        }
}

// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}

function createTree(_nameFile) {
	nodes = Tree;
	
	if (nodes.length > 0) {
                
		preloadIcons();
                var startNode = 1;
		if (_nameFile != null){ 
                  nameFile = _nameFile;
                  setOpenNodesName(nameFile);
                }
	        var nodeValues = nodes[getArrayId(startNode)].split("|");
	        document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		var recursedNodes = new Array();
		addNode(startNode,recursedNodes);
	}
	
}

function spylog() {
                //SpyLog 
                document.write("<script src=\"http://tools.spylog.ru/counter_cv.js\" "); 
                document.write("id=\"spylog_code\" "); 
                document.write("type=\"text/javascript\""); 
                document.write("counter=\"968547\""); 
                document.write("part=\"\" track_links=\"ext\" page_level=\"0\">"); 
                document.write("</script>"); 
                document.write("<noscript>"); 
                document.write("<a href=\"http://u9685.47.spylog.com/cnt?cid=968547&f=3&p=0\" target=\"_blank\">"); 
                //document.write("<img src=\"http://u9685.47.spylog.com/cnt?cid=968547&p=0\""); 
                document.write("<img src=\"http://u9685.47.spylog.com/cnt?cid=968547&p=0\" align=\"absbottom\""); 
                document.write("alt=\"SpyLOG\" border=\"0\" width=\"88\" height=\"31\"></a>"); 
                document.write("</noscript>"); 
                //document.write("<br>");  
                //SpyLog 
}

