var selectedSuggestionId = null;

function suggestOver(div_value) {

	// clear currently selected
	if (selectedSuggestionId != null) {
		// get row
		var rowString = selectedSuggestionId.match(/[\d]+/);
		var row = parseInt(rowString);
		// set back to unselected class
		if (row % 2 == 0) {
			suggestOut(document.getElementById(selectedSuggestionId));
		}
		else {
			suggestOutOdd(document.getElementById(selectedSuggestionId));
		}
	}


	// select
	div_value.className = 'suggest_link_over';
	selectedSuggestionId = div_value.id;
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggestion';
}

function suggestOutOdd(div_value) {
	div_value.className = 'suggestion-odd';
}


function checkKeyStroke(event) {
	// get selected row
	if (selectedSuggestionId != null) {
		var rowString = selectedSuggestionId.match(/[\d]+/);
		var row = parseInt(rowString);

		if (event.keyCode == 38) {
			// up arrow
			if (row > 0) {
				row--;
			}
		} else { 
			if (event.keyCode == 40) {
			// down arrow
				row++;
			} else {
				return;
			}
		}
		
		// get new row and highlight
		var newSelectedId = 'suggestion' + row;
		if (document.getElementById(newSelectedId) != null) {
			suggestOver(document.getElementById(newSelectedId));
			//checkForScroll(event, "livesearch", "suggestion", row);
		}
	}
	else {
		if (event.keyCode == 40) {
			// down arrow
			if (document.getElementById('suggestion0') != null) {
				suggestOver(document.getElementById('suggestion0'));
			}
		}
	}
	if (event.keyCode == 8) { 
		//backspace reset everything
		resetSearch();
	}
}

function checkEnter(event) {
	if (event.keyCode == 13) {
		if (document.getElementById(selectedSuggestionId) != null) {
			 eval(document.getElementById(selectedSuggestionId).getAttribute('onclick'));
	    } else {
			window.location=('http://www.austinkayak.com/catalog_search.php?keyword='+document.getElementById('keyword').value);
		}
	}
}

function goClick() {
	window.location=('http://www.austinkayak.com/catalog_search.php?keyword='+document.getElementById('keyword').value);
}


var lastSearch = null;
function checkSuggest() {
	var currentSearch = document.getElementById("keyword").value;
	if (currentSearch != lastSearch && currentSearch.length > 2) {
		// clear currently selected
		if (selectedSuggestionId != null) {
			// get row
			var rowString = selectedSuggestionId.match(/[\d]+/);
			var row = parseInt(rowString);
			// set back to unselected class
			if (row % 2 == 0) {
				//suggestOut(document.getElementById(selectedSuggestionId));
			}
			else {
				//suggestOutOdd(document.getElementById(selectedSuggestionId));
			}
			selectedSuggestionId = null;
		}
		
		// perform search
		lastSearch = currentSearch;
		showResult(currentSearch);
	}
}

var scrollLocation = 0;
        
function checkForScroll(event, elementId, innerElementPrefix, row) {
	 // Get a handle to the element which will be scrolled
	var el = document.getElementById(elementId);
	if ( !el ) {
		return;
	}
		
	// Get a handle to the element inside the scrolled element that is our current
	// scrolling target
	var innerEl = document.getElementById(innerElementPrefix + scrollLocation);
	
	// Get the current scroll offset.
	var currentScroll = innerEl.scrollTop;
	var latestScroll = currentScroll;

	// Check the event to see if they pressed a key we care about
	if ( event.keyCode == 40 && row > 11 ) {
		// They pressed the down arrow and they are at the bottom, we want to scroll down so long as we have
		// an element to work with up until the point where we actually move the 
		// scroll amount.
		while ( innerEl && currentScroll == latestScroll ) {
			// Attempt to scroll
			innerEl = document.getElementById(innerElementPrefix + (scrollLocation + 1));
			if ( innerEl ) {
				// There is such an element, move it into view and determine the new
				// scroll amount for our element
				scrollLocation++;
				innerEl.scrollIntoView();
				latestScroll = document.getElementById(elementId).scrollTop;
			}
		}
	} else if ( event.keyCode == 38  && row < 10 ) {
		// They pressed the up arrow, we want to scroll up so long as we have
		// an element to work with up until the point where we actually move the 
		// scroll amount.
		while ( innerEl && currentScroll == latestScroll ) {
			// Attempt to scroll
			innerEl = document.getElementById(innerElementPrefix + (scrollLocation - 1));
			if ( innerEl ) {
				// There is such an element, move it into view and determine the new
				// scroll amount for our element
				scrollLocation--;
				innerEl.scrollIntoView();
				latestScroll = document.getElementById(elementId).scrollTop;
			}
		}
	}
}