// Copyright (c) 2001 Sigma eHandel, All rights reserved.

var menuCounter = 0;

function coolSort( a, b ) {
	var coolList = new Array();
	switch( a.root.sortOrder ) {
		case 1: {
			if( a.caption == b.caption ) return 0;
			coolList[ 0 ] = a.caption;
			coolList[ 1 ] = b.caption;
			coolList.sort();
			if( coolList[ 0 ] == a.caption ) return -1; else return 1;
		}
		case 2: {
			if( a.caption == b.caption ) return 0;
			coolList[ 0 ] = a.caption;
			coolList[ 1 ] = b.caption;
			coolList.sort();
			if( coolList[ 0 ] == b.caption ) return -1; else return 1;
		}
		case 3: {
			if( a.ID == b.ID ) return 0;
			coolList[ 0 ] = a.ID;
			coolList[ 1 ] = b.ID;
			coolList.sort();
			if( coolList[ 0 ] == a.ID ) return -1; else return 1;
		}
		case 4: {
			if( a.ID == b.ID ) return 0;
			coolList[ 0 ] = a.ID;
			coolList[ 1 ] = b.ID;
			coolList.sort();
			if( coolList[ 0 ] == b.ID ) return -1; else return 1;
		}
	}
}

function onResize() {
	eval( "resizeMenu = menu" + menuCounter + "ref;" );
	docRef = frames[ resizeMenu.root.frame ].document;
	docRef.location.reload();
}

function coolUpdate() {
	docRef = frames[ this.root.frame ].document;
	if( document.layers ) {
		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();
			//docRef.location.href = url[0] + "?reload=" + Math.random(); + "&" + url[1];
		} else */
			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.active = true;
			
		this.root.lastActive = this;
			
		if( this.href ) {
			tagget = ( this.target ) ? this.target : this.root.frame;
			frames[ tagget ].location.href = this.href;
		}
		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 ].link( itemID );
}

function coolExpand( itemID ) {	
	if( this.ID == itemID  ) {
		this.expanded = !this.expanded;
		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 coolCollapse() {
	if( this.root != this ) this.expanded = false;
	if( this.items.length )
		for( this.part in this.items ) this.items[ this.part ].collapse();
}

function coolExplode() {
	this.expanded = true;
	if( this.items.length )
		for( this.part in this.items ) this.items[ this.part ].explode();
}

function coolEditItem() {
	args = ( coolEditItem.arguments.length == 1 ) ? coolEditItem.arguments[ 0 ] : coolEditItem.arguments;
	if( this.ID == args[ 0 ] ) {
		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 ];
		if( this.root.sortOrder ) this.parent.items.sort( coolSort );
	} else
		for( this.counter = 0; this.counter < this.items.length; this.counter++ )
			this.items[ this.counter ].editItem( args );
}

function coolDeleteItem( itemID ) {
	for( this.counter = 0; this.counter < this.items.length; this.counter++ )
		if( this.items[ this.counter ].ID == itemID ) {
			for( this.counter2 = this.counter; this.counter2 < ( this.items.length - 1 ); this.counter2++ )
				this.items[ this.counter2 ] = this.items[ this.counter2 + 1 ];
			this.items.length = ( this.items.length - 1 );
			break;
		} else
			this.items[ this.counter ].deleteItem( itemID );
}

function coolNewItem() {
	args = ( coolNewItem.arguments.length == 1 ) ? coolNewItem.arguments[ 0 ] : coolNewItem.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;
		if( this.root.sortOrder ) this.items.sort( coolSort );
	} else
		for( this.counter = 0; this.counter < this.items.length; this.counter++ )
			this.items[ this.counter ].newItem( 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 + "><img src=\"" + cur.root.pixel + "\" border=0></td>\n" + ind;
		else
			ind = "<td width=" + this.root.itemSize + " background=\"" + cur.root.line + "\"><img src=\"" + cur.root.pixel + "\" border=0></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.expanded ) ? this.root.minus : this.root.plus;
		alt = ( this.expanded ) ? "Stäng " : "Öppna ";
		iconBg = ( this.expanded ) ? " 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:";
	//if( this.parent == this.root ) temp += "parent." + this.root.refObj + ".collapse();";
	temp += "parent." + this.root.refObj + ".expand('" + 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--;
	}
	
	link = "<a href=\"javascript:"
	//if( this.parent == this.root ) link += "parent." + this.root.refObj + ".collapse();";
	link += "parent." + this.root.refObj + ".";
	link += ( this.href ) ? "link" : "expand";
	link += "('" + this.ID + "');\">";
	if( this.root.icons ) link += "<img src=\"" + this.root.pixel + "\" width=2 border=0>";
	
	temp += "<td width=" + ( ( this.root.width - steps ) * this.root.itemSize ) +
		" colspan=" + whatSpan + " valign=\"top\">" + link + cap + "</a></td>\n</tr>\n";
	this.root.code += temp;
	if( this.expanded )
		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.expanded = false;
	this.active = false;
	this.items = new Array();
	// Methods
	this.paint = coolItemPaint;
	this.newItem = coolNewItem;
	this.editItem = coolEditItem;
	this.deleteItem = coolDeleteItem;
	this.expand = coolExpand;
	this.link = coolLink;
	this.explode = coolExplode;
	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 += '<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">';
	this.code += "</head>\n";
	if( this.stylesheet ) this.code += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.stylesheet + "\">\n";
	this.code += "<body bgcolor=\"" + this.bgColor + "\" leftmargin=" + this.leftMargin + " topmargin=" + this.topMargin +
		" background=\"images/menu_bg.gif\" marginwidth=" + this.leftMargin + " marginheight=" + this.topMargin + ">\n" +
		this.headCode + "<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.code += "<tr>";
	for( x = 0; x < this.width; x++ )
		this.code += "<td width=" + this.itemSize + "><img src=\"" + this.pixel + "\" width=" + this.itemSize + " 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.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 ] : "Menu";
	this.bgColor = ( args.length > 5 ) ? args[ 5 ] : "#FFFFFF";
	// Properties
	this.items = new Array();
	this.last = 0;
	this.headCode = "<a href=\"base.asp?choise=0\" target=\"base\"></a><br>";
	this.code = "";
	this.footCode = "";
	this.root = this;
	this.expanded = true;
	this.lastActive = 0;
	this.activeColor = "#26688c";
	this.topMargin = 2;
	this.leftMargin = 2;
	this.icons = false;
	this.sortOrder = 1;
	// Images
	this.plus = "images/pixel.gif";
	this.minus = "images/menu_dot.gif";
	this.item = "images/item.gif";
	this.line = "images/pixel.gif";
	this.last = "images/pixel.gif";
	this.pixel = "images/pixel.gif";
	// Methods
	this.onPaint = 0;
	this.onExpand = 0;
	this.onLink = 0;
	this.newItem = coolNewItem;
	this.editItem = coolEditItem;
	this.deleteItem = coolDeleteItem;
	this.paint = coolMenuPaint;
	this.update = coolUpdate;
	this.link = coolLink;
	this.expand = coolExpand;
	this.explode = coolExplode;
	this.collapse = coolCollapse;
	// Create a referense object
	menuCounter++;
	this.refObj = "menu" + menuCounter + "ref";
	eval( this.refObj + " = this;" );
}
