var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
var pulldownon = false;
var liveSearchInput ='livesearch';
var limitStartChars = 1;
function liveSearchInit()
{
	if(navigator.userAgent.indexOf("Safari") > 0)
		document.getElementById('livesearch').addEventListener("keydown", liveSearchKeyPress, false);
	else if(navigator.product == "Gecko")
	{
		document.getElementById('livesearch').addEventListener("keypress", liveSearchKeyPress, false);
		document.getElementById('livesearch').addEventListener("blur", liveSearchHideDelayed, false);
	}
	else
	{
		document.getElementById('livesearch').attachEvent('onkeydown', liveSearchKeyPress);
		isIE = true;
	}
	document.getElementById('livesearch').setAttribute("autocomplete", "off");
}
function liveSearchHideDelayed()
{
	window.setTimeout("liveSearchHide()", 400);
}
function liveSearchHide()
{
	if(pulldownon == false)
	{
		document.getElementById("LSResult").style.display = "none";
		var highlight = document.getElementById("LSHighlight");
		if(highlight)
			highlight.removeAttribute("id");
	}
}
function togglePulldown(pulldown)
{
	if(pulldownon == false)
	{
		pulldownon = true;
		liveSearchProcessReqChange(pulldown);
	}
	else
	{
		pulldownon = false;
		liveSearchHide();
	}
}
function liveSearchKeyPress(event)
{
	//KEY DOWN	
	if(event.keyCode == 40)
	{
		highlight = document.getElementById("LSHighlight");
		if(!highlight) highlight = document.getElementById("LSResult").firstChild.firstChild;
		else
		{
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if(highlight) highlight.setAttribute("id", "LSHighlight");
		if(!isIE) event.preventDefault();
	} 
	//KEY UP
	else if(event.keyCode == 38) 
	{
		highlight = document.getElementById("LSHighlight");
		if(!highlight) highlight = document.getElementById("LSResult").firstChild.lastChild;
		else
		{
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if(highlight) highlight.setAttribute("id", "LSHighlight");
		//if(!isIE) event.preventDefault();
	} 
	//ESC
	else if(event.keyCode == 27)
	{
		highlight = document.getElementById("LSHighlight");
		if(highlight) highlight.removeAttribute("id");
		document.getElementById("LSResult").style.display = "none";
	} 
	//BACKSPACE - required for IE
	else if(event.keyCode == 8 && isIE) liveSearchStart();
}
function liveSearchStart()
{
	if(t) window.clearTimeout(t);
	if ($F(liveSearchInput).length >= limitStartChars) {
		t = window.setTimeout("liveSearchDoSearch()",300);
	}
}



function liveSearchDoSearch()
{
	if(typeof liveSearchRoot == "undefined") liveSearchRoot = "";
	if(typeof liveSearchRootSubDir == "undefined") liveSearchRootSubDir = "";
	if(typeof liveSearchParams == "undefined") liveSearchParams2 = "";
	else liveSearchParams2 = "&" + liveSearchParams;
	if(liveSearchLast != $F(liveSearchInput))
	{
		if($F(liveSearchInput) == "")
		{
			liveSearchHide();
			liveSearchLast = "";
			return false;
		}
		liveSearchLast = $F(liveSearchInput);
		
		if (liveSearchLast.length < limitStartChars) {
			return false;
		}
		pulldownon = false;
		
		new Ajax.Request('/groups/ajax_search/'+liveSearchLast, {
		  method: 'get',
		  onLoading: function() {
			loading();
		  },
		  onSuccess: function(transport) {
			live(transport.responseText);
		  }
		});
	}
}

function loading() {
	var  res = document.getElementById("LSResult");
	res.style.display = "block";
	var html = '<ul class=\'LSRes\'>';
	html += '<li loading="1" class="LSRow1"><img src="/img/ajax_indicator.gif" /><span> Загрузка...</span></li>';
	html += '</ul>';
	res.innerHTML = html;
}

function live(jData) 
{	
		jData = jData.evalJSON();
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		liveSearchLast = $F(liveSearchInput);
		var inhtml = '<ul class=\'LSRes\'>';
		var title ='';
		jData.each(
			function(e){
				liveSearchLast = trim(liveSearchLast);
				title = e.Group.title.toLowerCase().replace(liveSearchLast,'<b>'+liveSearchLast+'</b>');
				inhtml += '<li class="LSRow"><a href="/groups/view/'+escape(e.Group.url)+'">'+title+'</a></li>';
			}
		);	
		inhtml += '</ul>';
		res.innerHTML = inhtml;			
}

function uppercaseIt(val) {
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        return newVal;
}

var movie_id;
function liveSearchSubmit(el)
{
	var highlight = document.getElementById("LSHighlight");
	if(highlight && highlight.firstChild && (highlight.getAttribute('loading') == undefined))
	{
		$(liveSearchInput).value = highlight.down().innerHTML.gsub(/<\/?b>/,'').unescapeHTML(); 
		return false;	
	}
	return false;
}


function liveSearchHover(el)
{
	highlight = document.getElementById("LSHighlight");
	if(highlight) highlight.removeAttribute("id");
	el.setAttribute("id","LSHighlight");
}
function liveSearchClicked(el)
{
	highlight = document.getElementById("LSHighlight");
	if(highlight) highlight.removeAttribute("id");
	//$("livesearch").value = highlight.firstChild.innerHTML;
	el.setAttribute("id","LSHighlight");
	return liveSearchSubmit(el);
}
liveSearchInit();

function ajax_load_img(){
	return  '<img src="'+STATIC_DOMAIN+'/img/ajax_indicator.gif">';
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}