d=document;w=window;mousex=0;mousey=0;dv=null;_i = new Array();
// browser compability
var isDOM  = d.getElementById?true:false;
var isMSIE = (d.all?true:false) && (d.all.item?true:false);

if (isMSIE){
	var cn = (d.compatMode && d.compatMode == "CSS1Compat") ? true : false;
	var oCanvas = d.getElementsByTagName("HTML")[0];
}

// track mouse move
if (isMSIE){
	d.onmousemove=function(){
		if (cn){
			mousex=event.clientX+oCanvas.scrollLeft;
			mousey=event.clientY+oCanvas.scrollTop;
		}else{
			mousex=event.clientX+d.body.scrollLeft;
			mousey=event.clientY+d.body.scrollTop;
		}
		return true
	}
}else if (isDOM){
	d.onmousemove=function(e){
		mousex=e.pageX
		mousey=e.pageY
		return true
	}
}

// show tip after 1 second
function tip(){
	var t = dv.style;
	t.left = (mousex)+"px";
	t.top = (mousey+25)+"px";
	t.visibility="visible"
	return true
}

// show tip
function showtip(text){
	dv = d.createElement("DIV");
	dv.id="mytip";
	var mt = d.createTextNode(text);
	dv.className="tip";
	dv.appendChild(mt);
	d.body.appendChild(dv);
	w.setTimeout("tip()",1000);
	return true
}

// hide tip
function hidetip(){
	dv=d.getElementById("mytip");
	if (dv)d.body.removeChild(dv);
	return true
}

// MENUITEM class
function Item(deep,leaf){
	var Self = this;
	/* property */
	this.items=new Array();
	this.objitems = new Array();
	this.opened=true;
	this.leaf=leaf;
	this.label="";
	this.ref="";
	this.clone = new Array();
	
	/* create MENU ITEM (DOM) */
	var t = d.createElement("TABLE");
	t.border=0;
	t.cellPadding=0;
	t.cellSpacing=1;
	var tb = d.createElement("TBODY");
	this.id=t;
	var mr = d.createElement("TR");
	var md = d.createElement("TD");
	var mi1 = d.createElement("IMG")
	mi1.src = _i[1];
	mi1.border=0;

	var mi2 = d.createElement("IMG")
	mi2.src = _i[0];
	mi2.height=icon_h;
	mi2.width=icon_w;
	mi2.border=0;
	
	var md2 = d.createElement("TD");
	var md3 = d.createElement("TD");
	md3.width=icon_w;
	var mt = d.createTextNode("");
	var mi = d.createElement("IMG")
	mi.src = _i[11];
	mi.height=icon_h;
	mi.width=icon_w;
	mi.border=0;

	md2.width=icon_w;
	md.width="100%";
	md.appendChild(mt);
	md.className=window.className;
	md.onclick=accept2;
	md.onmouseover=function(){this.className=window.selclassName;}
	md.onmouseout=function(){this.className=window.className;}
	mr.onmouseover=function(){if(Self.tip)showtip(Self.tip);}
	mr.onmouseout=function(){if(Self.tip)hidetip();}
	
	for (var j=0;j<deep;j++){	
		var sd2 = d.createElement("TD");
		sd2.width=icon_w;
		var miv = d.createElement("IMG")
		miv.src = _i[0];
		for (var l=0;l<window.last.length;l++){
			if (window.last[l]==j)miv.src = _i[0];
		}
		miv.height=icon_h;
		miv.width=icon_w;
		miv.border=0;
		sd2.appendChild(miv);
		mr.appendChild(sd2);	
	}

	mr.appendChild(md3);	
	mr.appendChild(md2);	
	mr.appendChild(md);
	tb.appendChild(mr);
	md2.appendChild(mi1);
	md3.appendChild(mi2);
	t.appendChild(tb);
		
	var etxt = d.createTextNode("&nbsp;");
		
	if (!leaf){	
		var sr = d.createElement("TR");
		var sd = d.createElement("TD");
		sd.colSpan=deep+3;
		sr.appendChild(sd);
		tb.appendChild(sr);
		this.child = sd;
	}else{this.child=null}
	this.icon = mi1;
	this.sign = mi2;
	mi2.onclick=accept;
	
	/* Methods (prototype) */
	this.setLabel=setLabel;
	this.setImage=setImage;
	this.getLabel=getLabel;
	this.accept=accept;
	this.accept2=accept2;
	this.setRef=setRef;
	this.getRef=getRef;
	
	/* Methods (implement) */
	function setLabel(name){
		Self.label=name;
		mt.nodeValue=name;
	}

	function getLabel(){
		return Self.label;
	}

	function setRef(ref){
		Self.ref=ref;
	}

	function getRef(){
		return Self.ref;
	}
	
	function setImage(imagesrc,ref){
		Self.imagesrc=imagesrc;
		md.replaceChild(mi,mt)
		mi.src = imagesrc;
		Self.ref=ref
	}

	function accept2(){
		if (Self.ref){
			window.location=Self.ref
			return true;
		}
	}
	function accept(){	
		if (Self.opened){
			var s = Self.icon.src;
			if(s.indexOf(_i[2])>=0)md.className=window.selclassName;
			Self.opened=false;
			if (Self.items.length){
				mi2.src=_i[1];
			}
			for (var j=0;j<Self.items.length;j++){
				sd.removeChild(Self.items[j]);
			}
			if (Self.items.length)tb.removeChild(sr);
		} else {
			Self.opened=true;
			if (Self.items.length){
				mi2.src=_i[2];
			}
			for (var j=0;j<Self.items.length;j++){
				sd.appendChild(Self.items[j]);
			}
			if (Self.items.length)tb.appendChild(sr);
		}
	}
	return this;
}

function menu(id,m,imgstr,menustyle,itemstyle,title,titleicon,titlestyle,mbg){
	var Self=this;
	_i=imgstr;
	/* Properties*/
	this.items = new Array();
	this.objitems = new Array();
	this.id=id;
	this.retValue=null;
	this.deep=0
	this.clone = new Array();
	
	/* create MENU (DOM) */
	var t = d.createElement("TABLE");if (isMSIE)t.style.cursor='hand';else t.style.cursor='pointer';
	t.border=0;
	t.cellPadding=0;
	t.cellSpacing=0;t.width="100%";
	var tb = d.createElement("TBODY");
	t.appendChild(tb);

	// title
	if (title){
	var tr = d.createElement("TR");tb.appendChild(tr);
	var tdheader = d.createElement("TD");tr.appendChild(tdheader);

	var tdv = d.createElement("DIV");
	tdv.className=titlestyle;
	var t1 = d.createElement("TABLE");
	t1.border=0;t1.cellPadding=0;t1.width="100%";t1.cellSpacing=0;t1.className=titlestyle;
	var tb1 = d.createElement("TBODY");
	t1.appendChild(tb1);

	var tr = d.createElement("TR");
	tb1.appendChild(tr);
	if (titleicon){
		var td1 = d.createElement("TD");
		td1.rowSpan=2;td1.vAlign="bottom";
		var mi1 = d.createElement("IMG")
		mi1.src = titleicon;
		td1.appendChild(mi1);
		tr.appendChild(td1);
		td1 = d.createElement("TD");td1.height="13px";td1.width="100%"
		tr.appendChild(td1);
		td1 = d.createElement("TD");
		tr.appendChild(td1);
		tr = d.createElement("TR");
		tb1.appendChild(tr);
	}
	var td2 = d.createElement("TD");
	this.headeropen=1;td2.width="100%"
	var ma = d.createElement("A");
	ma.href="javascript:void(0);"
	td2.style.background="url("+_i[5]+") repeat-y left";
	var txt = d.createTextNode(title);
	ma.appendChild(txt);
	td2.appendChild(ma);

	tr.appendChild(td2);
	Self.title=tdv;
	tdv.onclick=function(){
		if (Self.headeropen){d.getElementById(Self.id).style.height=Self.title.offsetHeight;Self.titlesign.src=_i[4];Self.headeropen=0}
		else {d.getElementById(Self.id).style.height="auto";Self.titlesign.src=_i[3];Self.headeropen=1}
	}

	var td3 = d.createElement("TD");
	tr.appendChild(td3);
	var ma = d.createElement("A");
	ma.href="javascript:void(0);"
	mi1 = d.createElement("IMG");mi1.border=0;
	mi1.src = _i[3];
	Self.titlesign=mi1;
	ma.appendChild(mi1);
	td3.appendChild(ma);

	tdv.appendChild(t1);
	tdheader.appendChild(tdv);
	}
	// end title

	tr = d.createElement("TR");
	tb.appendChild(tr);
	var td = d.createElement("TD");
	tr.appendChild(td);
	td.className=mbg;
	window.showlines=false;
	window.className=menustyle;
	window.selclassName=itemstyle;
	window.last=new Array();
	
	/* Methods (prototype) */
	this.init=init;
	this.show = show;
	this.save=save;
	this.restore=restore;
	this.getItemByLabel=getItemByLabel;
	
	/* constructor call */
	this.init(m,td,Self);

	/* constructor */
	function init(m,n,s,last){
		if (typeof(m)=="object"){
		for (var i=0;i<m.length;i++){
			Self.deep++;
			var leaf = (typeof(m[i][4])!="object");
			var it = new Item(Self.deep-1,leaf);
			var l = m[i][0];
			var r = m[i][4];
			if (typeof(r)=="object") r="";
			it.tip=m[i][3];
			if ((l.indexOf(".gif")==-1)&&(l.indexOf(".jpeg")==-1)){
				it.setLabel(l);
				it.setRef(m[i][1]);
			} else {
				it.setImage(m[i][2],m[i][1]);
			}
			if (typeof(m[i][4])=="object"){
				if (!window.showlines){
					it.sign.src=_i[2];
				}
				if (m[i][2]){it.icon.src=m[i][2];}
				else{it.icon.src=_i[0];}
				
			}else{
				if (!window.showlines){
					it.sign.src=_i[0];
				}
				if (m[i][2]){it.icon.src=m[i][2];}
				else{it.icon.src=_i[0];}
			}	
			n.appendChild(it.id);
			s.items[s.items.length] = it.id;
			s.objitems[s.objitems.length] = it;
			if (i==m.length-1) {
				window.last[window.last.length]=Self.deep-1;
			}
			Self.init(m[i][4],it.child,it,last);
		}
		}
		if (window.last.length)
		if (window.last[window.last.length]==Self.deep-1){window.last.length--;}

		Self.deep--;
	}

	/* Methods (implement) */
	function getItemByLabel(name){
		if (arguments.length==1){var n=Self;}else{var n=arguments[1];}
		for (var i=0;i<n.objitems.length;i++){
			if (n.objitems[i].getLabel()==arguments[0]){
				Self.retValue=n.objitems[i];
				return n.objitems[i];
			}
			getItemByLabel(arguments[0],n.objitems[i]);
		}
		return Self.retValue;
	}	
	
	function save(name){
		if (arguments.length==1){var n=Self;}else{var n=arguments[1];}
		for (var i=0;i<n.objitems.length;i++){
			n.objitems[i].clone[name] = n.objitems[i].opened;
			save(arguments[0],n.objitems[i]);
		}
	}	

	function restore(name){
		if (arguments.length==1){var n=Self;}else{var n=arguments[1];}
		for (var i=0;i<n.objitems.length;i++){
			restore(arguments[0],n.objitems[i]);
			if (n.objitems[i].clone[name] != n.objitems[i].opened){
				n.objitems[i].accept();
			}
		}
	}	
	
	function show(){
		d.getElementById(Self.id).appendChild(t);
	}
	return this;
}

