
/**
 * Live search
 */
function amw_overlap_live_search(theme_url, input, event) {

	// check if key is ESC, and if so, clear the search
	if (amw_overlap_keycode(event) == 27) {
		input.value = '';
		$('results').update('');
		return;
	}

	// don't react on input smaller than 3 characters
	if (input.value.length < 3) {
		$('results').update('');
		return;
	}
		
	// setup the ajax URL
	var url = theme_url+'/ajax_search.php?s=' + encodeURIComponent(input.value);

	// send the request
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			$('results').update(transport.responseText);
		},
		onLoading: function() {
			$('searchicon').src = theme_url+'/img/spinner.gif';
			$('searchicon').addClassName('spinner');
		},
		onComplete: function() {
			$('searchicon').src = theme_url+'/img/magnify.png';
			$('searchicon').removeClassName('spinner');
		}
	});


}

/**
 * Gets the keyCode
 */
function amw_overlap_keycode(e) {

	var evt = e || window.event;
	return evt.keyCode;

}

/**
 * Sets the value of the searchform
 */
function amw_overlap_toggle_search_input(input, hasfocus) {
	
	default_text = 'search this site';
	
	if (hasfocus) {
		
		if (input.value == default_text) {
			input.value = '';
			// clear the search results
			$('results').update('');
		}
		
	} else {
	
		if (input.value == '') {
			input.value = 'search this site';
			// clear the search results
			$('results').update('');
		}
	}
	
}
