function addEvent(obj, ev, eh) {
	if (obj.addEventListener) obj.addEventListener(ev, eh, false);
	else if (obj.attachEvent) obj.attachEvent('on' + ev, eh);
}
function getEventObj(e) {
	if (e.originalTarget) return e.originalTarget;
	else if (e.srcElement) return e.srcElement;
	else return false;
}
function getAbsolutePosition(elm, root)
{
	var x = 0, y = 0;
	while( elm && elm != document.body )
	{
		if (typeof(root) != 'undefined' && elm == root)
		{
			break;
		}
		x += elm.offsetLeft;
		y += elm.offsetTop;
		elm = elm.offsetParent;
	}
	return { x: x, y: y };
}
var str_re = '^(http:\/\/' + location.host + ')?\/(?:euro2008\/|olymp2008\/)?(?:[a-z]+\/)?[a-z]+\/[0-9]{2}\/[0-9]{2}\/[0-9]{4}\/([0-9]+)\.shtml$';
var header_re = new RegExp(str_re, 'i');
header_re.compile(str_re, 'i');
function header()
{
	var _this = this;
	this.ajax = new ajax();
	// element with ancors
	this.el = null;
	// current ancor
	this.curHref = '#';
	this.cur = null;
	this.curId = 0;
	// timeout
	this.timeout = null;
	// content div
	this.div = null;
	this.ifr = null;
	// cache
	this.cache = {};
	/**
	 * attach object to ancors container
	 */
	this.attach = function attach(el)
	{
		if (typeof(el) == 'undefined')
		{
			el = document.body;
		}
		this.el = el;
		var list = this.el.getElementsByTagName('A');
		for (l = 0; l < list.length; l++)
		{
			if (list[l].getAttribute('header') == null)
			{
				continue;
			}
			if (typeof(list[l].onmouseover) != 'undefined')
			{
				list[l].preheader_onmouseover = list[l].onmouseover;
			}
			if (typeof(list[l].onmouseout) != 'undefined')
			{
				list[l].preheader_onmouseout = list[l].onmouseout;
			}
			list[l].onmouseover = this.mouseover;
			list[l].onmouseout = this.mouseout;
		}
	}
	/**
	 * detach object from ancors
	 */
	this.detach = function detach()
	{
		var list = this.el.getElementsByTagName('A');
		for (l in list)
		{
			if (typeof(list[l].preheader_onmouseover) != 'undefined')
			{
				list[l].onmouseover = list[l].preheader_onmouseover;
				list[l].preheader_onmouseover = null;
			}
			if (typeof(list[l].preheader_onmouseout) != 'undefined')
			{
				list[l].onmouseout = list[l].preheader_onmouseout;
				list[l].preheader_onmouseout = null;
			}
		}
	}
	this.mouseover = function mouseover(e)
	{
		if (typeof(e) == 'undefined')
		{
			e = window.event;
		}
		_this.clearTimeout();
		var cur = getEventObj(e);
		if (_this.div && _this.div.style && _this.div.style.display == 'block')
		{
			if (cur == _this.cur)
			{
				return;
			}
			var _cur = cur;
			while (_cur && typeof(_cur.parentNode) != 'undefined')
			{
				if (_cur == _this.div)
				{
					return;
				}
				_cur = _cur.parentNode;
			}
		}
		_this.hide();
		_this.curHref = cur.href;
		var id = header_re.exec(cur.href);
		if (typeof(id) != 'undefined' && id)
		{
			_this.cur = cur;
			_this.curId = id[2];
			_this.timeout = setTimeout(_this.display, 500);
		}
	}
	this.display = function display()
	{
		_this.load(_this.curId);
	}
	this.clearTimeout = function _clearTimeout()
	{
		if (this.timeout)
		{
			clearTimeout(this.timeout);
			this.timeout = null;
		}
	}
	this.mouseout = function mouseout(e)
	{
		_this.clearTimeout();
		_this.timeout = setTimeout(_this.hide, 300);
	}
	this.hide = function hide()
	{
		if (typeof(_this.div) != 'undefined' && _this.div)
		{
			_this.div.style.display = 'none';
		}
		_this.cur = null;
	}
	this.load = function load(id)
	{
		if (typeof(this.cache[id]) == 'undefined')
		{
			url = '/anons/' + id + '.shtml';
			this.ajax.sendRequest(this.loaded, url, null, null, 'text/plain;charset=windows-1251', true);
		}
		else
		{
			this.setText(this.cache[id]);
		}
	}
	this.loaded = function loaded()
	{
		if (typeof(_this.ajax.req.readyState) == 'undefined' || _this.ajax.req.readyState == 4)
		{
			_this.cache[_this.curId] = _this.ajax.req.responseText;
			_this.setText(_this.ajax.req.responseText);
		}
	}
	this.setText = function setText(text)
	{
		if (typeof(this.div) == 'undefined' || !this.div)
		{
			this.div = document.createElement('DIV');
			with (this.div.style)
			{
				position = 'absolute';
				border = '1px solid black';
				backgroundColor = '#F4F8ED';
				padding = '10px';
				fontSize = '80%';
				zIndex = 10000;
				width = '350px';
				height = 'auto';
				fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
			}
			addEvent(this.div, 'mouseover', this.mouseover);
			addEvent(this.div, 'mouseout', this.mouseout);
			if ((navigator.appVersion.indexOf('MSIE') > 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
				if (typeof(this.ifr) == 'undefined' || !this.ifr)
				{
					this.ifr = document.createElement('IFRAME');
					this.ifr.src = 'javascript:false';
					with (this.ifr.style)
					{
						opacity = 0;
						filter = 'alpha(opacity=0)';
						position = 'absolute';
						border = '1px solid black';
						backgroundColor = '#F4F8ED';
						display = 'block';
						zIndex = -1;
					}
				}
			}
			document.body.appendChild(this.div);
		}
		this.div.innerHTML = text;
		this.div.innerHTML += '<b><a href="' + this.curHref + '" title="читать далее">читать далее</b>';
		with (this.div.style)
		{
			height = 'auto';
			width = '350px';
			visibility = 'hidden';
			display = 'block';
		}
		this.timeout = setTimeout(this.setPosition, 10);
	}
	this.setPosition = function setPosition()
	{
		var _l, _t;
		with (_this.div.style)
		{
			var p = getAbsolutePosition(_this.cur);
			left = (_l = (p.x + _this.div.offsetWidth + _this.cur.offsetWidth / 2 + document.body.scrollLeft) * 1.1 > document.body.offsetWidth ? (p.x - _this.div.offsetWidth + _this.cur.offsetWidth / 2) : (p.x + _this.cur.offsetWidth / 2)) + 'px';
			top = (_t = (p.y - document.body.scrollTop) > _this.div.offsetHeight ? (p.y - _this.div.offsetHeight) : (p.y + _this.cur.offsetHeight)) + 'px';
			visibility = 'visible';
		}
		if (_this.ifr)
		{
			_this.div.appendChild(_this.ifr);
			with (_this.ifr.style)
			{
				top = left = '-2px';
				width = _this.div.offsetWidth + 2 + 'px';
				height = _this.div.offsetHeight + 2 + 'px';
				display = 'block';
			}
		}
	}
}
