<!-- BEGIN [% component.name %] -->
    [% # INCLUDE dumper.tt dump = c.stash %]
	[% # INCLUDE dumper.tt dump = c.authen.last_access %]
	[% # INCLUDE dumper.tt dump = c.user_profile.session_idle_timeout %]

    [% # c.debug("==== rendering ${component.name} ====") %]

<!-- new user messages -->
    [% IF c.authen.username; # skip if logged / timed out
        args = [];
        args.push('service='  _ c.cfg('settings').item('_centre') );
        args.push('username=' _ c.authen.username);
        args.push('app_url='  _ app_url);
        args.push('tt='       _ template.name);

		cgi_url = c.cfg('settings').base_href
			_ '/script/cgi/new_messages.cgi?'
			_ args.join(';');

		last_access_ms = c.authen.last_access * 1000;
		idle_time_ms   = c.user_profile.session_idle_timeout * 1000;
		interval_ms    = ( 60 * 1000 ) * 1; # mins (in millisecs)
    %]
        <script language="javascript" type="text/javascript">
			[% USE new_msgs
				= url( app_url _ '/ajax/seek_new_messages', tt = template.name); %]
			$(document).ready(function() { // on page load:
				jQuery.get("[% new_msgs %]", function(data) {
					$("#new_msg").html(data); // alert(data);
				});
			});
		</script>

		[% polling_window = idle_time_ms - interval_ms; # 1 interval short of idle-timeout %]
        <script language="javascript" type="text/javascript">
			var startTime = [% last_access_ms %]; // same as new Date().getTime()
			var interval = setInterval( function() {
				// will stop firing 1 interval-time shorter than idle timeout:
				var nowTime = new Date().getTime(); // alert(nowTime);
				if ( ( nowTime - startTime ) > [% polling_window %] ) {
					clearInterval(interval);
					return;
				} // alert( (nowTime - [% last_access_ms %]) / 1000 + ' seconds' );
				// require a unique string (eg time) to prevent brain-dead browser cacheing result:
				$('#new_msg').load( "[% cgi_url %];time=" + nowTime );
				//jQuery.get("[% cgi_url %];date=" + nowTime, function(data) { // same as above
				//	$("#new_msg").html(data); // alert(data);
				//});
			}, [% interval_ms %]);
		</script>
		<div id="new_msg" class="warning"></div>
    [% END %]
	<!-- end new user messages -->

    <script language="javascript" type="text/javascript">
	/*
		// test of setInterval & clearInterval function:
		var startTime = new Date().getTime();
		var interval = setInterval( function() {
			if ( new Date().getTime() - startTime > 60000 ) {
				clearInterval(interval);
				return;
			}
			// alert( (new Date().getTime()  - startTime) / 1000 );
		}, 10000);
	*/
	</script>

    <!-- stash messages -->
    [% IF c.stash.status_msg %]
        <p class="message">[% c.stash.status_msg %]</p>
    [% END %]

    [% IF c.stash.error_msg;
        error = c.stash.error_msg;
        IF is_code_ref(error); # split into list form: %]
            <div class="error">ERROR:</div>
            [% FOREACH e IN error %]<div class="indent error">* [% e %]</div>[% END %]
        [% ELSE %]
            <p class="error">ERROR: [% error %]</p>
        [% END %]
    [% END %]

    <!-- flash messages -->
    [% IF using_cap_flash; # using MessageStack instead & disabled to prevent c.flash producing undef msgs
        # PROCESS dumper.tt dump = c.flash.dump;
        # FOREACH type IN [ 'info', 'warning', 'error' ];
            # IF c.flash.has_key(type);
                # FOREACH message IN c.flash.get(type) -%]
                <p class="[% type %]">
                    [% type | upper %]: [% message %]
                </p>
                [% # END %]
            [% # END %]
        [% # END %]
    [% END %]

    [% INCLUDE site/snippets/message_stack.tt %]
<!-- END [% component.name %] -->
