/*---------------------------------------------------
// (cc) vTree ( veinara )
//	USAGE:
//---------------------------------------------------

//rootID number or none( set the root later )
	tree = new vTree( @rootID ); 

//adding a node( change Node`s generate function for different visualization of the data )
	tree.addNode( parentID, nodeID, [@arrayOfData] );

//open to ..
	tree.openTo( @nodeID );

//generating the tree
	document.write( tree.generate( ) );

//-------------------------------------------------*/


//---------------------------------------------------
//	NODE
//---------------------------------------------------

	function vNode( parentID, nodeID, nodeData )
	{
		this.parentID 		= parentID;
		this.nodeID 		= nodeID;
		this.nodeChildren 	= [];
		this.nodeData 		= nodeData;
		this.isExpanded		= false;
	}

	vNode.prototype.setData = function( nodeData )
	{
		this.nodeData = nodeData;
	}

	vNode.prototype.addChild = function( nodeID )
	{
		this.nodeChildren.push( nodeID );
	}

	vNode.prototype.generate = function( )
	{
		cssClass = "";
		if( this.isExpanded == true ) {
			cssClass = "active";
		}

		html = "\n"+'<a id="node-'+ this.nodeID +'" href="'+ this.nodeData[1] +'" class="'+ cssClass +'">';
		html += this.nodeData[0];
		html += '</a>';
		return html;		
	}

	vNode.prototype.generateReverse = function( )
	{
		cssClass = "";
		if( this.isExpanded == true ) {
			cssClass = "active";
		}
		html = "\n"+'<a id="node-'+ this.nodeID +'" href="'+ this.nodeData[1] +'" class="'+ cssClass +'">';
		html += this.nodeData[0];
		html += '</a>';
		return html;		
	}
	/*
	vNode.prototype.removeChild( nodeID )
	{
		key = array_search( nodeID, this.nodeChildren );
		if( key ) {
			unset( this.nodeChildren[key] );
		}
	}
	*/
//---------------------------------------------------
//	TREE
//---------------------------------------------------

	function vTree( containerID )
	{
		this.containerID	= containerID;
		this.aNodes			= {};
		this.rootID 		= null;
		this.selectedNode	= null;
		this.content 		= '';
	}

	vTree.prototype.setRoot = function( rootID )
	{
		this.rootID = rootID;
	}

	vTree.prototype.addNode = function( parentID, nodeID, nodeData )
	{
		if( typeof this.aNodes[nodeID] != 'undefined' ) {
			this.aNodes[nodeID].parentID = parentID;
			this.aNodes[nodeID].setData( nodeData );
		} else {
			this.aNodes[nodeID] = new vNode( parentID, nodeID, nodeData );
		}

		if( typeof this.aNodes[parentID] == 'undefined' ) {
			this.aNodes[parentID] = new vNode( null, parentID, [] );
		}
		this.aNodes[parentID].addChild( nodeID );
	}
/*
	vTree.prototype.removeNode = function( nodeID )
	{
		parentID = this.aNodes[nodeID].parentID;
		this.aNodes[parentID].removeChild( nodeID );
		unset( this.aNodes[nodeID] );
	}
*/
	vTree.prototype.getNode = function( nodeID )
	{
		return this.aNodes[nodeID];
	}

	vTree.prototype.generate = function( )
	{
		this.content = '';
		this.generateNode( this.aNodes[this.rootID].nodeChildren );
		document.getElementById( this.containerID ).innerHTML = this.content;
//		return this.content;
	}

	vTree.prototype.generateNode = function( nodes )
	{
		for( curID in nodes )
		{
			nodeID = nodes[curID];
			node = this.getNode( nodeID );
			branchID = "branch-"+ nodeID;
			
			if( node.nodeChildren.length > 0 )
			{
				cssClass = "inactive";
				if( node.isExpanded == true ) {
					cssClass = "active";
				}
				this.content += this.addCollapser( branchID, cssClass );
			}

			this.content += node.generate( );

			if( node.nodeChildren.length > 0 )
			{				
				cssClass = "collapsed";
				if( node.isExpanded == true ) {
					cssClass = "expanded";
				}
				this.content += "\n"+'<ul id="'+ branchID +'" class="'+ cssClass +'"><li>';
				this.generateNode( node.nodeChildren );
				this.content += "</li></ul>\n\n";
			}
		}
	}

	vTree.prototype.addCollapser = function( identifier, cssClass )
	{
		html = '<span class="'+ cssClass +'" identifier="'+ identifier +'" onclick="';
		html += "toggleNode( this )";
		html += '"></span>';
		return html;
	}
	
	vTree.prototype.generateReverse = function( nodeID )
	{
		this.content = '';
		this.generateNodeReverse( nodeID );
		return this.content;
	}

	vTree.prototype.generateNodeReverse = function( nodeID )
	{
		node = this.getNode( nodeID );
		this.content += node.generateReverse( );
		parentID = node.parentID;

		if( typeof parentID != 'undefined' ) {
			this.generateNodeReverse( parentID );
		}
		return html;
	}

	vTree.prototype.openTo = function( nodeID )
	{
		if( nodeID == this.rootID ) {
			return;
		}
		node = this.getNode( nodeID );
		node.isExpanded = true;
		this.openTo( node.parentID );
	}

	vTree.prototype.openAll = function( )
	{
		//to do
	}
//---------------------------------------------------


	function toggleNode( pointer )
	{
//		alert(  pointer.identifier );
		branch = document.getElementById( pointer.getAttribute( "identifier" ) );		
		if( branch.className == 'collapsed' ) {
			branch.className = 'expanded';
			pointer.className = "active";
//			pointer.innerHTML = "\/";
		} else {
			branch.className = 'collapsed';
			pointer.className = 'inactive';
//			pointer.innerHTML = ">";
		}
	}
