/** 
* 
* Defines class for a collapsible tree like structure
* 
 *  Copyright (c) 2008 Arlak, LLC. All rights reserved.
* 
*  version $Id$ 
*/ 
 
/*
	For each node
	  Create a div with the node data (id=f_x if it has data underneath it, id=d_x if it is a leaf node)
	  Create an empty invisible div underneath it (id=h_x)

	To add a node under a given node (say y under x)
	  Create the div structure that will display y
	  Set h_x.innerHTML to it

	To remove a node (say y, whose parent is x)
	  se h_x.innerHTML = ""
*/

function $(name){
	return document.getElementById(name);
}

// go through all children and create data for the div for parent of nodeData
// Send root node data

function AKTree(divName)
{
	this.folderClick = null;
	this.folderHTML = "";
	this.divName = divName;
	this.rootNodeId = -1;
	this.imgColl = new Array();
	this.imgOpen = new Array();
}

AKTree.prototype.addRoot = function(nodeData)
{
    this.rootNodeId = nodeData.id;
    this.node = new Array();
	var newNode = new Object;
    newNode.data = nodeData;
    newNode.children = null;
	this.node[this.rootNodeId] = newNode;
    $(this.divName).innerHTML = this.createDivForNode(nodeData);
}

AKTree.prototype.print = function(){
	var s = ""
	for(var i in this.node){
	   s += '[Node ' + i + ']: ' + this.printNodeData(this.node[i].data);
	}
	alert(s);
}

AKTree.prototype.printNodeData = function(nodeData){
	var s='id='+ nodeData.id + ' pid=' + nodeData.pid + ' text=' + nodeData.text + "\n";
	var id = nodeData.id;
	if(this.node[id].children != null){
		s += "    Children: ";
		for(var i = 0; i< this.node[id].children.length; i++){
			s += this.node[id].children[i] + ',';
		}
		s += "\n";
	}else{
	    s += "    No Children\n";
	}		
	return s;
}
// create data for the current node (and children)
AKTree.prototype.createDivForNode = function(nodeData){
	var imgWidth = (nodeData.indent+1)*16;
    var s = '\
	<div id="f_{nodeid}" class="nodeDiv"><table cellpadding="0" cellspacing="0"><tr>{frontImgs}<td width="16px">{folderHTML}</td>\
	<td class="akNodeText">{text}</td></tr></table> </div>';	
    var id = nodeData.id;
	var childS = '';
	if(this.node[id].children != null){
		for(var i=0;i<this.node[id].children.length; i++){
			childId = this.node[id].children[i];
			childS += this.createDivForNode(this.node[childId].data);
		}

		secondDivString =  '<div id="h_{nodeid}" style="visibility:inherit;display:inline;padding:0px;">' + childS + '</div>';

	}else{

	    secondDivString = '<div id="h_{nodeid}" style="visibility:inherit;padding:0px;"></div>';

	}
	var folderHTML = "";
	if(nodeData.isFolder){ 
		if(nodeData.lastNode == 1) var img = '/images/AKTree/plastnode.gif'; else var img = '/images/AKTree/pnode.gif';
		folderHTML = this.folderHTML.replace(/{nodeid}/g, id);
		folderHTML = folderHTML.replace(/{image}/g, img);	
		folderHTML = folderHTML.replace(/{id}/g, 'img_'+nodeData.id);	
	}else{ //alert(nodeData.last);
		if(nodeData.lastNode == 1) var img = '/images/AKTree/lastnode.gif'; else var img = '/images/AKTree/node.gif';
		folderHTML = this.folderHTML.replace(/{image}/g, img);
	}
	var frontImg = '';
	if(nodeData.indent > 0){
		 //alert(nodeData.indent);
		var count = nodeData.indent*1; 
		var siblings = this.getTotalSiblings(nodeData.id);
		//alert(nodeData.indent+'-----'+nodeData.text);
		for(var k=0;k<count-1;k++){		    
	//frontImg = frontImg+'<img height="100%" src="/images/AKTree/vertline.gif"/>';			
			if(k == 1){
				var parentNode = this.getNode(nodeData.pid);
				if(nodeData.indent == 3){
					var parentPNode = this.getNode(parentNode.data.pid);
					if(parentPNode.data.lastNode == 1){
						frontImg = frontImg+'<td class="blankTD" width="16px"></td>';
					}else{
						frontImg = frontImg+'<td class="vertlineTD" width="16px"></td>';
					}
				}else{
					frontImg = frontImg+'<td class="vertlineTD" width="16px"></td>';	
				}
			}else{
				frontImg = frontImg+'<td class="vertlineTD" width="16px"></td>';	
			}

		}
		if(siblings == 1 && count > 1){
			//frontImg = frontImg+'<img height="100%" src="/images/AKTree/blank.gif"/>';
			frontImg = frontImg+'<td class="blankTD" width="16px"></td>';

		}else{
			//frontImg = frontImg+'<img height="100%" src="/images/AKTree/vertline.gif"/>';
			var parentNode = this.getNode(nodeData.pid);			
			if(parentNode.data.lastNode == 1){					
				frontImg = frontImg+'<td class="blankTD" width="16px"></td>';
			}else{				
				frontImg = frontImg+'<td class="vertlineTD" width="16px"></td>';
			}
		}
		//folderHTML = frontImg+folderHTML; nodeData.lastNode == 1 && 
		
	}
	s = s.replace(/{frontImgs}/g, frontImg);
   return (s+secondDivString)
	.replace(/{nodeid}/g, id)
	.replace(/{indent}/g, nodeData.indent)
	.replace(/{folderHTML}/g, folderHTML)
	.replace(/{text}/g, nodeData.text);
    
}
AKTree.prototype.createDivForParentNode = function(nodeData){
    var s = '';
	var pid=nodeData.pid;
    for(i=0;i<this.node[pid].children.length; i++){
		child = this.node[pid].children[i];
		s += this.createDivForNode(this.node[child].data);
	}
	return s;	
}
AKTree.prototype.collapseNode = function(id)
{
	
	this.node[id].isCollapsed = true;
//	$("h_"+id).style.visibility='hidden';
	$("h_"+id).style.display='none'; 
	$("h_"+id).style.height="0px";
	
	var curNode = tree.getNode(id);  
	if(id > 0 && curNode.data.isFolder){
		if(curNode.data.lastNode == 1) var img = '/images/AKTree/plastnode.gif'; else var img = '/images/AKTree/pnode.gif';
		var name = 'img_'+id;	
		$(name).src = img;	
	}
}

AKTree.prototype.expandNode = function(id)
{
	this.node[id].isCollapsed=false;
//	$("h_"+id).style.visibility='inherit';
	$("h_"+id).style.display='inline';
	$("h_"+id).style.height="auto";
	
	var curNode = tree.getNode(id); 
	if(id > 0 && curNode.data.isFolder){ 
		if(curNode.data.lastNode == 1) var img = '/images/AKTree/mlastnode.gif'; else var img = '/images/AKTree/mnode.gif';
		var name = 'img_'+id;	
		$(name).src = img;
	}
	
}
AKTree.prototype.addNodeToData = function(nodeData)
{
	var pid = nodeData.pid; 
	
	if(this.node[pid].children == null){
		// create new children array
		this.node[pid].children = new Array();
	}
	var index = this.node[pid].children.length;
	var child = new Object;
	child.data = nodeData;
    child.children = null;
	child.indent = this.indent + this.node[pid].indent;
	this.node[nodeData.id] = child;
	this.node[pid].children[index] = nodeData.id;
}

// add to parents dom
AKTree.prototype.addNodeToDom = function (nodeData)
{
   var s = this.createDivForParentNode(nodeData);
   $("h_"+nodeData.pid).innerHTML = s;
}
AKTree.prototype.addNode = function(nodeData)
{
   if(this.rootNodeId == -1){ 
	this.addRoot(nodeData);
	}
	else{  
	   this.addNodeToData(nodeData); // add node to data to appropriate pid
	   this.addNodeToDom(nodeData);  // add node to dom at appropriate pid
	 }
}

function AKTreeNode()
{
    this.id = -1;
    this.pid = -1;
    this.text ="";
	this.isFolder=false;
	this.indent="0px";
	this.hasLocalData = true;
	this.isCollapsed = false;
	this.lastNode = 0;
}
AKTree.prototype.isNodeCollapsed = function(id)
{
	if(this.node[id].isCollapsed){
		return true;
	}else{
		return false;
	}
}
AKTree.prototype.getNode = function(id)
{ 
	return this.node[id];
}
AKTree.prototype.getTotalSiblings = function(id)
{ 	var nodes = this.node;
	var total = nodes.length;
	var curPid = nodes[id].data.pid; 
	var count = 0;
	
	for(var n=1;n<total;n++){
		if(typeof(nodes[n]) != 'undefined'){ 
			if(curPid == nodes[n].data.pid){
				count = count + 1;
			}			
		}
	}	
	
	return count;
}
AKTree.prototype.getAjaxData = function (cid){ 
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        	alert("Your browser does not support AJAX!");
        	return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {   		
			var txt = xmlHttp.responseText; //alert(txt);
			var cats = eval('(' + txt + ')');	
			for(var k = 0;k<cats.length;k++)
			{ 	//alert(cats[k]);
				addRootFirstChild(cats[k],2);
			}	
			for(var k = 0;k<cats.length;k++)
			{ 
				if(cats[k]['level'] == 0 && cats[k]['arr'] == 1){ 
					tree.collapseNode(cats[k]['id']);
				}
			}

        }
      }
    xmlHttp.open("GET","/admin/fivewords/updateCategoryTreeAjax?cid="+cid,true);
    xmlHttp.send(null);
 }