var href = escape(document.location.href);
if ('undefined' == typeof(__hn_site)) {
	__hn_site = __hn_site_id;
}

__hn_hilights_words = [];
__hn_used_objects   = [];
if (document.getElementById('hypernews') || document.getElementById('hypercontext') || document.getElementById('hypersearch')) {
	document.write('<scr' + 'ipt charset="windows-1251" src="http://www.hypernews.ru/hypernews_words.js?site=' + __hn_site + '&url='+href+'"></scr' + 'ipt>');
}

function __hn_make_hrefs() {
	__hn_forbidden_tags = [ 'A', 'H1', 'TEXTAREA', 'SCRIPT', 'OPTION', 'IFRAME', 'OBJECT' ];
	if ('undefined' != typeof(__hn_stop_tags)) {
		__hn_forbidden_tags = __hn_forbidden_tags.concat(__hn_stop_tags);
	}

	if ('undefined' == typeof(__hn_max_hl_single)) {
		__hn_max_hl_single = 2;
	}

	if ('undefined' == typeof(__hn_max_hl_total)) {
		__hn_max_hl_total = 15;
	}

	if ('undefined' == typeof(__hn_target_blank)) {
		__hn_target_blank = 1;
	}

	if ('undefined' == typeof(__hn_href_class)) {
		__hn_href_class = '';
	}

	var div = document.getElementById('hypernews');
	if (!div) { div = document.getElementById('hypercontext'); }
	if (!div) { div = document.getElementById('hypersearch'); }
	if (!div) {
		return;
	}

	var found_words = __hn_walk(div);

	var density = __hn_max_hl_total / found_words.length;

	var hl_words = [];
	var counter = 1 - density;
	for (var i = 0; i < found_words.length; ++i) {
		if (Math.floor(counter + density) - Math.floor(counter) >= 1) {
			hl_words.push(found_words[i]);
		}
		counter += density;
	}

	for (var i = 0; i < hl_words.length; ++i) {
		var newnode = document.createElement('hypernews');

		var current_block = hl_words[i][3];
		var offset = 0;
		while (hl_words[i] && current_block == hl_words[i][3]) {
			var beforeText = current_block.nodeValue.substring(offset, hl_words[i][1]);

			var link = document.createElement("A");
			link.href = 'http://www.hypernews.ru/search/?from=' + __hn_site + '&id=' + hl_words[i][2];
			if (__hn_target_blank) {
				link.target = '_blank';
			}
			if (__hn_href_class.length) {
				link.className = __hn_href_class;
			}
			link.appendChild(document.createTextNode(hl_words[i][0]));

			newnode.appendChild(document.createTextNode(beforeText));
			newnode.appendChild(link);

			offset = hl_words[i][1] + hl_words[i][0].length;

			++i;
		}
		--i;

		var afterText = current_block.nodeValue.substring(offset, current_block.nodeValue.length);
		newnode.appendChild(document.createTextNode(afterText));

		if (found_words.length) {
			current_block.parentNode.replaceChild(newnode, current_block);
		}
	}
}

function __hn_walk(obj)
{
	found_words = [];
	for (var i = 0; i < obj.childNodes.length; i++) {
		if (__hn_in_array(obj.childNodes[i].tagName, __hn_forbidden_tags)) {
			continue;
		}

		if (3 == obj.childNodes[i].nodeType) {
			if (__hn_in_array(obj.childNodes[i], __hn_used_objects)) {
				continue;
			}
			__hn_used_objects.push(obj.childNodes[i]);
			found_words = found_words.concat(__hn_replace(obj.childNodes[i]));
			continue;
		}

		found_words = found_words.concat(__hn_walk(obj.childNodes[i]));
	}

	return found_words;
}

function __hn_cmp(a, b)
{
	if (a[1] > b[1]) {
		return 1;
	} else if (a[1] < b[1]) {
		return -1;
	}

	if (a[0].length > b[0].length) {
		return -1;
	}

	return 1;
}


function __hn_replace(obj)
{
	var found_words = [];
	for (var i = 0; i < __hn_words.length; ++i) {
		var search_word = __hn_words[i][0];
		var offset = 0;
		var found  = -1;
		while ((found = obj.nodeValue.indexOf(search_word, offset)) > -1) {
			offset = found + search_word.length;

			if (__hn_hilights_words[__hn_words[i][1]] >= __hn_max_hl_single) {
				break;
			}

			if (found > 0 && __hn_isAlpha(obj.nodeValue.substr(found - 1, 1))) {
				continue;
			}

			if ((found + search_word.length < obj.nodeValue.length) && __hn_isAlpha(obj.nodeValue.substr(found + search_word.length, 1))) {
				continue;
			}

			found_words.push([search_word, found, __hn_words[i][1], obj]);

			if (undefined == __hn_hilights_words[__hn_words[i][1]]) {
				__hn_hilights_words[__hn_words[i][1]] = 1;
			} else {
				++__hn_hilights_words[__hn_words[i][1]];
			}
		}
	}

	found_words = found_words.sort(__hn_cmp);
	var prev_word = ['', -1];
	var result = [];
	for (var i = 0; i < found_words.length; ++i) {
		if (prev_word[1] + prev_word[0].length > found_words[i][1]) {
			continue;
		}
		prev_word = found_words[i];
		result.push(found_words[i]);
	}

	return result;
}

function __hn_in_array(needle, haystack)
{
	for (var i = 0; i < haystack.length; ++i) {
		if (haystack[i] == needle) {
			return 1;
		}
	}

	return 0;
}

function __hn_isAlpha(c) {
	var letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ';
	var q = letters.indexOf(c);
	if (q == -1) {
		return 0;
	} else {
		return 1;
	}
}
