//
// базовые функции
//

function ap_getObj(name) {
	// сделать ссылку на объект
	if (document.getElementById) return document.getElementById(name);
	else if (document.all) return document.all[name];
	else if (document.layers) return document.layers[name];
}

function show_hide_obj(obj_id, view) {
	// скрыть или отобразить obj_id
	obj=ap_getObj(obj_id);
	if (view==1) {
		obj.style.display="block";
	} else {
		obj.style.display="none";
	}
}

function LoadBigImage(src,w,h) {
	var l=screen.width/2-w/2;
	var t=screen.height/2-h/2-30;
	var scroll="no";
	var dw=screen.width-w;
	var dh=screen.height-h;
	if (dw<20 || dh<20) scroll="yes";
	var param="resizable=yes,scrollbars="+scroll+",Toolbar=0,location=0,Directories=0,Status=0,Menubar=0,"+"width="+w+", height="+h+", left="+l+", top="+t;
	windowID=window.open('', target="_blank",param);
	windowID.document.write("<html>\n<head>\n<title></title>\n</head>\n");
	windowID.document.write("<body leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" bottomargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\n");
	windowID.document.write("<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;height:100%;text-align:center;vertical-align:middle;\">\n<tr>\n<td>");
	windowID.document.write("<img src=\""+src+"\" width=\""+w+"\" height=\""+h+"\">\n");
	windowID.document.write("</td>\n</tr>\n</table>\n</body>\n</html>\n");
}

//
// библиотека меню
//

if(typeof HTMLElement != "undefined") {
	if(!HTMLElement.prototype.insertAdjacentElement) {
		HTMLElement.prototype.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;
			}
		}
		HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr) {
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var parsedHTML = r.createContextualFragment(htmlStr);
			this.insertAdjacentElement(where,parsedHTML)
		}	
		HTMLElement.prototype.insertAdjacentText = function(where,txtStr) {
			var parsedText = document.createTextNode(txtStr)
			this.insertAdjacentElement(where,parsedText)
		}
	}
	if (!HTMLElement.prototype.innerHTML) {
		HTMLElement.prototype.innerHTML = function (str) {
		   var r = this.ownerDocument.createRange();
		   r.selectNodeContents(this);
		   r.deleteContents();
		   var df = r.createContextualFragment(str);
		   this.appendChild(df);
		   return str;
		}	
		HTMLElement.prototype.outerHTML = function (str) {
		   var r = this.ownerDocument.createRange();
		   r.setStartBefore(this);
		   var df = r.createContextualFragment(str);
		   this.parentNode.replaceChild(df, this);
		   return str;
		}
	}
}

// глобальные переменные для меню
var cur_menu_id = "";
var menu_show = new Array();
var menu_gen = new Array();
var menu_id = "";
var menu_x = 0;
var menu_y = 0;
var popTimer = 0;

function getSty(id) {
	var isNS4 = (document.layers ? true : false);
    return (isNS4 ? ap_getObj(id) : ap_getObj(id).style);
} 

function Item(pid, text, url, img, img_over, type, ori, div_style, table_style, td_style) {
	this.pid = pid;
	this.text = text;
	this.url = url;
	this.img = (img?img:false);
	this.img_over = (img_over?img_over:false);
	this.ori = ori;
	this.type = type;
	this.div_style = (div_style?div_style:false);
	this.table_style = (table_style?table_style:false);
	this.td_style = (td_style?td_style:false);
	this.x = 0;
	this.y = 0;
	this.h = 0;
	this.w = 0;
}

function check_sys() {
	var 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; j<temp.length; j++) {
			if(menu_show[i] == temp[j]) {
				hide = false;
				break;
			}
		}
		if (hide == true) {
			getSty("a" + menu_show[i]).visibility = "hidden";
		}
	}
	if (cur_menu_id) {
		if (ap_getObj("a" + cur_menu_id)) {
			if (getSty("a" + cur_menu_id).visibility == "hidden") {
				ap_getObj("td_x_" + cur_menu_id).style.background = "#53748b"; //цвет фона mouse-out
				ap_getObj("td_y_" + cur_menu_id).src = "/common/img/isin/arr-h.gif";
				cur_menu_id = '';
			}
		} else {
			if (popTimer) {
				ap_getObj("td_x_" + cur_menu_id).style.background = "#53748b"; //цвет фона mouse-out
				ap_getObj("td_y_" + cur_menu_id).src = "/common/img/isin/arr-h.gif";
				cur_menu_id = '';
			}
		}
	}
}

function td_menu_over(id) {
	clearTimeout(popTimer);
	write_menu_custom(id);
	menu_status = id;
	check_sys();
	popTimer = 0;
	if (menu[id].pid == 0 && cur_menu_id) {
			ap_getObj("td_y_" + cur_menu_id).src = "/common/img/isin/arr-h.gif";
			ap_getObj("td_x_" + cur_menu_id).style.background = "#0075b0";
			cur_menu_id = '';
	}
	if (menu[id].pid == 0) {
			ap_getObj("td_y_" + id).src = "/common/img/isin/arr-v.gif";
			ap_getObj("td_x_" + id).style.background = "#0075b0";
			cur_menu_id = id;
	}
	if (ap_getObj("a" + id) && id != '') {
		getSty("a" + id).visibility = "visible";
		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;
	}
	check_sys();
}

function td_menu_out() {
	menu_status = 0;
	clearTimeout(popTimer);
	popTimer = setTimeout('check_sys()', 600);
}

//
// пишем меню
//

function write_menu_l1(ox, oy) {
	menu_x = ox;
	menu_y = oy;
	write_menu_custom(0);
}

function write_menu_other() {
}

function write_menu_custom(id) {
	var temp_menu = new Array();
	var index_menu = new Array();
	var str = '';

	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) {
			temp_menu[(temp_menu.length?temp_menu.length:0)] = menu[i];
			index_menu[(index_menu.length?index_menu.length:0)] = i;
		}
	}
	if (temp_menu.length) {
		if (menu_gen.length && menu[id].ori== 2) {
			xx = menu[id].x + menu[id].w;
			yy = menu[id].y;
		} else {
			xx = menu_gen.length?menu[id].x:'';
			yy = menu_gen.length?(menu[id].y + menu[id].h):'';
		}
		                                                

		if (!menu_gen.length) {
			str =  '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background: url(/common/img/isin/navigation-bg01.gif) repeat-x;">';
			str += '<tr>';
			for (i=0; i<temp_menu.length; i++){
				if (menu[index_menu[i]].div_style != false) {
					str += '<td id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '"  align="center" nowrap width="33%" background="/common/img/isin/main-menu-pipe01.gif" class="bgr"><div style="padding-top: 6px; padding-bottom: 6px; background: url(/common/img/isin/main-menu-pipe01.gif) repeat-x;"><img src="/common/img/isin/arr-v.gif" width="7" height="7" hspace="4" border="0" alt=""><b><span class="menu">'+ temp_menu[i].text +'</span></b>&nbsp;&nbsp;</div></td>';
				 } else {
					str += '<td id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" align="center" nowrap width="33%" background="/common/img/isin/main-menu-pipe01.gif" class="bgr"><div style="padding-top: 6px; padding-bottom: 6px;"><img id="td_y_' + index_menu[i] + '" name="td_y_' + index_menu[i] + '" src="/common/img/isin/arr-h.gif" width="7" height="7" hspace="4" border="0" alt=""><span class="menu">'+ temp_menu[i].text +'</span>&nbsp;&nbsp;</div></td>';
				}
	
			}
			str += '</tr>';
			str += '<tr>';
			for (i=0; i<temp_menu.length; i++) {
				str += '<td bgcolor="#53748b" id="td_x_' + index_menu[i] + '" name="td_x_' + index_menu[i] + '"><div style="width: 0px; height: 3px;"><spacer type="block" height="3"></spacer></div></td>';
			}
			str += '</tr>';
			str += '</table>';
		} else {
			str = "<div id='a" + id + "' style='position: absolute; left: " + xx + "px; top: " + yy + "px; width:" + (ap_getObj("td_" + id).offsetWidth) + "px; visibility: visible; z-index: 999999;'>";
			str += '<table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" width="0px">';
			str += '<tr>';
			str += '<td bgcolor="#0075b0"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
			str += '<td colspan="3" background="/common/img/isin/m-bg.gif" align="right"><img src="/common/img/isin/m-r.gif" width="4" height="4" border="0" alt=""></td>';
			str += '</tr>';
			for (i=0; i<temp_menu.length; i++){
				str += '<tr>';
				str += '<td bgcolor="#0075b0"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
				str += '<td id="td_' + index_menu[i] + '" name="td_' + index_menu[i] + '" onmouseover="td_menu_over(' + index_menu[i] + ');" onmouseout="td_menu_out();" nowrap><a href="' + temp_menu[i].url + '"' + ((temp_menu[i].table_style == '1')?(" target=_blank"):("")) + ' class="ddmenu"><img src="/common/img/isin/m-li.gif" width="3" height="3" hspace="5" vspace="2" border="0" alt="">' + temp_menu[i].text + '</a><div style="width: 0px; height: 3px;"><spacer type="block" height="3"></spacer></div></td>';
				str += '<td><div style="width: 10px; height: 0px;"><spacer type="block" width="10"></spacer></div></td>';
				str += '<td bgcolor="#0075b0"><div style="width: 1px; height: 0px;"><spacer type="block" width="1"></spacer></div></td>';
				str += '</tr>';
			}
			str += '<tr>';
			str += '<td colspan="4" bgcolor="#0075b0"><div style="width: 0px; height: 1px;"><spacer type="block" height="1"></spacer></div></td>';
			str += '</tr>';
			str += '</table>';
			str += "</div>";
		}
		if (menu_gen.length) {
			ap_getObj('place_menu2').innerHTML += str;
		} else {
			ap_getObj('place_menu').innerHTML = str;
		}
		for (i=0; i<temp_menu.length; i++) {
			if (menu_gen.length) {
				menu[index_menu[i]].x = ap_getObj("a" + id).offsetLeft + ap_getObj("td_" + index_menu[i]).offsetLeft;
				menu[index_menu[i]].y = ap_getObj("a" + id).offsetTop + ap_getObj("td_" + index_menu[i]).offsetTop;
			} else {
				menu[index_menu[i]].x = menu_x + ap_getObj("place_menu").offsetLeft + ap_getObj("td_" + index_menu[i]).offsetLeft;
				menu[index_menu[i]].y = menu_y + ap_getObj("td_" + index_menu[i]).offsetTop;
			}
			menu[index_menu[i]].h = ap_getObj("td_" + index_menu[i]).offsetHeight -3;
			menu[index_menu[i]].w = ap_getObj("td_" + index_menu[i]).offsetWidth;
		}
		menu_gen[(menu_gen.length?menu_gen.length:0)] = id;
	}
}

function clear_all() {
	menu_show = new Array();
	menu_gen = new Array();
	menu_id = "";
	menu_status = 0;
	ap_getObj('place_menu2').innerHTML = "";
	ap_getObj('place_menu').innerHTML = "";
	write_menu_l1(menu_x, menu_y);
}