// $Id: dmenu.js,v 1.1 2005/06/27 13:55:07 a Exp $

function writeMenu( data, class_name) {
	var o = eval( class_name+"=new Menu( '"+class_name+"');");
	o.menuDef = data["items"];
	var _Menu_PreloadImages = new Array();
	var i = 0;
	for ( var i2 in data["preload"]) {
		_Menu_PreloadImages[i] = new Image();
		_Menu_PreloadImages[i++].src = data["preload"][i2];
	}
	o.printMenu();
}

function Menu_strReplace(t, r, s){var value = "";var pos0 = 0;var pos1 = 0;if ( s.length > 0 ){while( 1 ){ pos1 = s.indexOf(t, pos0);if ( pos1 < 0 ) break;value += s.substring( pos0, pos1) + r;pos0 = pos1 + t.length;}value += s.substring( pos0, s.length);}return value;}

function Menu_openClose( o, num, mode) {
	var div =  document.getElementById( num+"_child");
	if ( div != null ) {
		var level = div.className.indexOf( "2" ) != -1 ? 2 : 1;
		if ( div.style.left == "" ) {
			var aTag = o;

			if( level == 1 ) {
				for( var i in this.divs) {
					this.close( i + "_child");
				}
				var leftpos = 0;
				var toppos = 0;
				do {
					leftpos += aTag.offsetLeft;
					toppos += aTag.offsetTop;
					aTag = aTag.offsetParent;
				} while( aTag.tagName != "BODY" );
				div.style.left = leftpos+196;
	    	    div.style.top = toppos;
		        this.divs[num] = div;
			} else {
				for( var i in this.divs2) {
					this.close( i + "_child");
				}
				div.style.left = aTag.parentNode.offsetLeft+aTag.parentNode.offsetWidth-1;
	    	    div.style.top = aTag.parentNode.offsetTop;
		        this.divs2[num] = div;
				this.sub2_active = true;
			}
		} else {
			if( mode == true ) {
				if ( this.timeOuts[num] ) {
					clearTimeout( this.timeOuts[num]);
				}
				if( level == 1 ) {
					if( this.sub2_active == false ) {
						for( var i in this.divs) {
							this.close( i + "_child");
						}
					}
				} else {
					for( var i in this.divs2) {
						this.close( i + "_child");
					}
					this.sub2_active = true;
				}
				div.style.visibility = "visible";
			} else {
				if( this.sub2_active == false ) {
					this.timeOuts[num] = setTimeout( this.name + ".close( '" + num + "_child" + "')", 500);
				}
				if( level == 2 ) {
					this.sub2_active = false;
				}
			}
		}
	}
}
function Menu_close( id) {
	var div = document.getElementById( id);
	var level = div.className.indexOf( "2" ) != -1 ? 2 : 1;
	if( level == 1 ) {
		for( var i in this.divs2) {
			this.close( i + "_child");
		}
	}
	div.style.visibility = "hidden";
}
function Menu_printMenu() {
	var mDef = this.menuDef;
	var mouse_move  = "onMouseOver=\"this.style.backgroundImage='url($$bg_img_over$$)';this.className='$$class_over$$';" + this.name + ".openClose( this, '$$num$$', true);\" onMouseOut=\"this.style.backgroundImage='url($$bg_img$$)';this.className='$$class$$';" + this.name + ".openClose( this, '$$num$$', false);\"";

	var menu = "";
	var child = "";
	var child2 = "";
	var temp = "";
	for( var i in mDef) {
		var cur = mDef[i];
		var next = (mDef[i-(-1)] || 0);

		if ( cur["level"] == 0 ) {
			if ( child2 != "" ) {
				child = this.strReplace( "$$child2$$", child2, child);
				child2 = "";
			}
			if ( child != "" ) {
				menu = this.strReplace( "$$child$$", child, menu);
				child = "";
			}
			temp = this.strReplace( "$$mouse_move$$", mouse_move, this.menuItem0);
			temp = this.strReplace( "$$bg_img$$", ( cur["bg_img"] || ""), temp);
			temp = this.strReplace( "$$bg_img_over$$", ( cur["bg_img_over"] || ""), temp);
			temp = this.strReplace( "$$text$$", ( cur["text"] || ""), temp);
			temp = this.strReplace( "$$left_img$$", ( cur["left_img"] || ""), temp);
			temp = this.strReplace( "$$left_img_over$$", ( cur["left_img_over"] || ""), temp);
			temp = this.strReplace( "$$action$$", ( cur["action"] || ""), temp);
			temp = this.strReplace( "$$class$$", ( cur["class"] || ""), temp);
			temp = this.strReplace( "$$class_over$$", ( cur["class_over"] || ""), temp);
			temp = this.strReplace( "$$num$$", i, temp);
			if ( !next || next["level"] == 0 ) {
				temp = this.strReplace( "$$child$$", "", temp);
			} else {
				temp = this.strReplace( "$$id_num$$", i, temp);
			}
			menu += temp;
		} else if ( cur["level"] == 1 ) {
			if ( child2 != "" ) {
				child = this.strReplace( "$$child2$$", child2, child);
				child2 = "";
			}
			temp = this.strReplace( "$$mouse_move$$", mouse_move, this.menuItem1);
			temp = this.strReplace( "$$bg_img$$", ( cur["bg_img"] || ""), temp);
			temp = this.strReplace( "$$bg_img_over$$", ( cur["bg_img_over"] || ""), temp);
			temp = this.strReplace( "$$text$$", ( cur["text"] || ""), temp);
			temp = this.strReplace( "$$left_img$$", ( cur["left_img"] || ""), temp);
			temp = this.strReplace( "$$left_img_over$$", ( cur["left_img_over"] || ""), temp);
			temp = this.strReplace( "$$action$$", ( cur["action"] || ""), temp);
			temp = this.strReplace( "$$class$$", ( cur["class"] || ""), temp);
			temp = this.strReplace( "$$class_over$$", ( cur["class_over"] || ""), temp);
			temp = this.strReplace( "$$num$$", i, temp);
			if ( !next || next["level"] == 1 || next["level"] == 0 ) {
				temp = this.strReplace( "$$child2$$", "", temp);
				temp = this.strReplace( "$$id_num$$", i+10000, temp);
			} else {
				temp = this.strReplace( "$$id_num$$", i, temp);
			}
			child += temp;
		} else {
			temp = this.strReplace( "$$mouse_move$$", mouse_move, this.menuItem2);
			temp = this.strReplace( "$$bg_img$$", ( cur["bg_img"] || ""), temp);
			temp = this.strReplace( "$$bg_img_over$$", ( cur["bg_img_over"] || ""), temp);
			temp = this.strReplace( "$$text$$", ( cur["text"] || ""), temp);
			temp = this.strReplace( "$$left_img$$", ( cur["left_img"] || ""), temp);
			temp = this.strReplace( "$$left_img_over$$", ( cur["left_img_over"] || ""), temp);
			temp = this.strReplace( "$$action$$", ( cur["action"] || ""), temp);
			temp = this.strReplace( "$$class$$", ( cur["class"] || ""), temp);
			temp = this.strReplace( "$$class_over$$", ( cur["class_over"] || ""), temp);
			temp = this.strReplace( "$$num$$", i, temp);
			child2 += temp;
		}
	}
	if ( child != "" ) {
		menu = this.strReplace( "$$child$$", child, menu);
	}
	if ( child2 != "" ) {
		menu = this.strReplace( "$$child2$$", child2, menu);
	}
	document.write( "<table width='204' cellpadding='0' cellspacing='0'><tr>" + menu + "</tr></table>");
}

function Menu( name) {
	__go = false;
	this.menuDef;
	this.timeOuts = [];
	this.divs = {};
	this.divs2 = {};
	this.sub2_active = false;
	this.name = name;
	this.menuItem0 = "<tr><td height='24' valign='middle' background='$$bg_img$$' title='$$text$$' class='$$class$$' onClick='if( !__go ){$$action$$;}' $$mouse_move$$ nowrap>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$$text$$<div class='menu_sub_items_div' ID='$$id_num$$_child'><table bgcolor='#d89f40' cellpadding='0' cellspacing='0' border='0'><tr><td colspan=2 bgcolor='#d89f40' height=1></td></tr>$$child$$<tr><td colspan=2 bgcolor='#d89f40' height=1></td></tr></table></div></td><td background='$$bg_img$$'><img id='left_img_$$num$$' src='$$left_img$$'></td></tr>";
	this.menuItem1 = "<tr height='24' bgcolor='white' title='$$text$$'><td bgcolor='#d89f40'><div style='width:1px'></div></td><td height='20' $$mouse_move$$ class='$$class$$' onClick='if( !__go ){$$action$$;__go = true;}' background='$$bg_img$$' nowrap>$$text$$&nbsp;&nbsp;&nbsp;&nbsp;<div class='menu_sub_items2_div' ID='$$id_num$$_child'><table cellpadding='0' cellspacing='0' border='0'>$$child2$$<tr><td colspan=2 bgcolor='#000000' height=1></td></tr></table></div></td><td bgcolor='#d89f40'><div style='width:1px'></div></td></tr>";
	this.menuItem2 = "<tr title='$$text$$'><td height='20' $$mouse_move$$ class='$$class$$' onClick='$$action$$;__go = true;' background='$$bg_img$$' nowrap>$$text$$&nbsp;&nbsp;&nbsp;&nbsp;</td><td bgcolor='#000000'><div style='width:1px'></div></td></tr>";

	this.printMenu = Menu_printMenu;
	this.strReplace = Menu_strReplace;
	this.openClose = Menu_openClose;
	this.close = Menu_close;
}
