function stabilMeny( layerObj, scrollObj, target, width, height ) {
	// Properties
	this.layer = layerObj;
	this.scroll = scrollObj;
	this.refObj = this.layer.ID + "_menu";
	eval( this.refObj + " = this;" );
	this.target = target;
	this.top = this.layer.top;
	this.left = this.layer.left;
	this.width = width;
	this.height = height;
	this.items = new Array();
	// Methods
	this.newItem = stabilNewItem;
	this.paint = stabilPaint;
	this.openItem = stabilOpenItem;
	this.linkbold = stabilLinkBold;
	this.expand = stabilExpand;
	this.click = stabilClick;
	this.trigger = stabilTrigger;
	// Initialize
	this.scroll.trigger = this.refObj + ".trigger();";
}	

function stabilNewItem( ID, parent, html, link, isHeading ) {
	this.items[ ID ] = html;
	this.items[ ID + "_link" ] = link;
	this.items[ ID + "_expanded" ] = false;
	this.items[ ID + "_isHeading" ] = isHeading;
	if( parent ) {
		if( !this.items[ parent + "_first_child" ] ) {
			this.items[ parent + "_first_child" ] = ID;
			this.items[ parent + "_last_child" ] = ID; 
		} else {
			this.items[ this.items[ parent + "_last_child" ] + "_next" ] = ID;
			this.items[ parent + "_last_child" ] = ID;
		}
	} else {
		if( !this.items[ "first_item" ] ) {
			this.items[ "first_item" ] = ID;
			this.items[ "last_item" ] = ID; 
		} else {
			this.items[ this.items[ "last_item" ] + "_next" ] = ID;
			this.items[ "last_item" ] = ID;
		}
	}
}

var arrowImg

function stabilPaint() {
	
	code = "<table width=100% cellpadding=1 cellspacing=0 border=0>\n";
	
	this.visCount = 0;
	for( current = this.items[ "first_item" ]; current; current = this.items[ current + "_next" ] ) {
		
		/* If-sats borttagen tills vidare
		if(document.all){
			link = ( this.items[ current + "_first_child" ] ) ?
			"<a href=\"" + this.items[ current + "_link" ] + "\" target=\"mainFrame\" onclick=\"setActiveMenuItem(" + current + ");" + this.refObj + ".click( '" + current + "' );\" onmouseup=\"return false;\" class=\"body11\">":
			"<a href=\"" + this.items[ current + "_link" ] + "\" target=\"mainFrame\" onclick=\"setActiveMenuItem(" + current + ");\" class=\"body11\">";
		}else{
			link = ( this.items[ current + "_first_child" ] ) ?
			"<a href=\"#\" onclick=\"nsGoTo('" + this.items[ current + "_link" ] + "');setActiveMenuItem(" + current + ");" + this.refObj + ".click( '" + current + "' );\" onmouseup=\"return false;\" class=\"body11\">":
			"<a href=\"#\" onclick=\"nsGoTo('" + this.items[ current + "_link" ] + "');setActiveMenuItem(" + current + ");\" class=\"body11\">";
		} */
		 
		link = ( this.items[ current + "_first_child" ] ) ?
		"<a href=\"#\" onclick=\"nsGoTo('" + this.items[ current + "_link" ] + "');setActiveMenuItem('" + current + "');" + this.refObj + ".click( '" + current + "' );\" onmouseup=\"return false;\">":
		"<a href=\"#\" onclick=\"nsGoTo('" + this.items[ current + "_link" ] + "');setActiveMenuItem('" + current + "');\">";
		
		
		if( this.items[ current + "_expanded" ] ){
			arrowImg = "<img src=\"images/icon_arrow_gray_graybg_acti.gif\" border=\"0\" vspace=\"4\" hspace=\"0\">";
		}else{
			arrowImg = "<img src=\"images/icon_arrow_gray_graybg.gif\" border=\"0\" vspace=\"4\" hspace=\"0\">";		
		}

		//if(this.items[ current + "_isHeading" ] == "True"){
		//	//Om rubrik
		//	code += "<tr><td colspan=3><img src=../images/pxl_trans.gif border=0 vspace=0 hspace=0 width=1 height=5></td></tr>\n";
		//	code += "<tr><td width=12><img src=\"../images/pxl_trans.gif\" width=\"1\" height=\"1\" border=\"0\" vspace=\"0\" hspace=\"0\"></td><td colspan=\"2\"><span class=heading16>" + this.items[ current ] + "</span></td></tr>\n";
		//}else{
				code += "<tr><td width=12 align=center>" + link + arrowImg + "</a></td><td colspan=\"2\">" + link + "<span style=\"font-size:11px;font-weight:bold;\">" + this.items[ current ] + "</span></a></td></tr>\n";	
		//}
		 
		if(openExtraID == current){
			this.items[ current + "_expanded" ] = true;
		}
		
		this.visCount++;
		
		//Child
		code_child = "";
		for( child = this.items[ current + "_first_child" ]; child; child = this.items[ child + "_next" ] ) {
			/*if( activeMenuItem == child){			
				arrowImg = "!<img src=\"../images/icon_arrow_menu.gif\" width=\"5\" height=\"11\" border=\"0\" vspace=\"0\" hspace=\"0\">";
				//this.items[ child + "_expanded" ] = true;				
			}else{
				arrowImg = "<img src=\"../images/pxl_trans.gif\" width=\"5\" height=\"1\" border=\"0\" vspace=\"0\" hspace=\"0\">";
			}
			*/
			
			link_child = "<a href=\"#\" onclick=\"nsGoTo('" + this.items[ child + "_link" ] + "');setActiveMenuItem('" + child + "');" + this.refObj + ".click( '" + child + "' );\" onmouseup=\"return false;\">";
			
			if( this.items[ child + "_expanded" ] ){
				arrowImg = link_child + "<img src=\"images/icon_arrow_gray_graybg_acti.gif\" border=\"0\" vspace=\"4\" hspace=\"0\"></a>";
			}else{
				arrowImg = link_child + "<img src=\"images/icon_arrow_gray_graybg.gif\" border=\"0\" vspace=\"2\" hspace=\"0\"></a>";		
			}
		
			code_child += "<tr><td width=12><img src=\"../images/pxl_trans.gif\" width=\"5\" height=\"1\" border=\"0\" vspace=\"0\" hspace=\"0\"></td><td width=12 align=center valign=top>" + arrowImg + "</td><td valign=top>";

			
			/*
			link_child = ( this.items[ child + "_link" ] ) ? 
			"<a href=\"#\" onclick=\"nsGoTo('" + this.items[ child + "_link" ] + "');setActiveMenuItem('" + child + "');" + this.refObj + ".click( '" + child + "' );\" onmouseup=\"return false;\">" + this.items[ child ] + "</a>" : this.items[ child ];
			*/
			code_child += link_child + "<span style=\"font-size:10px;font-weight:bold;\">" + this.items[ child ] + "</span></a></td></tr>\n";
			
			//Barnbarn
			code_subchild = "";
			for( subchild = this.items[ child + "_first_child" ]; subchild; subchild = this.items[ subchild + "_next" ] ) {
				
				arrowImg = "<img src=\"../images/pxl_trans.gif\" width=\"5\" height=\"1\" border=\"0\" vspace=\"0\" hspace=\"0\">";
				code_subchild += "<tr><td width=12>" + arrowImg + "</td><td width=\"6\"><img src=\"../images/pxl_trans.gif\" width=\"6\" height=\"1\" vspace=\"0\" hspace=\"0\" border=\"0\"></td><td>";
				
				link_subchild = "<a href=\"#\" onclick=\"nsGoTo('" + this.items[ subchild + "_link" ] + "');setActiveMenuItem('" + subchild + "');\" onfocus=\"this.blur();\"><span style=\"font-size:10px;font-weight:normal;\">" + this.items[ subchild ] + "</span></a>";
				
				code_subchild += link_subchild + "</td></tr>\n";

			}
			
			if( this.items[ child + "_expanded" ]){
				code_child += code_subchild;
				this.visCount++;
			}
			
		}
		if( this.items[ current + "_expanded" ] ){
			code += code_child
			this.visCount++;
		}
	}

	code += "</table>&nbsp;";
	this.layer.write( code );
	// Adept the menu
	/* 
	Vi skippar scrollen
	this.scroll.layer.move( this.left + this.width - 2, this.scroll.layer.top );
	this.scroll.backLayer.move( this.left + this.width - 2, this.top );
	this.scroll.height = this.height;
	this.scroll.max = this.visCount * 22 - this.height;
	if( this.scroll.max < 0 ) this.scroll.max = 0;
	if( this.scroll.max < this.scroll.pos ) this.scroll.pos = this.scroll.max;
	this.scroll.barLength = this.height / ( this.visCount * 22 );
	this.scroll.step = 11;
	this.scroll.paint();
	this.scroll.moveBy( 0 );
	// Clip the layer
	this.layer.clip( 0, 0 + this.scroll.pos, this.width, this.height + this.scroll.pos );
	*/
}

strLastTopItem = "0";
function stabilClick( ID ) {
	
	blnForceClose = false;
	
	//Om man byter huvudniva sa byts foregaende huvudniva
	if(this.items[ID + "_isHeading" ] == "True"){
		this.items[ strLastTopItem + "_expanded" ] = false;
		if(ID==strLastTopItem){
			blnForceClose = true;
			strLastTopItem = "0";
		}else{
			strLastTopItem = ID;
		}
	}
	
	if(blnForceClose){
		this.items[ ID + "_expanded" ] = this.items[ ID + "_expanded" ];;
	}else{
		this.items[ ID + "_expanded" ] = !this.items[ ID + "_expanded" ];
	}
	this.paint();
		
}


function stabilOpenItem( ID ) {
	this.items[ ID + "_expanded" ] = true;
	this.paint();
}

function stabilTrigger() {
	this.layer.move( this.layer.left, this.top - this.scroll.pos );
	this.layer.clip( 0, 0 + this.scroll.pos, this.width, this.height + this.scroll.pos );
}

function stabilExpand( ID ) {
	
	if( this.ID == ID  ) {
		this.root.collapse();
		this.expanded = !this.expanded;
		for( this.counter = 0; this.counter < this.items.length; this.counter++ )
				this.items[ this.counter ].expand( itemID );
		if( this.root.onExpand ) this.root.onExpand( this.ID );
		this.root.update();
	} else
		if( this.expanded && this.items.length )
			for( this.counter = 0; this.counter < this.items.length; this.counter++ )
				this.items[ this.counter ].expand( itemID );
}


function stabilLinkBold( ID ) 
{
	if( this.ID == ID  ) 
	{
		if( this.root.lastActive ) 
			for( cur = this.root.lastActive; cur.ID != cur.root.ID; cur = cur.parent )
				cur.active = false;
				
		for( cur = this; cur.ID != cur.root.ID; cur = cur.parent )
			cur.active = true;
			
		this.root.lastActive = this;
				
		if( this.root.onLink ) this.root.onLink( this.ID );
		this.root.update();
		
	} 
	else
		if( this.items.length )
			for( this.counter = 0; this.counter < this.items.length; this.counter++ )
				this.items[ this.counter ].linkbold( itemID );
		
}
