var is_opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
var is_ie    = (document.all && !is_opera)? true : false;
var is_dom   = (document.getElementById && !is_ie && !is_opera)? true : false;

var currentMcName = '';

function getParentDiv(obj){

	if(!obj) return;

	if(obj.tagName == "DIV") return obj;

	return getParentDiv(obj.parentNode);

}

function getAbsX(currentObj,insideDiv) { 

	var x =  (currentObj.x) ? currentObj.x : getAbsPos(currentObj,"Left"); 

	if(!insideDiv) return x;

	var objDiv = getParentDiv(currentObj);
	
	if(objDiv && is_dom) return x + objDiv.offsetLeft;

	return x;

}

function getAbsY(currentObj,insideDiv) { 

	var y = (currentObj.y) ? currentObj.y : getAbsPos(currentObj,"Top"); 

	if(!insideDiv) return y;

	var objDiv = getParentDiv(currentObj);

	if(objDiv && is_dom) return y + objDiv.offsetTop;

	return y;

}

function getAbsPos(currentObj,position) {

	 myPos = 0;

	 while (currentObj != null) {

	  myPos += currentObj["offset" + position];
	
	  currentObj = currentObj.offsetParent;

	 }

	 return myPos;

}

function getImgObj(objName){

	if (is_ie) return document.images[objName];

	if (is_dom)return document.getElementById(objName);

	return document.images[objName];

}

function containerNode(name,objContainer){ 

	this.name = name;

	this.con = objContainer;

}

function imgTag(objName,insideDiv){ 

	this.obj = getImgObj(objName);

	this.insideDiv = insideDiv;

	this.x = function (){ 

		return getAbsX(this.obj,this.insideDiv);

	}

	this.y = function (){ 

		return getAbsY(this.obj,this.insideDiv);
	}

}

function menuitem(mcName,name,text,link){

	this.drawmenu=function(){

		var str = '';

		str +='<tr><td id="cell'+name+'" name="cell'+name+'" align=center class="'+mc.celloffStyle+'" '

		if(mc.getindex(name) != -1) str +=" background='image/bannerArrow.gif' "

		str +='style=\''

		if(mc.getindex(name) != -1) str +="background-repeat: no-repeat;background-position: 97%;'";

		str +='cursor:pointer;padding-left: 5px;\' bgcolor='+mc.cellcolor+' ';

		str +=' onMouseOver = "'+mcName+'.overCell(this);'+mcName+'.showContainer(\''+mcName+'\',\''+name+'\');" onMouseOut = "'+mcName+'.outCell(this);'+mcName+'.hideContainer(\''+mcName+'\',\''+name+'\');" onMouseDown="location=\''+link+'\'">';

		str += '<img id="'+name+'p" name="'+name+'p" width=1 height=1 border=0 src="img/spacer.gif">';
		
		str += ''+text+'</td></tr>';

		return str;
	
	}

}

function menuControl(){	
	this.containerList = new Array();

	this.currentContainer = "";

	this.overCell= function(obj){

		if(!obj) return

		if(obj.tagName == "TD"){

			obj.className= this.cellonStyle;

			if(is_ie){

				this.outallCell(obj);

			}

			if(!is_ie){

				this.cellalltdstyle(obj,this.cellonStyle);

			}

		}

	}
	

	this.outCell= function(obj){

		if(!obj) return

		if(obj.tagName == "TD"){

			obj.className= this.celloffStyle;

			if(!is_ie){

				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]);

		}

	}

	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])

		}

	}

	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.addContainer=function(mcName, containerName, containerPosImg,containerWidth, containerOffx,containerOffy,insideDiv){
		currentMcName = mcName;

		with(this){

			containerList = containerList.concat([new containerNode(containerName,new menuContainer(containerName, containerPosImg, containerWidth, containerOffx, containerOffy, insideDiv))]);
		
		}

	}

	this.additem=function(mcName,parentconName,itemName,text,link){

		var obj = this.getContainer(parentconName);

		obj.additem(mcName,itemName,text,link);

	}

	this.drawmenu=function(){

		for( var i=0;i< this.containerList.length ;i++)

			this.containerList[i].con.drawmenu();

	}

	this.initmenu=function(){

		for(var i=0;i< this.containerList.length ;i++)

			this.containerList[i].con.initmenu();

	}

	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.showContainer=function(mcName,containerName,img,src){

		currentMcName=mcName;

		this.currentContainer = this.parent(containerName);

		this.hideOther(containerName);

		if(containerName.indexOf("_") < 0 && is_ie) this.outallCellid(containerName);

		this.realShow(containerName,img,src);

	if((this.currentContainer).indexOf("_") > 0 && is_ie)

	this.overCell(document.all["cell"+this.currentContainer]);

	}

	this.hideOther=function(containerName){ 

		var parentContainer;

		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) == "_"){

				parentContainer = containerName.substr(0,i);

				this.realhideOther(parentContainer,parseInt(containerName.substr(i+1)));

				this.hideOther(parentContainer);

			}

		}

	}

	this.realShow=function(containerName,img,src){

		var obj = this.getContainer(containerName);
		if(!obj) return;

		obj.menupos();

		obj.show();

	}

	this.realhideOther=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.hideContainer=function(mcName,containerName,img,src){

		currentMcName=mcName;
		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 (currentMcName+'.realHidden("'+containerName+'")', 400);

	}

	this.hideChild=function(containerName){

		this.realhideOther(containerName,-1);
		
	}

	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 (currentMcName+'.realHidden("'+containerName+'")', 400);

			return;

		}else{

			this.hideChild(containerName);

		}

		obj.hidden();

	}

	this.getindex=function(containerName){
		for(var i=0;i<this.containerList.length;i++){
			if(this.containerList[i].name == containerName) return i;
		}

		return -1;

	}

	this.getContainer=function(containerName){
		var index = this.getindex(containerName);

		if(index == -1) return null;

		return this.containerList[index].con;

	}

}

function menuContainer(divObjName,imgTagName,width,offsetX,offsetY,insideDiv){

	this.offsetX = offsetX;
	this.offsetY = offsetY;
	this.divObjName = divObjName;
	this.imgTagName = imgTagName;
	this.divObjStyle = "";
	this.imgTagObj = "";
	this.width = width;
	this.itemlist = new Array();
	this.insideDiv = insideDiv;
	this.timerID = -1;

	this.additem=function (mcName,name,text,link){

		currentMcName=mcName;

		this.itemlist = this.itemlist.concat([new menuitem(mcName,name,text,link)]);

	}

	this.drawmenu=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=0 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].drawmenu();
				if (i != itemlist.length - 1)
				{
					str += '<tr><td align=center><img src=img/menu_line.jpg></td></tr>';
				}
			}

			str +='</table>\n';	

			str +='</div>\n';	

			document.write(str);
		}	

	}

	this.menupos=function (){
		var nonIEoffsety;

		if(!is_ie) nonIEoffsety = 3;

		else nonIEoffsety = 1;

			this.divObjStyle.left = this.imgTagObj.x() + this.offsetX;

			this.divObjStyle.top = this.imgTagObj.y() + this.offsetY + nonIEoffsety;
			
	}

	this.initmenu=function(){

			if (is_dom) { this.divObjStyle = document.getElementById(this.divObjName).style; }

			else { this.divObjStyle = is_ie ? document.all[this.divObjName].style : document.layers[this.divObjName]; } 

			this.imgTagObj = new imgTag(this.imgTagName,this.insideDiv);

	}	

	this.show=function(){

		this.divObjStyle.visibility = "visible"; 

		if(this.timerID != -1) clearTimeout(this.timerID);

		this.timerID = -1;	

	}

	this.hidden=function(){

		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;

	}

}