/****************************************
*
*   Search Suggestions (www.givneysoftware.com.au)
*   - NOTE: Write your own this.formSubmitCheck Function before use
*
************/

function searchSuggestion() {

	/* Init Components */
	var elements = Object;
	var request = null;
	var selectFriend = 0;
	var databaseFile = "./ajax-members-list.php";
	var inputID = "keywordSearch";
	var viewID = "searchSuggestions";

	/* Captures all needed elements into an array */
	this.captureElements = function() {

		return {
		  input: document.getElementById(inputID),
		  view: document.getElementById(viewID)
		};

	};

	/* Helper Function to return all objects by ClassName */
	this.getClassname = function()  {

	    	var node = document.getElementsByTagName("body")[0];
		var _elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			var regex = new RegExp('\\b' + element + '\\b');
	    		var element = node.getElementsByTagName("*");
	    		for(var i=0,j=element.length; i<j; i++) {
				if(regex.test(element[i].className)) { _elements.push(element[i]); }
			}
		}

		return _elements;
	};

	/* Helper Function to return all objects by ID */
	this.getID = function() {
		var _elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			_elements.push(element);
		}
	
		return _elements;
	};

	/* Helper Function to return an Ajax Object */
	this.getXmlHttpRequestObject = function() {

		if (window.XMLHttpRequest) {
			return new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert('Cound not create XmlHttpRequest Object. Service can not run...');
		}

	};

	/* Stupid IE Fix (IE sends focus to the submit button on hitting the return key before the onkeyup event fires FFS) */
	this.stupidIEfix = function(e) {
		
		var key = (window.event) ? window.event.keyCode : e.keyCode ;

		switch (key) {
		  case 13:

			/* Perform action keyboard return key */
			if (selectFriend == 0) {
			
				/* If no contact is selected default to first contact in View Element */
				return true;

			} else {
			
				/* Get Highlighted Contact and Add to List Element */
				searchSuggestion.getID('ss_' + selectFriend).onclick();
				return false;
						
			}

			break;

		}


	}

	/* Capture Keyboard Events from Input Element */
	this.getKeys = function(e) {

		var key = (window.event) ? window.event.keyCode : e.keyCode ;

		switch (key) {

		  case 37:

			/* Highlight Suggestion with keyboard left arrow key */
			elements.view.innerHTML = "";
			selectFriend=0;
			return false;
			break;

		  case 39:

			/* Highlight Suggestion with keyboard right arrow key */
			elements.view.innerHTML = "";
			selectFriend=0;
			return false;
			break;

		  case 38:

			/* Highlight Suggestion with keyboard up arrow key */
			searchSuggestion.dimAll();
			var friendOptions = searchSuggestion.getClassname('ajaxFriend');
			selectFriend = (selectFriend>1) ? selectFriend-1 : 1 ;
			searchSuggestion.highlight( searchSuggestion.getID('ss_' + selectFriend) );
			var searchKeyword = searchSuggestion.getID('ss_' + selectFriend).getAttribute('title');
			if (searchKeyword.length > 0) { elements.input.value = searchKeyword; }
			return false;
			break;

		  case 40:

			/* Highlight Suggestion with keyboard down arrow key */
			trackBackspace=0;
			searchSuggestion.dimAll();
			var friendOptions = searchSuggestion.getClassname('ajaxFriend');
			selectFriend = (selectFriend<friendOptions.length) ? selectFriend+1 : friendOptions.length ;
			searchSuggestion.highlight( searchSuggestion.getID('ss_' + selectFriend) );
			var searchKeyword = searchSuggestion.getID('ss_' + selectFriend).getAttribute('title');
			if (searchKeyword.length > 0) { elements.input.value = searchKeyword; }
			return false;
			break;

		  default:

			/* Send ajax request to populate View Element with Suggestions */
			if(elements.input.value.length>=2) { searchSuggestion.getSuggestions(elements.input.value); } else { elements.view.innerHTML = ""; }
			return true;
			break;

		}
	
	};

	/* Highlight selected Friend in View Element */
	this.highlight = function(obj) {
		obj.style.backgroundColor = '#bf1e2e';
	};

	/* Dim selected Friend in View Element */
	this.dim = function(obj) {
	
		obj.style.backgroundColor = '#3b2314';

	};

	/* Dim all Friend in View Element */
	this.dimAll = function() {
	
		var options = searchSuggestion.getClassname('ajaxFriend');
		for (var i = 0; i < options.length; i++) {
			options[i].style.backgroundColor = '#3b2314';
		}

	};

	/* Called after keyboard event - Populates View Element with contacts (Notice: Is it's own callback function) */
	this.getSuggestions = function(keyword) {

		  if (request == null) {

			/* Create ajax Object and send search request */
			request = searchSuggestion.getXmlHttpRequestObject();
			request.open("GET", databaseFile + '?s=' + keyword, true);
			request.onreadystatechange = searchSuggestion.getSuggestions; 
			request.send(null);
			selectFriend=0;

		  } else {

			if (request.readyState == 4) {

				/* Populate View Element with Contacts */
				elements.view.innerHTML = request.responseText;				

				/* Reset Request Var for next search */
				request = null;

			}

		  }

	};

	/* Capture Elements as Objects */
	elements = this.captureElements();

	/* Set Elements Behaviour */
	if (elements.input.value == "") {
	elements.input.value = "Search Here";
	}
	elements.input.onfocus = function() { if (this.value=="Search Here") { this.value = ""; } }
	elements.input.setAttribute('autocomplete','off');
	elements.input.onblur = function() { setTimeout(function(){ elements.view.innerHTML = ""; },200); };
	elements.input.onkeydown = this.stupidIEfix;
	elements.input.onkeyup = this.getKeys;

}
