// Product: Synthetix Autosuggest V1.9
// Copyright: This source code is Copyright (c) 2010, 2011, Synthetix Ltd.
// Author: Peter McKean

/*****************************************************************/
/* The variables in this section are configurable */
/*****************************************************************/

// Width of in-page Autosuggest dropdown list
var asWidth = 200;

// Width of faqtAgent popup
var fa_width = 886;

// Height of faqtAgent popup
var fa_height = 668;

// URL of faqtAgent
var fa_url = "http://www.synthetix-ec2.com/clients/chelsea/index.html";

// If your faqtAgent contains different brand information in one knowledge-base, supply the "view" name below
var fa_view = "GTTR";

// Synthetix Agent ID
var agent_id ="FAQTAGENT-CHELSEA-LIVE";

/*****************************************************************/
/* Do not edit below this line */
/*****************************************************************/

var server_answers="79.125.12.236/answers/"+agent_id;
var lockTimer;
var lock_count=0;
var result_count=0;
var http_search_lock=false;
var http_search_queue=new Array();
var allow_autosuggest_hide=true;

function rand(x,y)
{
	return Math.floor(Math.random()*(y-x+1))+x;
}

function isIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function lock_increment()
{
	clearTimeout(lockTimer);
	if(lock_count<10)
	{
		lock_count++;
		lockTimer=setTimeout("lock_increment()",600);
	}
	else
		http_search_lock=false;
}

function http_search_lock_true()
{
	http_search_lock=true;
	lock_count=0;
	lock_increment();
}

function http_search_lock_false()
{
	clearTimeout(lockTimer);
	http_search_lock=false
}

function hide_autosuggest()
{
	as_on=false;
	document.getElementById(answerSuggestContainer).style.visibility="hidden";
}

function show_autosuggest()
{
	as_on=true;
	document.getElementById(answerSuggestContainer).style.visibility="visible";
}

var inputField = "";

function autosuggest(e)
{
	if (answerSuggestContainer == "fa_autosuggest_primary") {
		inputField = "searchtextPrimary";
	}
	else if (answerSuggestContainer == "fa_autosuggest_secondary") {
		inputField = "searchtextSecondary";
	}
	
	if(document.getElementById)
	{
		var obj = document.getElementById(inputField);
		if(obj.setAttribute) obj.setAttribute('autocomplete','off');
	}
	
	if(isIE())
		document.getElementById(answerSuggestContainer).style.width=asWidth+2
	else
		document.getElementById(answerSuggestContainer).style.width=asWidth;
	
	var unicode=e.keyCode? e.keyCode : e.charCode;
	
	if(unicode!=13)
	{
		keystring=document.getElementById(inputField).value;
		if(keystring!="")
		{
			if(result_count>0)
			{
				document.getElementById(answerSuggestContainer).scrollTop = 0;
				show_autosuggest();
			}
		}
		else
		{
			if(keystring=="")   server_search_results=new Array();
			hide_autosuggest();
		}
	
		if(keystring!="")
		{
			if(http_search_lock==false)
			{
				http_search_lock_true();
				http_search_queue=new Array();
				server_search_results=new Array();
				dataAction("http://"+server_answers+"/fa_autosuggest.php?query="+escape(keystring)+"&category=All&view="+fa_view,false);
			}
			else
				http_search_queue.push("http://"+server_answers+"/fa_autosuggest.php?query="+escape(keystring)+"&category=All&view="+fa_view);
		}
	}
}

function dataAction(durl,record_val)
{
	if(record_val==true) record(durl);
	var script 	= document.createElement('script');
	script.src 	= durl+"&rand="+rand(1,99999);
	script.type = 'text/javascript';
	script 		= document.getElementsByTagName('head')[0].appendChild(script);
}

function show_server_autosuggest_results()
{
	http_search_lock_false();
	if(server_search_results.length>0)
	{
		document.getElementById(answerSuggestContainer).scrollTop=0;
		show_autosuggest();
		aslist = "<table width=100% cellpadding=0 cellspacing=0 border=0>";
		for(x=0;x<server_search_results.length;x++)
		{
			search_resultsA=server_search_results[x].split("|");
			aslist+="<tr><td width=4></td><td valign=center class='as_padding' onClick='clickmode=\"search_result\";launch_faqtAgent(\""+search_resultsA[1]+"\");' onmouseover='this.className = \"as_padding_over\";' onmouseout='this.className = \"as_padding\";'>"+search_resultsA[0]+"</td><td width=4></td></tr>";
		}
		aslist+="</table>";
		document.getElementById(answerSuggestContainer).innerHTML=aslist;
	}
	
	if(http_search_queue.length>0)
	{
		http_search_lock_true();
		http_temp=http_search_queue[http_search_queue.length-1];
		http_search_queue=new Array();
		dataAction(http_temp,false);
	}
}

function launch_faqtAgent(val)
{
	window.open(fa_url+'?label='+val,'FaqtAgent','top=50, left=50, width='+fa_width+', height='+fa_height+', scrollbars=no, menubar=no, toolbar=no, resizable=no');
	hide_autosuggest();
	return true;
}

function query_faqtAgent(val)
{
	window.open(fa_url+'?search='+val,'FaqtAgent','top=50, left=50, width='+fa_width+', height='+fa_height+', scrollbars=no, menubar=no, toolbar=no, resizable=no');
	hide_autosuggest();
	return true;
}

function rss_faqtAgent(val)
{
	window.open(fa_url+'?question='+val,'FaqtAgent','top=50, left=50, width='+fa_width+', height='+fa_height+', scrollbars=no, menubar=no, toolbar=no, resizable=no');
	hide_autosuggest();
	return true;
}
