//AJAX search filter
//Ben Pritchard 2008
var searchWSURI = "/asmx/searchStream.asmx/getSearchResults?SearchPhrase=";
var searchPageURI = "/search.aspx?search=";
var conciergeSearchInput;
var searchDefaultValue = "";

if(document.getElementById("searchInput")){
	conciergeSearchInput = document.getElementById("searchInput");
	conciergeSearchInput.value = searchDefaultValue;	
}
//Get URL's
function getURL(title, id, template, cat)
{
var URL;

if (template == 0)
    URL = '/projectGH.aspx?id=' + id + '&cat=' + cat
else if (template == -1)
    URL = '/projectTH.aspx?id=' + id + '&cat=' + cat
else if (template == -2)
    URL = '/NewsItem.aspx?id=' + id
else 
    URL = '/content/'+title.replace(/&/g,"").replace(/ /g,"") + '_' + id + '.html'
return URL
}
    
//Tie in search page functionality
function doSearch()
{
    document.location.href = "/search.aspx?search=" + conciergeSearchInput.value    
}

function setPromptStyle(){
	conciergeSearchInput.setAttribute("class", "concPrompt");
	conciergeSearchInput.setAttribute("className", "concPrompt");	
}

function setActiveStyle(){
	conciergeSearchInput.setAttribute("class", "concActive");
	conciergeSearchInput.setAttribute("className", "concActive");	
}

var conciergeBox = document.getElementById("conciergeBox");
var conciergeResults = document.getElementById("conciergeResults");
var hideInt = null;

 
function getConciergeResults(key){
    if (key==13)
    {
        doSearch();
    }
    else
    {
        if (conciergeSearchInput.value.length >= 3) {           
       		search(conciergeSearchInput.value)
       	}else{
	        clearConciergeInt();
        conciergeBox.style.visibility = "hidden";
        }	
    }
}

function displayResults() {    
	conciergeBox.style.left = findPos(conciergeSearchInput)[0] - 300 + "px";
	conciergeBox.style.top = findPos(conciergeSearchInput)[1] - 100 + "px";
	conciergeBox.style.visibility = "visible";
	clearConciergeInt();
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

var archArr = new Array();
var enginArr = new Array();
var techArr = new Array();
var consArr = new Array();
var otherArr = new Array();

function hideResults(){
	if(hideInt == null){
		hideInt = setInterval(hide,500);
	}
}

function focusField(){
	if(conciergeSearchInput.value == searchDefaultValue){
		conciergeSearchInput.value = "";
	}
	setActiveStyle();
	
	clearConciergeInt();
}

function clearConciergeInt(){
	clearInterval(hideInt);
	hideInt = null;
}

function hide(){
	clearConciergeInt();
	conciergeBox.style.visibility = "hidden";
	
	conciergeSearchInput.value = searchDefaultValue;
	setPromptStyle();
	conciergeSearchInput.blur();
}

function search(searchTerm){
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
	{
	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
  catch (e)
	{
	try
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	catch (e)
	  {
	  alert("Your browser does not support AJAX!");
	  return false;
	  }
	}
  }
  xmlHttp.onreadystatechange=function()
	{
	if(xmlHttp.readyState==4)
	  {
		  try{
			  archArr = [];
			  enginArr = [];
		  	  techArr = [];
			  otherArr = [];

			  for(i=0;i<xmlHttp.responseXML.getElementsByTagName("result").length;i++){
				var targetArray;
				switch(xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("category").nodeValue.toLowerCase()){
					case "architecture":
						targetArray = archArr;
						break;
					case "engineering":
						targetArray = enginArr;
						break;
		            case "communications technology":
						targetArray = techArr;
						break;
					default:
						targetArray = otherArr;
						break;
				}
				targetArray.push(
				    {
				        title:xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("title").nodeValue,
				        url:getURL(xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("title").nodeValue ,xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("id").nodeValue,xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("template").nodeValue,xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("category").nodeValue), 
				        descrip:xmlHttp.responseXML.getElementsByTagName("result")[i].getAttributeNode("description").nodeValue
				    });
			  }
			  var resStr = "";
		conciergeResults.innerHTML = resStr;
		if(archArr.length > 0){
		resStr += '<div class="concBlueHeader">Architecture</div>';
		for(a=0;a<archArr.length;a++){
			resStr += '<div class="concResultGroup"><div class="concPageTitle"><a href="'+archArr[a].url+'">'+archArr[a].title +'</a></div><div>'+ archArr[a].descrip +'</div></div><div class="conciergeDivider"></div>';
		}
		}
		
		if(enginArr.length > 0){
		resStr += '<div class="concGreenHeader">Engineering</div>';
		for(a=0;a<enginArr.length;a++){
			resStr += '<div class="concResultGroup"><div class="concPageTitle"><a href="'+enginArr[a].url+'">'+enginArr[a].title +'</a></div><div>'+ enginArr[a].descrip +'</div></div><div class="conciergeDivider"></div>';
		}
		}
		
		if(techArr.length > 0){
		resStr += '<div class="concBlueHeader">Communications Technology</div>';
		for(a=0;a<techArr.length;a++){
			resStr += '<div class="concResultGroup"><div class="concPageTitle"><a href="'+techArr[a].url+'">'+techArr[a].title +'</a></div><div>'+ techArr[a].descrip +'</div></div><div class="conciergeDivider"></div>';
		}
		}				
		
		if(otherArr.length > 0){
		resStr += '<div class="concBlueHeader">Other</div>';
		for(a=0;a<otherArr.length;a++){
			resStr += '<div class="concResultGroup"><div class="concPageTitle"><a href="'+otherArr[a].url+'">'+otherArr[a].title +'</a></div><div>'+ otherArr[a].descrip +'</div></div><div class="conciergeDivider"></div>';
		}
		}
		
		if(resStr.length > 0){
		resStr += '<div class="concShowAllHeader" onclick="document.location.href = \'' + searchPageURI + searchTerm + '\'">See All Search Results <img src="/images/resultsArrow.gif" border="0" /></div>'	
	    conciergeResults.innerHTML = resStr;
	    displayResults();
		}
		}catch(e){
			//alert("Search could not be completed"); 
		  }
	  }
	}
  xmlHttp.open("GET",searchWSURI+searchTerm,true);
  xmlHttp.send(null);
  }