// this is mylib.js
// this library supports menu of depth 1
// it also supports direct menu item

// * the root *
var allobject = new Array();
var root = new Group(null, "Client Menu");
var doc = document;
var plusimg = new  Image(10, 10);
var minusimg = new Image(10, 10);
var docimg = new Image(10, 10);
var docheight = -1;

// *******************
// * the Group class *
// *******************
function Group(parent, desc) {
	// properties
	this.parent = parent;
	this.description = desc;
	this.children = new Array();
	this.navObject = null;
	this.icon = null;

	// steate mgt
	this.id = nextId(this);
	this.isOpen = true;

	// methods
	this.display = displayGroup;
	this.toggle = toggleGroup;
	this.getHeight = groupHeight;

	this.appendGroup = appendGroup;
	this.appendMenu = appendMenu;
}

function groupHeight() {
	var th = this.navObject.clip.height;
	if ( this.isOpen ) {
		for ( var i = 0 ; i < this.children.length ; ++i )
			th += this.children[i].navObject.clip.height;
	}
	return th;
}

function displayGroup() {
	if ( this != root ) {
		if ( NS ) {
			doc.writeln("<layer name='group" + this.id + "' top='" + doc.yPos + "' visibility=show>");
		}
		doc.write("<table ");
		if ( IE ) {
			doc.write(" id='group" + this.id + "' style='visiblity=block' cellpadding='0' ");
		} else {
			doc.write(" cellpadding='0' ");
		}
		doc.write(" border=0 cellspacing='0'>");
		doc.write("<tr>");
		doc.write("<td height='28px' valign='middle' width='100%' wrap>");
		doc.write("<a href='javascript:clickOnNode(" + this.id + ")'  onmouseover=\"javascript:windowStatus('" + this.description + "');return true;\">");
		if (this.id!=1){ 
		doc.write("<img border='0' vspace=5 src='images/hr_line.gif' width='169' height='3'><br>");
        }
        doc.write("<img name='img" + this.id + "' src='" + imagespath + "/clear.gif' height=0 width=0 border=0>&nbsp;");
		doc.write("<b><span style='font-size: 8.5pt' valign=middle>");
		doc.write(this.description);
		doc.write("</span></b>");
		doc.write("</a>");

		doc.write("</td></tr></table>");
		if ( NS ) {
			doc.write("</layer>");
		}

		// get the objects
		if ( IE ) {
			this.navObject = document.all["group" + this.id];
		} else {
			this.navObject = document.layers["group" + this.id];
			doc.yPos += this.navObject.clip.height;
		}
		this.icon = eval("document.img" + this.id);
	}

	// display it's childs
	for ( var i  = 0 ; i < this.children.length ; ++i )
		this.children[i].display();
}

function toggleGroup() {
	if ( this.isOpen ) {
		if ( NS ) {
			var th = 0;
			for ( var i = 0 ; i < this.children.length ; ++i ) {
				this.children[i].hide();
				th += this.children[i].navObject.clip.height;
			}
			th = 0 - th;

			// which is next direct element ?
			var lid = this.children[this.children.length - 1].id;
			for ( var i = lid + 1 ; i < allobject.length ; ++i )
				allobject[i].navObject.moveBy(0, th);
		} else if ( IE ) { 
			for ( var i = 0 ; i < this.children.length ; ++i ) {
				this.children[i].navObject.style.display = "none";
			}
		}
		this.icon.src = plusimg.src;
	} else {
		if ( NS ) {
			var th = 0;
			for ( var i = 0 ; i < this.children.length ; ++i ) {
				this.children[i].show();
				th += this.children[i].navObject.clip.height;
			}
			th = 0 + th;

			// which is next direct element ?
			var lid = this.children[this.children.length - 1].id;
			for ( var i = lid + 1 ; i < allobject.length ; ++i )
				allobject[i].navObject.moveBy(0, th);
		} else if ( IE ) {
			for ( var i = 0 ; i < this.children.length ; ++i ) {
				this.children[i].navObject.style.display = "block";
			}
		}
		this.icon.src = minusimg.src;
	}
	this.isOpen = ! this.isOpen;
}

function appendGroup(desc) {
	var g = new Group(this, desc);
	var l = this.children.length;
	this.children[l] = g;
	return g;
}

function appendMenu(desc, url) {
	var m = new Menu(this, desc, url);
	var l = this.children.length;
	this.children[l] = m;
	return m;
}


// ******************
// * the Item class *
// ******************
function Menu(parent, desc, url) {
	// properties
	this.parent = parent;
	this.description = desc;
	this.url = url;
	this.id = nextId(this);
	this.navObject = null;
	this.getHeight = menuHeight;

	// methods
	this.display = displayMenu;
	this.hide = hideMenu;
	this.show = showMenu;
}

function displayMenu() {
	var cls = 'submenu';
	if ( NS ) {
		doc.write("<layer name='menu" + this.id + "' top='" + doc.yPos + "' visibility=show>");
	}
	 
	doc.write("<table ");
	if ( IE ) {
		doc.write(" id='menu" + this.id + "' style='visiblity=block' cellpadding='2' ");
	} else {
		doc.write(" cellspaing='2' ");
	}
	doc.writeln(" border='0' cellpadding='0' cellspacing='0'  bordercolor='#111111' width='100%'  >");
	doc.writeln("<tr><td  nowrap >");
	if ( this.parent == root ) {
		//doc.write("<img src='" + imagespath + "/doc.gif' width=15 height=15>&nbsp;");
		//--> doc.writeln("<img src='" + imagespath + "/minus.gif' width=10 height=10>");
		doc.writeln("<img src='" + imagespath + "/doc.gif' width=18 height=10>");
		cls = 'mainmenu';
	} else {
		doc.writeln("<img src='" + imagespath + "/clear.gif' height=10 width=10> ");
	}
	// doc.write("<img src='" + imagespath + "/doc.gif' width=18 height=18>");
	//doc.writeln("<a href='" + this.url + "' target='" + target + "' onmouseover=\"javascript:windowStatus('" + this.description + "');return true;\">" + this.description + "</a>");
	doc.writeln("<a href='" + this.url + "' target='" + target + "' onmouseover=\"javascript:windowStatus('" + this.description + "');return true;\"><span style='font-size: 8.5pt'>" + this.description + "</span></a>");
	doc.writeln("</td></tr></table>");
	if ( NS ) {
		doc.writeln("</layer>");
	}

	// get the objects
	if ( IE ) {
		this.navObject = document.all["menu" + this.id];
	} else {
		this.navObject = document.layers["menu" + this.id];
		doc.yPos += this.navObject.clip.height;
	}
}

function hideMenu() {
	if ( NS )
		this.navObject.visibility = "hidden"
	else if ( IE )
		this.navObject.style.display = "none";
}

function showMenu() {
	if ( NS )
		this.navObject.visibility = "show"
	else if ( IE )
		this.navObject.style.display = "block";
}

function menuHeight() {
	return this.navObject.clip.height;
}

// misc functions
function nextId(obj) {
	var l = allobject.length;
	allobject[l] = obj;
	return l;
}

function initializeDocument() {
	// detect the browser
	NS = ! ( IE = ( doc.all != null ) )
	IE = ! ( NS = ( doc.layers != null ) )
	if ( ! doc.yPos ) 
		doc.yPos = 80;
	plusimg.src = imagespath + "";
	minusimg.src = imagespath + "";
	doc.src = imagespath + "/doc.gif";
	root.display();

	if ( NS ) {
		// get the total height before 
		var th = 0;
		for ( var i = 0 ; i < root.children.length ; ++i )
			th += root.children[i].getHeight();
		// alert(th);
		th += doc.layers["tbl"].clip.height;
		docheight = doc.height = th;
	}

	
	// now hide
	for ( var i = 0 ; i < root.children.length ; ++i )
		clickOnNode(root.children[i].id);
}

function clickOnNode(n) {
	if ( allobject[n].children )
		allobject[n].toggle();
}


function windowStatus(status) {
	window.status = status;
	return true;
}

function relinkimages() {
	// skip 0th element i.e. "root"
	for ( var i = 1 ; i < allobject.length ; ++i ) {
		if ( allobject[i].children ) {
			if ( allobject[i].isOpen )
				allobject[i].icon.src = minusimg.src;
			else
				allobject[i].icon.src = plusimg.src;
		}
	}
	doc.height = docheight;
	return false;
}

function showTable() {
	doc.writeln("<body alink='blue' vlink='blue' onresize='javascript:relinkimages();return false;'>");
	if ( doc.layers ) 
		doc.writeln("<layer name='tbl' id='tbl' top=8 visibility='show'><span style='font-size: 8.5pt'>");
	if ( doc.layers ) {
		doc.writeln("</span></layer>");
		doc.yPos = doc.layers["tbl"].clip.height;
	}
}