var selectionIndex = 0;
var matches = new Array();
var selectedCellID;
var suggestionTimer;

var selectedURL = "";

var maxSuggestions = 10;
var MAX_SUGGESTIONS = 10;
var maxSuggestionsForMore = 100;
var lastData = '';
var moreClicked = false;
var qSearchReady = false;
var qLoopCount = 0;

/*
function log ( code )
{
	div = document.getElementById ( "log" );
	div.innerHTML += "Event code " + code + "<br />\n";
}
*/

function suggest(e, txtSearch)
{
	var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	//log ( code );
	
	if ( suggestionTimer != null )
		window.clearTimeout ( suggestionTimer );
		
	if ( code >= 48 && code <= 90 )
		suggestionTimer = window.setTimeout("suggestGenerator(" + code + ", '" + txtSearch.id + "')", 100);
	else
		suggestGenerator(code, txtSearch.id);
}

function hideSuggestions()
{
	var div = document.getElementById("divAnswerSuggestions");

	selectionIndex = 0;
	matches = new Array();
	div.style.display = "none";
	toggleDropdowns(div);
}

function suggestGenerator(code, txtSearchID)
{	
	var txtSearch =  document.getElementById(txtSearchID)
	var div = document.getElementById("divAnswerSuggestions");
	var data = txtSearch.value;
	
	if((data == '' || lastData != data) && !moreClicked)
	{
		maxSuggestions = MAX_SUGGESTIONS;
	}
	lastData = data;
	
	div.style.display = "";
	var match = false;		
	var i, s;
	
	if ( code == 13 )
	{
		//alert ( GetBase() + '' + txtSearch.name );
		window.location = selectedURL; //GetBase() +
	}
	
	if ( code != 38 && code != 40 ) //not up or down
	{
		matches = new Array();
		matchcounts = new Array();
		matchurls = new Array();
		selectionIndex = 0;

		for ( i = 0; i < suggestions.length && (matches.length <= maxSuggestionsForMore || matches.length <= maxSuggestions); i++ )
		{
			s = suggestions[i];
			c = counts[i];
			u = answerurls[i];
		
			if ( data == "" || s.toUpperCase().indexOf ( data.toUpperCase() ) == 0 )
			{
				if(!match)
					match = true;
				
				matches.push(s);
				matchcounts.push(c);
				matchurls.push(u);
			}
		}
	}
		
	if(matches.length > 0)
	{
		if(code == 38 || code == 40) //38-up 40-down
		{
			if(code == 38) //up
			{
				selectionIndex --;
				if(selectionIndex < 0)
					selectionIndex = matches.length - 1;
			}
			else //down
			{
				selectionIndex ++;
				if(selectionIndex >= matches.length)
					selectionIndex = 0;
			}
			
			selectedURL = matchurls[selectionIndex];
		}
		else
		{
			selectedURL = matchurls[0];
		}
		
		var cellID;
		var innerHTML = "<table cellSpacing=\"0\" cellPadding=\"3\" border=\"0\">";
		for (i = 0; i < matches.length && i < maxSuggestions; i++)
		{
			s = matches[i];
			c = matchcounts[i];
			u = matchurls[i].replace(/\'/g, '\\\'');
			
			cellID = "td" + i;
			innerHTML += "<tr><td style=\"color:#3366AC; cursor: pointer;\" class=\"smtxt\" id=\"" + cellID + "\" onmouseover=\"highlightmatch(this); selectedURL = '" + u + "';\" onmouseout=\"unhighlightmatch(this);\" onmousedown=\"selectedURL = '" + u + "'; selectmatch(this);\"";

			if(i == selectionIndex)
			{
				innerHTML += " bgColor=#dfdbce";
				selectedCellID = cellID;
				if(code == 38 || code == 40)
				{
					txtSearch.value = s;
				}
			}
			
			innerHTML += ">" + s + " <span style='color: gray;'>(" + c + ")</span></td></tr>";
		}
		
		if(matches.length >= maxSuggestions && (data != '' || matches.length <= maxSuggestionsForMore))
		{
			innerHTML += "<tr><td style='color: #3366AC; cursor: pointer;' class=\"smtxt\" onmousedown=\"maxSuggestions = 9999; moreClicked = true; firstUpdate();\" onmouseover=\"this.bgColor = '#cccccc';\" onmouseout=\"this.bgColor = 'white';\" >More...</td></tr>";
		}
		
		innerHTML += "</table>";
		div.innerHTML = innerHTML;
	}
	else
	{
		hideSuggestions();
		txtSearch.value = txtSearch.value.toLowerCase();
	}
	
	toggleDropdowns(div);
	
	moreClicked = false;
}

function highlightmatch(td)
{
	var selectedTD = document.getElementById(selectedCellID);
	selectedTD.bgColor = "white";
	td.bgColor = "#cccccc";
	//td.style.color = "#cf142b";
}

function unhighlightmatch(td)
{
	td.bgColor = "white";
	//td.style.color = "#000000";
}

function selectmatch(td)
{
	//alert ( GetBase() + '' + td.name );
	window.location = selectedURL; //GetBase() + 
}

function toggleDropdowns(div)
{	
	if ( navigator.appName == "Microsoft Internet Explorer" )
	{
		var dds = document.body.getElementsByTagName("SELECT");
		var divX1 = findX(div);
		var divX2 = divX1 + div.clientWidth;
		var divY1 = findY(div);
		var divY2 = divY1 + div.clientHeight;
		
		/*
			alert(divX1);
			alert(divX2);
			alert(divY1);
			alert(divY2);
		*/
			
		var dd;
		var ddX1;
		var ddX2;
		var ddY1;
		var ddY2;
		
		for (var i = 0; i < dds.length; i++)
		{
			dd = dds[i];

			var ddX1 = findX(dd);
			var ddX2 = ddX1 + dd.clientWidth;
			var ddY1 = findY(dd);
			var ddY2 = ddY1 + dd.clientHeight;
	/*
			alert(ddX1);
			alert(ddX2);
			alert(ddY1);
			alert(ddY2);
	*/
			dd.style.visibility = "visible";

			if ( div.style.display == "" && (((ddX1 > divX1 && ddX1 < divX2) || (ddX2 > divX1 && ddX2 < divX2) || (ddX1 <= divX1 && ddX2 >= divX2)) && ((ddY1 > divY1 && ddY1 < divY2) || (ddY2 > divY1 && ddY2 < divY2) || (ddY1 <= divY1 && ddY2 >= divY2))))
				dd.style.visibility = "hidden";
		}
	}
}

function findX(obj)
{
	var curleft = 0;
	if ( obj.offsetParent )
	{
		while ( obj.offsetParent )
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
	{
		curleft += obj.x;
	}
		
	return curleft;
}

function findY(obj)
{
	var curtop = 0;
	if ( obj.offsetParent )
	{
		while ( obj.offsetParent )
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if ( obj.y )
	{
		curtop += obj.y;
	}
	
	return curtop;
}

function firstUpdate()
{
	++qLoopCount;
	
	if(qSearchReady)
		suggestGenerator(50, 'txtQuestionSearch');
	else
		setTimeout(firstUpdate, 200);
}

setTimeout(firstUpdate, 500);
