﻿/**************************************************************************
	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);

var graphicStr ="";

var axmlHttp = GetXmlHttpObject();
function GetXmlHttpObject(){  
	var page_request = false
	browser="IE"
	if (window.XMLHttpRequest) {// if Mozilla, Safari etc
		browser="other"
		page_request = new XMLHttpRequest()
		
	}else if (window.ActiveXObject){ // if IE
		
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
		try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){}
		}
	}

	return page_request
}

function ajaxinclude(url, d) {
	var page_request = GetXmlHttpObject()
	if(!page_request)
		return false
	dest = d;
	page_request.open('GET', url, false) 
	page_request.send(null)
	writecontent(page_request)
}
function writecontent(page_request){

	if (window.location.href.indexOf("http")==-1 || page_request.status==200){
	
	
		if(dest==""){
			document.write(page_request.responseText)
		
		}else{
			document.getElementById(dest).innerHTML = page_request.responseText
			
		}
	}
}

function getCurrMsec(){
	var da = new Date();
	return da.getMilliseconds();
}


// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = "img/plus.gif";
	icons[1] = new Image();
	icons[1].src = "img/plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = "img/minus.gif";
	icons[3] = new Image();
	icons[3].src = "img/minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = "img/icon_fla.gif";
	icons[5] = new Image();
	icons[5].src = "img/icon_swf.gif";
	icons[6] = new Image();
	icons[6].src = "img/icon_jpg.gif";
	icons[7] = new Image();
	icons[7].src = "img/imgfolder.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode) {
	graphicStr ="";
	nodes = arrName;
	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("|");
			
			graphicStr+= "<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img border=\"0\" src=\"img/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />";
		} else 
			graphicStr+= "<img border=\"0\"  src=\"img/base.gif\" align=\"absbottom\" alt=\"\" />Downloads<br />";
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
		
		// echo out
		document.getElementById("tree").innerHTML= "<bR>Graphics 圖畫<BR>" + graphicStr;
	}
}
// 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) 
					graphicStr+="<img border=\"0\"  src=\"img/line.gif\" align=\"absbottom\" alt=\"\" />";
				else  
					graphicStr+="<img border=\"0\" src=\"img/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) {
					graphicStr+="<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img border=\"0\" id=\"join" + nodeValues[0] + "\" src=\"img/";
					 	if (ino) 
							graphicStr+="minus";
						else 
							graphicStr+="plus";
					graphicStr+="bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>";
				} else {
					graphicStr+= "<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img border=\"0\" id=\"join" + nodeValues[0] + "\" src=\"img/";
						if (ino) 
							graphicStr+="minus";
						else graphicStr+="plus";
					graphicStr+=".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>";
				}
			} else {
				if (ls) graphicStr+="<img border=\"0\" src=\"img/joinbottom.gif\" align=\"absbottom\" alt=\"\" />";
				else graphicStr+="<img border=\"0\" src=\"img/join.gif\" align=\"absbottom\" alt=\"\" />";
			}

			// Start link
			graphicStr+="<a href=\"javascript:forceDownload('" + nodeValues[3] + "');\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">";
			
			// Write out folder & page icons
			if (hcn) {
			//	alert(nodeValues[2] +", " + nodeValues[3])
				
				if(nodeValues[2] == "fla") {
					graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"" + icons[4].src + "\" align=\"absbottom\" alt=\"Folder\" />";
				} else if(nodeValues[2] == "swf"){
					graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"" + icons[5].src + "\" align=\"absbottom\" alt=\"Folder\" />";
				}else if(nodeValues[2] == "jpg") {
					graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"" + icons[6].src + "\" align=\"absbottom\" alt=\"Folder\" />";
				} else {
					graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"" + icons[7].src + "\" align=\"absbottom\" alt=\"Folder\" />";
				}
				
				//graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"" + icons[4].src + "\" align=\"absbottom\" alt=\"Folder\" />";
				//	if (ino) graphicStr+="open"
				//graphicStr+=".gif\" align=\"absbottom\" alt=\"Folder\" />"
			} else {
				graphicStr+="<img border=\"0\" id=\"icon" + nodeValues[0] + "\" src=\"img/page.gif\" align=\"absbottom\" alt=\"Page\" />"
				// Write out node name
				graphicStr+=nodeValues[2];
			}
			
			// End link
			graphicStr+="</a><br />";
			
			// If node has children write out divs and go deeper
			if (hcn) {
				graphicStr+="<div id=\"div" + nodeValues[0] + "\""
					if (!ino) graphicStr+=" style=\"display: none;\""
				graphicStr+=">";
				addNode(nodeValues[0], recursedNodes);
				graphicStr+="</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[7].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';
	}
}

function forceDownload(fn) {
	var url = "forceDownload.php?file="+ fn;
	
	popLink(url, 400,300);
	
	axmlHttp=GetXmlHttpObject()	
	if (axmlHttp==null){
		document.getElementById("alertMsg").innerHTML="Browser does not support HTTP Request"
		return
	} 
	var url2="./includes/log.php?type=download";

	url2=url2+"&d="+getCurrMsec();
	axmlHttp.onreadystatechange=forceDownloadHandler
	axmlHttp.open("GET",url2,true)
	axmlHttp.send(null)
	
}

function forceDownloadHandler() {
	if (axmlHttp.readyState==4 || axmlHttp.readyState=="complete")
	{ 
		document.getElementById("alertMsg").innerHTML=axmlHttp.responseText;
		
	}

}

function popLink(linky, wS, hS) {
	var w=wS;
	var h=hS;
	var t=(screen.height/2)-h/2;
	var l=(screen.width/2)-w/2;
	var myBars = "directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no";
	var myOptions = "scrollbars=yes,width="+w+",height="+h+",resizable=no,top="+t+",left="+l;
	var myFeatures = myBars + "," + myOptions;
	var indexLink=linky;
	var dimen=("width="+w+",height="+h);
	window.open(indexLink,"",myFeatures);
	
}

// 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;
}
