function ContactOptionsBoxProvider( target, mandatory, dataSource )
{
	var that = new OptionsBoxProvider( target, mandatory ),
		old = { init: that.init, load: that.load, filter: that.filter },
		//spacify = /[^a-z0-9]+/g; // what about "äöüß"?
		spacify = /[,\."'\/\\&_-]+/g;
	
	/*
	that.init = function( ob, source )
	{
		old.init( ob, source );		
	};
	*/
	
	that.load = function( cb )
	{
		//console.log( 'entered load' );
		// this needs to be loaded only once
		if( that.items.selection )
		{
			// execute callback
			if( cb )
				cb( that.items.selection );
				
			return;
		}
			
		that.status = OptionsBoxProvider.LOADING;
		that.items.selection = [];

		var t;
		if( t = RodocsContacts.getAll() )
		{
			jQuery.each( t, function(i,o)
			{
				that.items.selection.push( {
					i : o.id,
					t : o.name,
					s : o.online ? 'online' : 'offline',
					q : o.name.toLowerCase().replace( spacify, ' ' ),
					img : o.avatar.mini,
					o : o
				} );
			});
		}
		
		// execute callback
		if( cb )
			cb( that.items.selection );
	};

	that.cache = function(){ return that.items.selection };
	
	that.select = function( record )
	{
		that.source.value = record.t;
		location.href = record.o.uri;
	};
	
	that.enterKey = function( e, source )
	{
		location.href = '/suche/benutzer?name='+ encodeURI(source.value) +'&submit=query';
		e.stopPropagation();
		e.preventDefault();
	};
	
	that.addElement = function( record, hilite )
	{
		// clean current options
		if( that.status != OptionsBoxProvider.ADDING )
		{
			that.ob.removeItems();
			that.status = OptionsBoxProvider.ADDING;
		}
			
		var className = 'combined',
			c = document.createElement( 'div' ),
			t = document.createElement( 'h4' ),
			s = null,
			img = null;
			
		t.appendChild( document.createTextNode( record.t ) );		
		jQuery( t ).hilite( hilite );
		c.appendChild( t );
		
		if( record.s )
		{
			s = document.createElement( 'p' );
			s.appendChild( document.createTextNode( record.s ) );
			c.appendChild( s );
		}
		
		if( record.img )
		{
			img = document.createElement( 'img' );
			img.src = record.img;
			img.alt = '';
			c.insertBefore( img, t );
			
			className = 'combined icon';
		}
		
		className += record.online ? ' online' : ' offline';

		c.record = record;
		that.ob.addItem( c, className );
	};
	
	return that;
}

function initialize_contactSearch(){
	
	if( typeof( RS ) == 'undefined' || typeof( RS.primary ) == 'undefined' || !RS.primary )
	{
		//console.log( 'RodocsContacts aborted since handling secondary window here' );
		return;
	}
	
	if( typeof( RS ) == 'undefined' || typeof( RS.user ) == 'undefined' || typeof( RS.user.name ) == 'undefined' )
	{
		//console.log( 'RodocsContacts aborted since handling guest here' );
		return;
	}
	
	// come back later if data has not been loaded yet
	if( RodocsContacts.getAll() === null )
	{
		window.setTimeout( initialize_contactSearch , 500 );
		return;	
	}
	
	if( !jQuery('#contactSearch').length )
		return;
	 
	var query = document.createElement( 'input' ),
		result = document.createElement( 'input' );

	query.type = 'text';
	query.name = 'contactSearch';
	query.className = 'OptionsBoxQuery';
	query.id = 'contactSearch';
	
	result.type = 'hidden';
	result.name = 'contact';
	
	// remove original select
	jQuery('#contactSearch').replaceWith( jQuery( query ) );
	jQuery( result ).insertBefore( jQuery(query) );

	var provider = new ContactOptionsBoxProvider( result, false );
	
	provider.initialized = 'Name des Benutzers eingeben'; 
	provider.loading = 'Lade Daten…';
	provider.notfound = 'Kein Benutzer gefunden';
	provider.notselected = 'Es wurde keine Benutzer ausgewählt';
	
	new OptionsBox( query, provider );
}

jQuery( initialize_contactSearch );
