// wrapper to make mmenu object-oriented
// ueli leutwyler 
// (c) insign gmbh 2002

menuexists = Array();
menucount = 1;
menuinit=false;

// screenpos: 	center;left;right;middle;top;bottom" or a combination of "center:middle" 
//		OR name of a picture
//		if screenpos is a picture top and left will be relativ to 0,0 pos of picture

function popdn()
{
}

function menuw(id,top,left,width,borderwidth,screenPos,propAr,alwaysVis,textAlignement,filter,followScrolling,horizMenu,keepAlive)
{

	if(typeof(menutpl)=="undefinded")
	{
		alert("Es ist kein Template für die Menus definiert!");
		return;
	}
	
	//properties
	this.id			= id;
	if(!id){alert("Keine id für dieses Menu vorhanden!");return};
	this.width 		= width?width:menutpl[2];
	this.left		= left?left:menutpl[1];
	this.top		= top?top:menutpl[0];
	this.borderwidth	= borderwidth?borderwidth:menutpl[3];
	this.screenPos		= screenPos?screenPos:menutpl[4];
	this.propAr		= propAr?propAr:menutpl[5];
	this.alwaysVis		= alwaysVis?alwaysVis:menutpl[6];
	this.textAlignement	= textAlignement?textAlignement:menutpl[7];
	this.filter		= filter?filter:menutpl[8];
	this.followScrolling	= followScrolling?followScrolling:menutpl[9];
	this.horizMenu		= horizMenu?horizMenu:menutpl[10];
	this.keepAlive		= keepAlive?keepAlive:menutpl[11];

	// methods
	this.addItem = menu_addItem;
	this.make    = menu_make;
	
	//holder
	this.items	= Array();
}

function menu_addItem(description,url,myvoid,status,target,sepbar)
{
	
	newItem=new menuItem(description,url,myvoid,target,status,sepbar); 
	this.items[this.items.length] = newItem;
	return newItem;
}

function menuItem(description,url,myvoid,target,status,sepbar)
{
	// properties
	this.description 		= description;
	this.url			= url?url:"";
	this.myvoid			= myvoid?myvoid:"";
	this.target			= target?target:"";
	this.sepbar			= sepbar?sepbar:1;
	this.status			= status?status:description;
	this.link			= (myvoid&&myvoid!=0) ? linktemplate.replace("%void%",myvoid):url;
	this.submenu		= null;
	
	if(target&&target!=""&&linkwraper.indexOf("javascript:")==-1)
	{
		 this.link="javascript:openwin(\""+this.link+"\")";
	}
	//Methods
	this.addSub	 = menu_addSub;
	
}

// screenpos: 	center;left;right;middle;top;bottom" or a combination of "center:middle" 
//		OR name of a picture
//		if screenpos is a picture top and left will be relativ to 0,0 pos of picture

function menu_addSub(id,top,left,width,borderwidth,screenPos,propAr,alwaysVis,textAlignement,filter,followScrolling,horizMenu,keepAlive)
{
	return this.submenu=new menuw(id,top,left,width,borderwidth,screenPos,propAr,alwaysVis,textAlignement,filter,followScrolling,horizMenu,keepAlive);
}

function menu_make()
{
	var i=0;
	
	// check if screenPos is name of a picture
	if(document.images[this.screenPos])
	{
		this.left=getImageXfromLeft(this.screenPos)+this.left;	
		this.top=getImageYfromTop(this.screenPos)+this.top;	
	}
	
	
	// make menu array
	var myAr=[
	this.id.toString(),
	this.top,
	this.left,
	this.width,
	this.borderwidth,
	this.screenPos,
	this.propAr,
	this.alwaysVis,
	this.textAlignement,
	this.filter,
	this.followScrolling,
	this.horizMenu,
	this.keepAlive,
	menutpl[12],
	menutpl[13],
	menutpl[14],
	menutpl[15],,,,,""
	];

	// add items
	for(i=0;i<this.items.length;i++)
	{
		myAr[myAr.length]=this.items[i].description;
		if(this.items[i].submenu)
		{
			myAr[myAr.length]="show-menu="+this.items[i].submenu.id;
			myAr[myAr.length]=this.items[i].link;
		}
		else
		{
			myAr[myAr.length]=this.items[i].link;
			myAr[myAr.length]="";
		}
		myAr[myAr.length]=this.items[i].status;
		myAr[myAr.length]=this.items[i].sepbar;
		
		// make submenu recusively
		if(this.items[i].submenu) this.items[i].submenu.make();
	}
	addmenu(menu = myAr);
	menuexists[this.id]=menucount;
	menucount++;
}



function popItUp(number)
{
	if(!menuinit) return;
		//do mouseover with layer
		if(menuexists[number])
		{
			popup(menuexists[number]);
		}
		
}

function openwin(source)
{
	window.open(source);
}

// funcitons to get position of a picture
function docjslib_getRealLeft(imgElem) {
	xPos = document.images[imgElem].offsetLeft;
	tempEl = document.images[imgElem].offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

function docjslib_getRealTop(imgElem) {
	yPos = document.images[imgElem].offsetTop;
	tempEl = document.images[imgElem].offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function getImageXfromLeft(imgID) {
  if (document.images[imgID].x) return document.images[imgID].x
  else return docjslib_getRealLeft(imgID);
}

function getImageYfromTop(imgID) {
  if (document.images[imgID].y) return document.images[imgID].y
  else return docjslib_getRealTop(imgID);
}


