// 38667853507382806697711146496815
// Object name: Node
//
// Description: Will create a HTML menu tree depending on a CSS.
//
// Usage: For a single root without a tree structure add the following commands.
//
// var root1 = Node("menu","menu","my_html_id","my_url","my_css_class","url_caption", false);
// root1.createNodeTree();
//
// Where the first input is the HTML id of the div tag that will hold the complete structure.
// the next, is the parent root div, which in this case still must be the menu div.
// It has no parent root in the menu, because it is one it self.
//
// To create a root and a tree branch, do the following.
//
// var root1 = Node("menu","menu","root1_html_id","my_url","my_css_class","url_caption", true);
// var child1 = Node(null,"root1_html_id","my_html_id","my_url","my_css_branch_class","url_caption", false);
// root1.addBranch(child1);
// root1.createNodeTree();
// child.createNodeTree();

// where the first one is like before, but the next will take a null, because it's not connected
// to the menu div directly. Next input "root1_html_id" is the parent html id that we are
// connectiing this branch to. Then we are connecting the branch to the tree and finally we
// are creating the thing so that it will show up on screen. To add more, you'll get the idea,
// just let it grow like an African tree!
//
// Created by: Morgan Su aka Windmiller Œ Webbstudion
//
// Copyright: For ever and ever, eternal all over and again. Œ2008 - 2017
// If you want to use it, do it without disconnecting this text!
//
// This version is changed, to match Prodib AB 08-03-11 By: Morgan Sundin
// Changes that I have made.. changed the +/- to graphical sign.

// Additional changes where made, to better work in IE6!! :(
// 08-04-x (someday, can't remember when exactly)

//Global array to hold all the root_nodes
//To be able to use more than one menu that
//will have its own events, you'll have to
//lift this following one into the object.
//Otherwise it will have they all will have
//the same root events on fold / unfold.
var gTree_holder = new Array();

//Class/Object name: Node
function Node(menu_id, root_id, id, href, css_class, caption, has_children) {
  this._id = id; //My name
  this._href = href; //My url
  this._caption = caption;
  this._root_id = root_id;
  this._menu_id = menu_id;
  this._has_children = has_children; //..
  this._css_class = css_class; //Css class to connect.
	this._root_node = null; //Can only have one root node.
	this._branch_node = new Array(); //Can have zero or more branches
}

Node.prototype.createNodeTree = function() {
  this.createHTMLNode(this._root_id,this);
  //Save evry single "first" root.
  if(this._menu_id!=null&&this._has_children){
    gTree_holder.push(this);
  }
}

Node.prototype.getId = function() {
    return this._id;  //My name
}

Node.prototype.getUrl = function() {
	return this._href; //My url
}

Node.prototype.addRoot = function(root) {
  this._root_node = root; //Add a root/branch to the branch
}

Node.prototype.addBranch = function(branch) {
  this._branch_node.push(branch); //Add a branch.
  branch.addRoot(this); //Add a root to the branch at the same time.
}

Node.prototype.unfoldMyBranches = function(not_this_id) {
  //Close every "first" root before you proceed
  for(f=0;f<gTree_holder.length;f++) {
    //Not me! And is no first root!!!
    if(this._id!=gTree_holder[f]._id && this._menu_id!=null) {
      for(q=0;q<gTree_holder[f]._branch_node.length;q++) {
        document.getElementById(gTree_holder[f]._branch_node[q]._id).style.display = "none"; //Unfold us   
        var folding = document.getElementById(gTree_holder[f]._id+'_folding');
	      //folding.innerHTML = "<img src='img/menyclosed.gif' border='0' />";
	      //folding.innerText = " | + |";
	     }
    }
  }
  
  //Fold all the other branches
  if(this._root_node!=null) {
    var alles = this._root_node;
    for(e=0;e<alles._branch_node.length;e++) {
      //But not me!
      if(this._id!=alles._branch_node[e]._id) {
        //Close all of the others on this level..
        for(w=0;w<alles._branch_node[e]._branch_node.length;w++) {
          document.getElementById(alles._branch_node[e]._branch_node[w]._id).style.display = "none"; //Unfold us
          var folding = document.getElementById(alles._branch_node[e]._id+'_folding');
		      //folding.innerHTML = "<img src='img/menyclosed.gif' border='0' />";
		      //folding.innerText = " | + |";
        }
      }
    }
  }
  
  //Every branch.. in my tree..
  for(o=0;o<this._branch_node.length;o++) {
    var branch = this._branch_node[o];
    document.getElementById(branch._id).style.display = (document.getElementById(branch._id).style.display=="block")? "none" : "block"; //Unfold us
  }
  this.folderSigns();
}

Node.prototype.folderSigns = function() {

if(this._branch_node.length>0){

		var folding = document.getElementById(this._id+'_folding');
		 
		if(folding.innerHTML.length == 37 || folding.innerHTML.length == 69) {
		 
			//folding.innerHTML = "<img src='menyopen.gif' border='0' />";
		        //folding.innerText = " | - |";
		}else if( folding.innerHTML.length == 6){
		//folding.innerHTML = "<img src='menyopen.gif' border='0' />";
    }else{

		  //      folding.innerHTML = "<img src='img/menyclosed.gif' border='0' />"; 
        		//folding.innerText = " | + |"; //and Mozilla FF!! Crax
		}
	}
}

Node.prototype.createHTMLNode = function(root_id, childroot) {
	var root = document.getElementById(root_id);
	var child = document.createElement("div");
	child.setAttribute("id",childroot._id);
	child.id = childroot._id;
	child.setAttribute("className",childroot._css_class); 	//IE tweak.
	child.setAttribute("class",childroot._css_class); 	//Mozilla tweak
  
	//Create root link.
	var anchor = document.createElement("a");
	var hrefattr = document.createAttribute("href");
	hrefattr.nodeValue = childroot._href;
	anchor.setAttributeNode(hrefattr);
	anchor.appendChild(document.createTextNode(childroot._caption));
	child.appendChild(anchor);
	// --
  
  	if(childroot._has_children) {
    		//Create folder activator.
    		var anchor2 = document.createElement("a");
    		var hrefattr2 = document.createAttribute("href");
    		hrefattr2.nodeValue = "javascript:void(0);";
    		anchor2.id = childroot._id+'_folding';
    		anchor2.setAttributeNode(hrefattr2);
    		anchor2.appendChild(document.createTextNode(" | + |")); //Yes..
    		//anchor2.innerText = " | + |"; //Crax becuz of the IE vs FF fighting!
    		anchor2.innerHTML = "<img src='img/menyclosed.gif' border='0' />";
    		child.appendChild(anchor2);
    
        //Changed 080306 By: Morgan S - Webbstudion
        // * anchor2.onclick to anchor, to make the text clickable
        // instead of the +/- sign, as it was before.
    		//This function will hide and show the children.
  	  	anchor.onclick = (function(p) {
  		  return function(){ childroot.unfoldMyBranches(p) }
    		})();
  	}
  	child.appendChild(anchor);
  	root.appendChild(child); //Add it to the parent node.
}

// Helper function to find a node by attribute href.
function findByHref(href, rootNodeArray) {

    var flattenedNodes = flattenTreeNodes(rootNodeArray);

    var foundNode = null;
    for (i = 0; i < flattenedNodes.length; i++) {
        if (flattenedNodes[i]._href.indexOf(href) > -1) {
            foundNode = flattenedNodes[i];
        }
    }

    return foundNode;
}

// Helper function to flatten tree node structure into an array of nodes.
function flattenTreeNodes(rootNodeArray) {

    var treeNodes = rootNodeArray;
    var flattenedNodes = new Array();

    while (treeNodes.length > 0) {
        var currNode = treeNodes.shift();
        if (currNode._has_children == true) {
            for (i = 0; i < currNode._branch_node.length; i++) {
                treeNodes.push(currNode._branch_node[i]);
            }
        }
        flattenedNodes.push(currNode);
    }

    return flattenedNodes;
}

// Helper function to expand all nodes on the tree path from the given node to the root node.
function expandToRoot(aNode) {

    var currNodeInRootPath = aNode;
    while (currNodeInRootPath != null) {
        if (currNodeInRootPath._has_children == true) {
            currNodeInRootPath.unfoldMyBranches();
        }
        currNodeInRootPath = currNodeInRootPath._root_node;
    }
}

//End class object Node.
//38667853507382806697711146496815
