
/*				_javascript/livesearch.js 2.0
*/

/* TO DO

v1
get real results from mySQL
onsubmit have xhr return results from searchbox value in form of JSON 
process JSON requests using javascript and bring up matches popup



v2
soundex and fuzzy matching
soundex and fuzzy matching with popup
prepopulate question with popup
scroll suggestor ul
bold matching characters
convert ajax output to JSON


*/



function LiveSearch(oSearchbox,oSuggestor,fClickResponse){


	this.searchbox=oSearchbox;
	this.suggestor=oSuggestor;
	this.response=fClickResponse;

	this.keywords=new Array();
	this.values=new Array();


	this.activeSuggestion=null;
	this.resultsLoaded=false;
	

	this.initialize();
	
}


LiveSearch.prototype.initialize = function(){
	

	var oThis=this;

	this.searchbox.onkeyup = function(oEvent){
		
		// IE
		if(!oEvent){
			oEvent = window.event;
		}

		oThis.handleKeyUp(oEvent);

	}


	this.searchbox.onkeydown = function(oEvent){
		
		// IE
		if(!oEvent){
			oEvent = window.event;
		}
		oThis.handleKeyDown(oEvent);

		// do not submit on enter
		if(oEvent.keyCode==13){return false;}

	}

	// blur
	this.searchbox.onblur = function(oEvent){

		oThis.hideSuggestor();

	}


	// turn off autocompletes on forms
	var formElement=document.getElementsByTagName('form')[0];
	formElement.setAttribute('autocomplete','off');

}


LiveSearch.prototype.hideSuggestor = function(){
	
	var oThis=this;

	setTimeout(function(){oThis.suggestor.style.visibility='hidden'},350);

}




LiveSearch.prototype.getResults = function(){


		var url="http://www.localmaleescorts.com/_ajax/liveSearch.php?search="+escape(this.searchString);

		var oThis=this;

		// do request
		
		httpRequest.open("GET", url, true);
	    httpRequest.onreadystatechange=function(){
			fetchAjaxResponse(oThis,oThis.loadResults);}; // document.fetchAJAXresponse actually calls function, we want
													// document.fetchAJAXresponse to make oThis call the function
	    httpRequest.send(null);
		

}



LiveSearch.prototype.loadResults = function(){


	var matchesARR=httpRequest.responseText.split('|');


	for(i=0;i<matchesARR.length;i++){

		matchARR=matchesARR[i].split('#');

		// insert into arrays
		this.keywords[i]=matchARR[0];
		this.values[i]=matchARR[1];

	}

	this.resultsLoaded=true;

	this.sortResults();

}




LiveSearch.prototype.sortResults = function(){



	// config
	var suggestionLimit=10;


	// clear options
	if(this.suggestor.hasChildNodes()==true){
		while(this.suggestor.childNodes.length>=1){
			this.suggestor.removeChild(this.suggestor.firstChild);       
		} 
	}


	// reset activeSuggestion
	this.activeSuggestion=null;


	// add matching options
	var matchCount=0;
	var i=0;
	
	// add list items to list
	while((i<this.keywords.length)&&(matchCount<suggestionLimit)){

		var oThis=this;

		if((this.keywords[i].substr(0,this.searchString.length).toLowerCase())==this.searchString.toLowerCase()){

			// increase select size
			 matchCount++;

			
			// add list item
			var suggestionListItem=document.createElement('li');
			//var suggestionLink=document.createElement("a");
			//suggestionListItem.appendChild(suggestionLink);
			this.suggestor.appendChild(suggestionListItem);
			
			suggestionListItem.innerHTML=this.keywords[i];
			//suggestionLink.innerHTML=this.keywords[i];



			//suggestionLink.onmouseover=function(){
			suggestionListItem.onmouseover=function(){
				oThis.mouseoverSuggestion(this);
			};
			//suggestionLink.onclick=function(){
			suggestionListItem.onclick=function(){
				oThis.selectSuggestion(this);
			}

			
		}

		// hide 
		if(matchCount==0){
			this.suggestor.style.visibility='hidden';
		} else {
			this.suggestor.style.visibility='visible';			
		}

		i++;

	}



}


LiveSearch.prototype.clearResults = function(){

	// clear options
	if(this.suggestor.hasChildNodes()==true){
		while(this.suggestor.childNodes.length>=1){
			this.suggestor.removeChild(this.suggestor.firstChild);       
		} 
	}

	this.suggestor.style.visibility='hidden';


	this.keywords.length=0;
	this.values.length=0;

	this.resultsLoaded=false;

}



LiveSearch.prototype.nextSuggestion = function(){

	
	// check to see if there are list items and that the active one is not the last one
	if((this.suggestor.hasChildNodes()==true) && (this.activeSuggestion != this.suggestor.lastChild)){
		
		if(this.activeSuggestion != null){
			this.activeSuggestion.className='';
			this.activeSuggestion=this.activeSuggestion.nextSibling;
		} else {
			this.activeSuggestion=this.suggestor.firstChild;
		}

		this.activeSuggestion.className='active';

	}


}

LiveSearch.prototype.previousSuggestion = function(){


	if((this.suggestor.hasChildNodes()==true) && (this.activeSuggestion != null)){

		this.activeSuggestion.className='';

		if(this.activeSuggestion != this.suggestor.firstChild){

			this.activeSuggestion=this.activeSuggestion.previousSibling;

		} else {
			this.activeSuggestion=null;
		}

		this.activeSuggestion.className='active';

	}



}




LiveSearch.prototype.mouseoverSuggestion = function(oListItem){



	if((this.activeSuggestion != null) && (this.activeSuggestion != oListItem)){

		this.activeSuggestion.className='';

	}

	oListItem.className='active';
	this.activeSuggestion=oListItem;

}


LiveSearch.prototype.selectSuggestion = function(){
	
	if(this.activeSuggestion != null){
		this.searchbox.value=this.activeSuggestion.innerHTML;
	}
	this.suggestor.style.visibility='hidden';
	
	if(this.response != null){this.response.call();}

}








/*  [[ EVENT HANDLERS ]]  */


LiveSearch.prototype.handleKeyUp = function(oEvent){


	this.searchString=this.searchbox.value.replace(/^\s+|\s+$/g,"");	

	var iKeyCode = oEvent.keyCode;
	
	if (((iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) && (iKeyCode != 8 && iKeyCode != 46 && iKeyCode != 32)) {



	} else {

		
		if(iKeyCode==13){return false;}


		switch(this.searchString.length){
			
			case 1: // grab mysql search results
				this.getResults();
				break;
			
			case 0: // clear live search results
				this.clearResults();
				break;

			default: // sort search results
				if(this.resultsLoaded==false){this.getResults();} 
				else{this.sortResults();}
				break;

		}
	}

}



LiveSearch.prototype.handleKeyDown = function(oEvent){


	var iKeyCode = oEvent.keyCode;



	switch(oEvent.keyCode) {
        case 38: //up arrow
            this.previousSuggestion();
            break;
        case 40: //down arrow
            this.nextSuggestion();
            break;
        case 13: //enter
            this.selectSuggestion();
			return false;
            break;
    }

}










/*			created on [ 10.06.2009 ] Grenard Madrigal
/*			updated on [ 09.29.2009 ] Grenard Madrigal
/*			evolve, create © 2009
*/





