var totalLevel;
var seqID = 0;
var lastClicked = 1;
newAction = new treeNode(null,false,null,null,null,null);
newAction.id = null;

function treeNode(strDesc,JelPage,PipLink,PipOrJour,PipUrl,parentNode)
{
	this.id = seqID++;
	this.desc = strDesc;
	this.JelPage = JelPage;
	this.PipLink = PipLink;
	this.PipOrJour = PipOrJour;
	this.PipUrl = PipUrl;
	if (parent.OpenAll == "True"){
	    this.isOpened = true;
	 }else{
	    this.isOpened = false;
	 }

	this.isLastNode = true;
	this.parentNode = parentNode;
	this.childNode = new String();
	this.addChild = function addChild(strDesc,JelPage,PipLink,PipOrJour,PipUrl) { if(this.childNode=="") this.childNode = new Array(); else this.childNode[this.childNode.length-1].isLastNode = false; this.childNode[this.childNode.length] = new treeNode(strDesc,JelPage,PipLink,PipOrJour,PipUrl,this); }
}


function getTotalLevels(rootNode,currentLevel,isLastNode)
{
	var i;
	if(rootNode!=null) 
	{
		if(newAction.id==rootNode.id) { rootNode.isOpened=newAction.isOpened; lastClicked=rootNode.id;}
		if(currentLevel > totalLevel) totalLevel=currentLevel;
		for(i=0;i<rootNode.childNode.length && rootNode.isOpened;i++)
		{	
			getTotalLevels(rootNode.childNode[i],currentLevel+1,i==rootNode.childNode.length-1);
		}
	}
}	

function drawTree(doc,rootNode,currentLevel,isLastNode)
{
	var i;
	if(rootNode!=null) 
	{
		addFolderNode(doc,rootNode,currentLevel,isLastNode);
		for(i=0;i<rootNode.childNode.length && rootNode.isOpened;i++)
		{	
			drawTree(doc,rootNode.childNode[i],currentLevel+1,i==rootNode.childNode.length-1);
		}
	}
}	

function browseFolder(nodeID,action)
{
	newAction.id = nodeID;
	newAction.isOpened = action;
}

function addFolderNode(doc,node,currentLevel,isLastNode)
{
	thisLevel = totalLevel-currentLevel;
	doc.write("\<TR VALIGN=MIDDLE\>\n");
	str = "";
	tempNode = node;
	i = currentLevel;
	while(tempNode.parentNode!=null && tempNode.parentNode!='undefined')
	{
		if(i < currentLevel)
		{
		if(!tempNode.isLastNode)
			str = "\t\<TD width=20 ALIGN=LEFT\>\<IMG VSPACE=0 HSPACE=0 BORDER=0 SRC='images/imgI.gif'\>\</TD\>\n" + str;
		else
			str = "\t\<TD width=20 ALIGN=LEFT\>&nbsp;\</TD\>\n" + str;
		}
		tempNode = tempNode.parentNode;
		i--;
	}
	
	strTopTenImgAll = "\<IMG BORDER=0 ALIGN=ABSMIDDLE SRC=\"images/recent_Top_Ten.gif\"\>";
	//"\<IMG BORDER=0 ALIGN=ABSMIDDLE SRC=\"images/AllTIme_top_ten.gif\"\>";
    strTopTenImg = "\<IMG BORDER=0 ALIGN=ABSMIDDLE SRC=\"images/alltime_Top_Ten.gif\"\>";
	
	doc.write(str);
	iconConnector = node.isLastNode ? "images/imgL.gif" : "images/imgI-.gif"; 
	iconName = (node.childNode=="" ? (node.JelPage!=null ? "images/imgLeafLinkNode.gif" : "images/imgLeafNode.gif") : (!node.isOpened ? "images/imgFolder0.gif" : "images/imgFolder1.gif"));
	if(currentLevel >0)
		doc.write("\t\<TD width=20 ALIGN=LEFT\>\<IMG VSPACE=0 HSPACE=0 BORDER=0 SRC='" + iconConnector + "'\>\</TD\>\n");

	strStartTD = "\t<TD NoWRAP width=100% COLSPAN=" + thisLevel + " ALIGN=LEFT>";
	strIconTag = "<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='" + iconName + "'\>\</a\>\<font class=\"MajLink\"\>";
	 
	strEndTD = "</a></TD\>\n";
	onOpenFolder = "onClick='parent.browseFolder(" + node.id + ",true);parent.initializeTree(parent.treeFrame.document);'"
	onCloseFolder = "onClick='parent.browseFolder(" + node.id + ",false);parent.initializeTree(parent.treeFrame.document);'"
	
	//check what font we should use
	var strClass = "";
	/*
	if (node.PipOrJour == "MajNet"){
	    if (node.id==lastClicked){
	       strClass = "MajLinkSel";
		}else{
		   strClass = "MajLink";
		}
		  node.JelPage = null; // set the JelPage to null for major networks
	 }else 
	 */
	 if(node.PipOrJour == "Pub"){
	    if (node.id==lastClicked){
	       strClass = "MinLinkSel";  
		}else{
		   strClass = "MinLink";  
		}  
	 }else{
	    if (node.id==lastClicked){
	       strClass = "JourLinkSel";  
		}else{
		   strClass = "JourLink";  
		}
	 }
	
if (node.JelPage==null){
	   strLinkTag = "<a href=\"javascript://\"";
	   if(node.childNode!="")
		strLinkTag += node.isOpened ? onCloseFolder : onOpenFolder;
		strLinkTag += " class=\"TreeLinks\" "  + (node.id==lastClicked ? "name=\"txtgoto\"" : "") + ">";
		doc.write(strStartTD + strLinkTag + strIconTag + node.desc + "\</a\></TD\>\n"  + strEndTD);
		doc.write("\</TR\>\n");
		
	  }else{ // we have an hr link
	   strLinkTag = "<a href=\"javascript://\"";
	   if(node.childNode!="")
		strLinkTag += node.isOpened ? onCloseFolder : onOpenFolder;
		strLinkTag += " class=\"TreeLinks\" "  + (node.id==lastClicked ? "name=\"txtgoto\"" : "") + ">";
		if (node.PipLink=="Null"){
		  strIconTag = "<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='" + iconName + "'\>\</a\>\<a href=\"" + node.JelPage + " \" class=\"" + strClass + "\"" + " target=\"_blank\"\>";
		  strEndTD =  "\</a\>";
		  infoLink = "";
		  
		}else{
		  strIconTag = "\<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='" + iconName + "'\>\</a\>\<a href=\"" + node.JelPage + " \" class=\"" + strClass + "\"" + " target=\"_blank\"\>";
	      
		  if (node.PipLink != "null"){
		      strEndTD =  "\</a\><a href=\"javascript:void(0);\" onclick=\"window.open('" + node.PipLink + "','Pip_Block','toolbar=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=550');\"" + " class=\"RealLink\" " + "\>\<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='images/info.gif'></a>";		  		  
          }else{
		      strEndTD =  "\</a\>";		  		  
		  }
		  //strPipLink = node.PipLink.substr( node.PipLink.lastIndexOf("/")  + 1, node.PipLink.lastIndexOf(".") - node.PipLink.lastIndexOf("/") - 1)
          infoLink = "\<a href=\"" + node.PipUrl + "\" target=\"_blank\"\>\<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='images/www.gif'\>\</a\>"
		  		  
		           //infoLink = "\<a href=\"javascript:void(0);\" onclick=\"window.open('" + node.PipUrl + ",'Pip_Block','toolbar=no,menubar=no,scrollbars=yes,resizable=no,width=680,height=500');\" target=\"_blank\"\>\<IMG VSPACE=0 HSPACE=5 BORDER=0 ALIGN=ABSMIDDLE SRC='images/www.gif'\>\</a\>"
		  
		}        	
		
		doc.write(strStartTD + strLinkTag + strIconTag + node.desc + strEndTD + " " + infoLink );
//		doc.write(strStartTD + strLinkTag + strIconTagTT + node.desc + strEndTDTT);
		doc.write("\</TR\>\n");
	  }


}


