var menuCounter = 0;

function onResize() {
	eval( "resizeMenu = menu" + menuCounter + "ref;" );
	docRef = resizeMenu.getDoc();
	docRef.location.reload();
}

function coolGetDoc() {
	if( frames[ this.frame ].frames.length )
		return frames[ this.frame ].frames[ this.frame ].document;
	else
		return frames[ this.frame ].document;
}

function coolUpdate() {
	docRef = this.root.getDoc();
	if( document.layers ) {
		// alert( this.root.paint() );
		docRef.open();
		docRef.write( this.root.paint() );
		docRef.close();
		docRef.onresize = onResize;
	} else {
		agent = navigator.userAgent.toLowerCase()
		if( agent.indexOf( "msie" ) == -1 ) {
			url = docRef.location.href.split( "/" );
			url = url[ url.length - 1 ];
			url = url.split( "?" );
			url = url[ 0 ];
			docRef.location.href = url + "?reload=" + Math.random();
		} else {
			if(docRef.body){
				docRef.body.innerHTML = this.root.paint();
			}
		}
	}
}

function coolLink( 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.expanded = true;
			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;
		}

		if( this.root.onLink ) this.root.onLink( this.ID );

		this.root.update();
			
	} else
		if( this.items.length )
			for( part in this.items ) this.items[ part ].link( itemID );
}

function coolExpand( itemID ) {
	if( this.ID == itemID  ) {
		this.expanded = !this.expanded;
		if( this.root.onExpand ) this.root.onExpand( this.ID );
		if( !this.root.preloaded[ itemID ] ) {
			this.root.preloaded[ itemID ] = true;
			// Specifikt för BIG menyn
			frames.buffer.location.href = "preload.asp?nodid=" + itemID;
		} else this.root.update();
	} else
		if( this.expanded && this.items.length )
			for( part in this.items ) this.items[ part ].expand( itemID );
}

function coolCollapse() {
	if( this.root != this ) this.expanded = false;
	if( this.items.length )
		for( this.part in this.items ) this.items[ this.part ].collapse();
}

function coolNewItem() {
	args = ( coolNewItem.arguments.length == 1 ) ? coolNewItem.arguments[ 0 ] : coolNewItem.arguments;
	if( this.ID == args[ 0 ] ) {
		y = this.items.length;
		this.items[ y ] = new coolItem( args );
		this.items[ y ].parent = this;
		this.items[ y ].root = this.root;
	} else
		for( part in this.items ) this.items[ part ].newItem( args );
}

/* Detta är den gamla itempaint metoden
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=16><img src=\"" + cur.root.pixel + "\" border=0></td>\n" + ind;
		else
			ind = "<td width=16 background=\"" + cur.root.line + "\"><img src=\"" + cur.root.pixel + "\" border=0></td>\n" + ind;
		steps++;
	}
	if( this.items.length ) img = ( this.expanded ) ? this.root.minus : this.root.plus;
	if( this.items.length ) alt = ( this.expanded ) ? "Stäng " : "Öppna ";
	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 + "\"" : "";
	img = "<img src=\"" + img + "\" border=0";
	img += ">";
	cap = ( this.caption ) ? this.caption : this.ID;
	if( this.active ) cap = "<font color=\"" + this.root.activeColor + "\">" + cap + "</font>";
	temp = "<tr>" + ind + "<td" + bgimg + " valign=\"top\">" +
		"<a href=\"javascript:parent." + this.root.refObj + ".expand('" + this.ID + "');\">" + img + "</a></td>\n" +
		"<td width=" + ( ( this.root.width - steps ) * 16 ) +
		" colspan=" + ( this.root.width - steps ) + " valign=\"top\">";
	temp += "<a href=\"javascript:parent." + this.root.refObj + ".";
	temp += ( this.items.length ) ? "expand" : "link";
	temp += "('" + this.ID + "');\">" + cap + "</a>";
	temp += "</td>\n</tr>\n";
	this.root.code += temp;
	if( this.expanded ) for( part in this.items ) this.items[ part ].paint();
}
*/

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=16><img src=\"" + cur.root.pixel + "\" border=0></td>\n" + ind;
		else
			ind = "<td width=16 background=\"" + cur.root.line + "\"><img src=\"" + cur.root.pixel + "\" border=0></td>\n" + ind;
		steps++;
	}
	if( this.items.length ) {
		bgimg = ( this.parent.items[ this.parent.items.length - 1 ] == this ) ? "" : " background=\"" + this.root.line + "\"";
		img = ( this.expanded ) ? this.root.minus : this.root.plus;
		// alt = ( this.expanded ) ? "Stäng " : "Öppna ";
		alt = "";
	} 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 = "";
	}
	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 = "<font color=\"" + this.root.activeColor + "\">" + cap + "</font>";
	
	temp = "<tr>" + ind + "<td" + bgimg + " valign=\"top\">" +
		"<a href=\"javascript:parent." + this.root.refObj + ".expand('" + this.ID + "');\">" + img + "</a></td>\n";

	whatSpan = this.root.width - steps;

	link = "<a href=\"javascript:parent." + this.root.refObj + ".";
	link += ( this.items.length ) ? "expand" : "link";
	link += "('" + this.ID + "');\">";
	
	temp += "<td width=" + ( ( this.root.width - steps ) * 16 ) +
		" colspan=" + whatSpan + " valign=\"top\">" + link + cap + "</a></td>\n</tr>\n";
	this.root.code += temp;
	if( this.expanded ) for( part in this.items ) this.items[ part ].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.expanded = false;
	this.active = false;
	this.items = new Array();
	// Methods
	this.paint = coolItemPaint;
	this.newItem = coolNewItem;
	this.expand = coolExpand;
	this.link = coolLink;
	this.collapse = coolCollapse;
}

function coolMenuPaint() {
	args = coolMenuPaint.arguments;
	if( args.length ) this.expand( args[ 0 ] );
	this.code = "<html>\n<head>\n";
	if( this.title ) this.code += "<title>" + this.title + "</title>";
	this.code += "</head>\n";
	if( this.stylesheet ) this.code += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.stylesheet + "\">\n";
	this.code += "<body background=\"" + this.backgr + "\" leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>\n" + this.headCode;
	if( this.box1 ) this.code += this.box1Code;
	if( this.box2 ) this.code += this.box2Code;
	this.code += "<table width=" + ( this.width * 16 ) + " cellpadding=0 cellspacing=0 border=0>";
	for( part in this.items ) this.items[ part ].paint();
	this.code += "<tr>";
	for( x = 0; x < this.width; x++ )
		this.code += "<td width=16><img src=\"" + this.pixel + "\" width=16 height=0 border=0></td>";
	this.code += "</tr>\n</table>\n" + this.footCode +
		"</body>\n</html>";
	if( this.onPaint ) this.onPaint();
	return this.code;
}

function coolMenu() {
	args = coolMenu.arguments;
	// Handle arguments
	this.ID = ( args.length ) ? args[ 0 ] : 0;
	this.frame = ( args.length > 1 ) ? args[ 1 ] : 0;
	this.width = ( args.length > 2 ) ? args[ 2 ] : 16;
	this.stylesheet = ( args.length > 3 ) ? args[ 3 ] : 0;
	this.title = ( args.length > 4 ) ? args[ 4 ] : "Menu";
	this.bgColor = ( args.length > 5 ) ? args[ 5 ] : "#FFFFFF";
	// Properties
	this.items = new Array();
	this.last = 0;
	this.headCode = "";
	this.code = "";
	this.footCode = "";
	this.root = this;
	this.expanded = true;
	this.lastActive = 0;
	this.activeColor = "#FF0000";
	// Images
	this.plus = "plus.gif";
	this.minus = "minus.gif";
	this.item = "item.gif";
	this.line = "line.gif";
	this.last = "last.gif";
	this.pixel = "pixel.gif";
	// Big 
	this.backgr="backgr.gif";
	// Methods
	this.onPaint = 0;
	this.onExpand = 0;
	this.onLink = 0;
	this.newItem = coolNewItem;
	this.paint = coolMenuPaint;
	this.update = coolUpdate;
	this.link = coolLink;
	this.expand = coolExpand;
	this.collapse = coolCollapse;
	this.getDoc = coolGetDoc;
	// Create a referense object
	menuCounter++;
	this.refObj = "menu" + menuCounter + "ref";
	eval( this.refObj + " = this;" );
	// specific for the BIG menu
	this.preloaded = new Array();
	this.box1 = false;
	this.box1Code = new String();
	this.box2 = false;
	this.box2Code = new String();
	this.boxExtra = new String();
}
