
// written by Adir Eini, August 5, 2003
// written for Olympus fitness inc. visit at www.olympusfit.co.il
// visit www.cs.bgu.ac.il/~einia

var grand=0; //anchor for solving byVal problems
var ie;
var ns;
var offset=0;
function getWindowWidth() // my own browsercheck function
{
	var browser=navigator.appName;
	ie=(browser.indexOf("Microsoft") >= 0)?true:(browser.indexOf("Netscape") >= 0)?false:'unknown';
	ns=!ie;
	return (ie)?(document.body.clientWidth+10):(window.innerWidth-7);
    //=10 and -7 are for correction for scrollbar
}

function MenuItem(id,parentId,caption,link,width,myStyle,basicCellColor,selectionCellColor,dir)
{
	this.id=id;
	this.parentId=parentId;
	this.caption=caption;
	this.link=link;
	this.myStyle=myStyle;
	this.fontSize=12;
	this.width=width;
	this.height=18;
	this.dir=dir;
	
	this.basicCellColor=basicCellColor;
	this.selectionCellColor=selectionCellColor;
	this.align="vert"; //vertical alignment is default ,"hor" for horizontal
	this.parent=0;
	this.xpos=20; //x position
	this.ypos=30;// y position
	this.subMenuCount=0;
	this.subMenus=new Array();
	
	this.rect=0; // pointer to the table rect
	this.focus=-1; // -1:hidden, 1:visible	
	this.positionElements=positionElements;
	this.buildRects=buildRects;
	this.addMenuItem=addMenuItem; //user add, using values
	this.getMenuItem=getMenuItem; 
	this.add=add; //user add, using a ready menu
	this.init=init;
	this.applyFocusToSelectedTree=applyFocusToSelectedTree;
	this.realyRemoveFocusFromNonFocusedElements=realyRemoveFocusFromNonFocusedElements;
	this.removeFocusFromNonFocusedElements=removeFocusFromNonFocusedElements;
	this.realyRemoveFocusFromNonFocusedElementsDownwards=realyRemoveFocusFromNonFocusedElementsDownwards;
	this.realyRemoveFocusFromNonFocusedElementsUpwards=realyRemoveFocusFromNonFocusedElementsUpwards;
	//next 2 methods are for repositioning after resizing
	this.rebuildRects=rebuildRects;
	this.rebuildAfterResize=rebuildAfterResize;
	
	this.paint=paint;
	this.GRANDPARENT=this;
	eval(this.vname + "=this");
}

function add(menuItem) // preferably non-user
{
	var parent;
	//alert ("myId "+this.id+" child's parent"+ menuItem.parentId);
	if (this.id==menuItem.parentId) //add to the main menu
	{
		parent=this;
		this.width+=menuItem.width;
	}
	else
		parent=this.getMenuItem(menuItem.parentId);// add to sub menus
	if (parent==null)
		window.alert('problem in menu - child inserted without parent');
	// will now assign father-child relationship
	
	parent.subMenus[parent.subMenuCount]=menuItem;
	parent.subMenuCount++;
	menuItem.parent=parent;
	menuItem.focus=-1;
	menuItem.GRANDPARENT=parent.GRANDPARENT;
	grand=menuItem.GRANDPARENT;
	menuItem.index=parent.index+1; // for showing/hiding menus
}

function addMenuItem(id,parentId,caption,link,width,myStyle,basicCellColor,selectionCellColor,dir) //user available
{
	var item=new MenuItem(id,parentId,caption,link,width,myStyle,basicCellColor,selectionCellColor,dir);
	this.add(item);
}

function isFatherOf(A,id) //recursively check if A is the an ancestor of an item with the given id
{
	if (A.id==id) return true; //found
	else // check in each of submenus
		for (var i=0;i<A.subMenuCount;i++)
			if (isFatherOf(A.subMenus[i],id)) return true;
		
	return false; //default - return false
}

function getMenuItem(id) //retrieves a decendant of a menu with specified id
{
	if (this.id==id) //found it
		return this;
	else //search submenus
		for (var i=0;i<this.subMenuCount;i++)
			if (isFatherOf(this.subMenus[i],id)) //found it
				return this.subMenus[i].getMenuItem(id);
	return null;//default - not found
}

function positionElements()
{
	if (this.subMenuCount==0) return;
	if (this.align=="hor")
	{
		this.subMenus[0].xpos=this.xpos;
		for (var i=0;i<this.subMenuCount;i++)
		{
			this.subMenus[i].ypos=this.ypos; //assign ypos
			if (i>0)
				this.subMenus[i].xpos=this.subMenus[i-1].xpos+this.subMenus[i-1].width-1;
			this.subMenus[i].positionElements();
		}
	}
	else //vertical
	{
		if (this.parent.align=='vert')
			this.subMenus[0].ypos=this.ypos;
		else
			this.subMenus[0].ypos=this.ypos+this.height; // add -1 for border compensating
		for (var i=0;i<this.subMenuCount;i++)
		{
			
			if (this.parent.align=="vert") //if we need to cascade a menu
				if (this.dir='rtl')
					this.subMenus[i].xpos=this.xpos-this.subMenus[i].width; //assign xpos
				else // ltr
					this.subMenus[i].xpos=this.xpos+this.width;
			else // parent was horizontal
				if (this.dir='rtl')
					this.subMenus[i].xpos=this.xpos+this.width-this.subMenus[i].width; //assign xpos
				else
					this.subMenus[i].xpos=this.xpos;
			if (i>0)
				this.subMenus[i].ypos=this.subMenus[i-1].ypos+this.subMenus[i-1].height-1; // -1 is for border compensating
			this.subMenus[i].positionElements();
		}
	}
}

function rebuildAfterResize()
{
	offset=menu.xpos-(getWindowWidth()/2-menu.width/2);
	menu.xpos=getWindowWidth()/2-menu.width/2;
	menu.rebuildRects();
}

function rebuildRects() //after document resize
{
	for (var i=0;i<this.subMenuCount;i++)
	{
		var myItem=this.subMenus[i];
		myItem.xpos-=offset;
		document.getElementById('menu'+myItem.id).style.left=myItem.xpos+"px";
		document.getElementById('back'+myItem.id).style.left=myItem.xpos+"px";
		myItem.rebuildRects(); //recursive call
	}
}

function buildRects()
{
	for (var i=0;i<this.subMenuCount;i++)
	{
		var myItem=this.subMenus[i];
		var tableDeclare="<table id='menu"+myItem.id+"' style='"+myItem.myStyle+"width:"+myItem.width+"px;height:"+myItem.height+"px;visibility:hidden;position:absolute; left:"+myItem.xpos+"px;top:"+myItem.ypos+"px; z-index:10'>"
		
		var cell="<tr><td>"+myItem.caption+"</td></tr></table>";
		document.write(tableDeclare+cell);
		var backgroundTableDeclare="<table id='back"+myItem.id+"' style='background-color:cccccc;visibility:hidden;border-top-style: none;border-right-style: none;border-left-style: none;border-bottom-style: none;width:"+myItem.width+"px;height:"+myItem.height+"px;position:absolute; left:"+(myItem.xpos+2)+"px;top:"+(myItem.ypos+2)+"px;z-index:9'><tr><td></td></tr></table>"
		document.write(backgroundTableDeclare);
		myItem.rect=document.getElementById("menu"+myItem.id)
		
		// assign event handling
		myItem.rect.onmouseover=new Function (myItem.vname+".applyFocusToSelectedTree("+myItem.id+");paint("+this.GRANDPARENT.id+");return false;");
		myItem.rect.onmouseout=	new Function (myItem.vname+".removeFocusFromNonFocusedElements("+myItem.id+");return false;");
		myItem.rect.onmousedown=new Function((myItem.link=="")?"":("window.location='"+myItem.link+"'"));
		myItem.buildRects(); //recursive call
	}
}

function applyFocusToSelectedTree(id) // gives focus to all parent tree, and immediate submenu
{
	var gp=this.GRANDPARENT;
	var item=gp.getMenuItem(id);
	document.getElementById('menu'+id).style.backgroundColor=this.selectionCellColor;
	
	var parent=item.parent;
	if (parent.id!=gp.id) //focus parent tree
		this.applyFocusToSelectedTree(parent.id);
	for (var i=0;i<item.subMenuCount;i++) //focus submenu
		item.subMenus[i].focus=1;
}

function removeFocusFromNonFocusedElements(id)
{
	var gp=this.GRANDPARENT;
	var item=gp.getMenuItem(id); 
	
	item.realyRemoveFocusFromNonFocusedElements(); 
	setTimeout("paint("+gp.id+")",700);
}

function realyRemoveFocusFromNonFocusedElements()
{
	this.realyRemoveFocusFromNonFocusedElementsDownwards();
	this.realyRemoveFocusFromNonFocusedElementsUpwards();
}

function realyRemoveFocusFromNonFocusedElementsDownwards()
{
	
	document.getElementById('menu'+this.id).style.backgroundColor=this.basicCellColor;	
	for (var i=0;i<this.subMenuCount;i++)
	{
		if (this.id!=grand.id)
			this.subMenus[i].focus=-1;
		this.subMenus[i].realyRemoveFocusFromNonFocusedElementsDownwards();
	}
}

function realyRemoveFocusFromNonFocusedElementsUpwards()
{
	if (this.id!=grand.id)
	{	
		var rect=document.getElementById('menu'+this.id);
		rect.style.backgroundColor=this.basicCellColor;	
	}
	
	if (this.parentId!=grand.id)
	{
		for (var i=0;i<this.parent.subMenuCount;i++)
			this.parent.subMenus[i].focus=-1; 
		this.parent.realyRemoveFocusFromNonFocusedElementsUpwards();
	}
	
}

function paint(id) //repaints whole menu
{
	var myItem=grand.getMenuItem(id);
	for (var i=0;i<myItem.subMenuCount;i++)
	{
		var item=document.getElementById('menu'+myItem.subMenus[i].id);
		item.style.visibility=(1==myItem.subMenus[i].focus)?"visible":"hidden";
		var shadow=document.getElementById('back'+myItem.subMenus[i].id);
		shadow.style.visibility=item.style.visibility;
		paint(myItem.subMenus[i].id);
	}
}

function init()
{
	for (var i=0;i<this.subMenuCount;i++)
		this.subMenus[i].focus=1;
	paint(this.GRANDPARENT.id);
}

function fixFlashOverlay(id) //for fixing flash overlay problems
{
	document.getElementById(id).WMODE=(ie)?'transparent':'opaque';
}

