var PopupMenuDelay  = 500; // время задерки перед скрытием
var PopupMenuTimer  = new Object (); // отложенные объекты для скрывания (через PopupMenuDelay мс)
var PopupMenuParent = new Object (); // родители
function PMS (ID, PID, n_style, is_vertical, delta_X, delta_Y) { // показать
	PopupMenuParent[ID] = PID;
	for (var ID_temp=ID; ID_temp; ID_temp=PopupMenuParent[ID_temp]) {
		ClearPMH (ID_temp);   // не скрывать этот (потом)
		if (PopupMenuParent[ID_temp]) {
			var prefix = '';
		} else {
			var prefix = 'm';
		}
		var l = document.getElementById('MID' + ID_temp);
		if (l && l.className != 'p' + prefix + n_style + '_a') {
			l.className  = 'p' + prefix + n_style + '_o';
		}
		var limage = document.getElementById('MIDI' + ID_temp);
		if (limage && limage.src) {
			limage.src = limage.src.replace(new RegExp("[^/\]*\.gif$", "g"), 'popup_menu_' + prefix + n_style + '_o.gif');
		}
	}
	for (var ID_temp in PopupMenuTimer) {
		if (!PopupMenuTimer[ID_temp]) {
			continue;
		}
		PMH2 (ID_temp, n_style);  // скрыть (сразу же)
	}
	var l = document.getElementById('M' + ID);
	if (l && l.style.visibility == 'visible') {
		return;
	}

	PopupMenuSetLayerPosition(ID, is_vertical, delta_X, delta_Y);  // позиционировать (IE only)
	var l = document.getElementById('M' + ID);
	if (l) {
		l.style.visibility = 'visible';
	}
	PMSHideSelect('M' + ID, 'visible');
}

function ClearPMH (ID) {  // не скрывать этот (потом)
	if (PopupMenuTimer[ID]) {
		clearTimeout (PopupMenuTimer[ID]);
		PopupMenuTimer[ID] = 0;
	}
}
function PMH (ID, n_style) {  // скрыть (потом)
	for (var ID_temp=ID; ID_temp; ID_temp=PopupMenuParent[ID_temp]) {
		ClearPMH (ID_temp);
		PopupMenuTimer[ID_temp] = setTimeout ('PMH2 (' + ID_temp + ', ' + n_style + ')', PopupMenuDelay);
	}
}
function PMH2 (ID, n_style) { // скрыть (сразу же)
	ClearPMH (ID);  // не скрывать этот (потом)
	var l = document.getElementById('M' + ID);
	if (l) {
		if (l.style.visibility == 'hidden') {
			return;
		}
		l.style.visibility = 'hidden';
	}

	if (PopupMenuParent[ID]) {
		var prefix = '';
	} else {
		var prefix = 'm';
	}
	var l = document.getElementById('MID' + ID);
	if (l && l.className != 'p' + prefix + n_style + '_a') {
		l.className  = 'p' + prefix + n_style + '';
	}
	var limage = document.getElementById('MIDI' + ID);
	if (limage && limage.src) {
		limage.src = limage.src.replace(new RegExp("[^/\]*\.gif$", "g"), 'popup_menu_' + prefix + n_style + '.gif');
	}
	PMSHideSelect('M' + ID, 'hidden');
	for (var ID_temp=PopupMenuParent[ID]; ; ID_temp=PopupMenuParent[ID_temp]) {
		if (ID_temp) {
			PMSHideSelect('M' + ID_temp, 'visible');
		} else {
			break;
		}
	}
}
function PMCS (el, n_style, new_class) { // изменить стиль
	if (el.className != 'p' + n_style + '_a') {
		el.className  = new_class;
	}
}
function PMSHideSelect(id, visibility) {  // показать/скрыть <select> под выпадающим меню (IE only)
	curr = document.getElementById(id);
	if (!curr) {
		return;
	}
	var anti_visibility;
	if (visibility == 'visible') {
		anti_visibility = 'hidden';
	} else {
		anti_visibility = 'visible';
	}
	layer_coord = get_coord(curr);
	var sel=document.getElementsByTagName('SELECT');
	for (var i=0; i<sel.length; i++) {
		select_coord = get_coord(sel[i]);
		if (select_coord['x'] + select_coord['w'] > layer_coord['x'] &&
		select_coord['x']                     < layer_coord['x'] + layer_coord['w'] &&
		select_coord['y'] + select_coord['h'] > layer_coord['y'] &&
		select_coord['y']                     < layer_coord['y'] + layer_coord['h'] &&
		sel[i].style.visibility != anti_visibility
		) {
			sel[i].style.visibility  = anti_visibility;
		}
	}
}

function get_coord (el) {  // найти координаты текущего объекта
	var coord = {
	'x': 0,
	'y': 0,
	'w': 0,
	'h': 0
	}
	if (!el) {
		return coord;
	}
	var el_p;
	if (el.offsetParent) { // сдвиг относительно родителя
		el_p=el;
		while (el_p.offsetParent){
			el_p        = el_p.offsetParent;
			coord['x'] += el_p.offsetLeft;
			coord['y'] += el_p.offsetTop;
		}
	}
	coord['x'] += el.offsetLeft;
	coord['y'] += el.offsetTop
	coord['w']  = el.offsetWidth;
	coord['h']  = el.offsetHeight
	return coord;
}

function PopupMenuSetLayerPosition(ID, is_vertical, delta_X, delta_Y) { // позиционировать (IE only)
	MID  = document.getElementById('MID' + ID);
	if (!MID) {
		return;
	}
	layer_coord = get_coord(MID);
	currMenu    = document.getElementById('M' + ID);
	if (currMenu) {
		currMenu.style.left = layer_coord['x'] + (is_vertical? layer_coord['w']: 0)  + delta_X;
		currMenu.style.top  = layer_coord['y'] + (is_vertical? 0: layer_coord['h'])  + delta_Y;
	}
}
function isElementCoord (mouseX, mouseY, coord) {
	if (
	(mouseX > coord['x']) &&
	(mouseX < (coord['x'] + coord['w'])) &&
	(mouseY > coord['y']) &&
	(mouseY < (coord['y'] + coord['h']))
	) {
		return true;
	} else {
		return false;
	}
}

