var menu_x = 0;
var menu_y = 0;

function DM_write_menu(ox, oy){
	var elem ;
	if (isNS4 || isOP && navVersion < 7) { return false }
	if (ox+oy>0) {
		menu_x = ox?ox:0
		menu_y = oy?oy:0
		if(!document.body.insertAdjacentHTML) {DM_defineDOMparts();}
		document.body.insertAdjacentHTML("afterBegin","<div id='DM_place' name='DM_place' style='z-index:499; position: absolute; left: " + menu_x + "px; top: " + menu_y + "px; visibility: visible;'></div>");
	}
	else {
		menu_x = ox?ox:getRealLeft(getRef("DM_place"));
		menu_y = oy?oy:getRealTop(getRef("DM_place"));
	}
	
	menu[0].contbl_id = menu_current[1];
	if (DM_show_path) for (j=1; j<menu_current.length ;j++) 
		for(i=1; i<menu.length; i++)
			if(menu_current[j]==menu[i].contbl_id) {
				menu[i].active = true;
				if (menu[i].img_over) {
					menu[i].img = menu[i].img_over;
					menu[i].img_over = null;
				}
				if (menu[i].img) {
					menu[i].td_over_style = null; 
				}
				menu[i].td_style = DM_td_active_style;
				menu[i].td_div_class = "DMtdDivActive";
			}
	write_menu_custom(0);
	return true;
}

function write_menu_custom(id){
	var index_menu = new Array();
	var str = '', ind, xx, yy;
	for(i=0; i<menu_gen.length; i++) if (menu_gen[i] == id) return true;
	for(i=1; i<menu.length; i++) if (menu[i].pid == id) index_menu[(index_menu.length?index_menu.length:0)] = i;
	if (index_menu.length){
		xx = 0;
		yy = 0;
		if (!id){
			xx = menu_x; 
			yy = menu_y;
		}
		else 
			if (menu[id].ori == 1){
				xx = menu[id].x + menu[id].w;
				yy = menu[id].y-1;
			}else if (menu[id].ori == 2) {
				xx = menu[id].x-1;
				yy = menu[id].y + menu[id].h;
			}else {
				xx = menu[id].x-1;
				yy = menu[id].y-1;
			}
		str = menu_gen.length?"<div id='DM_div_" + id + "'" + (id?(" class=" + menu[id].div_class):"") + " style='z-index:5" + (id<10?"0":"") + id + " ;position: absolute; left: " + xx + "px; top: " + yy + "px; visibility: hidden;'>":'';
		str += "<table cellpadding='0' cellspacing='0' border='0' class='" + menu[id].table_class + "'>";		str += menu[id].type?"<tr>":'';
		for(i=0; i<index_menu.length; i++){
			ind = index_menu[i];
			str += menu[id].type?'':"<tr>";
			str += "<td id='DM_td_" + ind + "' name='DM_td_" + ind + "'" + (menu[ind].w?"width="+menu[ind].w:"") + " onmouseover='DM_td_over(" + ind + ");' onmouseout='DM_td_out();' ";
			if(menu[ind].img){
				if (menu[ind].img_over){
					menu[ind].image=new Image(); 
					menu[ind].image.src=menu[ind].img;
					menu[ind].image_over=new Image(); 
					menu[ind].image_over.src=menu[ind].img_over;
				}
				str += ">"
			}else{
				str += (menu[ind].url?" onclick=\"link('" + menu[ind].url + "');\" ":'');
				if (!id) {str += " nowrap ";}
				str += " style= '"+menu[ind].td_style+"'>"
			}
			if(!menu[ind].img){str += "<div class='" + menu[ind].td_div_class + "' id='DM_td_div_" + ind + "' name='DM_td_div_" + ind + "'>";}
			str += menu[ind].url?"<a href=" + menu[ind].url + ">":'';
			str += menu[ind].img?"<img src='" + menu[ind].img + "' alt='" + menu[ind].text + "' id='DM_td_img_" + ind + "' name='DM_td_img_" + ind + "' "+ (menu[ind].w?"width='"+menu[ind].w+"' ":"")+ (menu[ind].h?"height='"+menu[ind].h+"' ":"") +" border='0'>":menu[ind].text;
			str += (menu[ind].url?"</a>":'');
			if(!menu[ind].img){str += "</div>"}
			str += "</td>";
			str += menu[id].type?'':"</tr>";
		}
		str += menu[id].type?"</tr>":'';
		str += "</table>";
		str += menu_gen.length?"</div>":'';		
		if(menu_gen.length){
			document.body.insertAdjacentHTML("afterBegin",str);
		}else{
			getRef('DM_place').innerHTML = str;
		}
		if (id) menu[id].child = true;
		for(i=0; i<index_menu.length; i++){
			ind = index_menu[i];
			menu[ind].h = getRef("DM_td_" + ind).offsetHeight;
			if (!menu[ind].w){
				iw = getRef("DM_td_" + ind).offsetWidth;
				if(iw<DM_min_width) iw=DM_min_width;
				if(iw>DM_max_width) iw=DM_max_width;
				getSty("DM_td_" + ind).width = iw;
				menu[ind].w = iw;
				menu[ind].td_style += " width:" +iw + ";";
				menu[ind].td_over_style += " width:" +iw + ";";
			}
		}
		if (id){
			if(menu[id].ori == 3){
				getRef("DM_div_" + id).offsetWidth;
				getSty("DM_div_" + id).left=getRealLeft(getRef("DM_div_" + id))-getRef("DM_div_" + id).offsetWidth+1;	
			}
			else if (menu[id].ori == 0){
				getRef("DM_div_" + id).offsetHeight;
				getSty("DM_div_" + id).top=getRealTop(getRef("DM_div_" + id))-getRef("DM_div_" + id).offsetHeight+1;			
			}
		}
		for(i=0; i<index_menu.length; i++){
			ind = index_menu[i];
			menu[ind].x = getRealLeft(getRef("DM_td_" + ind));
			menu[ind].y = getRealTop(getRef("DM_td_" + ind));
		}
		menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
	}
	return true;
}

