var isNC    = (document.layers) ? 1 : 0;
var isOPERA = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var isIE    = (document.all && !isOPERA)? true : false;
var isDOM   = (document.getElementById && !isIE && !isOPERA)? true : false;


////General function////////////////////////////////////////////////////////////////////////////
function getDivStyle(divname) {
	 var style;
	 if (isDOM) { style = document.getElementById(divname).style; }
	 else { style = isIE ? document.all[divname].style
						 : document.layers[divname]; } // NS4
	 return style;
}
function setPosition(posObj,showObjStyle,offx,offy) {
	showObjStyle.left = posObj.x() + offx;
	showObjStyle.top = posObj.y() + offy;
}
function getParentDiv(obj){
	if(!obj) return;
	if(obj.tagName == "DIV") return obj;
	return getParentDiv(obj.parentNode);
}
function getAbsX(elt,isInDiv) { 

	var x =  (elt.x) ? elt.x : getAbsPos(elt,"Left"); 
	if(!isInDiv) return x;
	var divobj = getParentDiv(elt);
	
	if(divobj && isDOM) return x + divobj.offsetLeft;
	return x;
}
function getAbsY(elt,isInDiv) { 
	var y = (elt.y) ? elt.y : getAbsPos(elt,"Top"); 
	if(!isInDiv) return y;
	var divobj = getParentDiv(elt);
	if(divobj && isDOM) return y + divobj.offsetTop;
	return y;
}
function getAbsPos(elt,which) {
	 iPos = 0;
	 while (elt != null) {
	  iPos += elt["offset" + which];
	  elt = elt.offsetParent;
	 }
	 return iPos;
}
function getImgObj(objName){
	if (isIE) return document.images[objName];
	if (isDOM)return document.getElementById(objName);
	return document.images[objName];
}



/////Classes//////////////////////////////////////////////////////////////////////////////////////
function posImg(objName,isInDiv){ //class posImg(ImgObj)
	this.obj = getImgObj(objName);
	this.isInDiv = isInDiv;
	this.x = function (){ 
		return getAbsX(this.obj,this.isInDiv);}
	this.y = function (){ 
		return getAbsY(this.obj,this.isInDiv);}
}

function menuContainer(divObjName,posImgName,width,offx,offy,isInDiv){ //class menuContainer(parameters)
	this.divObjName = divObjName;
	this.posImgName = posImgName;
	this.offx = offx;
	this.offy = offy;
	//if(isDOM) this.offy = this.offy + 5;
	this.divObjStyle = "";
	this.posImgObj = "";
	this.width = width;
	this.itemlist = new Array();
	this.isInDiv = isInDiv;
	this.timerID = -1;

	this.attachedimg = "";
	this.attachedimgsrc = "";

	this.additem=function (name,title,link,target){
		this.itemlist = this.itemlist.concat([new menuitem(name,title,link,target)]);
	}
	this.menuw=function (){
		with(this){
			var str = "";
			str +='<div id="'+divObjName+'" name="'+divObjName+'" style="position: absolute; left:0; top:0; visibility:hidden;">\n';
			str +='<table width='+width+' cellpadding='+mc.cellpadding+' cellspacing='+mc.cellspacing+' border='+mc.border+' bgcolor='+ mc.tablecolor +' style=\'filter="progid:DXImageTransform.Microsoft.Shadow(Color=#000000, Strength=3, Direction=135);"\'>\n';
			for(var i=0;i<itemlist.length;i++){
				str += itemlist[i].menuw();
			}
			str +='</table>\n';	
			str +='</div>\n';	
			document.write(str);
		}	
	}
	this.menupos=function (){
		var nonIEoffsety = 1;
		if(!isIE) nonIEoffsety = 3;
			this.divObjStyle.left = this.posImgObj.x() + this.offx;
			this.divObjStyle.top = this.posImgObj.y() + this.offy + nonIEoffsety;						
			}
	this.menuinit=function(){
			this.divObjStyle = getDivStyle(this.divObjName);
			this.posImgObj = new posImg(this.posImgName,this.isInDiv);
			}	
	this.show=function(){
		//window.status = this.divObjName
		this.divObjStyle.visibility = "visible"; 
		if(this.timerID != -1) clearTimeout(this.timerID);
		this.timerID = -1;	
	}
	this.hidden=function(){
		if(this.attachedimg !=""){
			getImgObj(this.attachedimg).src = this.attachedimgsrc;
			this.attachedimg = "";
			this.attachedimgsrc = "";
		}
		this.divObjStyle.visibility = "hidden"; 
		if(this.timerID != -1) clearTimeout(this.timerID);
		this.timerID = -1;
	}
	this.isShow=function(){
		if(this.divObjStyle.visibility == "hidden") return false;
		return true;
	}

}
function menuitem(name,title,link,target){ //name = div name in Child
	this.name = name;
	this.title = title;
	this.link = link;
	this.target = target;
	this.menuw=function(){
		var isMac = false
		//if(navigator.platform == "MacPPC") isMac = true
		var str = '';
		var action = ' onMouseOver = "mc.overCell(this);mc.sCon(\''+this.name+'\');" onMouseOut = "mc.outCell(this);mc.hCon(\''+this.name+'\');" onMouseDown="location=\''+this.link+'\'"';
		//var action2 = '';//' onMouseOver = "mc.sCon(\''+this.name+'\');" onMouseOut = "mc.hCon(\''+this.name+'\');" ';

		str +='<tr><td id="cell'+this.name+'" name="cell'+this.name+'" class="'+mc.celloffStyle+'" '
if(mc.getindex(this.name) != -1) str +=" background='"+mc.arrow+"' "
		str +='style=\''
if(mc.getindex(this.name) != -1) str +="background-repeat: no-repeat;background-position: 97%;'";
		str +='cursor:pointer;padding-left: 5px;'		
		str +='\' bgcolor='+mc.cellcolor+' ';
		str +=action;
		str +='>';
		str += '<img id="'+this.name+'p" name="'+this.name+'p" width=1 height=1 border=0 src="1x1.gif">';
		str += ''+this.title+'';		
		str +='</td></tr>';
		return str;
	}
}

function ContainerNode(name,containerObj){ 
	this.name = name;
	this.con = containerObj;
}
function menuControl(){	//containerName m0, m1, m2 and matching the index of containerList
	this.containerList = new Array();
	this.currentContainer = "";

	this.cellpadding = 0;
	this.cellspacing = 0;
	this.tablecolor = "";
	this.border = 0;
	this.cellcolor = "";
	this.cellonStyle = "";
	this.celloffStyle = "";
	this.cellonAStyle = "";
	this.celloffAStyle = "";
	this.arrow = "js/arrow.gif";


	this.overCell= function(obj){
		if(!obj) return
		if(obj.tagName == "TD"){
			obj.className= this.cellonStyle;
			if(isIE){
				this.outallCell(obj);
			}
			if(!isIE){
				this.cellalltdstyle(obj,this.cellonStyle);
			}

		}
	}
	this.outCell= function(obj){
		if(!obj) return
		if(obj.tagName == "TD"){
			obj.className= this.celloffStyle;
			if(!isIE){
				this.cellalltdstyle(obj,this.celloffStyle);
			}
		}
	}
	this.outallCellid=function(objid){

		var tdobj = this.getContainer(objid);
		if(!tdobj) return;
		for(var j=tdobj.itemlist.length-1;j>=0;j--){
			this.outCell(document.all["cell"+objid+"_" +j]);
		}
		//window.status =  ;
	}
	this.outallCell=function(obj){
		var tdstr = (obj.id).substr(4);
		var i = tdstr.length
		while(i > 0 && tdstr.indexOf("_") > 0){
			i--;
			if(tdstr.substr(i,1) == "_") break;
		}
		var tdobj = this.getContainer(tdstr.substr(0,i));
		for(var j=tdobj.itemlist.length-1;j>=0;j--){
			if(obj.id != "cell"+tdstr.substr(0,i)+"_" +j) this.outCell(document.all["cell"+tdstr.substr(0,i)+"_" +j])
		}
		//window.status =  ;
	}
	this.cellalltdstyle= function(obj,sty){
		if(!obj) return;
		for(var i=0;i<obj.childNodes.length;i++){
			if(obj.childNodes[i].tagName == "TD") obj.childNodes[i].className = sty;
			this.cellalltdstyle(obj.childNodes[i],sty);
		}
	}
	this.addCon=function(containerName, containerPosImg,containerWidth,containerOffx,containerOffy,isInDiv){
		with(this){
			containerList = containerList.concat([new ContainerNode(containerName,new menuContainer(containerName, containerPosImg,containerWidth,containerOffx,containerOffy,isInDiv))]);
		}
	}
	this.additem=function(parentconName,itemName,title,link,target){
		var obj = this.getContainer(parentconName);
		obj.additem(itemName,title,link,target);
	}
	this.menuw=function(){
		for( var i=0;i< this.containerList.length ;i++)
			this.containerList[i].con.menuw();
	}
	this.menuinit=function(){
		for(var i=0;i< this.containerList.length ;i++)
			this.containerList[i].con.menuinit();
	}
	this.parent=function(containerName){
		if(containerName.indexOf("_") == -1) return containerName;
		for(var i=containerName.length;i>=0;i--){
			if(containerName.substr(i,1) == "_"){
				return containerName.substr(0,i);
			}
		}
		return "";
	}
	this.sCon=function(containerName,img,src){
		this.currentContainer = this.parent(containerName);
		this.hOther(containerName);
		if(containerName.indexOf("_") < 0 && isIE)
			this.outallCellid(containerName);
		this.realShow(containerName,img,src);
//window.status = this.currentContainer
if((this.currentContainer).indexOf("_") > 0 && isIE)
	this.overCell(document.all["cell"+this.currentContainer]);

	}
	this.realShow=function(containerName,img,src){
		var obj = this.getContainer(containerName);
		if(!obj) return;
		obj.menupos();
		obj.show();
		if(src && obj.attachedimgsrc == ""){
		var attachedimg = getImgObj(img)
		obj.attachedimg = img;
		obj.attachedimgsrc = attachedimg.src;
		attachedimg.src = src;
		//window.status = src;
		}

	}
	this.hOther=function(containerName){ 
		var pConName;
		if(containerName.indexOf("_") < 0){
			for(var i=0;i< this.containerList.length ;i++)
				this.realHidden(this.containerList[i].name);
			return;
		}
		for(var i=containerName.length;i>=0;i--){
			if(containerName.substr(i,1) == "_"){
				pConName = containerName.substr(0,i);
				//window.status += ";" +parseInt(containerName.substr(i+1))
				this.realhOther(pConName,parseInt(containerName.substr(i+1)));
				this.hOther(pConName);
			}
		}
	}
	this.realhOther=function(containerName,showItem){
		var obj = this.getContainer(containerName);

		for(var i=0;i<obj.itemlist.length;i++){
			if(i != showItem){
				this.realHidden(obj.itemlist[i].name);
			}
		}
	}
	this.hCon=function(containerName,img,src){
		this.currentContainer = "";
		var obj = this.getContainer(containerName);
		if(!obj) return;
		obj.menupos();
		if(obj.timerID != -1) clearTimeout(obj.timerID);
		obj.timerID = -1;
		obj.timerID = setTimeout ('mc.realHidden("'+containerName+'")', 500);
	}
	this.realHidden=function(containerName,img,src){	
		var obj = this.getContainer(containerName);
		if(!obj) return;		
		if(this.currentContainer.indexOf(containerName) >= 0){
			if(obj.timerID != -1) clearTimeout(obj.timerID);
			obj.timerID = -1;
			obj.timerID = setTimeout ('mc.realHidden("'+containerName+'")', 500);
			return;
		}else{
			this.hChild(containerName);
		}
		obj.hidden();
	}
	this.hChild=function(containerName){
		this.realhOther(containerName,-1);
		
	}
	this.getContainer=function(containerName){
		var index = this.getindex(containerName);
		if(index == -1) return null;
		return this.containerList[index].con;
	}
	this.getindex=function(containerName){
		for(var i=0;i<this.containerList.length;i++){
			if(this.containerList[i].name == containerName) return i;
		}
		return -1;
	}
}


