function Fupdate() {
	docRef = frames[ this.root.frame ].document;
	if( document.layers ) {
		docRef.open();
		docRef.write( this.root.paint() );
		docRef.close();
		docRef.onresize = onResize;
	} else {
			docRef.body.innerHTML = this.root.paint();
	
	} 
}

function Fnodelink( itemID ) {
	if( this.ID == itemID  ) {
		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.href ) {
			tagget = ( this.target ) ? this.target : this.root.frame;
			//if( frames[ tagget ].location.href.indexOf( this.href ) == -1 ) frames[ tagget ].location.href = this.href;
			frames[ tagget ].location.href = this.href;
		}
		if( this.root.onnodelink ) this.root.onnodelink( 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 ].nodelink( itemID );
}

function Fexplode( itemID ) {	
	this.collapse();
	if( this.ID == itemID  ) {
		this.nodeexpanded = !this.nodeexpanded;
		if( this.root.onnodeexpand ) this.root.onnodeexpand( this.ID );
		this.root.update();
	} else
		if( this.nodeexpanded && this.items.length )
			for( this.counter = 0; this.counter < this.items.length; this.counter++ )
				this.items[ this.counter ].nodeexpand( itemID );	
}

function FnodeCollapse() {
	if( this.root != this ) this.nodeexpanded = false;
	if( this.items.length )
		for( this.part in this.items ) this.items[ this.part ].collapse();
}

function coolExplode() {
	this.nodeexpanded = true;
	if( this.items.length )
		for( this.part in this.items ) this.items[ this.part ].explode();
}

function newnodeobject() {
	args = ( newnodeobject.arguments.length == 1 ) ? newnodeobject.arguments[ 0 ] : newnodeobject.arguments;
	if( this.ID == args[ 0 ] ) {
		this.newID = this.items.length;
		this.items[ this.newID ] = new coolItem( args );
		this.items[ this.newID ].parent = this;
		this.items[ this.newID ].root = this.root;
		
	} else
		for( this.counter = 0; this.counter < this.items.length; this.counter++ )
			this.items[ this.counter ].newobject( args );
}

function coolItemPaint() {
	steps = 0; ind = "";
	for( cur = this.parent; cur.ID != cur.root.ID; cur = cur.parent ) {
		if( cur.parent.items[ cur.parent.items.length - 1 ] == cur )
			ind = "<td width=" + this.root.itemSize + "></td>\n" + ind;
		else
			ind = "<td width=" + this.root.itemSize + " background=\"" + cur.root.line + "\"></td>\n" + ind;
		steps++;
	}
	icon = ( this == this.root.lastActive ) ? this.root.open : this.root.closed;   
	if( this.items.length ) {
		bgimg = ( this.parent.items[ this.parent.items.length - 1 ] == this ) ? "" : " background=\"" + this.root.line + "\"";
		img = ( this.nodeexpanded ) ? this.root.minus : this.root.plus;
        img = (this.catImg[this.ID]) ? 
                    this.catImg[this.ID] :
                        ( this.nodeexpanded ) ? 
                            this.root.minus : this.root.plus; 
                            
		alt = ( this.nodeexpanded ) ? "Stäng " : "Öppna ";
		iconBg = ( this.nodeexpanded ) ? " background=\"" + this.root.line + "\"" : "";
	} else {
		img = ( this.parent.items[ this.parent.items.length - 1 ] == this ) ? this.root.last : this.root.item;
		bgimg = ( img == this.root.item ) ? " background=\"" + this.root.line + "\"" : "";
		alt = "";
		iconBg = "";
	}
	img = "<img src=\"" + img + "\" border=0";
	if( alt ) img += " alt=\"" + alt + this.caption + "\"";
	img += ">";
	cap = ( this.caption ) ? this.caption : this.ID;
                    
	if( this.active ) cap = "<small>" + cap + "</small>";
	
	temp = "<tr>" + ind + "<td" + bgimg + " valign=\"middle\" align=\"center\">" +
		"<a href=\"javascript:";
	temp += "parent." + this.root.refObj + ".nodeexpand('" + this.ID + "');\">" + img + "</a></td>\n";

	whatSpan = this.root.width - steps;

	if( this.root.icons ) {
		temp += "<td width=\"" + this.root.itemSize + "\" valign=\"top\"" + iconBg + ">" +
		"<img src=\"" + icon + "\" border=0></td>";
		whatSpan--;
	}
	
	nodelink = "<a href=\"javascript:"
	nodelink += "parent." + this.root.refObj + ".";
	nodelink += ( this.href ) ? "nodelink" : "nodeexpand"; 
	nodelink += "('" + this.ID + "');\">";
	if( this.root.icons ) nodelink += "";
	
	temp += "<td width=" + ( ( this.root.width - steps ) * this.root.itemSize ) +
		" colspan=" + whatSpan + " valign=\"top\">" + nodelink + cap + "</a></td>\n</tr>\n";
	this.root.content += temp;
	if( this.nodeexpanded )
		for( this.counter = 0; this.counter < this.items.length; this.counter++ )
			this.items[ this.counter ].paint();
}

function coolItem() {
	args = ( coolItem.arguments.length == 1 ) ? coolItem.arguments[ 0 ] : coolItem.arguments;
	// Properties
	this.ID = args[ 1 ];
	if( args.length > 2 ) this.caption = args[ 2 ];                  
	if( args.length > 3 ) this.href = args[ 3 ];
	if( args.length > 4 ) this.target = args[ 4 ];
    
    this.catImg = new Array();
    if( args.length > 5 ) 
        this.catImg[this.ID] = args[ 5 ];
    
       
	this.nodeexpanded = false;
	this.active = false;
	this.items = new Array();
	this.paint = coolItemPaint;
	this.newobject = newnodeobject;
	this.nodeexpand = Fexplode;
	this.nodelink = Fnodelink;
	this.explode = coolExplode;
	this.collapse = FnodeCollapse;
}

function FMenuPaint() {
	args = FMenuPaint.arguments;
	if( args.length ) this.nodeexpand( args[ 0 ] );
	this.content = "<html>\n<head>\n";
	if( this.title ) this.content += "<title>" + this.title + "</title>";
	this.content += "</head>\n";
	if( this.stylesheet ) this.content += "<nodelink rel=\"stylesheet\" type=\"text/css\" href=\"" + this.stylesheet + "\">\n";
	this.content += "<body bgcolor=\"" + this.bgColor + "\" leftmargin=" + this.leftMargin + " topmargin=" + this.topMargin +
		" background=\"images/menu.gif\" marginwidth=" + this.leftMargin + " marginheight=" + this.topMargin + ">\n" +
		this.headcontent + "<table width=" + ( this.width * this.itemSize ) + " cellpadding=0 cellspacing=0 border=0>";
	
	for( this.counter = 0; this.counter < this.items.length; this.counter++ )
		this.items[ this.counter ].paint();
	
	this.content += "<tr>";
	for( x = 0; x < this.width; x++ )
	this.content += "</tr>\n</table>\n" + this.footcontent +
		"</body>\n</html>";
	if( this.onPaint ) this.onPaint();
	return this.content;
}

function FMenu() {
	args = FMenu.arguments;
	// Handle arguments
	this.ID = ( args.length ) ? args[ 0 ] : 0;
	this.frame = ( args.length > 1 ) ? args[ 1 ] : 0;
	this.itemSize = 12;
	this.width = ( args.length > 2 ) ? args[ 2 ] : this.itemSize;
	this.stylesheet = ( args.length > 3 ) ? args[ 3 ] : 0;
	this.title = ( args.length > 4 ) ? args[ 4 ] : "treemenu";
	this.bgColor = ( args.length > 5 && args[5]) ? args[ 5 ] : "#FFFFFF";
	// Properties
	this.items = new Array();
	this.last = 0;
	this.headcontent = "<a href=\"index.asp\" target=\"_top\">Startsida</a><br>";
	this.content = "";
	this.footcontent = "";
	this.root = this;
	this.nodeexpanded = true;
	this.lastActive = 0;
	this.topMargin = 2;
	this.leftMargin = 2;
	this.icons = false;
	// Images
	this.plus  = "images/pos.gif";
	this.minus = "images/min.gif"
	this.item = "images/item.gif";
	this.last = "images/item.gif";
	this.pixel = "images/pixel.gif";
	// Methods
	this.onPaint = 0;
	this.onnodeexpand = 0;
	this.onnodelink = 0;
	this.newobject = newnodeobject;
	this.paint = FMenuPaint;
	this.update = Fupdate;
	this.nodelink = Fnodelink;
	this.nodeexpand = Fexplode;
	//this.explode = coolExplode;
	this.collapse = FnodeCollapse;
	this.refObj = "treemenu";
	eval( this.refObj + " = this;" );
}
