function eowTree(treeName)
{
	this.node0i = "<table width=\"&treewidth;\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"tabela\">";
	this.node1i = "<tr id=\"row&rowID;\" height=\"18\"><td class=\"row&nivel;\" onmouseover=\"javascript: this.className='rowHover';\" onmouseout=\"javascript: this.className='row&nivel;';\">";
	this.linkStr = "<a href=\"&url;\" class=\"txtNivel&nivel;\">";
	this.imgStri  = "<img id=\"img&rowID;\" src=\"imagens/bt";
	this.imgStrf  = "Submenu.gif\" width=\"11\" height=\"11\" align=\"left\" style=\"position:absolute;margin-left:5px\" onClick=\"&treeName;.oc('&rowID;')\"/>";
	this.node1f = "</a></td></tr>";
	this.separador = "<tr><td height=\"1\" bgcolor='#c3c2c0'></td></tr>";
	this.node0f = "</table>";
	this.imagemSetinha = "<img src='../UI/Imagens/arrowBlackMini.gif' width='3px' height='5px' border='0' style='margin-right:3px'/>"

    // Arrays for nodes and icons
    this.obj = treeName;
    this.nodes = new Array();
    this.openNodes = new Array();
    this.selectedNode = 0;
    this.initScroll = 0;
    this.altura = 0;
    this.largura = "169px";
    this.intervalo = null;
    this.icons = new Array(2);
};


// Loads all icons that are used in the tree
eowTree.prototype.preloadIcons = function() {
	this.icons[0] = new Image();
	this.icons[0].src = "../UI/imagens/btMaisPatrocinadores.gif";
	this.icons[1] = new Image();
	this.icons[1].src = "../UI/imagens/btMenosPatrocinadores.gif ";	
	this.icons[2] = new Image();
	this.icons[2].src = "../UI/imagens/arrowBlackMini.gif";	
};

// Create the tree
eowTree.prototype.createTree = function(startNode, openNode, treeWidth) {		
	if(treeWidth)
        this.largura = treeWidth;        

	if (this.nodes.length > 0) {
		this.preloadIcons();
		
		// Faz um reset aos openNodes
		var j;
		this.setOpen( 0 );
		
		if (openNode != null){
			this.selectedNode = openNode;
			this.updateOpenNodes( this.selectedNode );
		}
		// Fim do reset aos openNodes'

		if (startNode == null) startNode = 0;
		//if (openNode != 0 || openNode != null) setOpenNodes(openNode);
		
		if (startNode !=0) {
			var nodeValues = this.nodes[this.getArrayId(startNode)].split("|");	
		}			

		document.write(this.node0i.replace("&treewidth;", this.largura));
		
		var recursedNodes = new Array();
		this.addNode(startNode, recursedNodes);
		
		document.write(this.node0f);		
	}
	
	this.updateOpenCloseStatus();	
};


// Adds a new node to the tree
eowTree.prototype.addNode = function(parentNode, recursedNodes) {	
	for (var i = 0; i < this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var hcn	= this.hasChildNode(nodeValues[0]);
			var ino = this.isOpen( nodeValues[0] );
			var nivel = recursedNodes.length;
			var selected = "";
			
			//alert( "nodeID="+nodeValues[0] );
			//alert( "nodeName="+nodeValues[2] );
			//alert("nivel="+nivel);
			
			recursedNodes.push(1);
			
			if ( nodeValues[0] == this.selectedNode ){
				selected = "_selected";
				this.initScroll = this.altura;
			}
			
			//abre td
			document.write( this.node1i.replace("&rowID;", nodeValues[0]+this.obj ).replace("&nivel;", nivel).replace("&nivel;", nivel));			
			
			this.altura += 8;
			
			
			if (hcn)//Processa Imagem
			 {
				document.write( this.imgStri.replace( "&rowID;", nodeValues[0]+this.obj ) );
				
				if (ino){
					document.write("Menos");
				} else {
					document.write("Mais");
				}
				
				document.write( this.imgStrf.replace( "&rowID;", nodeValues[0] ).replace("&treeName;", this.obj) );
			}

			   
			
			document.write( this.linkStr.replace("&url;", nodeValues[3] ).replace("&nivel;", nivel+selected) );
			 if( nivel >= 1 && !hcn)//Coloca imagem com setinha
			        document.write( this.imagemSetinha );
			document.write(nodeValues[2] + this.node1f ); //fecha td
			
			if (nivel == 0){
				document.write( this.separador.replace("&treewidth;", this.largura) );
				this.altura += 1;
			}			

			if (hcn) {
				this.addNode(nodeValues[0], recursedNodes);
			}
			
			recursedNodes.pop();
		}
	}
};

eowTree.prototype.updateOpenCloseStatus = function(){
	//debugOpenNodes();
	var i;
	for (i=0; i<this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		var theRow = this.findObj("row" + nodeValues[0]+this.obj);
		var theIcon = this.findObj("img" + nodeValues[0]+this.obj);
		
		if ( this.hasChildNode(nodeValues[0]) ){
			if ( this.isOpen( nodeValues[0] ) )
				theIcon.src = this.icons[1].src;
			else
				theIcon.src = this.icons[0].src;
		}
		
		if ( this.isOpen( nodeValues[1] ) ){
			theRow.style.display = '';
		} else {
			theRow.style.display = 'none';
		}
	}
	
	//this.showHideArrows();
}

eowTree.prototype.updateOpenNodes = function( endNode ){
	//debugOpenNodes();
	var i;
	for (i=0; i<this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if ( nodeValues[0] == endNode ){
			if ( (nodeValues[1] != 0) && (nodeValues[1] != null) ){
				this.setOpen( nodeValues[1] );
				this.updateOpenNodes( nodeValues[1] );
			}
		}
	}
};

// Opens or closes a node
eowTree.prototype.oc = function(node) {
	//alert( "oc(" + node + ")");
	if ( this.isOpen( node ) )
		this.setClose( node );
	else
		this.setOpen( node );
	
	//setArray( "openNodes", openNodes );
	this.updateOpenCloseStatus();
};

eowTree.prototype.setOpen = function( node ){
//alert( "open(" + node + ")");
	if (! this.isOpen(node) ){
	    //alert( "opening(" + node + ")");
		this.openNodes.push( node );
	}
};

eowTree.prototype.setClose = function( node ){
	for (i=0;i<this.openNodes.length;i++){
		if ( this.openNodes[i] == node){
			this.openNodes[i] = null;
		}
	}
};

eowTree.prototype.isOpen = function( node ){
	//alert( "isOpen("+node+") openNodes.length=" + openNodes.length );
	for (i=0;i<this.openNodes.length;i++){
		//alert( "openNodes["+i+"]=" + openNodes[i] + " == " + node + "?" );
		if ( this.openNodes[i] == node)
			return true;
	}
	return false
};

// Returns the position of a node in the array
eowTree.prototype.getArrayId = function(node) {
	for (i=0; i<this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
};

// Checks if a node has any children
eowTree.prototype.hasChildNode = function(parentNode) {
	for (i=0; i< this.nodes.length; i++) {
		var nodeValues = this.nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
};

eowTree.prototype.getChilds = function(parentNode, recursivo) {
	//alert( "getChilds("+parentNode+")" );
	var result = new Array();
	if ( this.hasChildNode(parentNode) ){
		for (i=0; i< this.nodes.length; i++) {
			var nodeValues = this.nodes[i].split("|");
			if (nodeValues[1] == parentNode){
				if ( this.hasChildNode( nodeValues[0] ) && (nodeValues[0] != parentNode) ){
					var i, childs = this.getChilds( nodeValues[0], true );
					for (k=0;k<childs.length;k++){
						result.push( childs[k] );
					}
				}
				result.push(nodeValues[0]);	
			}
		}
	}
	
	var k, tmp = "";
	for (k=0;k<result.length;k++){
		tmp+="  result("+k+")="+result[k];
	}
	//alert( tmp );
	return result;
};

// 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;
}

eowTree.prototype.findObj = function (theObj, theDoc){
  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = this.findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  return foundObj;
}

/*function init(){
    //alert( "initScroll="+initScroll + " menu.scrollHeight="+findObj('divMenu').scrollHeight );
    findObj('divMenu').scrollTop = initScroll;
	showHideArrows();
}

function showHideArrows(){
    //alert( "showHideArrows()" );
	try{
		var btUp = findObj('btUp');
		var btDw = findObj('btDw');
		var menu = findObj('divMenu');
		//alert( menu.scrollHeight );
		if ( menu.scrollHeight > 300 ){
			btUp.style.display = "";
			btDw.style.display = "";
		} else {
			btUp.style.display = "none";
			btDw.style.display = "none";
		}
	} catch (e){
		txt="Ocorreu o seguinte erro: " + e.description + "\n\n";
  	//alert(txt)
	}
}

function goUp(){
	findObj('divMenu').scrollTop += 10;
}
function goDw(){
	findObj('divMenu').scrollTop -= 10;
}

function debugOpenNodes(){
	alert( "openNodes.length="+openNodes.length);
	for (i=0;i<openNodes.length;i++){
		alert( "openNodes[" + i + "] = " + openNodes[i] );
	}
}*/

