//////////////////////////////////////////////////////////////
// Global variables
//
var imgStore = new ImageStoreObject();
var isIE = (document.all ? true : false);
var isNav = (document.layers ? true : false);

var bgUseMainColor = true;
var bgHeadColor = "bgcolor=#333333";
var bgMainColor = "bgcolor=#ffffff";
var bgAltColor = "bgcolor=#eeeeee";
var managerText = '';
var editText = '';
var moveText = '';
var deleteText = '';

function NextBGColor() {
	var bgcolor = bgUseMainColor ? bgMainColor : bgAltColor;
	bgUseMainColor = !bgUseMainColor;
	return bgcolor;
}

function GetFolderIcon(isopen, id) {
	var iconref = (isopen) ? imgStore.getTag("iconOpen", id) : imgStore.getTag("iconClosed", id);
	var retval;
		retval = "<a name='" + id + "'></a><a href='Javascript:clickHandler(" + isopen + ", " + id + ");'>" + iconref + "</a>";
	return retval;
}


//////////////////////////////////////////////////////////////
// Title object
//
// This object defines a Title column in a row
//
// Title constructor
function TitleColumn(id, title) {
   this.id = id;
   this.title = title;
   // methods
   this.display = TitleColumnDisplay;
}

function TitleColumnDisplay() {
   return(this.title);
}

//////////////////////////////////////////////////////////////
//  Document object
//
//  This object defines a document (leaf) on the tree
//
//  Document constructor
function DocumentObject(id, parentid, parentrank, title, filename, filesize, datetime) {
	this.id = id;
	this.parentid = parentid;
	this.isselected = false;
	this.rank = parentrank + 1;

	this.title    = new TitleColumn(id, title);
	this.filename = filename;
	this.filesize = filesize;
	this.datetime = datetime;

	// methods
	this.display        = DocumentDisplay;
	this.getIndent      = DocumentGetIndent;
	this.getColSpan     = DocumentGetColSpan;
}

function DocumentDisplay(cols) {
	var textOut = "<tr valign=\"top\" " + NextBGColor() + ">" + '\n' +
	              "<td width=\"25%\"><table border=0 cellspacing=0 cellpadding = 0>" + '\n' +
	              "<tr valign=\"top\">" + '\n' +
	              this.getIndent();
	var tdOpen    = "<td width=\"" + 75/4 + "%\" nowrap>";
	var tdClose   = "</td>" + '\n';
	textOut += "<td>" +'\n' +
	           this.title.display() + '\n' + "</td>" +
	           "<td width=16>" + imgStore.getTag("spacer16", "") + "</td>" + '\n' +
	           "</tr></table></td>" + '\n' +
	           tdOpen + this.filename + tdClose +
	           tdOpen + this.filesize + tdClose +
	           tdOpen + this.datetime + tdClose;
	textOut += "</tr>";
	return textOut;
}

function DocumentGetIndent() {
	var indent = "";
	for (var i = 0; i < (this.rank - 3); i++) {
		indent += "<td width=16>" + imgStore.getTag("spacer16", "") + "</td>" + '\n';
	}
	indent += "<td width=7>" + imgStore.getTag("tick7", "") + "</td>" +'\n';
	indent += "<td width=9>" + imgStore.getTag("spacer9", "") + "</td>" + '\n';
	indent += "<td width=4>" + imgStore.getTag("spacer4", "") + "</td>" + '\n';
	return indent;
}

function DocumentGetColSpan(cols) {
	var span = (cols - (this.rank + 5)) + 6;
	var spantext = (span <= 1) ? "" : ("colspan=" + span);
	return spantext;
}

//////////////////////////////////////////////////////////////
//  Folder object
//
//  This object defines a folder (branch) on the tree.
//

//  Folder constructor
function FolderObject(id, parentid, parentrank, title, isopen, flags) {
	this.id = id;
	this.parentid = parentid;
	this.title = unescape(title);
	this.isopen = (id < 1 ? true : isopen);
	this.isselected = false;
	this.rank = parentrank + 1;
	this.flags = flags;
	this.documents = new Array();
	this.folders = new Array();

	// methods
	this.AddFolder      = FolderAddFolder;
	this.AddDocument    = FolderAddDocument;
	this.display        = FolderDisplay;
	this.getIndent      = FolderGetIndent;
	this.getColSpan     = FolderGetColSpan;

//alert("id = " + this.id + ", title = " + this.title + ", isopen = " + this.isopen + ", flags = " + this.flags);
//alert("node " + this.title + " - id = " + this.id + ", parent = " + this.parentid);
}

function FolderAddFolder(id, title, isopen, flags) {
	var newfolder = new FolderObject(id, this.id, this.rank, title, isopen, flags);
	this.folders[this.folders.length] = newfolder;
	return newfolder;
}

function FolderAddDocument(id, title, filename, filesize, datetime) {
	var newdocument = new DocumentObject(id, this.id, this.rank, title, filename, filesize, datetime);
	this.documents[this.documents.length] = newdocument;
	return newdocument;
}

// This is the main display function for tree nodes.
// It displays the current folder, then walks the document list
// and displays each document, then displays each of the
// child folders.
function FolderDisplay(cols) {
	var bgcolor = NextBGColor();
	var textOut = "<tr valign=\"top\" " + bgcolor + ">";
	textOut += "<td colspan=" + this.getColSpan(cols) + "><table border=0 cellspacing=0 cellpadding=0>" + '\n' +
	           "<tr valign=\"top\">" + this.getIndent(false) +
	           "<td nowrap>" + this.title + "</td>" + '\n' +
	           "</tr></table></td>" + '\n';
	textOut += "</tr>" + '\n';
	if (this.isopen) {
		for (var j = 0; j < this.folders.length; j++) {
			textOut += this.folders[j].display(cols);
		}
		for (var i = 0; i < this.documents.length; i++) {
			textOut += this.documents[i].display(cols);
		}
	}
	return textOut;
}

function FolderGetIndent() {
	var indent = "";
	for (var i = 0; i < (this.rank - 2); i++) {
		indent += "<td>" + imgStore.getTag("spacer16") + "</td>" + '\n';
	}
	// add the appropriate +- and folder icons
	indent += "<td>" +  GetFolderIcon(this.isopen, this.id) + "</td>" + '\n';
	indent += "<td width=4>" + imgStore.getTag("spacer4", "") + "</td>" + '\n';
	return indent;
}

function FolderGetColSpan(cols) {
	var span = (cols - this.rank) + 4;
	span -= 3;
	if (span < 1) {
		span = 1;
	}
	return span;
}

//////////////////////////////////////////////////////////////
//  Tree Object
//
//  This object contains all the item data objects
//  and controls the layout and rendering of each
//
//
// Tree Object constuctor
function TreeObject() {
	// members
	this.count = -1;
	this.container = -1;
	this.topNode = new FolderObject(0, 0, 0, "_top", false, false);
	this.maxrank = 1;
	this.allFolders = new Array(1);
	this.name = 'theTree';
	this.docColumns = 5;
	this.header = new Array();
	this.headerAlign = 'left';

	this.noDocs = false;

	// debug
	this.debug = false;

	// methods
	this.findFolder     = TreeFindFolder;
	this.addFolder      = TreeAddFolder;
	this.addDocument    = TreeAddDocument;
	this.addHeader      = TreeAddHeader;
	this.display        = TreeDisplay;
	this.displayHeader  = TreeDisplayHeader;
	this.getVisibleRank = TreeGetVisibleRank;
	this.setFolderState = TreeSetFolderState;

	// initialize
	this.allFolders[0] = this.topNode;
	this.topNode.isopen = true;
}

function TreeSetFolderState(fid, isopen) {
	if (this.folderState) {
		if (isopen) {
			this.folderState.openfolder(fid);
		} else {
			this.folderState.closefolder(fid);
		}
		this.folderState.currentfocus = fid;
	}
}

function TreeGetVisibleRank() {
	var maxrank = 1;
	for (var i = 0; i < this.allFolders.length; i++) {
		with (this.allFolders[i]) {
			if (isopen && (documents.length || folders.length) && (rank >= maxrank)) {
				maxrank = rank;
			}
		}
	}
	return maxrank;
}

// find a folder by it's id
function TreeFindFolder(id) {
	var objref = -1;
	for (var i = 0; i < this.allFolders.length; i++) {
		if (this.allFolders[i].id == id) {
			objref = this.allFolders[i];
			break;
		}
	}
	return objref;
}

// add a folder item to the tree object
function TreeAddFolder(id, parentid, title, isopen, flags) {
	var objref;
	var newobj;
	objref = this.findFolder(parentid);
	// if we don't find the parent folder use "Group Documents"
	if (objref == -1) {
		objref = this.topNode;
	}
	newobj = objref.AddFolder(id, title, isopen, flags);
	if (newobj != -1) {
		this.allFolders[this.allFolders.length] = newobj;
		if (this.folderState && (this.folderState.expandall || (this.folderState.find(id) != -1))) {
			newobj.isopen = true;
		}
	}
}

// add a document item to the tree object
function TreeAddDocument(id, parentid, title, filename, filesize, datetime) {
	var objref = this.findFolder(parentid);
	// if we don't find the parent folder use "Group Documents"
	if (objref == -1) {
		objref = this.topNode;
	}
	objref.AddDocument(id, title, filename, filesize, datetime);
}

// add the column headers
// *****  NOTE *****
// the call to add the header MUST be done AFTER
// the addDocument calls, so we know the max rank
function TreeAddHeader(col1, col2, col3, col4, alignment) {
	if (col1 != "") { this.header[0] = col1; }
	if (col2 != "") { this.header[1] = col2; }
	if (col3 != "") { this.header[2] = col3; }
	if (col4 != "") { this.header[3] = col4; }
	if ((alignment != "") && (alignment != null)){this.headerAlign = alignment;}
}

// display the tree
function TreeDisplay() {
	this.maxrank = this.getVisibleRank();
	var cols = this.maxrank + this.docColumns;

	// set first line color to main color
	bgUseMainColor = true;

	// now write the table with all the tree entries
	var textOut = "<table width=100% border=0 cellspacing=0 cellpadding=3>" + '\n';
		textOut += this.displayHeader(cols);
		for (var i = 0; i < this.topNode.folders.length; i++) {
			textOut += this.topNode.folders[i].display(cols);
		}
	textOut += "</table>" + '\n';

	if (isNav) {
		var docobj = eval('document.layers["' + this.container + '"].document');
		docobj.open();
		docobj.write(textOut);
		docobj.close();
	} else {
		var divobj;
		if (document.getElementById) {
			divobj = document.getElementById(this.container);
		} else {
			divobj = eval("document.all." + this.container);
		}
	divobj.innerHTML = textOut;
	}
	if (this.debug) {
		var debugwin = window.open("/docs/new.html", "debugwin");
		debugwin.document.open("text/html", "replace");
		debugwin.document.write(textOut);
		debugwin.document.close();
	}
}

// Tree header
function TreeDisplayHeader() {
	var header = "";
	header += "<tr valign=\"top\" height=22 " + bgHeadColor + ">" + '\n' +
	          "<td width=\"25%\"><table border=0 cellpadding=0 cellspacing=0><tr valign=\"top\"><td width=16>" + imgStore.getTag("spacer16", "") + "</td>" + '\n' +
	          "<td width=4>" + imgStore.getTag("spacer4", "") + "</td>" + '\n' +
	          "<td nowrap align=" + this.headerAlign + " class=\"copyHead\">" + this.header[0] + "</td></tr></table></td>" + '\n';
	for (var i = 1; i < this.header.length; i++) {
		header += "<td width=\"" +  75/(this.header.length - 1) + "%\" nowrap align=" + this.headerAlign + " class=\"copyHead\">" + this.header[i] + "</td>" + '\n';
	}
	header += "</tr>" + '\n';
	return header;
}

// This is the tree object 'class' factory - it creates a tree object and
// returns the reference to the global object - and keeps a copy of the ref
// for internal use.
var theTree = -1;

function CreateTreeObject() {
	theTree = new TreeObject();
	return theTree;
}

// Event handlers
function clickHandler(isopen, id) {
	var fobj = theTree.findFolder(id);
	if (fobj != -1) {
		fobj.isopen = !isopen;
		theTree.setFolderState(id, fobj.isopen);
		theTree.display();
	}
}