jQuery(document).ready( function() {
	bindBlur();
	jQuery('#suggestBox').bind("mouseenter", function(e) {
			unbindBlur();
	});
	jQuery('#suggestBox').bind("mouseleave", function(e) {
			bindBlur();
	});
	jQuery('#queryString').bind("keyup", function(e) {
		if (e.keyCode == 13) {
			jQuery('#list').children().eq(0).click(); 
			return false;
		} else {
			search(this.value)
		}
	});
});

function unbindBlur() {
	jQuery('#queryString').unbind("blur");
}

function bindBlur() {
	jQuery('#queryString').bind("blur", function(e) {
		jQuery('#suggestBox').hide();return false;	
	});
}

function search(queryString) {
	if (queryString.length == 0) {
		jQuery('#suggestBox').hide();
	} else {
		jQuery.get("/fileadmin/templates/haeuslbauer/lookup.php", {search: queryString, action: "list"}, function(data){
			if(data.length > 0) {
				jQuery('#suggestBox').show();
				jQuery('#list').html(data);
			} else {
				jQuery('#suggestBox').hide();
			}
		});
	}
} 

function fill(value) {
	jQuery('#queryString').val(value);
	jQuery('#suggestBox').hide();
	jQuery.get("/fileadmin/templates/haeuslbauer/lookup.php", {search: value, action: "detail"}, function(data) {
		jQuery('#descriptionText').html(data);
		jQuery('#descriptionText').show();
	});
}

function getText(queryString) {
	jQuery.get("lookup.php", {search: queryString, action: "list"}, function(data){
		if(data.length >0) {
			jQuery('#suggestBox').show();
			jQuery('#list').html(data);
		} else {
			jQuery('#suggestBox').hide();
		}
	});
}

