// Simpul object

function Simpul(id, pid, name, url, title, target, icon, iconOpen, open) {

	this.id = id;

	this.pid = pid;

	this.name = name;

	this.url = url;

	this.title = title;

	this.target = target;

	this.icon = icon;

	this.iconOpen = iconOpen;

	this._io = open || false;

	this._is = false;

	this._ls = false;

	this._hc = false;

	this._ai = 0;

	this._p;

};

// Adds a new simpul to the simpul array

dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {

	this.aSimpuls[this.aSimpuls.length] = new Simpul(id, pid, name, url, title, target, icon, iconOpen, open);

};



// Open/close all simpuls

dTree.prototype.openAll = function() {

	this.oAll(true);

};

dTree.prototype.closeAll = function() {

	this.oAll(false);

};



// Outputs the tree to the page

dTree.prototype.toString = function() {

	var str = '<div class="dtree">\n';

	if (document.getElementById) {

		if (this.config.useCookies) this.selectedSimpul = this.getSelected();

		str += this.addSimpul(this.akar);

	} else str += 'Browser not supported.';

	str += '</div>';

	if (!this.selectedFound) this.selectedSimpul = null;

	this.completed = true;

	return str;

};



// Creates the tree structure

dTree.prototype.addSimpul = function(pSimpul) {

	var str = '';

	var n=0;

	if (this.config.inOrder) n = pSimpul._ai;

	for (n; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n].pid == pSimpul.id) {

			var cn = this.aSimpuls[n];

			cn._p = pSimpul;

			cn._ai = n;

			this.setCS(cn);

			if (!cn.target && this.config.target) cn.target = this.config.target;

			if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);

			if (!this.config.folderLinks && cn._hc) cn.url = null;

			if (this.config.useSelection && cn.id == this.selectedSimpul && !this.selectedFound) {

					cn._is = true;

					this.selectedSimpul = n;

					this.selectedFound = true;

			}

			str += this.simpul(cn, n);

			if (cn._ls) break;

		}

	}

	return str;

};



// Creates the simpul icon, url and text

dTree.prototype.simpul = function(simpul, simpulId) {

	var str = '<div class="dTreeSimpul">' + this.indent(simpul, simpulId);

	if (this.config.useIcons) {

		if (!simpul.icon) simpul.icon = (this.akar.id == simpul.pid) ? this.icon.akar : ((simpul._hc) ? this.icon.folder : this.icon.simpul);

		if (!simpul.iconOpen) simpul.iconOpen = (simpul._hc) ? this.icon.folderOpen : this.icon.simpul;

		if (this.akar.id == simpul.pid) {

			simpul.icon = this.icon.akar;

			simpul.iconOpen = this.icon.akar;

		}

		str += '<img id="i' + this.obj + simpulId + '" src="' + ((simpul._io) ? simpul.iconOpen : simpul.icon) + '" alt="" />';

	}

	if (simpul.url) {

		str += '<a id="s' + this.obj + simpulId + '" class="' + ((this.config.useSelection) ? ((simpul._is ? 'simpulSel' : 'simpul')) : 'simpul') + '" href="' + simpul.url + '"';

		if (simpul.title) str += ' title="' + simpul.title + '"';

		if (simpul.target) str += ' target="' + simpul.target + '"';

		if (this.config.useStatusText) str += ' onmouseover="window.status=\'' + simpul.name + '\';return true;" onmouseout="window.status=\'\';return true;" ';

		if (this.config.useSelection && ((simpul._hc && this.config.folderLinks) || !simpul._hc))

			str += ' onclick="javascript: ' + this.obj + '.s(' + simpulId + ');"';

		str += '>';

	}

	else if ((!this.config.folderLinks || !simpul.url) && simpul._hc && simpul.pid != this.akar.id)

		str += '<a href="javascript: ' + this.obj + '.o(' + simpulId + ');" class="simpul">';

	str += simpul.name;

	if (simpul.url || ((!this.config.folderLinks || !simpul.url) && simpul._hc)) str += '</a>';

	str += '</div>';

	if (simpul._hc) {

		str += '<div id="d' + this.obj + simpulId + '" class="clip" style="display:' + ((this.akar.id == simpul.pid || simpul._io) ? 'block' : 'none') + ';">';

		str += this.addSimpul(simpul);

		str += '</div>';

	}

	this.aIndent.pop();

	return str;

};



// Adds the kosong and garis icons

dTree.prototype.indent = function(simpul, simpulId) {

	var str = '';

	if (this.akar.id != simpul.pid) {

		for (var n=0; n<this.aIndent.length; n++)

			str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.garis : this.icon.kosong ) + '" alt="" />';

		(simpul._ls) ? this.aIndent.push(0) : this.aIndent.push(1);

		if (simpul._hc) {

			str += '<a href="javascript: ' + this.obj + '.o(' + simpulId + ');"><img id="j' + this.obj + simpulId + '" src="';

			if (!this.config.useLines) str += (simpul._io) ? this.icon.nlMinus : this.icon.nlPlus;

			else str += ( (simpul._io) ? ((simpul._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((simpul._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );

			str += '" alt="" /></a>';

		} else str += '<img src="' + ( (this.config.useLines) ? ((simpul._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.kosong) + '" alt="" />';

	}

	return str;

};



// Checks if a simpul has any children and if it is the last sibling

dTree.prototype.setCS = function(simpul) {

	var lastId;

	for (var n=0; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n].pid == simpul.id) simpul._hc = true;

		if (this.aSimpuls[n].pid == simpul.pid) lastId = this.aSimpuls[n].id;

	}

	if (lastId==simpul.id) simpul._ls = true;

};



// Returns the selected simpul

dTree.prototype.getSelected = function() {

	var sn = this.getCookie('cs' + this.obj);

	return (sn) ? sn : null;

};



// Highlights the selected simpul

dTree.prototype.s = function(id) {

	if (!this.config.useSelection) return;

	var cn = this.aSimpuls[id];

	if (cn._hc && !this.config.folderLinks) return;

	if (this.selectedSimpul != id) {

		if (this.selectedSimpul || this.selectedSimpul==0) {

			eOld = document.getElementById("s" + this.obj + this.selectedSimpul);

			eOld.className = "simpul";

		}

		eNew = document.getElementById("s" + this.obj + id);

		eNew.className = "simpulSel";

		this.selectedSimpul = id;

		if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);

	}

};



// Toggle Open or close

dTree.prototype.o = function(id) {

	var cn = this.aSimpuls[id];

	this.simpulStatus(!cn._io, id, cn._ls);

	cn._io = !cn._io;

	if (this.config.closeSameLevel) this.closeLevel(cn);

	if (this.config.useCookies) this.updateCookie();

};



// Open or close all simpuls

dTree.prototype.oAll = function(status) {

	for (var n=0; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n]._hc && this.aSimpuls[n].pid != this.akar.id) {

			this.simpulStatus(status, n, this.aSimpuls[n]._ls)

			this.aSimpuls[n]._io = status;

		}

	}

	if (this.config.useCookies) this.updateCookie();

};



// Opens the tree to a specific simpul

dTree.prototype.openTo = function(nId, bSelect, bFirst) {

	if (!bFirst) {

		for (var n=0; n<this.aSimpuls.length; n++) {

			if (this.aSimpuls[n].id == nId) {

				nId=n;

				break;

			}

		}

	}

	var cn=this.aSimpuls[nId];

	if (cn.pid==this.akar.id || !cn._p) return;

	cn._io = true;

	cn._is = bSelect;

	if (this.completed && cn._hc) this.simpulStatus(true, cn._ai, cn._ls);

	if (this.completed && bSelect) this.s(cn._ai);

	else if (bSelect) this._sn=cn._ai;

	this.openTo(cn._p._ai, false, true);

};



// Closes all simpuls on the same level as certain simpul

dTree.prototype.closeLevel = function(simpul) {

	for (var n=0; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n].pid == simpul.pid && this.aSimpuls[n].id != simpul.id && this.aSimpuls[n]._hc) {

			this.simpulStatus(false, n, this.aSimpuls[n]._ls);

			this.aSimpuls[n]._io = false;

			this.closeAllChildren(this.aSimpuls[n]);

		}

	}

}



// Closes all children of a simpul

dTree.prototype.closeAllChildren = function(simpul) {

	for (var n=0; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n].pid == simpul.id && this.aSimpuls[n]._hc) {

			if (this.aSimpuls[n]._io) this.simpulStatus(false, n, this.aSimpuls[n]._ls);

			this.aSimpuls[n]._io = false;

			this.closeAllChildren(this.aSimpuls[n]);		

		}

	}

}



// Change the status of a simpul(open or closed)

dTree.prototype.simpulStatus = function(status, id, bottom) {

	eDiv	= document.getElementById('d' + this.obj + id);

	eJoin	= document.getElementById('j' + this.obj + id);

	if (this.config.useIcons) {

		eIcon	= document.getElementById('i' + this.obj + id);

		eIcon.src = (status) ? this.aSimpuls[id].iconOpen : this.aSimpuls[id].icon;

	}

	eJoin.src = (this.config.useLines)?

	((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):

	((status)?this.icon.nlMinus:this.icon.nlPlus);

	eDiv.style.display = (status) ? 'block': 'none';

};





// [Cookie] Clears a cookie

dTree.prototype.clearCookie = function() {

	var now = new Date();

	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);

	this.setCookie('co'+this.obj, 'cookieValue', yesterday);

	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);

};



// [Cookie] Sets value in a cookie

dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {

	document.cookie =

		escape(cookieName) + '=' + escape(cookieValue)

		+ (expires ? '; expires=' + expires.toGMTString() : '')

		+ (path ? '; path=' + path : '')

		+ (domain ? '; domain=' + domain : '')

		+ (secure ? '; secure' : '');

};



// [Cookie] Gets a value from a cookie

dTree.prototype.getCookie = function(cookieName) {

	var cookieValue = '';

	var posName = document.cookie.indexOf(escape(cookieName) + '=');

	if (posName != -1) {

		var posValue = posName + (escape(cookieName) + '=').length;

		var endPos = document.cookie.indexOf(';', posValue);

		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));

		else cookieValue = unescape(document.cookie.substring(posValue));

	}

	return (cookieValue);

};



// [Cookie] Returns ids of open simpuls as a string

dTree.prototype.updateCookie = function() {

	var str = '';

	for (var n=0; n<this.aSimpuls.length; n++) {

		if (this.aSimpuls[n]._io && this.aSimpuls[n].pid != this.akar.id) {

			if (str) str += '.';

			str += this.aSimpuls[n].id;

		}

	}

	this.setCookie('co' + this.obj, str);

};



// [Cookie] Checks if a simpul id is in a cookie

dTree.prototype.isOpen = function(id) {

	var aOpen = this.getCookie('co' + this.obj).split('.');

	for (var n=0; n<aOpen.length; n++)

		if (aOpen[n] == id) return true;

	return false;

};



// If Push and pop is not implemented by the browser

if (!Array.prototype.push) {

	Array.prototype.push = function array_push() {

		for(var i=0;i<arguments.length;i++)

			this[this.length]=arguments[i];

		return this.length;

	}

};

if (!Array.prototype.pop) {

	Array.prototype.pop = function array_pop() {

		lastElement = this[this.length-1];

		this.length = Math.max(this.length-1,0);

		return lastElement;

	}

};