function isSpace(str) {
	for (i = 0; i < str.length; i++) if (str[i] != " ") return false;
	return true;
}

function inputHandler(inp, default_val) {
	var default_val = (default_val == undefined) ? 'Поиск по сайту' : default_val;
	inp.onfocus = function e() { if (this.value == default_val) this.value = ""; }
	inp.onblur = function e() { if (this.value == "" || isSpace(this.value)) this.value = default_val; }
}


function trim(string) {
	return string.replace(/^(\s*)(.*)(\S)(\s*)$/, "$2$3");
}

function nextItem(item, nodeName) {
	if (!item) return
	var next = item.nextSibling
	while (next) {
		if (next.nodeName == nodeName) return next
		next = next.nextSibling
	}
	return null
}

function firstChild(prnt, nodeName) {
	if (!prnt) return
	var next = prnt.firstChild
	while (next) {
		if (next.nodeName == nodeName) return next
		next = next.nextSibling
	}
	return null
}

function addEvent(o, e, h) { (o.attachEvent) ? o.attachEvent("on" + e, h) : o.addEventListener(e, h, false); }

function _select() { this.elm_arr = []; }
_select.prototype = {
	openElm : -1,
	init : function(elm, arr, act) {
		if (arr.length < 2) { elm.onclick = function e() { return false; }; elm.className = "no-active"; return; }
		var current_elm = _select.elm_arr.length;
		_select.elm_arr[current_elm] = {"elm" : elm, "vis" : true};
		this.createList(current_elm, arr, act);
		elm.onclick = function() { _select.invertDisplayUL(current_elm); return false; }
		elm.parentNode.getElementsByTagName("ul")[0].onclick = function e() { _select.invertDisplayUL(current_elm); return true; }
		document.onclick = function e() { _select.checkShow(); }
	},
	
	createList : function(eln, arr, act) {
		var ul = document.createElement("ul");
		ul.style.display = "none";
		for (i = 0; i < arr.length; i++) {
			li = document.createElement("li");
			if (arr[i][0] == act) {
				var spn = document.createElement("span");
				spn.appendChild(document.createTextNode(arr[i][0]));
				li.appendChild(spn);
				ul.style.top = (-i * 19 - 10) + "px";
			}
			else {
				var a = document.createElement("a");
				a.appendChild(document.createTextNode(arr[i][0]));
				a.href = arr[i][1];
				li.appendChild(a);
			}
			ul.appendChild(li);
		}
		_select.elm_arr[eln].elm.parentNode.appendChild(ul);
	},
	
	invertDisplayUL : function(eln) {
		ul = _select.elm_arr[eln].elm.parentNode.getElementsByTagName("ul")[0];
		ul.style.display = (ul.style.display == "none") ? "block" : "none";
		if (this.openElm >= 0 && this.openElm != eln) {
			_select.elm_arr[this.openElm].elm.parentNode.getElementsByTagName("ul")[0].style.display = "none";
			_select.elm_arr[this.openElm].vis = 3;
		}
		_select.elm_arr[eln].vis = 1;
		this.openElm = eln;
		_select.elm_arr[eln].elm.blur();
	},
	
	checkShow : function() {
		if (this.openElm < 0) return;
		switch (_select.elm_arr[this.openElm].vis) {
			case 1:
			_select.elm_arr[this.openElm].vis = 2;
			break;
		case 2:
			_select.elm_arr[this.openElm].vis = 3;
			_select.elm_arr[this.openElm].elm.parentNode.getElementsByTagName("ul")[0].style.display = "none";
			break;
		}
	}
}

var _select = new _select();