// Arrays for nodes and icons
var nodes			= new Array();;
var openNodes	= new Array();
var icons			= new Array(6);
var webPath;
var adminLink;
var moveLink;

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = webPath + "/template/style/images/tree/plus.gif";
	icons[1] = new Image();
	icons[1].src = webPath + "/template/style/images/tree/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = webPath + "/template/style/images/tree/minus.gif";
	icons[3] = new Image();
	icons[3].src = webPath + "/template/style/images/tree/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = webPath + "/template/style/images/tree/folder.gif";
	icons[5] = new Image();
	icons[5].src = webPath + "/template/style/images/tree/folderopen.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode, WebPath, AdminLink, MoveLink, TabulkaKategorie, Redirect) 
{
  webPath = WebPath;
  adminLink = AdminLink;
  moveLink = MoveLink;
	nodes = arrName;
	tabulkaKategorie = TabulkaKategorie;
	redirect = Redirect;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\""+ webPath +"/template/style/images/tree/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		}
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\""+ webPath +"/template/style/images/tree/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\""+ webPath +"/template/style/images/tree/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) {
				if (ls) {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\""+ webPath +"/template/style/images/tree/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Otevřít/Zavřít\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\""+ webPath +"/template/style/images/tree/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\""+ webPath +"/template/style/images/tree/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\""+ webPath +"/template/style/images/tree/join.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Start link
      document.write("<a ");
			if (nodeValues[0] == openNodes[0]) document.write('style="color:red;" ');
      
      if (!adminLink)
      {
      if (!moveLink)
      document.write("href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			else
			document.write("href=\"javascript:SelectCategories('" + nodeValues[0] + "','" + nodeValues[2] + "', 1)\" onmouseover=\"SelectCategories('" + nodeValues[0] + "','" + nodeValues[2] + "', 0)\">");
			}
      else 
      document.write("href=\"#\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
      			
			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon" + nodeValues[0] + "\" style=\"height:1px\" src=\""+ webPath +"/template/style/images/tree/folder")
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else document.write("<img id=\"icon" + nodeValues[0] + "\"  style=\"height:1px\" src=\""+ webPath +"/template/style/images/tree/page.gif\" align=\"absbottom\" alt=\"Page\" />");
					
			// Write out node name
      document.write(nodeValues[2]);
      
        
			
			// End link
			if (!adminLink)
			document.write("</a><br />");
			else
			{
			 var rand = 1+Math.random();
       var randSmazat = 2+Math.random();    
       var randPridat = 3+Math.random();
       var randPresunout = 4+Math.random();
			 document.write("</a> - <a href=\"#\" style=\"text-decoration: underline;font-weight:bold;\" onclick=\"Fce_View("+randPridat+");\">Přidat</a> - <a href=\"#\" onclick=\"Fce_View("+rand+");\" style=\"text-decoration: underline;font-weight:bold;\">Upravit</a> - <a href=\"#\" onclick=\"Fce_View("+randSmazat+");\" style=\"text-decoration: underline;font-weight:bold;\">Smazat</a> - <a href=\"#\" onclick=\"Fce_View("+randPresunout+");\" style=\"text-decoration: underline;font-weight:bold;\">Přesunout</a>    <div id=\""+randPridat+"\" class=\"hide\"><form method=\"post\" action=\"" + webPath + "/admin/form.php\"><h5 style=\"margin:0\">Přidáváte do: <strong>"+nodeValues[2]+"</strong></h5>Název nové kategorie: <input id=\"input"+randPridat+"\" type=\"text\" name=\"nazev\" style=\"width:100px\"/><input type=\"submit\" value=\"Přidat\" class=\"button\" /><input type=\"hidden\" name=\"id_kategorie\" value=\""+nodeValues[0]+"\"/><input type=\"hidden\" name=\"frm_pridat_kategorii\" value=\"1\"/><input type=\"hidden\" name=\"table\" value=\""+tabulkaKategorie+"\"/><input type=\"hidden\" name=\"frm_redirect\" value=\""+redirect+"\"/></form></div>                           <div id=\""+rand+"\" class=\"hide\"><form method=\"post\" action=\"" + webPath + "/admin/form.php\"><h5 style=\"margin:0\">Upravujete: "+nodeValues[2]+"</h5><input id=\"input"+rand+"\" type=\"text\" name=\"nazev\" style=\"width:100px\" value=\""+nodeValues[2]+"\"/><input type=\"submit\" value=\"Uložit\" class=\"button\" /><input type=\"hidden\" name=\"id_kategorie\" value=\""+nodeValues[0]+"\"/><input type=\"hidden\" name=\"frm_upravit_kategorii\" value=\"1\"/><input type=\"hidden\" name=\"table\" value=\""+tabulkaKategorie+"\"/><input type=\"hidden\" name=\"frm_redirect\" value=\""+redirect+"\"/></form></div>                     <div id=\""+randSmazat+"\" class=\"hide\"><form method=\"post\" action=\"" + webPath + "/admin/form.php\"><h5 style=\"margin:0\"><strong>Opravdu si přejete smazat kategorii "+nodeValues[2]+" a všechny její součásti</strong>?</h5><center><input type=\"submit\" value=\"Ano - smazat\" class=\"button\" /></center><input type=\"hidden\" name=\"id_kategorie\" value=\""+nodeValues[0]+"\"/><input type=\"hidden\" name=\"frm_smazat_kategorii\" value=\"1\"/><input type=\"hidden\" name=\"table\" value=\""+tabulkaKategorie+"\"/><input type=\"hidden\" name=\"frm_redirect\" value=\""+redirect+"\"/></form>  </div>                               <div id=\""+randPresunout+"\" class=\"hide\"><form name=\"presunout"+randPresunout+"\" method=\"post\" action=\"" + webPath + "/admin/form.php\"><h5 style=\"margin:0\"><strong>Přesunout kategorii "+nodeValues[2]+"</strong></h5><input type=\"hidden\" name=\"id_nove_kategorie\" value=\"-1\"/>Kam přesunout: <input id=\"input"+randPridat+"\" type=\"text\" name=\"nazev_nove_kategorie\" style=\"width:180px\" disabled=\"disabled\"/> - <a href=\""+webPath+"/admin/index.php?page=select_categories&name=presunout"+randPresunout+"&table="+tabulkaKategorie+"\"  onclick=\"window.open('"+webPath+"/admin/index.php?page=select_categories&name=presunout"+randPresunout+"&table="+tabulkaKategorie+"', '_selectCategories', 'HEIGHT=600,resizable=yes,scrollbars=yes,WIDTH=290');return false;\"  target=\"_selectCategories\" style=\"text-decoration: underline;font-weight:bold;\">Vybrat kategorii</a> <input type=\"submit\" value=\"Přesunout\" class=\"button\" /><input type=\"hidden\" name=\"id_kategorie\" value=\""+nodeValues[0]+"\"/><input type=\"hidden\" name=\"frm_presunout_kategorii\" value=\"1\"/><input type=\"hidden\" name=\"table\" value=\""+tabulkaKategorie+"\"/><input type=\"hidden\" name=\"frm_redirect\" value=\""+redirect+"\"/></form></div> <br />");
			}
			
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}
// Opens or closes a node
function oc(node, bottom) {
	var theDiv = document.getElementById("div" + node);
	var theJoin	= document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}
// 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;
}

