

function stackEvent( obj, eventType, func )
{
	var oldEvent = eval( "obj." + eventType );
	var oldInnerSource = "";
	var newInnerSource = func.toString()
	newInnerSource = newInnerSource.substring( newInnerSource.indexOf( "{" ) + 1, newInnerSource.lastIndexOf( "}" ) );

	if( typeof oldEvent == "function" )
	{
		oldInnerSource = oldEvent.toString()
		oldInnerSource = oldInnerSource.substring( oldInnerSource.indexOf( "{" ) + 1, oldInnerSource.lastIndexOf( "}" ) );
	}
	eval( "obj." + eventType + " = function(){ " + oldInnerSource + newInnerSource + " }" );
}


var basezIndex=10000;



function MyMenu(options)
{
	if(!options)options=new Object();
	
	this.groupsArr=new Array();
	this.elementArr=new Array();
	this.menuHndlOptions=menuHndlOptions;
	
	this.linkMenuEle=linkMenuEle;
	this.closeMenu=closeMenu;
	this.addToContainer=addToContainer;
	this.menuHndlOptions(options);
}


function menuHndlOptions(options)
{
	if(!options.intrevaltime)this.intrevaltime=400;
	else this.intrevaltime=options.intrevaltime;
	
	if(options.onSelectCls)this.onSelectCls=options.onSelectCls;	
	if(options.onmouseoverCls)this.onmouseoverCls=options.onmouseoverCls;
	if(options.baseCls)this.baseCls=options.baseCls;
	
	
	if(options.closeEvent)this.closeEvent=options.closeEvent;
	if(options.openEvent)this.openEvent=options.openEvent;
	if(options.direction)this.direction=options.direction;
	
	if(options.opendir)this.opendir=options.opendir;

}



function addToContainer(contObjOrID,menuEleObjOrID,options,menuEleLinkObjOrID)
{

	var contObj=getObj(contObjOrID);
	if(!contObj)return;
	
	if(menuEleObjOrID)var menuEleObj=getObj(menuEleObjOrID);
	if(!menuEleObj)menuEleObj=contObj;
	
	
	
	//no double attach
	if(menuEleObj.parentCont)return;
	
	//attach parents
	contObj.masterParent=this;
	menuEleObj.masterParent=this;
	
	//attach link obj and reset non link attributs
	if(menuEleLinkObjOrID)
	{
		menuEleObj.menuEleLinkObj=getObj(menuEleLinkObjOrID);
		menuEleObj.menuEleLinkObj.style.border='0px';
		menuEleObj.menuEleLinkObj.style.backgroundImage="url('shakuf.gif')";
		menuEleObj.menuEleLinkObj.style.height='0px';
	}
	//connect cont to menue ele
	menuEleObj.parentCont=contObj;
	if(!contObj.menuChildren)contObj.menuChildren=new Array();
	contObj.menuChildren.push(menuEleObj);
	
	//basic menu ele events
	stackEvent(menuEleObj,'onmouseover',menuEleObjOnmouseoverEvent);
	stackEvent(menuEleObj,'onmouseout',menuEleObjOnmouseoutEvent);
	
	
	
	if(!options)options=new Object();
	
	//start events
	if(!options.closeEvent)options.closeEvent=this.closeEvent;
	
	switch(options.closeEvent)
	{
		case 'onclick':
			addEvent(document.body, 'click', function(){getObj(contObj.id).closeMenu();}, false);
			stackEvent(contObj,'onmouseout',focuslost);
		break;
		case 'onmouseout':
		default:
			stackEvent(contObj,'onmouseout',tryCloseMenu);
		break;
	}
	
	
	if(!options.openEvent)options.openEvent=this.openEvent;
	
	switch(options.openEvent)
	{
		case 'onclick':
			stackEvent(menuEleObj,'onclick',openMenuEle);
		break;
		case 'onmouseover':
		default:
			//alert('in');
			stackEvent(menuEleObj,'onmouseover',openMenuEle);
		break;
	}
	
	
	//cont funcs
	contObj.closeMenu=closeMenu;
	contObj.setIsFocus=setIsFocus;
	contObj.closeAllChildren=closeAllChildren;
	//contObj.onmouseover=focus;
	stackEvent(contObj,'onmouseover',menufocus);
	
	//menu ele funcs
	menuEleObj.setIsFocus=setIsFocus;
	menuEleObj.elemHndlOptions=elemHndlOptions;

	menuEleObj.setBaseCls=setBaseCls;
	menuEleObj.setOnmouseoverCls=setOnmouseoverCls;
	menuEleObj.setOnSelectCls=setOnSelectCls;
	
	//hndl general options
	menuEleObj.elemHndlOptions(options);
}




function elemHndlOptions(options)
{
	//alert(printProps(options,this.id))
	if(options.baseCls)
		this.baseCls=options.baseCls;
	else if(this.masterParent.baseCls)
		this.baseCls=this.masterParent.baseCls;
	else this.baseCls=this.className;
	
	this.className=this.baseCls;
	
	//if(this.menuEleLinkObj)this.menuEleLinkObj.className=this.baseCls;
	
	
	if(options.onSelectCls)
		this.onSelectCls=options.onSelectCls;
	else if(this.masterParent.onSelectCls)
		this.onSelectCls=this.masterParent.onSelectCls;	
	else this.onSelectCls=this.className;
	
	if(options.onmouseoverCls)
		this.onmouseoverCls=options.onmouseoverCls;
	else if(this.masterParent.onmouseoverCls)
		this.onmouseoverCls=this.masterParent.onmouseoverCls;
	else this.onmouseoverCls=this.className;
	
	if(options.direction)this.direction=options.direction;
	else if(this.masterParent.direction)this.direction=this.masterParent.direction;
	else this.direction='ltr';
	
	//opendir : side,above,under
	//alert(options.opendir)
	if(options.opendir)this.opendir=options.opendir;
	else if(this.masterParent.opendir)this.opendir=this.masterParent.opendir;
	else this.opendir='under';

	if(!options.offsetX)this.offsetX=0;
	else this.offsetX=options.offsetX;
	
	if(!options.offsetY)this.offsetY=0;
	else this.offsetY=options.offsetY;
	
	
}


function setBaseCls()
{
	this.className=this.baseCls;
	if(this.menuEleLinkObj)this.menuEleLinkObj.className=this.baseCls;
	
}

function setOnmouseoverCls()
{
	this.className=this.onmouseoverCls;
	if(this.menuEleLinkObj)this.menuEleLinkObj.className=this.onmouseoverCls;
}

function setOnSelectCls()
{
	this.className=this.onSelectCls;
	if(this.menuEleLinkObj)this.menuEleLinkObj.className=this.onSelectCls;
}


function menuEleObjOnmouseoverEvent()
{
	//tsttextarea.value+='Onmouseover:'+this.id+'\n';
	//this.className=this.onmouseoverCls;
	this.setOnmouseoverCls()
	this.setIsFocus(true);
}

function menuEleObjOnmouseoutEvent()
{
	//tsttextarea.value+='Onmouseout:'+this.id+'\n';
	//this.className=this.baseCls;
	this.setBaseCls();
	this.setIsFocus(false);
	if(this.childMenuCont && this.childMenuCont.style.display=='block')
		this.setOnSelectCls()
		//this.className=this.onSelectCls;
}



function linkMenuEle(menuEleObjOrID,contObjOrID)
{
	//get parent and child objects
	var parentObj=getObj(menuEleObjOrID);
	var childObj=getObj(contObjOrID);
	if(!parentObj || !childObj)return;
	
	//tsttextarea.value+=childObj.id+' '+childObj.parentCont+'\n';
	if(!parentObj.parentCont)this.addToContainer(parentObj);
	
	//no double link on same parent/child pair
	if(parentObj.childMenuCont)return;
	
	
	//link parent to child child to parent
	if(!parentObj.parentMenu)parentObj.parentMenu=null;
	parentObj.childMenuCont=childObj;
	childObj.parentMenu=parentObj;
	
	//default css for child
	childObj.style.position='absolute';
	childObj.style.display='none';
	
	//start options hnd
	//if(!options)options=new Object();
	
	
	
	
	
	//parentObj.closeAllChildren=closeAllChildren;
	
}

function setIsFocus(isFocus)
{
	//tsttextarea.value+=this.id+' '+isFocus+'\n';
	if(isFocus==true)this.masterParent.infocusObj=this;
	this.masterParent.isFocus=isFocus;
}

function menufocus()
{
	//this.className=this.onmouseoverCls;
	this.setIsFocus(true);
}

function focuslost()
{
	//this.className=this.onmouseoverCls;
	this.setIsFocus(false);
}


function openMenuEle()
{

	if(!this.parentCont.parentMenu)this.masterParent.currentOpenParent=this;
	
	
	clearInterval(this.masterParent.oInterval);
	this.parentCont.closeAllChildren();
	
	if(!this.childMenuCont)return;
	
	var pos=findPos(this);
	
	
	
	this.childMenuCont.style.display='block';
	
	//add offset by dir and opendir
	if(this.direction=='rtl')
	{
		if(this.opendir=='side')pos.xx-=this.childMenuCont.offsetWidth;
		else
		{
			if(this.opendir=='above')pos.yy-=this.childMenuCont.offsetHeight+2;
			else pos.yy+=this.offsetHeight+2;//under		
			if(this.childMenuCont.offsetWidth>this.offsetWidth)
				pos.xx-=this.childMenuCont.offsetWidth-this.offsetWidth;
			else pos.xx+=this.offsetWidth-this.childMenuCont.offsetWidth;
		}
		
	}
	else //ltr
	{
		if(this.opendir=='side')pos.xx+=this.offsetWidth;
		else if(this.opendir=='above')pos.yy-=this.childMenuCont.offsetHeight+2;
		else pos.yy+=this.offsetHeight+2;//under
	}
	
	
	//adde defined offsets
	pos.yy+=this.offsetY;
	pos.xx+=this.offsetX;
	
	
	
	if(!this.style.zIndex)this.style.zIndex=basezIndex;
	
	this.childMenuCont.style.zIndex=this.style.zIndex+1;
	this.childMenuCont.style.left=pos.xx+'px';
	this.childMenuCont.style.top=pos.yy+'px';
	//alert(this.style.zIndex)
	createBack(this.childMenuCont);
}




function closeAllChildren()
{
	
	closeAllChildrenRec(this);
}


function closeAllChildrenRec(obj)
{
	if(!obj)return;
	
	if(!obj.menuChildren)return;
	var tmpobj;
	for(var i=0;i<obj.menuChildren.length;i++)
	{
		
		//obj.menuChildren[i].className=obj.menuChildren[i].baseCls;
		tmpobj=obj.menuChildren[i].childMenuCont;
		if(!tmpobj)continue;
		//tsttextarea.value+=tmpobj.id+'\n';
		if(tmpobj.style.display=='block')
		{
			//tsttextarea.value+=tmpobj.id+'\n';
			tmpobj.style.display='none';
			removeBack(tmpobj);
			if(tmpobj.parentMenu && tmpobj.parentMenu!=tmpobj.masterParent.infocusObj)
			{
				//tsttextarea.value+=tmpobj.parentMenu.id+' '+tmpobj.masterParent.infocusObj.id+'\n';
				//tmpobj.parentMenu.className=tmpobj.parentMenu.baseCls;
				tmpobj.parentMenu.setBaseCls();
			}
			closeAllChildrenRec(tmpobj);
			return;
		}
	}
}




function tryCloseMenu()
{
	//tsttextarea.value+=this.id+'\n';
	this.setIsFocus(false);
	var str='getObj(\''+this.id+'\').closeMenu()';
	//alert(str);
	clearInterval(this.masterParent.oInterval);
	this.masterParent.oInterval=window.setTimeout(str,this.masterParent.intrevaltime);
}

function closeMenu()
{
	//alert('in')
	if(this.masterParent.isFocus==true)return;
	if(!this.masterParent.currentOpenParent)return;
	
	this.masterParent.currentOpenParent.parentCont.closeAllChildren();
	//this.masterParent.currentOpenParent.className=this.masterParent.currentOpenParent.baseCls;
	this.masterParent.currentOpenParent.setBaseCls();
	
	
	//this.masterParent.closeMenuAll();
}




//misc funcs
function getObj(objOrID)
{
	if(typeof(objOrID)=='string')return document.getElementById(objOrID);
	return objOrID;
}

function  printProps(obj, objName) {
  var output = "" ;
  for (var prop in obj) {
    output += objName + "." + prop + " = " + obj[prop] + "\n" ;
  }
  return output ;
}

