﻿/**************************************************************************
	Copyright (c) 2001-2003 Geir Landrö (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hjavascripts/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

// Arrays for nodes and icons

var nodes			= new Array();;
var openNodes		= new Array();
var icons			= new Array(6); //6+(2 menu back)


// Loads all icons that are used in the tree
function styleTest(elm,girinti) {
//	for(var si=0;si<document.styleSheets.length;si++) {
//		for (var sc=0;sc<document.styleSheets(si).rules.length;sc++) {
			//alert(si + " : " +document.styleSheets(si).cssText)
//		}
//	}
//	alert(document.styleSheets("treeSatir").rules[1].style.backgroundPositionX)
//	document.styleSheets("treeSatir").rules[1].style.backgroundPositionX = "15px"
document.all[elm].style.backgroundPositionX=girinti + 'px';
}

function preloadIcons(overOut) {
	var actionSrc = overOut.split("|");
	icons[0] = new Image();
	icons[0].src = "../images/tree/plus.gif";
	icons[1] = new Image();
	icons[1].src = "../images/tree/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "../images/tree/minus.gif";
	icons[3] = new Image();
	icons[3].src = "../images/tree/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "../images/tree/folder.gif";
	icons[5] = new Image();
	icons[5].src = "../images/tree/folderopen.gif";
	if (actionSrc[0]!="") {
		icons[6] = new Image();
		icons[6].src = actionSrc[0] //"../images/tree/parent_over.gif";
	}
	if (actionSrc[1]!="") {
		icons[7] = new Image();
		icons[7].src = actionSrc[1] //"../images/tree/parent_out.gif";
	}
}
// Create the tree

function createTree(baslik, arrName, klas, overOut, startNode, openNode, activeNode) {

	nodes = arrName;
	if (nodes.length > 0) {
		preloadIcons(overOut);
		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=\"../images/tree/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"../images/tree/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a>");
		} else {
//		if (baslik!="") document.write("<img src=\"../images/tree/base.gif\" align=\"absbottom\" alt=\"\" />" + baslik + "<br />");
		if (baslik!="") document.write("<img src=\"../images/tree/base.gif\" align=\"absbottom\" alt=\"\" />" + baslik + "");
		}
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes,0,activeNode);
	}

}
// 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) {
//oc(openNode,0,0)
//alert(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) {
	var hcns = ""
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) hcns ++
	}
	if (hcns=="") {
	return false;
	}
	else {
	return hcns;
	}
}
// 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,addTekrar,activeNode) {
//alert(parentNode)
if (parentNode==0) document.write("<table style='border-collapse: collapse; border-style: solid; border-width: 0px; border-collapse: collapse; height:25; width:100%;'>")
//if (parentNode!=0) document.write("<table class='tre2'>")
	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]);
						
if (parentNode==0) {
	for (var g=0;g<=nodeValues.length;g++) {
		//alert(nodeValues[g])
	}
}

//----table yazımı----------
// yedek | -- if (parentNode==0) document.write("<tr id='div" + nodeValues[1] + nodeValues[0] + "' class='td1' onmouseover='this.className=\"td1over\"' onmouseout='this.className=\"td1\"'><td  >")
			document.write("<tr id='div" + nodeValues[1] + nodeValues[0] + "' class='td1' ")
			if (activeNode==nodeValues[0]) document.write ("bgcolor='#CCCCCC' disabled ")

// onmouse over ve out için resim aktarılmışsa resim aktarılmamışsa varsayılan background color
if (icons[6]) {
		if (parentNode==0) document.write("onmouseover='this.style.backgroundImage=\"url(\" + icons[6].src + \")\"' onmouseout='this.style.backgroundImage=\"url(\" + icons[7].src + \")\"' ><td  >")
	} else {
		if (parentNode==0) document.write("onmouseover='this.style.backgroundColor=\"#C2D0ED\"' onmouseout='this.style.backgroundColor=\"\"' ><td  >")
	}
if (parentNode!=0) { 
// onmouse over ve out için resim aktarılmışsa resim aktarılmamışsa varsayılan background color
	if (icons[6]) { 
			document.write("onmouseover='this.style.backgroundImage=\"url(\" + icons[6].src + \")\"' onmouseout='this.style.backgroundImage=\"url(\" + icons[7].src + \")\"'");
	} else {
			document.write("onmouseover='this.style.backgroundColor=\"#C2D0ED\"' onmouseout='this.style.backgroundColor=\"\"'");
	} 
		if (!ino) document.write(" style=\"display: none;\"");
		document.write ("><td>")
			styleTest("div" + nodeValues[1] + nodeValues[0],addTekrar*17)
		}
//----table yazımı----------

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"../images/tree/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"../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,0);\"><img id=\"join" + nodeValues[0] + "\" src=\"../images/tree/");
					 	if (ino) { document.write("minus"); }
						else { document.write("plus"); }
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>"); 
				} else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0,0);\"><img id=\"join" + nodeValues[0] + "\" src=\"../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=\"../images/tree/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"../images/tree/join.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"../images/tree/folder")
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"../images/tree/page.gif\" align=\"absbottom\" alt=\"Page\" />");

//------------------------------------------------------------------------
			
			if (nodeValues[20]) document.write("<span style='background-color: " + nodeValues[20] + "'>");

//------------------------------------------------------------------------
			// edit linki başlangıç
			if (nodeValues[5]) {
				document.write("[<a target=\"" + nodeValues[7] + "\" href=\"" + nodeValues[6] + "\" onmouseover=\"window.status='" + "Bu menüyü düzenle..." + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[5]);
				document.write("</a>]");
			}
			// edit link bitiş
			
			// Sil linki başlangıç
			if (nodeValues[8]) {
				document.write("[<a target=\"" + nodeValues[10] + "\" href=\"" + nodeValues[9] + "\" onmouseover=\"window.status='" + "Bu menüyü ve alt menülerini sil..." + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[8]);
				document.write("</a>]");
			}
			// Sil link bitiş			
			// link1 başlangıç
			if (nodeValues[11]) {
				document.write("[<a target=\"" + nodeValues[13] + "\" href=\"" + nodeValues[12] + "\" onmouseover=\"window.status='" + "Bu menüyü ve alt menülerini bir yukarı taşı..." + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[11]);
				document.write("</a>]");
			}
			// Link1 link bitiş			
			// link2 başlangıç
			if (nodeValues[14]) {
				document.write("[<a target=\"" + nodeValues[16] + "\" href=\"" + nodeValues[15] + "\" onmouseover=\"window.status='" + "Bu menüyü ve alt menülerini bir alta taşı..." + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[14]);
				document.write("</a>]");
			}
			// Link2 link bitiş	
			// link3 başlangıç
			if (nodeValues[17]) {
				document.write("[<a target=\"" + nodeValues[19] + "\" href=\"" + nodeValues[18] + "\" onmouseover=\"window.status='" + "Bu menüyü ve alt menülerini Yayınla / Gizle" + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[17]);
				document.write("</a>]");
			}
			// Link3 link bitiş	
			
			// türkçe edit başlangıç
/*
			if (nodeValues[21]) {
				document.write("[<a target=\"" + nodeValues[23] + "\" href=\"" + nodeValues[22] + "\" onmouseover=\"window.status='" + "Türkçe İçeriği Düzenle" + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[21]);
				document.write("</a>]");
			}  */
			// türkçe edit bitiş
						// türkçe edit başlangıç
			if (nodeValues[24]) {
//				document.write("[<a target=\"" + nodeValues[26] + "\" href=\"" + nodeValues[25] + "\" onmouseover=\"window.status='" + "English Page Edit" + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
				document.write(nodeValues[24]);
//				document.write("</a>]");
			}
			// türkçe edit bitiş
			
			
			if (nodeValues[5]) document.write ("&nbsp;&nbsp;&nbsp;")
//------------------------------------------------------------------------			
			if (hcn) {
					if (ls) { 
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1,0);\">");
					document.write(nodeValues[2] + "</a>"); 
					}
					else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0,0);\">");
					document.write(nodeValues[2] + "</a>"); 
					}
			}
			else {
					// Start link
					document.write("<a target=\"" + nodeValues[4] + "\" href=\"" + nodeValues[3] + "&openNode=" + parentNode + "&activeNode=" + nodeValues[0] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
					// Write out node name
					document.write(nodeValues[2]);
					// End link
					document.write("</a> ");
			}
//------------------------------------------------------------------------
			if (nodeValues[20]) document.write("</span>");

//------------------------------------------------------------------------			

			
			//document.write ("<br />");

			// If node has children write out divs and go deeper
			if (hcn) {
//				document.write("<div style='border-style: solid; border-width: 0px; background-color:  '  id=\"div" + nodeValues[0] + "\"");
//marker bir
//					if (!ino) document.write(" style=\"display: none;\"");
//				document.write(">");
//				document.write("<table class='tre2'>")

				addNode(nodeValues[0], recursedNodes,addTekrar+1,activeNode);
//				document.write("</table>")
//				document.write("</div>");
			}
			// remove last line or empty icon 
			recursedNodes.pop();
//--- table yazımı--------------------
if (parentNode!=0) document.write("</td></tr>")
		}
//-------------------------------------
if (parentNode==0) document.write("</td></tr>")
	}
if (parentNode==0) document.write("</table>")
//--- table yazımı--------------------
}
// Opens or closes a node
function oc(node, bottom, tekrar) {

var acik
	for(var f=0;f<nodes.length;f++) {
	
		var nodeValues = nodes[f].split("|");
			if (node==nodeValues[1]) { 
				var toplam = hasChildNode(nodeValues[0]);
				var ls	= lastSibling(nodeValues[0], nodeValues[1]);


				//---------------------------------------------------------------------------
					var theDiv = document.getElementById("div" + nodeValues[1] + nodeValues[0]);
					var theJoin	= document.getElementById("join" + node);
					var theIcon = document.getElementById("icon" + node);

					if (theDiv.style.display == 'none') {
						if (tekrar==0 && tekrar==0) {

							if (bottom==1) theJoin.src = icons[3].src;
							else theJoin.src = icons[2].src;
							theIcon.src = icons[5].src;
							theDiv.style.display = '';
							acik = true
						}
					} else {

						if (bottom==1) theJoin.src = icons[1].src;
						else theJoin.src = icons[0].src;
					
						theIcon.src = icons[4].src;
						theDiv.style.display = 'none';
						acik = false
						//ococococococococococococococococococococococ
							if (toplam) { 

								if (ls) { oc(nodeValues[0],1,tekrar+1); }
								else    { oc(nodeValues[0],0,tekrar+1); }
							} 
						//ococococococococococococococococococococococ

					}
				//--------------------------------------------------------------------------- 
			}
	} 
/*
	for (z=1;z<=(toplam);z++) {
		var theDiv = document.getElementById("div" + node + z );
		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;
}

