package wicket.extensions.ajax.markup.html.autocomplete;

import wicket.RequestCycle;
import wicket.Response;
import wicket.ajax.AbstractDefaultAjaxBehavior;
import wicket.behavior.AbstractAjaxBehavior;
import wicket.markup.html.PackageResourceReference;
import wicket.util.string.JavascriptUtils;

/**
 * @since 1.2
 * 
 * @author Janne Hietam&auml;ki (jannehietamaki)
 */
public abstract class AbstractAutoCompleteBehavior extends AbstractAjaxBehavior
{
        private static final PackageResourceReference AUTOCOMPLETE_JS = new PackageResourceReference(AutoCompleteBehavior.class, "wicket-autocomplete.js");
        private static final PackageResourceReference AJAX_JS = new PackageResourceReference(AbstractDefaultAjaxBehavior.class, "wicket-ajax.js");

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected String getImplementationId()
	{
	    return "wicket-default-autocomplete";
	}

	protected void onBind()
	{
		getComponent().setOutputMarkupId(true);
	}

	protected void onRenderHeadInitContribution(Response response)
	{
	    super.onRenderHeadInitContribution(response);
	    writeJsReference(response, AJAX_JS);
	    writeJsReference(response, AUTOCOMPLETE_JS);
	}

	protected void onComponentRendered()
	{
		Response response=getComponent().getResponse();
		final String id = getComponent().getMarkupId();
		response.write(JavascriptUtils.SCRIPT_OPEN_TAG);
		response.write("new WicketAutoComplete('"+id+"','"+getCallbackUrl()+"');");
		response.write(JavascriptUtils.SCRIPT_CLOSE_TAG);
	}

	/**
	 * @see wicket.behavior.IBehaviorListener#onRequest()
	 */
	public final void onRequest()
	{
		final RequestCycle requestCycle = RequestCycle.get();
		final String val = requestCycle.getRequest().getParameter("q");
		onRequest(val, requestCycle);
	}

	/**
	 * Callback for the ajax event generated by the javascript. This is
	 * where we need to generate our response.
	 * 
	 * @param input
	 *            the input entered so far
	 * @param requestCycle
	 *            current request cycle
	 */
	protected abstract void onRequest(String input, RequestCycle requestCycle);
}

@@ -174,37 +174,30 @@
 ket.
-behavior.AbstractAjaxBehavior
+ajax.AjaxRequestTarget
 ;%0D%0Ai
@@ -441,16 +441,23 @@
 Abstract
+Default
 AjaxBeha
@@ -465,24 +465,17 @@
 ior%0D%0A%7B%0D%0A
-        
+%09
 private 
@@ -619,399 +619,78 @@
 );%0D%0A
-        private static final PackageResourceReference AJAX_JS = new PackageResourceReference(AbstractDefaultAjaxBehavior.class, %22wicket-ajax.js%22);%0D%0A%0D%0A%09/**%0D%0A%09 * %0D%0A%09 */%0D%0A%09private static final long serialVersionUID = 1L;%0D%0A%0D%0A%09protected String getImplementationId()%0D%0A%09%7B%0D%0A%09    return %22wicket-default-autocomplete%22;%0D%0A%09%7D%0D%0A%0D%0A%09protected void onBind()%0D%0A%09%7B%0D%0A%09%09getComponent().setOutputMarkupId(true);%0D%0A%09%7D
+%09%0D%0A%09/**%0D%0A%09 * %0D%0A%09 */%0D%0A%09private static final long serialVersionUID = 1L;
 %0D%0A%0D%0A
@@ -759,20 +759,17 @@
 )%0D%0A%09%7B%0D%0A%09
-    
+%09
 super.on
@@ -812,55 +812,9 @@
 ;%0D%0A%09
-    writeJsReference(response, AJAX_JS);%0D%0A%09    
+%09
 writ
@@ -1201,108 +1201,56 @@
 %0A%0D%0A%09
-/**%0D%0A%09 * @see wicket.behavior.IBehaviorListener#onRequest()%0D%0A%09 */%0D%0A%09public final void onRequest()%0D%0A%09
+protected void respond(AjaxRequestTarget target)
 %7B%0D%0A%09
@@ -1403,22 +1403,25 @@
 tCycle);
+%09%09
 %0D%0A%09%7D%0D%0A
+%09
 %0D%0A%09/**%0D%0A

extend AbstractDefaultAjaxBehavior to prevent duplicate contribution of wicket-ajax.js