/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();
//The selected Item in the responce list
var ajax_list_activeItem;
//The div-container which should contain all the listed elements
var ajax_optionDiv;

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest(e) {
	if(e == null) e = window.event;
	if(e == null || e.keyCode == 8 || e.keyCode >= 46) {
		if (searchReq.readyState == 4 || searchReq.readyState == 0) {
			var str = escape(document.getElementById('txtSearch').value);
			searchReq.open("GET", 'includes/liveSearch.php?search=' + str, true);
			searchReq.onreadystatechange = handleSearchSuggest; 
			searchReq.send(null);
		}
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		ajax_optionDiv = document.getElementById('search_suggest');
		ajax_optionDiv.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			
		var suggest = '<div onmouseover="javascript:ajax_options_rollOverActiveItem(this,false);" ';
		suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
		suggest += 'class="suggest_link"';
		
			if(i == 0) {//first element		
				suggest += ' id="first_suggest_link"';
			}
			else if(i == str.length - 2) {//last element
				suggest += ' id="last_suggest_link"';
			}
			
			suggest += '>' + str[i] + '</div>';
			ajax_optionDiv.innerHTML += suggest;
		}
		if(str.length > 1) ajax_options_show();
		else ajax_options_hide();
	}
}

//Beim Klick auf eine Element oder beim Drücken der Enter-Taste
function setSearch(value) {
	document.getElementById('txtSearch').value = value;
	document.getElementById('search_suggest').innerHTML = '';
	ajax_options_hide();
}

//Key Funktionen registrieren
function registerKeyUp() {
	document.getElementById('txtSearch').onkeyup = searchSuggest;
	document.onkeydown = ajax_option_keyNavigation;
	document.onclick = ajax_options_hide;
}

//div-container anzeigen
function ajax_options_show()
{
	if(ajax_optionDiv) ajax_optionDiv.style.display='';	
}

//div-container verstecken
function ajax_options_hide()
{
	if(ajax_optionDiv) ajax_optionDiv.style.display='none';	
}

//Übergebenes Element in Liste markieren durch setzen der Klasse
function ajax_options_rollOverActiveItem(item,fromKeyBoard)
{
	if(ajax_list_activeItem) ajax_list_activeItem.className='suggest_link';
	item.className='suggest_link_over';
	ajax_list_activeItem = item;
	
	//Scrolling
	if(fromKeyBoard){
		if(ajax_list_activeItem.offsetTop>ajax_optionDiv.offsetHeight){
			ajax_optionDiv.scrollTop = ajax_list_activeItem.offsetTop - ajax_optionDiv.offsetHeight + ajax_list_activeItem.offsetHeight + 2 ;
		}
		if(ajax_list_activeItem.offsetTop<ajax_optionDiv.scrollTop)
		{
			ajax_optionDiv.scrollTop = 0;	
		}
	}
}


//Drücken der Pfeil-, Enter- und Escape-Taste behandeln
function ajax_option_keyNavigation(e)
{
	if(e == null) e = window.event;
	if(!ajax_optionDiv || ajax_optionDiv.style.display=='none' || e == null) {
		return;
	}

	if(e.keyCode==38)
	{	// Up arrow
		if(!ajax_list_activeItem || !ajax_list_activeItem.previousSibling)
		{
			ajax_options_rollOverActiveItem(document.getElementById('last_suggest_link'),true);
		}
		else
		{
			ajax_options_rollOverActiveItem(ajax_list_activeItem.previousSibling,true);
		}
	}
	else if(e.keyCode==40)
	{	// Down arrow
		if(!ajax_list_activeItem || !ajax_list_activeItem.nextSibling)
		{
			ajax_options_rollOverActiveItem(document.getElementById('first_suggest_link'),true);
		}
		else
		{
			ajax_options_rollOverActiveItem(ajax_list_activeItem.nextSibling,true);
		}
	}
	else if(e.keyCode==13 || e.keyCode==9)
	{	// Enter key or tab key
		if(ajax_list_activeItem && ajax_list_activeItem.className=='suggest_link_over') 
		{
			if(document.all) setSearch(ajax_list_activeItem.innerText);
			else setSearch(ajax_list_activeItem.textContent);
			ajax_options_hide();
		}
		else
		{
			//Suche bei Enter absenden, wenn kein Punkt der Ajax Suche gewählt wurde			
			if(e.keyCode==13) dokument.suche.submit();			
		}
		
		if(e.keyCode==13)return false; else return true;
	}
	else if(e.keyCode==27)
	{	// Escape key
		ajax_options_hide();			
	}
}

