
var menus = [];
var isNetscape;

var ALT_HEIGHT=22;
var CAT_HEIGHT=0;

isNetscape=(document.getElementById && !document.all?true:false);
if(navigator.userAgent.indexOf("Opera")!=-1)isNetscape=true;

function findPosX(obj)
{

	var curleft = 0;
	if (document.getElementById || document.all)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (document.layers)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (document.getElementById || document.all)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (document.layers)
		curtop += obj.y;
	return curtop;
}


// --- menu class ---
function menu (item_struct, pos, styles, menuLang, expandTableAccordingly) {
	// browser check
	var theMenuLang=new String(menuLang);
	this.menuLang=theMenuLang;
	this.item_struct = item_struct;
	this.pos = pos;
	//maybe the login small form has to be bypassed, as is the case for the Login Page:
	if(expandTableAccordingly)this.pos["block_top"][0]=findPosY(document.getElementById('menuAppendCellsHere'));
	this.styles = styles;
	this.id = menus.length;
	this.items = [];
	this.children = [];
	
	//the append pictures switch:
	this.appendSlashPics = (expandTableAccordingly?false:true);
		
	this.add_item = menu_add_item;
	this.hide = menu_hide;
	
	this.onclick = menu_onclick;
	this.onmouseout = menu_onmouseout;
	this.onmouseover = menu_onmouseover;
	this.onmousedown = menu_onmousedown;
	
	var i;
	for (i = 0; i < this.item_struct.length; i++)
		new menu_item(i, this, this);
	for (i = 0; i < this.children.length; i++)
		this.children[i].visibility(true);
	menus[this.id] = this;
	if(expandTableAccordingly){
		var tr=document.createElement('tr');
		var td=document.createElement('td');
		var im=document.createElement('img');
		im.setAttribute('src', 'index_files/blank.gif');
		im.setAttribute('height', 22*this.children.length);
		td.appendChild(im);
		tr.appendChild(td);
		document.getElementById('menuAppendCellsHere').appendChild(tr);	
	}
}
function menu_add_item (item) {
	var id = this.items.length;
	this.items[id] = item;
	return (id);
}
function menu_hide () {
	for (var i = 0; i < this.items.length; i++) {
		this.items[i].visibility(false);
		this.items[i].switch_style('onmouseout');
	}
}
function menu_onclick (id) {
	var item = this.items[id];
	return (item.fields[1] ? true : false);
}
function menu_onmouseout (id) {
	if(this.active_item) {
		with(this.active_item) {
			if(changingImage) {
				//changingImage.setAttribute('src', 'img_yoga/b_simplu.gif');
			}
		}
		this.hide_timer = setTimeout('menus['+ this.id +'].hide();',
			this.pos['hide_delay'][this.active_item.depth]);
		if (this.active_item.id == id)
			this.active_item = null;
	}
}
function menu_onmouseover (id) {
	
	this.active_item = this.items[id];
	
	if(!this.active_item.madeChildren) {
		for (i = 0; i < this.active_item.childCount; i++)
			new menu_item (this.active_item.path + '_' + i, this.active_item, this.active_item.container, i);
		this.active_item.madeChildren=1;
	}
	clearTimeout(this.hide_timer);
	var curr_item, visib;
	for (var i = 0; i < this.items.length; i++) {
		curr_item = this.items[i];
		visib = (curr_item.arrpath.slice(0, curr_item.depth).join('_') ==
			this.active_item.arrpath.slice(0, curr_item.depth).join('_'));
		if (visib)
			curr_item.switch_style (
				curr_item == this.active_item ? 'onmouseover' : 'onmouseout');
		curr_item.visibility(visib);
	}
}
function menu_onmousedown (id) {
	this.items[id].switch_style('onmousedown');
}
// --- menu item Class ---
function menu_item (path, parent, container, childNumber) {
	//freshly added stuff:
	this.childNumber=childNumber;
	this.changingImage=null;
	//the rest of the stuff was already here:
	this.path = new String (path);
	this.parent = parent;
	this.container = container;
	this.arrpath = this.path.split('_');
	this.depth = this.arrpath.length - 1;
	// get pointer to item's data in the structure
	var struct_path = '', i;
	for (i = 0; i <= this.depth; i++)
		struct_path += '[' + (Number(this.arrpath[i]) + (i ? 2 : 0)) + ']';
	eval('this.fields = this.container.item_struct' + struct_path);
	if (!this.fields) return;
	
	// assign methods	
	this.get_x = mitem_get_x;
	this.get_y = mitem_get_y;
	// these methods may be different for different browsers (i.e. non DOM compatible)
	this.init = mitem_init;
	this.visibility = mitem_visibility;
	this.switch_style = mitem_switch_style;
	
	// register in the collections
	this.id = this.container.add_item(this);	
	
	if (isNaN(parent.myPos)) {
		this.myPos = 0
		for(var j=0; j<=this.depth+1; j++)this.myPos+=this.container.pos['block_top'][j];
	}
	else this.myPos=parent.myPos;
	if((parent.children.length==0) && !isNaN(parent.myPos))parent.myPos+=this.container.pos['block_top'][this.depth];
	parent.children[parent.children.length] = this;
	
	// init recursively
	this.init();
	this.children = [];
	var child_count = this.fields.length - 2;
	this.childCount=child_count;
	
	//register a bool variable to say whether the children have been generated or not:
	this.madeChildren=0;
			
	this.switch_style('onmouseout');
}
function mitem_init() {
		var newKid;

		if(!isNetscape) {
			newKid=document.createElement ('<a class="m' + this.container.id + 'l' + this.depth + 'o" style="position: absolute; cursor: hand;" onmouseover=menus[' + this.container.id + '].onmouseover(' + this.id + ') onmouseout=menus['+this.container.id+'].onmouseout('+this.id+') onclick=menus['+this.container.id+'].onclick('+this.id+') onmousedown=menus['+this.container.id+'].onmousedown('+this.id+')>');
			newKid.style.pixelTop=this.get_y();
			newKid.style.height=this.container.pos['height'][this.depth];
			newKid.style.pixelLeft=this.get_x();
			newKid.style.width= this.container.pos['width'][this.depth];
			newKid.style.visibility='hidden';
			newKid.style.background='black';
			newKid.style.color='white';
			newKid.style.zIndex= this.depth+2;
			newKid.id='mi_' + this.container.id + '_'+ this.id;
			//newKid.class='m' + this.container.id + 'l' + this.depth +'o';
			if(this.fields[1]!="")newKid.href= this.fields[1];
		} 
		else {
			newKid=document.createElement("a");
			newKid.className= "m"+this.container.id+"l"+this.depth+"o";
			//newKid.style.position="absolute";
			//newKid.style.cursor="hand";
			newKid.style.position="absolute";
			newKid.style.cursor=(isNetscape?"pointer":"hand");
			newKid.style.top=this.get_y();//;
			newKid.style.left=this.get_x();//-2;
			newKid.style.height=this.container.pos['height'][this.depth];
			newKid.style.width= this.container.pos['width'][this.depth];
			//newKid.setAttribute("style", '"position:absolute;cursor:hand"');
			newKid.setAttribute("onmouseover", 'menus[' + this.container.id + '].onmouseover(' + this.id + ')');
			newKid.setAttribute("onmouseout", 'menus['+this.container.id+'].onmouseout('+this.id+')');
			newKid.setAttribute("onclick", 'menus['+this.container.id+'].onclick('+this.id+')');
			newKid.setAttribute("onmousedown", 'menus['+this.container.id+'].onmousedown('+this.id+')');
		}
		
		newKid.style.width= this.container.pos['width'][this.depth];
		newKid.style.visibility='hidden';
		newKid.style.background='black';
		newKid.style.color='white';
		newKid.style.zIndex= this.depth+2;
		newKid.id='mi_' + this.container.id + '_'+ this.id;
		//check category / additional language here
		if(!isNaN(this.fields[1]) && parseInt(this.fields[1])) {
			var intItem=parseInt(this.fields[1]);
			var floatItem=this.fields[1];
			var forumId=0; //if this.fields[1] is decimal, then it is part of the forums submenu!
			floatItem-=intItem;
			while(floatItem>0.0001) {
				var firstNum=parseInt(10*floatItem);
				forumId=10*forumId+firstNum;
				floatItem=10*floatItem-firstNum;
			}
					
			newKid.href= "content.aspx?item=" + intItem + (floatItem?"&forumId=" + forumId: "") + "&lang=" + this.container.menuLang;
		}
		else newKid.href="javascript:void(0)"
				
		var newKidOfKid=document.createElement('div');
		newKid.appendChild(newKidOfKid);
		newKidOfKid.className='m'  + this.container.id + 'l' + this.depth + 'i';
				
		var innerText=document.createTextNode(this.fields[0]);
		newKidOfKid.appendChild(innerText);
		
		document.body.appendChild(newKid);
		newKid.style.height=this.container.pos['height'][this.depth]-(document.all?0:6);
				
		if(!this.depth) {
			CAT_HEIGHT+=ALT_HEIGHT;
	
		}
		
		if(newKidOfKid.offsetHeight>newKid.offsetHeight)newKid.style.height=newKidOfKid.offsetHeight;
		
		this.element = document.getElementById('mi_' + this.container.id + '_' + this.id);
		//if(this.depth>0) this.parent.myPos=findPosY(this.element)+this.element.clientHeight;
		this.myPos=parseInt(isNetscape?this.element.style.top:this.element.style.pixelTop);
		this.myHeight=this.element.offsetHeight;
}
function mitem_visibility(make_visible) {
	if (make_visible != null) {
		if (this.visible == make_visible) return;
		this.visible = make_visible;
		if (make_visible)
			this.element.style.visibility = 'visible';
		else if (this.depth)
			this.element.style.visibility = 'hidden';
	}
	return (this.visible);
}
function mitem_get_x() {
	var value = 0;
	for (var i = 0; i <= this.depth; i++) {
		value += this.container.pos['block_left'][i] + this.arrpath[i] * this.container.pos['left'][i];
	}
	return (value);
}
function mitem_get_y() {
	var value = this.parent.myPos;
	if(this.depth==0) value=this.container.pos['block_top'][0]+this.arrpath[0]*this.container.pos['top'][0]	;
	for(var j=0; j<this.childNumber; j++) {
		if(isNetscape) value+=this.parent.children[j].element.offsetHeight-2;
		else value +=this.parent.children[j].element.clientHeight;
	}
		
	return (value);
}
function mitem_switch_style(state) {
	if (this.state == state) return;
	this.state = state;
	var style = this.container.styles[state];
	for (var i = 0; i < style.length; i += 2)
		if (style[i] && style[i+1])
			eval('this.element.style.' + style[i] + "='" 
			+ style[i+1][this.depth] + "';");
}
// that's all folks
