var imgPlus = new Image();
imgPlus.src = IMG_PLUS;
var imgMinus = new Image();
imgMinus.src = IMG_MINUS;

var objLocalTree = null;
var huh=1

var INDENT_WIDTH = 5;

function jsTree() {
    this.root = null;           //the root node of the tree
    this.nodes = new Array;     //array for all nodes in the tree   
    objLocalTree = this;
}

jsTree.prototype.createRoot = function(strText, strURL) {
    this.root = new jsTreeNode(strText, strURL);
    this.root.id = "root";
    this.nodes["root"] = this.root;
    this.root.expanded = true;
    return this.root;
}

jsTree.prototype.buildDOM = function() {
    this.root.addToDOM(document.getElementById("collTree"));
}

jsTree.prototype.toggleExpand = function(strNodeID) {
    var objNode = this.nodes[strNodeID];
	if (objNode.expanded) {
        objNode.collapse();
		AddToCollapseList(strNodeID);
	} else {
        objNode.expand();
		RemoveFromCollapseList(strNodeID);
	}
	//alert(GetCookie("CollapsedList"));
}

function jsTreeNode(strText, strURL) {
    this.icon = strTREEICON;        //the icon to display
    this.text = strText;            //the text to display
    this.url = strURL;              //the URL to link to
    this.target = strTARGET;        //the target for the URL
    this.indent = 0;                //the indent for the node
    this.expanded = false;          //is this node expanded?
    this.childNodes = new Array;    //the collection of child nodes
}

jsTreeNode.prototype.addChild = function (strText, strURL) {
    var objNode = new jsTreeNode(strText, strURL);
    objNode.id = this.id + "_" + this.childNodes.length;
    objNode.indent = this.indent + 1;
    this.childNodes[this.childNodes.length] = objNode;
    objLocalTree.nodes[objNode.id] = objNode;
    this.expanded=true
    return objNode;
}

jsTreeNode.prototype.addToDOM = function (objDOMParent) {
    var hid;
    var strHTMLLink="";
    if(this.url!="") {
       var strHTMLLink = "<a href=\"" + this.url + "\"";	   
       if (this.target)strHTMLLink += " target=\"" + this.target + "\"'>";
    } 
    var objNodeDiv = document.createElement("div");
    if (this.text=="") {hid="style='visibility:hidden;position:absolute;'"} else {hid=""};
    objDOMParent.appendChild(objNodeDiv);
    var d = new jsDocument;
    d.write("<table "+hid+" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style='margin-top:2px;'><tr>");
    if (this.indent > 1) {
        d.write("<td width=\"");
//Oduzeto-Dodato
        if (this.indent > 2) {d.write(this.indent * INDENT_WIDTH+INDENT_WIDTH) } else {d.write(this.indent * INDENT_WIDTH)}
//        d.write(this.indent * INDENT_WIDTH);
//Dovde
       d.write("\"><img src='ccImages/spacer.gif' width='1' height='1' border='0' /></td>");
   }
    huh=this.indent
    if (this.indent = 1) {
    
        d.write("<td width=\"9\">");
        
        if (this.childNodes.length > 0) {
            d.write("<a href=\"javascript:objLocalTree.toggleExpand('");
            d.write(this.id);
            d.write("')\"><img src=\"");
            d.write(this.expanded ? imgMinus.src : imgPlus.src);
            d.write("\" border=\"0\" width='9' height='9' id=\"");
            d.write("imgPM_" + this.id);
            d.write("\" /></a>");
//Dodato			
        } else {
			d.write("<img src='ccImages/spacer.gif' width='9' height='1' border='0' />")
		}
//Dovde
        
        d.write("</td>");
    }
    
//    d.write("<td width=\"1\">" +strHTMLLink + "<img width='1' height='1' src=\"" + this.icon + "\" border=\"0\" />");
//    if(this.url!="") { d.write("</a>"); }
	if (huh == 1) {
       d.write("</td><td nowrap=\"nowrap\" style='padding-left:5px;' class='leftNavSubTitle'>" + strHTMLLink +  this.text);
	} else {
       d.write("</td><td nowrap=\"nowrap\" style='padding-left:5px;' class='leftNavLinks'>" + strHTMLLink + this.text + "</div>");
	}
    if(this.url!="") { d.write("</a>"); }
    d.write("</td></tr></table>");
        
    objNodeDiv.innerHTML = d;
    
    var objChildNodesLayer = document.createElement("div");
    objChildNodesLayer.setAttribute("id", "divChildren_" + this.id);
    objChildNodesLayer.style.position = "relative";
    objChildNodesLayer.style.display = (this.expanded ? "block" : "none");
    objNodeDiv.appendChild(objChildNodesLayer);
    for (var i=0; i < this.childNodes.length; i++)
        this.childNodes[i].addToDOM(objChildNodesLayer);
}

jsTreeNode.prototype.collapse = function () {

    if (!this.expanded) {
        throw "Node is already collapsed"
    } else {
        this.expanded = false;
        document.images["imgPM_" + this.id].src = imgPlus.src;
        document.getElementById("divChildren_" + this.id).style.display = "none";
    }
}


jsTreeNode.prototype.expand = function () {
    if (this.expanded) {
        throw "Node is already expanded"
    } else {
        this.expanded = true;
        document.images["imgPM_" + this.id].src = imgMinus.src;
        document.getElementById("divChildren_" + this.id).style.display = "block";
    }
}

function jsDocument() {
	this.text = new Array();		//array to store the string
	this.write = function (str) { this.text[this.text.length] = str; }
	this.writeln = function (str) { this.text[this.text.length] = str + "\n"; }
	this.toString = function () { return this.text.join(""); }
	this.clear = function () { delete this.text; this.text = null; this.text = new Array; }
}

   function AddToCollapseList(nodeID) {	   
      var t= new Date();
      var d = new Date(t.getTime() + 1200000); //expires in 20 min 20*60*1000
      var myLst=GetCookie("CollapsedList");
      if (myLst.indexOf(nodeID +"#")>-1) { return; }
      document.cookie = "CollapsedList=" + myLst + nodeID + "#;expires="+d.toGMTString+";";
   }

   function RemoveFromCollapseList(nodeID) {
      var t= new Date();
      var d = new Date(t.getTime() + 1200000); //expires in 20 min 20*60*1000

      var myLst=GetCookie("CollapsedList");
      if (myLst!="") {
         var re = new RegExp(nodeID +"#")
         var rs = myLst.replace(re,"");
         document.cookie = "CollapsedList=" + rs + ";expires="+d.toGMTString+";";
      } 
   }

   function RemoveAllFromCollapseList() {
      var t= new Date();
      var d = new Date(t.getTime() + 1200000); //expires in 20 min 20*60*1000
      document.cookie = "CollapsedList=;expires="+d.toGMTString+";";
   }

   function collapseCookied() {
      var myLst=GetCookie("CollapsedList");
      var nds = myLst.split("#");

      for(i = 0; i < nds.length-1; i++){
         objLocalTree.toggleExpand(nds[i])
      }

   }
   
