function setDef(t){
//	t.value=''; 
	//t.focused = true; - Not properly worked.
	t.focus();// Jan-18 Changes to 139 bug

}

function searchField(id, languages){
	var sfild = this;
	var xmlHttp;
	this.curIndex = -1;
	pre_ur = "../..";
	pre_ur = "";
	
	this.input = document.getElementById(id);
	if (this.input) {
		this.query = '';
		this.focused = false;
		this.input.onkeyup = function(e) {
			e = e || window.event;
			if (e.keyCode == 13){
				if(sfild.curIndex == -1){
					sfild.search(this.value);
				}
				else{
					var suggestionNodes = sfild.autocomplete.getElementsByTagName('a');
					document.location.href = suggestionNodes[sfild.curIndex].href;
				}
				
			}
			else if(e.keyCode == 38){
				sfild.prevSuggestion();
			}
			else if(e.keyCode == 40){
				sfild.nextSuggestion();
			}
			else {
				sfild.curIndex = -1;
				sfild.searchTag(this.value);
				
			}
		}

		this.input.onclick = function()
		{
			sfild.curIndex = -1;
			if(this.value == search_default_text)
				this.value = '';
			sfild.setFocused(true);
			if (this.value != '')
			{
				sfild.searchTag(this.value);
			}
		}
		
		this.input.onblur = function() {
			if(logged){
				$('#allsearch').animate({ 
				    width: "280px"
				  }, 400 );
			}
			if (!sfild.isFocused()) {
				sfild.setTags();
			}
			if(this.value=='')
				this.value=search_default_text;
			
		}
		
		this.input.onfocus = function() {
			if($('#text_s_msgs').css('display') != "none")
				$('#text_s_msgs').hide();
			if(logged){
				$('#allsearch').animate({ 
				    width: "320px"
				  }, 400 );
			}
			if(this.value == search_default_text)
				this.value = '';
			
		}
		
		
		this.hidden = document.createElement('INPUT');
		this.hidden.type = 'hidden';
		this.hidden.setAttribute('id','parent_id')
		this.input.parentNode.insertBefore(this.hidden, this.input);
		this.autocomplete = document.createElement('DIV');
		this.autocomplete.className = 'searchautocomplete';
		this.autocomplete.style.display = 'none';
		this.autocomplete.style.zIndex = 10;
		this.autocomplete.style.top = "27px";
		this.autocomplete.onmouseover = function() {
			sfild.setFocused(true);
		}
		this.autocomplete.onmouseout = function() {
			sfild.setFocused(false);
		}

		this.ttagsList = document.createElement('SPAN');
		this.ttagsList.innerHTML = tagsQSearch;
		this.ttagsList.className = 'satitle';

		this.tartList = document.createElement('SPAN');
		this.tartList.innerHTML = articleQSearch;
		this.tartList.className = 'satitle';

		this.tagsList = document.createElement('UL');
		this.tagsList.className = 'articles';
		
		this.artList = document.createElement('UL');
		this.artList.className = 'articles';
		
		this.autocomplete.appendChild(this.ttagsList);
		this.autocomplete.appendChild(this.tagsList);
		this.autocomplete.appendChild(this.tartList);		
		this.autocomplete.appendChild(this.artList);
		this.input.parentNode.appendChild(this.autocomplete);
	}

	this.nextSuggestion = function() {
	    var suggestionNodes = this.autocomplete.getElementsByTagName('a');
	    if(this.curIndex >= suggestionNodes.length-1){
        	this.curIndex = -1;
        }
	    if (suggestionNodes.length > 0 && this.curIndex < suggestionNodes.length-1) {
	        var node = suggestionNodes[++this.curIndex];
	        this.highlightSuggestion(node);
	    }
	}
	
	this.prevSuggestion = function() {
		var suggestionNodes = this.autocomplete.getElementsByTagName('a');
		if(this.curIndex <= 0){
        	this.curIndex = suggestionNodes.length;
        }
	    if (suggestionNodes.length > 0 && this.curIndex > 0) {
	        var node = suggestionNodes[--this.curIndex];
	        this.highlightSuggestion(node);
	    }
	}
	
	this.highlightSuggestion = function (sNode) {
		var suggestionNodes = this.autocomplete.getElementsByTagName('a');
	    for (var i=0; i < suggestionNodes.length; i++) {
	        var node = suggestionNodes[i];
	        if (node == sNode) {
	        	node.style.backgroundColor='#E2E2E2';
	        	this.curIndex = i;
	        }
	        else { 
	        	node.style.backgroundColor='white';
	        }
	    }
	}
	
	this.isFocused = function() {
		return this.focused;
	}

	this.searchTag = function(query) {
		if (query != '') {
			this.sendRequest(query, 0);
		} else {
			this.sendRequest('', 0);
			this.setTags();
		}
		this.hidden.value = '';
		this.query = query;
	}
	
	this.search = function(query)
	{
		url = pre_ur+'/search.php?keywords='+ query +'&terms=all&sd=d&t=0&submit=Пошук';
		document.location.href = url;
	}
	
	this.setFocused = function(focused) {
		this.focused = focused;
	}

	this.setTag = function(pid) {
		this.hidden.value = '';
		this.setTags();		
	}
	
	this.setTags = function(tagData) {
		this.tagsList.innerHTML = '';
		this.artList.innerHTML = '';

		var regexp;
		this.ttagsList.style.display = 'none';
		this.tartList.style.display = 'none';

		this.artList.style.display = 'none';
		this.tagsList.style.display = 'none';
		if (tagData && (tagData.length > 0) && this.query != '') {
			
			for (var i = 0; i < tagData.length; i++) {
				var item = document.createElement('LI');
				var anchor = document.createElement('A');
				if (this.query != '') {
					regexp = new RegExp('(' + this.query.escape() + ')', 'i');
				}	
				
				var ur = pre_ur+'/' + tagData[i].url;
				anchor.href = ur;

				var pos = tagData[i].name.search(regexp);
				
				if (pos){
					var toqur = tagData[i].name.substr(0,pos);
				}
				var qur = tagData[i].name.substr(pos,this.query.length);
				var afterqur = tagData[i].name.substr((pos+this.query.length));

				if (tagData[i].pid){
					var pPath = document.createTextNode(tagData[i].path);
					anchor.appendChild(pPath);
				}
				var sp = document.createElement('SPAN');
				
				sp.setAttribute('class','highlight');
				sp.setAttribute('className','highlight');
				sp.appendChild(document.createTextNode(qur));
				
				if (pos)
					anchor.appendChild(document.createTextNode(toqur));
				anchor.appendChild(sp);
				anchor.appendChild(document.createTextNode(afterqur));
				if(tagData[i].poster)
				{
					var autor = document.createElement('SPAN');
						autor.style.color = '#999';
						autor.appendChild(document.createTextNode(' ('+ tagData[i].poster +')'));
					anchor.appendChild(autor);
				}
				
				$(anchor).mouseover(function(){sfild.highlightSuggestion(this)}); 	

				item.appendChild(anchor);
				if (tagData[i].pid){
					this.ttagsList.style.display = 'block';
					this.tagsList.style.display = 'block';
					this.tagsList.appendChild(item);
				}
				else{
					this.tartList.style.display = 'block';
					this.artList.style.display = 'block';
					this.artList.appendChild(item);
				}	
			}
				
			this.autocomplete.style.display = 'block';
			
			
		}
		else{
			this.autocomplete.style.display = 'none';
		}
	}


	this.clear = function() {
		this.query = '';
		this.input.value = '';
		this.hidden.value = '';
		this.languages = '';
		this.setState(false);
	}

	this.sendRequest = function(query, root) {
		if (typeof(xmlHttp) == 'undefined') {
			xmlHttp = new XMLHttpRequest();
		}
		if (xmlHttp) {
			var url = '/modules/taxonomy/ajax.php?search=' + encodeURI(query);
			if (root)
			{
				url += 'root=' + root;
			}
			xmlHttp.open('GET', url, true);
			xmlHttp.onreadystatechange = function() {
				sfild.recvResponse();
			}
			xmlHttp.send(null);
		}
	}

	this.recvResponse = function() {
		try {
			if (xmlHttp.readyState == 4) {
				var tagData = new Array();
				if (xmlHttp.status == 200) {
					var xml = xmlHttp.responseXML;

					if ((xml.firstChild.nodeName == 'search') && (xml.firstChild.childNodes.length > 0)) {
						var ts = xml.firstChild.childNodes[0];
						var art = xml.firstChild.childNodes[1];
						if (ts.nodeName == 'tags') {
							for (var i = 0; i < ts.childNodes.length; i++) {
								if (ts.childNodes[i].nodeName == 'tag'){
									var sfild = new Object();
									sfild.id = ts.childNodes[i].getAttribute('id');
									sfild.approved = ts.childNodes[i].getAttribute('approved');
									sfild.pid = ts.childNodes[i].getAttribute('pid');
									sfild.path = ts.childNodes[i].getAttribute('path');
									sfild.url = ts.childNodes[i].getAttribute('tag_url');
									sfild.name = ts.childNodes[i].firstChild.nodeValue;
									tagData.push(sfild);
								}

							}

						}
						if (art.nodeName == 'articles') {
							for (var i = 0; i < art.childNodes.length; i++) {
								if (art.childNodes[i].nodeName == 'article'){
									var sfild = new Object();
									sfild.id = art.childNodes[i].getAttribute('id');
									sfild.approved = art.childNodes[i].getAttribute('approved');
									sfild.poster = art.childNodes[i].getAttribute('poster_name');
									sfild.url = art.childNodes[i].getAttribute('topic_url');
									sfild.name = art.childNodes[i].firstChild.nodeValue;
									tagData.push(sfild);
								}
							}
						}
					}
				}
				this.setTags(tagData);
			}
		} catch(e) {}
	}
}

