var evnt = {
	getSrcElement: function(evt) {
		return (evt ? evt.target : event.srcElement);
	},
		
	cancelBubble: function(evt) {
		if(evt)
			evt.stopPropagation();
		else
			window.event.cancelBubble = true;
	}
}

var oButton = {
	init: function(id) {
		var e = document.getElementById(id);
		if( e == null ) return;
		
		e.onmousedown=oButton.onmousedown;
		e.onmouseover=oButton.onmouseover;
		e.onmouseout=oButton.onmouseout;
	},
	
	//prevent the button movement on mozilla browser
	onmousedown: function(evt) {	
		evnt.cancelBubble(evt);
		
		return false;
	},
	
	onmouseover: function(evt) {
		var e = evnt.getSrcElement(evt);
		e = getTagObj(e, "BUTTON");
		e.className = "hover";
	},
	
	onmouseout: function(evt) {
		var e = evnt.getSrcElement(evt);
		e = getTagObj(e, "BUTTON");
		e.className = "";
	}
}

/** start sub folder menu objects **/
function fixHover(e) {
	e.style.width = e.offsetWidth + "px";
}

function MenuProto() {
	this._id = null;
	
	this.init = function(id) {
		this._id = id;
		var menu = document.getElementById(id);
		var list = menu.getElementsByTagName("DIV");
		for(var i=0; i < list.length; i++) {
			var item = list[i];
			
			item.onclick = this.onclick;
			item.onmouseover = this.onmouseover;
			item.onmouseout = this.onmouseout;
		}
	};
	
	this.onclick = function(evt) {
		var oMenu_clicked = evnt.getSrcElement(evt);
		if( oMenu_clicked != null && oMenu_clicked.nodeName != "DIV" ) {
			//for safari when user clicks on the text instead of DIV
			oMenu_clicked = oMenu_clicked.parentNode;
		}
		
		if( oMenu_clicked.tagName != "A" ) {
			var a = oMenu_clicked.getElementsByTagName("A");
			if( a != null && a[0] != null ) {
				document.location.href=a[0].href + a[0].returl;
			}
		}
	};
	
	this.onmouseover = function(evt) {
		var oMenu_mouseover = evnt.getSrcElement(evt);
		oMenu_mouseover = getTagObj(oMenu_mouseover, "DIV");		
		oMenu_mouseover.className = "hover";
		
		if( document.all ) {		
			//adjust width
			fixHover(oMenu_mouseover);
		}
	};
	
	this.onmouseout = function(evt) {
		var oMenu_mouseout = evnt.getSrcElement(evt);
		oMenu_mouseout = getTagObj(oMenu_mouseout, "DIV");		
		oMenu_mouseout.className = "";
	};
	
	this.hide = function() {
		var e = document.getElementById(this._id);
		e.style.visibility  = "hidden";
		this.hideShadow();
		resetMenuState(this._id);
	};
	
	this.show = function(eMenuList, top, left) {
		if( eMenuList != null ) {
			eMenuList.style.left = left;
			eMenuList.style.top = top;
			eMenuList.style.visibility = "visible";
			
			this.showShadow(eMenuList, top, left);
		}
	};
	
	this.toggle = function(eMenuList, top, left) {
		if( eMenuList.style.visibility == "visible" ) {
			this.hide();			
		} else {
			this.show(eMenuList, top, left);	
		}
	};
	
	this.showShadow = function(eMenuList, top, left) {
		var sh = document.getElementById(this._id + "_s");
		sh.style.left = left + 3;
		sh.style.top = top + 5;
		
		var oImg_h = document.getElementById(this._id + "_imgh");
		oImg_h.style.height = eMenuList.offsetHeight - 9;
		
		var oImg_w = document.getElementById(this._id + "_imgw");
		var width = eMenuList.offsetWidth;
		oImg_w.style.width = width - 9;
		
		sh.style.width = width + 2;
		sh.style.visibility = "visible";
	};
	
	this.hideShadow = function() {
		var sh = document.getElementById(this._id + "_s");
		if( sh != null )
			sh.style.visibility = "hidden";
	};
}
var oMenu = new MenuProto();

function ButtonMenuProto() {
	this._id = null;
	
	this.init = function(menuid) {
		var e = document.getElementById(this._id);
		e.onclick = this.onclick;
		e.onmousedown = this.onmousedown;
		e.onmouseover = this.onmouseover;
		e.onmouseout = this.onmouseout;

		e._menuid = menuid;
	};
	
	this.onclick = function(evt) {
		var oButton_clicked = evnt.getSrcElement(evt);
		if( oButton_clicked != null && oButton_clicked.nodeName != "BUTTON" ) {
			//for safari when user clicks on the text instead of button area
			oButton_clicked = oButton_clicked.parentNode;

			evnt.cancelBubble(evt);	//cancel text propagation
		}

		if( oButton_clicked != null ) {
			var eMenuList = document.getElementById(this._menuid);

			var top = oCoor.getTop(oButton_clicked) + oButton_clicked.offsetHeight;
			var left = oCoor.getLeft(oButton_clicked);
			
			eMenuList.style.visibility = "visible";
			eMenuList.style.top = top;
			eMenuList.style.left = left;
		}
	};
	
	this.onmousedown = function(evt) {
		evnt.cancelBubble(evt);
		
		return false;
	};
	
	this.onmouseover = function(evt) {
		var e = evnt.getSrcElement(evt);
		e = getTagObj(e, "BUTTON");
		e.className = "menubtn hover";
	};
	
	this.onmouseout = function(evt) {
		var e = evnt.getSrcElement(evt);
		e = getTagObj(e, "BUTTON");
		e.className = "menubtn";
	};
}

function ButtonMenu(btnid, menuid) {
	this._id = btnid;
}

ButtonMenu.prototype = new ButtonMenuProto();

function LinkMenuProto() {
	this._id = null;
	
	this.onclick = function(evt) {};
	
	this.init = function(menuid) {
		var e = document.getElementById(this._id);
		e.onclick = this.onclick;
		e.onmousedown = this.onmousedown;
		e.onmouseover = this.onmouseover;
		e.onmouseout = this.onmouseout;

		e._menuid = menuid;
	};
	
	this.onmousedown = function(evt) {
		evnt.cancelBubble(evt);
		
		return false;
	};
	
	this.onmouseover = function(evt) {
		if( document.all ) {
			var e = evnt.getSrcElement(evt);
			e.style.textDecorationUnderline = true;
		}
	};
	
	this.onmouseout = function(evt) {
		if( document.all ) {
			var e = evnt.getSrcElement(evt);
			e.style.textDecorationUnderline = false;
		}
	}
}

function LinkMenu(btnid) {
	this._id = btnid;
}

LinkMenu.prototype = new LinkMenuProto();

function SubMenuProto() {
	this._submenus = null;
	this._objName = null;
	this._id = null;
	this._style = null;
	this._curMouseEvt = null;
	this._btnid = null;
	this._bShowPath = false;
	this._disableSelOn = null;
	this.folderPath = null;
	
	//mouse events
	this.onclick = function(evt) {};//does nothing
	this.onmouseover = function(evt) {};	//does nothing
	this.onmouseout = function(evt) {};
	
	this.init = function(id) {
		var container = document.getElementById(id);
		
		var item, cn;
		var list = container.getElementsByTagName("DIV");		
		for(var i=0; i < list.length; i++) {
			item = list[i];
			if(this._disableSelOn != null) {
				var id = item.id.substring(2);
				if( this._disableSelOn[id] != null && this._disableSelOn[id] == true ) {
					this.disable(item);
				} else {
					this.enable(item);
				}

			}

			cn = item.className;
			if( cn != "pnodeDisable" && cn != "pnodeSelDisable" ) {
				item.onmouseover = this.onmouseover;
				item.onmouseout = this.onmouseout;
				
				//selected should not have an onclick
				if( cn == "" || cn == "pnode" || cn == "pnodeSel") {
					item.onclick = this.onclick;
				}
			}
		}
	};
	
	this.selectedPath = function(id) {
		var container = document.getElementById(id);
		var list = container.getElementsByTagName("DIV");
		var item;
		var path = false;
		for(var i=0; i < list.length; i++) {
			item = list[i];
			path = this.checkPath(item);
			if( path == true ) break;
		}
	};
	
	this.setPath = function(item) {
		if( item.className == "" )
			item.className = "selected";
		else
			item.className = "pnodeSel";
	};

	this.checkPath = function(item) {		
		if( this.folderPath == null ) return;
		
		if( item.className == "selected" || item.className == "pnodeSel" ) return;
		
		var id = (item.id == "0" || item.id == "1" ) ? item.id : item.id.substring(2);
		for( var i=0; i < this.folderPath.length; i++ ) {
			if( this.folderPath[i] == id ) {
				this.setPath(item);
				return true;
			}
		}
		
		return false;
	};
	
	this.removePath = function(item) {
		if( item.className == "selected" )
			item.className = "";
		else if( item.className == "pnodeSel" )
			item.className = "pnode";
	};
	
	this.resetSubPath = function(id) {
		if( this._submenus != null ) {
			var menuObj = this._submenus[id];
			if( menuObj != null ) {
				var list = menuObj.getElementsByTagName("DIV");
				for(var i=0; i < list.length; i++) {
					this.removePath(list[i]);
				}
			}
		}
	};
	
	this.resetFolderPath = function() {
		//remove from main
		var menuObj = document.getElementById(this._id);
		var list = menuObj.getElementsByTagName("DIV");
		for( var i=0; i < list.length; i++ ) {
			this.removePath(list[i]);
		}
	};
	
	this.execURL = function(url) {
		var oIf = document.getElementById("ifSubfolder");
		if( oIf != null ) {
			if( oIf.contentWindow ) {//IE5.5+,Mozilla 5.0+,Safari
				oIf.contentWindow.location.replace(url);
			} else if(oIf.contentDocument && oIf.contentDocument.location) {//For NS6
				oIf.contentDocument.location.replace(url);
			} else {
				oIf.src = url;
			}
		}
	};
	
	this.pnodeOnMouseOver = function(oMenu_mouseover) {
		this._curMouseEvt = oMenu_mouseover;
		
		//remove previous highlights and hide previous menus
		this.hideMenu(oMenu_mouseover);
		
		var cn = oMenu_mouseover.className;
		if( cn == "" ) {
			oMenu_mouseover.className = "hover";
			
		} else if( cn == "pnode" ) {
			oMenu_mouseover.className = "pnode hover";
		}
		
		if( document.all && oMenu_mouseover.parentNode.tagName != "TD" ) {
			//adjust width
			fixHover(oMenu_mouseover);
		}
	};
	
	this.pnodeOnMouseOut = function(evt) {
		this._curMouseEvt = null;
		
		var oMenu_mouseout = evnt.getSrcElement(evt);
		oMenu_mouseout = getTagObj(oMenu_mouseout, "DIV");
		
		if( oMenu_mouseout.className == "hover" )
			oMenu_mouseout.className = "";

	};
	
	this.getChildMenu = function(pid) {
		var pe = document.getElementById(pid);
		var e = document.getElementById("pf" + pid.substring(2));
		
		if( e != null && e._sync == true ) {
			this.setCoor(pid);
			
			var menuid = "pf" + pid.substring(2);
			this.init(menuid);
			this.setStyle(menuid);
			this.showSubMenu(menuid);

		} else {
			this.execURL("/mysearch/getFolder?t=get&fid=" + pid.substring(2) + "&obj=" + this._objName + "&pid=" + pid);
		}
	};
	
	this.setSubMenu = function(html, pid, fLevel) {
		var menuid = "pf" + pid.substring(2);
		var oMenu = document.getElementById(menuid);
		if( oMenu != null ) {//existing menu that needed to be resynced
			oMenu.innerHTML = html;
			oMenu._sync = true;
			
		} else {//new menu
			var e = this.createMenuBody(pid.substring(2));
			this._submenus[e.id] = e;
			if( fLevel != null )
				e["_fLevel"] = fLevel;

			e.className = this._style;
			e.innerHTML = html;
			e._sync = true;

			var oIframe = document.getElementById("ifSubfolder");
			document.body.insertBefore(e, oIframe);
		}
		
		if( this._curMouseEvt != null && this._curMouseEvt.id.substring(2) == pid.substring(2) ) {
			this.setCoor(pid);
			this.init(menuid);
			setTimeout(this._objName + ".showSubMenu('" + menuid + "')", 0);	//slow down firefox.
		}
	};
	
	this.showSubMenu = function(id) {
		if( this._curMouseEvt != null && this._curMouseEvt.id.substring(2) == id.substring(2) ) {
			//b/c it's sharing menu we have to make sure it's a clean sub menu by resetting selected folder.
			this.resetSubPath(id);
			if( this._bShowPath == true ) {
				this.selectedPath(id);
			}
			
			var fn = document.getElementById(id);

			fn.style.display = "block";
			fn.style.visibility = "visible";
			
			this.showShadow(id.substring(2), fn);
		}
	};
	
	this.hideMenu = function(e) {
		var ignoreid = "pf" + e.id.substring(2);

		var list = e.parentNode.getElementsByTagName("DIV");
		var item = null;

		for(var i=0; i<list.length; i++) {
			item = list[i];
			var cn = item.className;
			if( cn == "hover" ) {
				item.className = "";
			} else if( cn == "pnode hover" ) {
				item.className = "pnode";
			}
			
			if( item.className.indexOf("pnode") != -1 ) {
				var menu = this._submenus["pf"+item.id.substring(2)];
				if( menu != null && menu.style.visibility == "visible" && ignoreid != menu.id ) {
					menu.style.visibility = "hidden";
					
					this.hideShadow(item.id.substring(2));
					//menu.className = "hideSub";
					resetMenuState(menu.id);//recursively reset child menu
				}
			}
		}
	};
	
	this.hide = function() {
		var e = document.getElementById(this._id);
		e.style.visibility  = "hidden";
		
		this.hideShadow(this._id);
	};
	
	this.resetMenuState = function() {
		resetMenuState(this._id);
	};
	
	this.show = function(top, left) {
		//b/c it's sharing menu we have to make sure it's a clean sub menu by resetting selected folder.
		this.resetSubPath(this._id);
		if( this._bShowPath == true ) {
			this.selectedPath(this._id);
		}
		
		this.init(this._id);

		this.setShadowCoor(this._id, left, top);
		
		var e = document.getElementById(this._id);
		e.style.left = left;
		e.style.top = top;
		this.setZindex(e);
		
		e.style.visibility  = "visible";
		
		this.showShadow(this._id, e);
	}
	
	this.toggle = function(top, left) {
		var e = document.getElementById(this._id);
		if( e.style.visibility == "visible" ) 
			this.hide();
		else
			this.show(top, left);
	}
	
	this.setZindex = function(fn) {
		if( fn.style.zIndex == 0 ) {
			fn.style.zIndex = fn._fLevel + 3;
		}
	};
	
	this.setShadowZindex = function(sh, fn) {
		if( sh.style.zIndex == 0 ) {
			sh.style.zIndex = fn._fLevel+2;
		}
	};
	
	this.setCoor = function(pid) {
		var pfn = document.getElementById(pid);
		var fn = document.getElementById("pf" + pid.substring(2));
		var posXR, posXL, shadowLeft, shadowTop;
		
		this.setZindex(fn);
		
		fn.style.top = shadowTop = oCoor.getTop(pfn);
		var left = oCoor.getLeft(pfn);
		if( oBrowser.isSafari() == true && pfn.id.substring(0,2) != "ln" )
			posXR = left + (pfn.offsetWidth - 12);
		else
			posXR = left + (pfn.offsetWidth - 2);
			
		if( oBrowser.isSafari() == true && pfn.id.substring(0,2) != "ln" )
			posXL = left - (fn.offsetWidth + 10);
		else
			posXL = left - (fn.offsetWidth - 5);

		//determine it's going to be on the left or right of the parent menu
		if( (posXR + fn.offsetWidth) > document.body.clientWidth && posXL > 0 ) {						
			fn.style.left = shadowLeft = posXL;
		} else {
			fn.style.left = shadowLeft = posXR;
		}
		
		this.setShadowCoor(pid.substring(2), shadowLeft, shadowTop);
	};
	
	this.setShadowCoor = function(id, left, top) {
		var sh = document.getElementById("sh" + id);
		if( sh == null ) {
			this.createShadow(id);
			sh = document.getElementById("sh" + id);
		}
		
		sh.style.left = left + 5;
		sh.style.top = top + 5;
	};
	
	this.showShadow = function(id, fn) {
		var oImg_h = document.getElementById("sh" + id + "_imgh");
		oImg_h.style.height = fn.offsetHeight - 9;
		
		var oImg_w = document.getElementById("sh" + id + "_imgw");
		var width = fn.offsetWidth;
		oImg_w.style.width = width - 9;
		
		var sh = document.getElementById("sh" + id);
		sh.style.width = width;	
		this.setShadowZindex(sh,fn);
		
		sh.style.visibility = "visible";
	};
	
	this.hideShadow = function(id) {
		var sh = document.getElementById("sh" + id);
		if( sh != null )
			sh.style.visibility = "hidden";
	};
	
	this.resetSubMenu = function() {
		var e = document.getElementById("ifSubfolder");
		e.src = "/subfolder.htm";
	};
	
	this.resetState = function() {
		var e = document.getElementById(this._id);
		var list = e.getElementsByTagName("DIV");
		for(var i=0; i < list.length; i++) {
			var item = list[i];
			if( item.className == "pnode hover" ) {
				item.className = "pnode";
				
				
			}
		}
	};
	
	this.setStyle = function(menuid) {
		var menu = document.getElementById(menuid);
		if( menu.className != this._style ) {
			menu.className = this._style;
		}
	};
	
	this.createMenuBody = function(fid) {
		var e = document.createElement("DIV");
		e.className = "";
		e.id = "pf"+fid;

		return e;
	};
	
	this.createShadow = function(id) {
		var e = document.createElement("DIV");
		e.className = "shadow";
		e.id = "sh" + id;
		
		var str = "";
		if( document.all && oBrowser.isOpera() == false ) {
			str += '<div align="right"><img style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\', src=\'/mjsp/img/mj1/en_US/s_rt.png\');" width="5" height="4"/></div>';
			str += '<div align="right"><img id="sh' + id + '_imgh" style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\', src=\'/mjsp/img/mj1/en_US/s_r.png\');" width="5" height="1"/></div>';
			str += '<div><img style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\', src=\'/mjsp/img/mj1/en_US/s_bl.png\');" width="4" height="5"/><img id="sh' + id + '_imgw" style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\', src=\'/mjsp/img/mj1/en_US/s_b.png\');" width="1" height="5"/><img style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\'scale\', src=\'/mjsp/img/mj1/en_US/s_br.png\');" width="5" height="5"/></div>';
		} else {
			str += '<div align="right"><img src="/mjsp/img/mj1/en_US/s_rt.png" width="5" height="4"/></div>';
			str += '<div align="right"><img id="sh' + id + '_imgh" src="/mjsp/img/mj1/en_US/s_r.png" width="5" height="1"/></div>';
			str += '<div><img src="/mjsp/img/mj1/en_US/s_bl.png" width="4" height="5"/><img id="sh' + id + '_imgw" src="/mjsp/img/mj1/en_US/s_b.png" width="1" height="5"/><img src="/mjsp/img/mj1/en_US/s_br.png" width="5" height="5"/></div>';
		}
			   
		e.innerHTML = str;
		var oContent = document.getElementById("content");
		//document.body.insertBefore(e, oContent);
		oContent.appendChild(e);
	};
	
	this.sleep = function(oMenu_mouseover) {
		setTimeout(this._objName + ".wakeUp('" + oMenu_mouseover.id + "')", 350);
	};
	
	this.wakeUp = function(pid) {
		if( this._curMouseEvt != null && this._curMouseEvt.id == pid ) {
			this.getChildMenu(pid);
		}
	};
	
	this.cancelShow = function() {
		this._curMouseEvt = null;
	};
	
	this.disable = function(e) {
		if( e.className == "pnode" )
			e.className = "pnodeDisable";
		else if( e.className == "pnodeSel" )
			e.className = "pnodeSelDisable";
		else if( e.className == "" )
			e.className = "disable";
			
		if( e.onmouseover != null ) {
			e.onclick = null;
			e.onmouseover = null;
			e.onmouseout = null;
		}
	};
	
	this.enable = function(e) {
		if( e.className == "pnodeDisable" )
			e.className = "pnode";
		else if( e.className == "pnodeSelDisable" )
			e.className = "pnodeSel";
		else if( e.className == "disable" )
			e.className = "";
	};
	
	this.disableSelOn = function() {
		this._disableSelOn = {};
	};
	
	this.addDisableSel = function(id, bSel) {
		this._disableSelOn[id] = bSel;	
	};
}

//initializes the submenu events
function SubMenu(pid, name, style, bShowPath) {
	this._objName = name;
	this._id = pid;
	this._style = style;
	this._submenus = submenus;
	this._bShowPath = bShowPath;
}

var submenus = {};
function hideSubMenu() {
	var list = submenus;
	var item = null;

	for(var id in list) {
		item = list[id];
		
		if( item.style.visibility == "visible" ) {
			item.style.visibility = "hidden";
			var oSh = document.getElementById("sh" + id.substring(2));
			if( oSh ) {
				oSh.style.visibility = "hidden";
			}
			resetMenuState(item.id);
		}
	}
}

function resetMenuState(id) {
	var container = document.getElementById(id);
	var list = container.getElementsByTagName("DIV");
	for(var i=0; i < list.length; i++) {
		var item = list[i];
		var cn = item.className;
		if( cn == "hover" ) {
			item.className = "";
		} else if( cn == "pnode hover" ) {
			item.className = "pnode";
		}
		
		if( item.className  == "pnode" || item.className == "pnodeSel" ) {
			//get menu
			var menu = submenus["pf"+item.id.substring(2)];
			if( menu != null && menu.style.visibility == "visible" ) {
				menu.style.visibility = "hidden";
				
				//hide shadow
				var oSh = document.getElementById("sh" + item.id.substring(2));
				if( oSh ) {
					oSh.style.visibility = "hidden";
				}
				
				//menu.className = "hideSub";
				resetMenuState("pf"+item.id.substring(2));//recursively reset child menu
			}
		}
	}
}

SubMenu.prototype = new SubMenuProto();

/** end sub folder menu objects **/