//search elements
var pubs = [];
var pubsonly = [];
var publinks = [];
var pubsonlylinks = [];

jQuery(document).ready(function(){
	
	jQuery('#searchinput').click(function(){
		jQuery(this).val('');
	});

for(i=0;i<pubArray.length;i++){
			
			
	//search stuff
	pubs.push(pubArray[i][1]+pubArray[i][7]); 
	publinks[pubArray[i][1]+pubArray[i][7]] = '<li class="_'+ i +'_">' + pubArray[i][1] + '&nbsp;' + pubArray[i][7] + '</li>';
	
	pubs.push(pubArray[i][7]);
	publinks[pubArray[i][7]] = '<li class="_'+ i +'_">' + pubArray[i][1] + '&nbsp;' + pubArray[i][7] + '</a></li>';
	
	pubsonly[i] = pubArray[i][1]+pubArray[i][7];
	pubsonlylinks[pubArray[i][1]+pubArray[i][7]] = '<li class="_'+ i +'_">' + pubArray[i][1] + '&nbsp;' + pubArray[i][7] + '</a></li>';
	
	
	}
	
	
	
	var oTextbox = new AutoSuggestControl(document.getElementById("searchinput"), new StateSuggestions());
	
});


/* Suggest Stuff */
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/, 
							oProvider /*:SuggestionProvider*/) {
/**
* Suggestion provider for the autosuggest feature.
* @scope private.
*/

	this.provider /*:SuggestionProvider*/ = oProvider;

/**
* The textbox to capture.
* @scope private
*/

	this.textbox /*:HTMLInputElement*/ = oTextbox;

	//initialize the control
	this.init();
}

/**
* Autosuggests one or more suggestions for what the user has typed.
* If no suggestions are passed in, then no autosuggest occurs.
* @scope private
* @param aSuggestions An array of suggestion strings.
*/
AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {

	//make sure there's at least one suggestion
	if (aSuggestions.length > 0) {
		this.typeAhead(aSuggestions);
	}
};
 
 
/**
* Handles keyup events.
* @scope private
* @param oEvent The event object for the keyup event.
*/

AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {
	var iKeyCode = oEvent.keyCode;

	//alert(iKeyCode);

	//make sure not to interfere with non-character keys
	if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
		//ignore
		if(iKeyCode==8 || 13){
			if(jQuery('#searchinput').val()!=""){
				this.provider.requestSuggestions(this);
			}else{
				document.getElementById("side_bar").innerHTML = '';
			}
		}
	} else {
		if (jQuery('#searchinput').val() != "") {
			//request suggestions from the suggestion provider
			this.provider.requestSuggestions(this);
		}else{
			document.getElementById("side_bar").innerHTML = '';
		}
	}
};

/**
* Initializes the textbox with event handlers for
* auto suggest functionality.
* @scope private
*/

AutoSuggestControl.prototype.init = function () {

	//save a reference to this object
	var oThis = this;

	//assign the onkeyup event handler
	this.textbox.onkeyup = function (oEvent) {

		//check for the proper location of the event object
		if (!oEvent) {
			oEvent = window.event;
		}    

		//call the handleKeyUp() method with the event object
		oThis.handleKeyUp(oEvent);
	};
};

/**
* Selects a range of text in the textbox.
* @scope public
* @param iStart The start index (base 0) of the selection.
* @param iLength The number of characters to select.
*/

AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {
 
	//use text ranges for Internet Explorer
	if (this.textbox.createTextRange) {
		var oRange = this.textbox.createTextRange(); 
		oRange.moveStart("character", iStart); 
		oRange.moveEnd("character", iLength - this.textbox.value.length);      
		oRange.select();
   
		//use setSelectionRange() for Mozilla
	} else if (this.textbox.setSelectionRange) {
		this.textbox.setSelectionRange(iStart, iLength);
	}     

	//set focus back to the textbox
	this.textbox.focus();      
}; 

/**
* Inserts a suggestion into the textbox, highlighting the 
* suggested part of the text.
* @scope private
* @param sSuggestion The suggestion for the textbox.
*/

AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:Array*/) {
 
	//check for support of typeahead functionality
	if (this.textbox.createTextRange || this.textbox.setSelectionRange){
	
	
		var iLen = this.textbox.value.length; 

		//this.textbox.value = sSuggestion[0]; 
		document.getElementById("side_bar").innerHTML = '';
		_dupesholder = [];
		len = sSuggestion.length;

		for(i=0;i<len;i++){
	 
			//if(in_array(sSuggestion[i],_dupesholder)){      
				document.getElementById("side_bar").innerHTML +=  publinks[sSuggestion[i]];      
				_dupesholder.push(sSuggestion[i]);

				//find id
				temp = publinks[sSuggestion[i]];      
				id = temp.substring((temp.indexOf('_')+1),temp.lastIndexOf('_'));
				//gmarkers[id].show();
			//}
		}
		//this.selectRange(iLen, sSuggestion.length);
		
		attachSidebarEvents();
	}
};


function attachSidebarEvents() {
		jQuery("#side_bar li").click(function () {
		var	className = jQuery(this).attr("class");
		var i = className.substr(1, className.length - 2);
		
		jQuery('#pubid').val(i);
		jQuery('#searchinput').val(jQuery(this).text());
		document.getElementById("side_bar").innerHTML = "";
		
	});
}

function in_array(needle, haystack){  
	len = haystack.length;  
	for(i=0;i<len;i++){
		if(haystack[i]==needle){
			return false;
			break;
		}
	}
	return true;
}
 
function StateSuggestions() {
	this.states = pubs;
}

/**
* Request suggestions for the given autosuggest control. 
* @scope protected
* @param oAutoSuggestControl The autosuggest control to provide suggestions for.
*/

StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
	var aSuggestions = [];
	var sTextboxValue = oAutoSuggestControl.textbox.value;
	if (sTextboxValue.length > 0){

		//search for matching states
		for (var i=0; i < this.states.length; i++) { 
			state = this.states[i].toLowerCase();
			text = sTextboxValue.toLowerCase();

			if (state.indexOf(text) == 0) {
				aSuggestions.push(this.states[i]);
			} 
		}

		if(aSuggestions.length == 0){
			document.getElementById("side_bar").innerHTML = '';
		}
	}else{
		document.getElementById("side_bar").innerHTML = '';
		//showAllMarkers();
		for(i=0;i<pubsonly.length;i++){
			document.getElementById("side_bar").innerHTML +=  pubsonlylinks[pubsonly[i]];
		}
	}

	//provide suggestions to the control
	oAutoSuggestControl.autosuggest(aSuggestions);
};



