var menu = new Array();
var menu_x = 0;
var menu_y = 0;
var menu_fixed = true;
var menu_current = new Array();
var menu_status = 0;
var DM_other_resize;

var navVersion = parseFloat(navigator.appVersion.indexOf("MSIE")>0?navigator.appVersion.split(";")[1].substr(6):navigator.appVersion);
var navName = navigator.appName;
var navAgent = navigator.userAgent;
var isWin = (navAgent.indexOf('Win')!=-1);
var isMac = (navAgent.indexOf('Mac')!=-1);
var isSFR = (navName.indexOf('Konqueror')!=-1&&(navVersion>=5))||(navAgent.indexOf('Safari')!=-1);
var isOP = (navAgent.indexOf('Opera')!=-1&&navVersion>=4);
if (isOP && navName.indexOf('Opera')==-1) {navVersion = (parseFloat(navigator.userAgent.split(") ")[1].split(" ")[1]))}
var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var menu_show = new Array();
var menu_gen = new Array();
var menu_id = "";
var outTimer = 0;
var overTimer = 0;


function DM_defineDOMparts() {
	document.body.insertAdjacentElement = function(where,parsedNode){
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling)             
				this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
		return true;
	}
	document.body.insertAdjacentHTML = function(where,htmlStr){
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
		return true;
	}
	document.body.insertAdjacentText = function(where,txtStr){
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
		return true;
	}
}

function newinnerHTML (obj, str){
	   var r = obj.ownerDocument.createRange();
	   r.selectNodeContents(obj);
	   r.deleteContents();
	   var df = r.createContextualFragment(str);
	   obj.appendChild(df);
	   return str;
	}
	
function newouterHTML (obj, str) {
	   var r = obj.ownerDocument.createRange();
	   r.setStartBefore(obj);
	   var df = r.createContextualFragment(str);
	   obj.parentNode.replaceChild(df, obj);
	   return str;
}


function getRef(id) {
    if (isDOM) return document.getElementById(id);
    if (isIE4) return document.all[id];
    if (isNS4) return document.layers[id];
	return null;
}

function getSty(id) {
    return (isNS4 ? getRef(id) : getRef(id).style);
} 

function getRealLeft(el) {
    var xPos = 0, tempEl = el;
//    if(isIE4 || isDOM) if(tempEl.tagName == "TD") xPos += tempEl.clientLeft;
    while (tempEl != null) {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
}

function getRealTop(el) {
    var yPos = 0, tempEl = el;
//    if(isIE4 || isDOM) if(tempEl.tagName == "TD") yPos += tempEl.clientTop;
    while (tempEl != null) {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;	
}

function link(link) {
document.location=link;
}

function Item(pid, contbl_id, text, url, img, img_over, width, height, type, ori, div_class, table_class, td_style, td_over_style, td_div_class//, temp1, temp2, ..., tempN
		){
	this.pid			= pid;
	this.contbl_id		= contbl_id;
	this.text			= text;
	this.url			= url;
	this.img			= (img?img:null);
	this.img_over		= (img_over?img_over:null);
	this.h				= (height?height:0);
	this.w				= (width?width:0);
	this.ori			= ori;
	this.type			= type?true:false;
	this.div_class		= (div_class?div_class:"DMdefault");
	this.table_class 	= (table_class?table_class:"DMdefault");
	this.td_style		= (td_style?td_style:DM_td_style);
	this.td_over_style 	= (td_over_style?td_over_style:(DM_td_over_style)?DM_td_over_style:this.td_style);
	this.td_over_style += "  cursor:hand; ";
	this.td_div_class 	= td_div_class?td_div_class:"DMtdDivDefault";
	this.x				= 0;
	this.y				= 0;
	this.child			= false;
	this.active			= false;

// !!! Uncoment for arguments !!!
/*	if(Item.arguments.length>10){
		var a = Item.arguments; 
		for(i=1; (i+9)<a.length; i++)
			eval("this.temp" + i + "= a[i+9]");
	}
*/
}

function DM_check_sys(){
	var temp = new Array();
	var show_temp = new Array();
	var i = "";
	var hide;
	temp[temp.length] = menu_status;
	if (menu_status != 0)
		if(menu[menu_status].pid != '')
		{
			i=menu[menu_status].pid;
			temp[temp.length] = i;
			while (menu[i].pid != ''){
				i=menu[i].pid;
				temp[temp.length] = i;
			}
		}
	for(i=0; i<menu_show.length; i++){
		hide = true;
		for(j=0; hide && j<temp.length; j++)
			if(menu_show[i] == temp[j]){
				hide = false;
				show_temp[show_temp.length] = menu_show[i];
			}
		if(hide == true){
			if (menu[menu_show[i]].child) getSty("DM_div_" + menu_show[i]).visibility = "hidden";
			if (menu[menu_show[i]].img_over) document.images["DM_td_img_"+menu_show[i]].src = menu[menu_show[i]].image.src
			else if (!menu[menu_show[i]].img && !isMac) getSty("DM_td_" + menu_show[i]).cssText = menu[menu_show[i]].td_style;
		}
	}
	menu_show = show_temp;
	return true;
}

function DM_td_over(id){
	clearTimeout(overTimer);
	overTimer = setTimeout('DM_td_doover('+id+')', DM_over_delay);
}

function DM_td_doover(id){
	clearTimeout(outTimer);
	write_menu_custom(id);
	menu_status = id;
	var add = true;
	for (i=0; i<menu_show.length; i++) if(menu_show[i] == id) add = false;
	if (add == true) menu_show[menu_show.length] = id;
	if (menu[id].img_over) document.images["DM_td_img_"+id].src = menu[id].image_over.src
	else if (!menu[id].img && !isMac) getSty("DM_td_" + id).cssText = menu[id].td_over_style;
	if (menu[id].child && id != ''){
		getSty("DM_div_" + id).visibility = "visible";
	}
	DM_check_sys();
	return true;
}

function DM_td_out(){
	menu_status = 0;
	clearTimeout(outTimer);
	clearTimeout(overTimer);
	outTimer = setTimeout('DM_check_sys()', DM_out_delay);
	return true;
}

function DM_resize_handler(){
	if(menu_gen && menu_gen.length){
		var ref, i, id;
		if (menu.length>1) DM_check_sys();
		var menu_dx = menu_x; 
		var menu_dy = menu_y; 
		DM_check_rootpoint()
		menu_dx = menu_x - menu_dx;
		menu_dy = menu_y - menu_dy;
		if(menu_dx || menu_dy) {
			for(i=1; i<menu.length; i++) if(menu[i]) {
				menu[i].x += menu_dx;
				menu[i].y += menu_dy;
			}
			for(i=1; i<menu_gen.length; i++){
				id = menu_gen[i]; 
				ref = getSty("DM_div_" + id);
				ref.left = menu[id].x
				ref.top = menu[id].y;
			}
		}
	}
	DM_other_resize();
}

function DM_check_rootpoint(){
		menu_x = getRealLeft(getRef("DM_place"));
		menu_y = getRealTop(getRef("DM_place"));
}

function DM_write_menu(ox, oy){
	var elem ;
	ox=ox?ox:0;
	oy=oy?oy:0;
	if (isNS4 || isOP && navVersion < 7) {  return false }
	if(!document.body.insertAdjacentHTML) {DM_defineDOMparts();}
	if (ox+oy>0) {
		menu_x = ox?ox:0
		menu_y = oy?oy:0
		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_fixed = false;
	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[i] && 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);
	if(!menu_fixed) {
		DM_check_rootpoint();
		DM_other_resize = (window.onresize) ? window.onresize :  new Function;
		window.onresize = DM_resize_handler;
	}
	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] && 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 if (menu[id].ori == 3) {
				xx = menu[id].x-1;
				yy = menu[id].y;
			}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:1" + (id<10?"0":"") + id + " ; position: absolute; left: " + xx + "px; top: " + yy + "px; visibility: hidden;'>":'';
		str += "<table cellpadding='0' cellspacing='0' border='0' class='" + (id?(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 + "');\" ":'');
				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 + "'>";
				if(DM_td_image) {str += '<table cellpadding="0" cellspacing="0" border="0"><tr valign="'+ DM_td_image[3] +'"><td><img src="'+ DM_td_image[0] +'" width="'+ DM_td_image[1] +'" height="'+ DM_td_image[2] +'" alt="" border="0"></td><td>'}
			}
			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){
				if(DM_td_image) {str += "</td></tr></table>"}
				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){
			var ref = getRef("DM_div_" + id);
			var sty = getSty("DM_div_" + id);			
			if(menu[id].ori == 3) sty.left=xx-ref.offsetWidth+1;	
			else if (menu[id].ori == 0) sty.top=yy-ref.offsetHeight+1;
			menu[id].x = getRealLeft(ref);
			menu[id].y = getRealTop(ref);
		}
		for(i=0; i<index_menu.length; i++){
			var ind = index_menu[i];
			var ref = getRef("DM_td_" + ind);
			menu[ind].x = getRealLeft(ref);
			menu[ind].y = getRealTop(ref);
		}
		menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
	}
	return true;
}
