String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var HASH_CHUNKS = location.hash.toString();
var SEARCH_STRING = null;
var SEARCH_START = 0;
if (location.pathname == "/search") {
	if (HASH_CHUNKS.charAt(0) != '#') location = "/";
	SEARCH_STRING = HASH_CHUNKS.substring(1);
	var matches = SEARCH_STRING.match(/^start=(\d+),(.*)$/);
	if (matches == null) location = "/";
	SEARCH_START = matches[1];
	SEARCH_STRING = decodeURI(matches[2].trim());
	if (SEARCH_STRING == "") location = "/";
	HASH_CHUNKS = null;
} else if (HASH_CHUNKS.charAt(0) == '#')
	HASH_CHUNKS = HASH_CHUNKS.substring(1).split(",");
else
	HASH_CHUNKS = null;

$(function($) {
	//footerPartnersAlign();

	$("div.hdrSearch input").val(SEARCH_STRING == null ? "поиск по сайту" : SEARCH_STRING).attr("nonempty", SEARCH_STRING == null ? "" : 1).focus(function () {
		var me = $(this);
		if (me.attr("nonempty") != 1) me.val("");
	}).blur(function () {
		var me = $(this);
		if (me.val().trim() == "") me.val("поиск по сайту").attr("nonempty", "");
		else me.attr("nonempty", 1);
	}).keyup(function (e) {
		var me = $(this);
		if (e.keyCode == 13 && me.val().trim() != "") {
			me.attr("nonempty", 1);
			search_search();
		}
	});
	$("div.hdrSearch a").attr({ "href" : "#", "title" : "Искать!" }).click(search_search);
	if (SEARCH_STRING != null) search_execute(SEARCH_STRING, SEARCH_START);

	if (HASH_CHUNKS != null)
		for (var i = 0; i != HASH_CHUNKS.length; i++)
			if (HASH_CHUNKS[i].substring(0, 10) == "highlight=") {
				highlightTerms(HASH_CHUNKS[i].substring(10));
				break;
			}
});

function search_search() {
	var input = $("div.hdrSearch input");
	if (input.attr("nonempty") != 1) {
		input.focus();
		return false;
	}
	var token = input.val().trim();
	input.val(token);
	location = "/search#start=0," + token;
	if (location.pathname == "/search") search_execute(SEARCH_STRING = token, 0);
	return false;
}

function highlightTerms(terms) {
	terms = terms.trim().split(/[\s\n\r]+/);
	var neTerms = [];
	for (var i = 0; i != terms.length; i++)
		if (terms[i].length != 0) neTerms[neTerms.length] = terms[i].replace(/([\\\|\[\]\*\.\?\(\)])/g, "\\$1");
	terms = new RegExp(terms.join("|"), "gi");
	var firstMatch = null;
	var replacer = function (element) {
		var childNodes = element.childNodes;
		var len = childNodes.length;
		while (len--) {
			var node = childNodes[len];
			var name = node.nodeName.toLowerCase();
			if (name == "html" || name == "head" || name == "style" || name == "title" || name == "link" || name == "meta" || name == "script" || name == "object" || name == "embed" || name == "iframe") continue;
			if (node.nodeType === 1 && element.childNodes.length != 0)
				replacer(node);
			if (node.nodeType !== 3 || !terms.test(node.data)) continue;
			var pos = -1;
			var chunks = [];
			var data = node.data;
			var localFirstMatch = null;
			var match;
			var lastIndex = 0;
			terms.lastIndex = 0;
			while ((match = terms.exec(data)) != null) {
				if (match["index"] > lastIndex) chunks[chunks.length] = document.createTextNode(data.substring(lastIndex, match["index"]));
				var innerText = document.createTextNode(match[0]);
				var span = document.createElement("span");
				span.className = "highlight";
				span.appendChild(innerText);
				chunks[chunks.length] = span;
				if (localFirstMatch == null) localFirstMatch = span;
				lastIndex = terms.lastIndex;
			}
			firstMatch = localFirstMatch;
			if (lastIndex != 0 && lastIndex != data.length) chunks[chunks.length] = document.createTextNode(data.substring(lastIndex));
			for (var i = 0; i != chunks.length; i++)
				element.insertBefore(chunks[i], node);
			element.removeChild(node);
		}
	};
	replacer(document.body);
	if (firstMatch !== null) $.scrollTo(firstMatch);
}

function footerPartnersAlign() {
	var main = $("div#footerPartners");
	var width = 0;
	main.children("div").each(function (i) {
		var elem = $(this);
		var pos = elem.position();
		pos = pos.left + elem.outerWidth();
		if (pos > width) width = pos;
	});
	main.css("padding-left", ((main.parent().width() - width) >> 1) + "px");
}
