/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.ajax;

import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.util.time.Duration;

/**
 * A behavior that generates an AJAX update callback at a regular interval.
 * 
 * @since 1.2
 * 
 * @author Igor Vaynberg (ivaynberg)
 * 
 */
public abstract class AbstractAjaxTimerBehavior extends AbstractDefaultAjaxBehavior
{
	/** The update interval */
	private final Duration updateInterval;

	private boolean stopped = false;

	/**
	 * Construct.
	 * 
	 * @param updateInterval
	 *            Duration between AJAX callbacks
	 */
	public AbstractAjaxTimerBehavior(final Duration updateInterval)
	{
		this.updateInterval = updateInterval;
	}

	/**
	 * Stops the timer
	 */
	public final void stop()
	{
		stopped = true;
	}

	/**
	 * @see org.apache.wicket.behavior.AbstractAjaxBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
	 */
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);

		if (!stopped)
		{
			response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
		}
	}

	/**
	 * @param updateInterval
	 *            Duration between AJAX callbacks
	 * @return JS script
	 */
	protected final String getJsTimeoutCall(final Duration updateInterval)
	{
		// this might look strange, but it is necessary for IE not to leak :(
		return "setTimeout(\"" + getCallbackScript() + "\", "
				+ updateInterval.getMilliseconds() + ");";
	}

	protected CharSequence getCallbackScript()
	{
		String componentId = getComponent().getMarkupId();
		String precondition = "var c = Wicket.$('" + componentId + "'); return typeof(c) != 'undefined' && c != null";
		
		return getCallbackScript("wicketAjaxGet('"
				+ getCallbackUrl(onlyTargetActivePage()) + "'", null, null, precondition);
	}
	
	protected boolean onlyTargetActivePage() {
		return true;
	}
	
	/**
	 * 
	 * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
	 */
	protected final void respond(final AjaxRequestTarget target)
	{
		onTimer(target);
		
		if (!stopped)
		{
			target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
		}
	}

	/**
	 * Listener method for the AJAX timer event.
	 * 
	 * @param target
	 *            The request target
	 */
	protected abstract void onTimer(final AjaxRequestTarget target);
}

@@ -829,16 +829,47 @@
 .ajax;%0A%0A
+import org.apache.wicket.Page;%0A
 import o
@@ -2306,16 +2306,97 @@
 pt()%0A%09%7B%0A
+%09%09String precondition = null;%0A%09%09%0A%09%09if ( !(getComponent() instanceof Page) )%0A%09%09%7B%0A%09
 %09%09String
@@ -2442,23 +2442,17 @@
 Id();%0A%09%09
-String 
+%09
 precondi
@@ -2547,16 +2547,20 @@
  null%22;%0A
+%09%09%7D%0A
 %09%09%0A%09%09ret

test if the component is not a page. Because if it is a page then the precondition isn't set.