
// send activity report every 4 minutes if user has been typing in an input-field since the last interval
// to accomplish all <input[text]> and <textarea> need to be keyup-monitored
// keyup will set activityFlag which, if true, will trigger a request on activityReportInterval

function RodocsActivityNotification()
{
	var that = this,
		action = false,
		interval = null;
	
	function keyUpListener( e )
	{
		action = true;
	}
	
	jQuery( document ).bind( 'keyup', keyUpListener );
	
	function intervalListener()
	{
		var request = null;
		request = jQuery.get(
			'/system/aktivitaet/?format=json&ident=' + storage.get('ident') + (action ? '&activity=yes' : ''), null,
			function( data )
			{
				// show error Bubble
				// terminate interval if session transparently died
				if( request.status == 204 )
				{
					// the request was executed properly but no content had to be sent to the client
					return;
				}
				
				if( request.status != 200 )
				{
					// if( typeof( RodocsBubble ) != 'undefined' && typeof( RodocsBubble.ajaxError ) == 'function' )
						// RodocsBubble.ajaxError( request );
				}
				else
				{
					var d = new DialogBox( 'Fehler', jQuery( data )[0] );
					d.box().type( AnyBox.MODAL ).visualizer( AnyBox.fadeVisualizer ).show();	
				}
				
				window.clearInterval( interval );
				interval = null;
			}, 'html'
		);
		
		action = false;
	}
	
	interval = window.setInterval( intervalListener, 180000 ); // 3 minutes
}

jQuery( function()
{
	if( typeof( RS ) == 'undefined' || typeof( RS.primary ) == 'undefined' || !RS.primary )
	{
		//console.log( 'RodocsMessages aborted since handling secondary window here' );
		return;
	}
	
	if( typeof( RS ) == 'undefined' || typeof( RS.user ) == 'undefined' || typeof( RS.user.name ) == 'undefined' )
	{
		//console.log( 'RodocsMessages aborted since handling guest here' );
		return;
	}
		
	new RodocsActivityNotification(); 
} );