/*Node class*/
function node(nodeid, html, secondlink, pnode){
	var cnodes = new Array();//Child Nodes Array
	//Variables
	this.nodeid = nodeid; //Node ID
	this.pnode = pnode; //Ref to parent node
	this.cnodes = cnodes; //Child nodes array
	this.html = html; //Inner HTML
	this.secondlink = secondlink; //Second Link
	//Methods
	this.attachNode = attachNode;
	this.getHTML = getHTML;
	this.getNodeHTML = getNodeHTML;
}

function attachNode(nid, html, secondlink,pid){
	if(pid == this.nodeid){
		this.cnodes[this.cnodes.length] = new node(nid, html, secondlink, this)
	}else{
		for(var i = 0; i < this.cnodes.length; i++){
			this.cnodes[i].attachNode(nid, html, secondlink, pid);
		}
	}
}

function getHTML(){
	var out = "";
	var res = "";
	if(this.html.indexOf(location.pathname + location.search) != -1 && this.html.charCodeAt(this.html.indexOf(location.pathname + location.search) + location.pathname.length + location.search.length) == 34 || (location.pathname + location.search) == this.secondlink){
		out = " ";
		for(var i = 0; i < this.cnodes.length; i++){
			out += this.cnodes[i].html;
		}
		return out;
	}else{
		for(var i = 0; i < this.cnodes.length; i++){
			res = this.cnodes[i].getHTML();
			if(res != ""){
				for(var j=0; j < this.cnodes.length; j++){
					if(j != i){
						out = out + this.cnodes[j].html;
					}else{
						out = out + this.cnodes[j].html + res;
					}
				}
			}
		}
		return out;
	}
}

function getNodeHTML(out){
	out += this.html;
	for(var i = 0; i < this.cnodes.length; i++){
		 out = this.cnodes[i].getNodeHTML(out);
	}
	return out;
}

/*Menu class*/
function menu(thead, struct, tfoot){
	var nodes = new Array();//Root elements
	this.nodes = nodes;
	this.thead = thead;//Table header
	this.tfoot = tfoot;//Table Footer
	this.getMenuHTML = getMenuHTML;
	this.getAllHTML = getAllHTML;
		
	for(var i = 0; i < struct.length; i++){
		if(struct[i][3] == null){
			this.nodes[this.nodes.length] = new node(struct[i][0],struct[i][1],struct[i][2],null);
		}else{
			for(var j = 0; j < this.nodes.length; j++){
				this.nodes[j].attachNode(struct[i][0],struct[i][1],struct[i][2],struct[i][3])
			}
		}
	}
}

function getMenuHTML(){
	var out = "";
	var res = "";
	for(var i = 0; i < this.nodes.length; i++){
		res = this.nodes[i].getHTML();
		out += this.nodes[i].html + res;
	}
	out = this.thead + out + this.tfoot;
	return out;
}

function getAllHTML(){
	var out = "";
	for(var i = 0; i < this.nodes.length; i++){
		out = this.nodes[i].getNodeHTML(out);
	}
	return out;
}


shown=''
function OpenFile(url,width,height) {

var Left = (screen.width - width) / 2 ;
var Top = (screen.height - height) / 2 ;

if( shown=='' || shown.closed) {

window.focus();
shown=window.open(url,"OpenFile","top="+Top+",left="+Left+",width="+width+",height="+height+",status=no,high");

}
else{
shown.close();
shown=window.open(url,"OpenFile","top="+Top+",left="+Left+",width="+width+",height="+height+",status=no,high");
}
return false;
}

