/* Сокращение document.getElementById */
function $(id) {
	return document.getElementById(id);
}


/* Создает текстовый нод с содержанием content */
function create_txt(parent, content) {
	var txt = document.createTextNode(content);
	parent.appendChild(txt);

	return txt;
}


/* Создает элемент name с атрибутами atts
 * Если parent != null добавляет к нему элемент
 */
function create_element(parent, name, atts) {
	var el = document.createElement(name);
	for (var aname in atts)
		el.setAttribute(aname, atts[aname]);
	
	if (parent != null)
		parent.appendChild(el);

	return el;
}


/* Добавляет листенер func к элементу node по событию type (без приставки «on») */
function add_event(node, type, func) {
	if (typeof(node.addEventListener) == 'function')
		node.addEventListener(type, func, false);
	else if (typeof(node.attachEvent) == 'object')
		node.attachEvent('on'+type, func);
	else
		return false;
	return true;
}

/* Возвраащет порядковый номер элемента среди собратьев */
function get_child_num(node) {
	var n = node.previousSibling;
	for (var i = 0; n; i++)
		n = n.previousSibling;
	return i;
}


/* Возвращает смещение указанной стороны элемента от соответствующей границы */
function get_offset(node, side) {
	var offset = node['offset'+side];
	for (var p = node.offsetParent; p; p = p.offsetParent)
		offset += p['offset'+side];
	return offset;
}

/* Возвращает смещение относительно левой границы окна */
function get_left_offset(node) {
	return get_offset(node, 'Left');
}

/* Возвращает смещение относительно верхней границы окна */
function get_top_offset(node) {
	return get_offset(node, 'Top');
}

/* Показывает изображение в виде слоя. По умолчанию изображение центрируется.
 * Если изображение больше, чем экран пользователя, то открывается новое окно.
 * Если передать желаемые координаты, то функция постарается выстроится относительно них (но так, чтобы быть в пределеах экрана
 */
function show_big_image(url, width, height) {
	
}

/* Счетчики */
function liveinternet() {
	var src = 'http://counter.yadro.ru/hit?t14.11;r'+escape(document.referrer);
	if (typeof(screen)=="undefined")
		src += ';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth ? screen.colorDepth : screen.pixelDepth);
	src += ';u'+escape(document.URL)+';'+Math.random();
	document.write('<a href="http://www.liveinternet.ru/click" target="_blank">\
		<img src="'+src+' alt="" title="LiveInternet: показано число просмотров за 24 часа, посетителей за 24 часа и за сегодня" width="88" height="31">\
	</a>');
}


/* Всплывающие подпункты меню */
function menu_show_subtypes(type_id) {
	type_id = parseInt(type_id);
	if (typeof(window.menu_items[type_id]) == 'undefined')
		return true;

	var el = $('menu_subtyper');
	if (el == null) {
		el = document.createElement('div');
		el.setAttribute('id', 'menu_subtyper');
		document.body.appendChild(el);
	}
	else {
		while (el.hasChildNodes())
			el.removeChild(el.firstChild);
		el.style.display = 'block';
		el.onmouseover = function() {window.menu_hide_view = 101;menu_set_opacity(el, 100);};
		el.onmouseout = function () {menu_hide_subtypes();};
	}
	window.menu_hide_view = 101;
	menu_set_opacity(el, 100);
	


	el.style.left = get_left_offset($('menu-item-'+type_id)) + 20 + 'px';
	el.style.top = get_top_offset($('menu-item-'+type_id)) + 15 + 'px';

	for (var i in window.menu_items[type_id]) {
		if (window.menu_items[type_id][i].url == '') {
			var span = document.createElement('span');
			span.appendChild(document.createTextNode(window.menu_items[type_id][i].name));
			el.appendChild(span);
		}
		else {
			var a = document.createElement('a');
			a.setAttribute('href', window.menu_items[type_id][i].url);
			a.appendChild(document.createTextNode(window.menu_items[type_id][i].name));
			el.appendChild(a);
		}
	}

	return false;
}

function menu_hide_subtypes(type_id) {
	window.menu_hide_view = 100;
	setTimeout(menu_hide_subtyper, 1000);
}
function menu_hide_subtyper() {
	var el = $('menu_subtyper');
	if (el == null)
		return;

	if (window.menu_hide_view > 100)
		return;

	window.menu_hide_view -= 20;
	menu_set_opacity(el);
	
	if (window.menu_hide_view > 0 && window.menu_hide_view <= 100)
		setTimeout(menu_hide_subtyper, 100);
}

function menu_set_opacity(element) {
	if (window.menu_hide_view <= 0) {
		element.style.display = 'none';
		window.menu_hide_view = 101;
		var percent = 100;
	}
	else if (window.menu_hide_view > 100)
		var percent = 100;
	else
		var percent = window.menu_hide_view;
	if (typeof(document.body.style.filter) != 'undfined')
		element.style.filter = 'alpha(opacity='+ percent + ')';
	if (typeof(element.style.opacity) != 'undfined')
		element.style.opacity = String(percent/100);
}