(function( $ ){
	$.fn.liveSearch=function(options){
		var settings = {
			searchUrl:location.href,
			parameters: {
					'type':9999,
					'ajax':1,
					'function':'search'					
			},
			searchParameter:'search',
			gfxPath:'/typo3conf/ext/idefa_rbsadmin/res',
			clickAction:function(data){alert('Chose '+data.title);},
			maxLength:35,
			fieldText:'Search',
			typeGrace:600 //Time in ms before sending the search
		}
		return this.each(function(){
			if ( options ){ 
				$.extend( settings, options );
			}
			var $this=$(this);
			$(this).hide();
			var id=$this.attr('id');
			if ( id === undefined ) {
				id='liveSearch'+(((1+Math.random())*0x10000)|0).toString(16).substring(1);
				this.id=id;
			}
			var field='';
			field+='<div id="suggest-'+id+'" class="livesearch" style="margin-top:0px"><input type="text" class="livesearch-field" value="'+settings.fieldText+'" id="'+id+'Suggest" autocomplete="off"><div id="'+id+'SuggestIndicator" style="display: none;" class="livesearch-indicator"><img alt="Searching..." src="'+settings.gfxPath+'/spinner.gif"></div><div id="'+id+'SuggestChoices" class="livesearch-choices"><!-- results --></div></div>';
			$this.after(field);
			var lastSearch='';
			function search(terms){
				$(this).data('isSearching',undefined);
				if ( lastSearch == terms ) { return; }
				lastSearch=terms;
				$('#'+id+'SuggestIndicator').show();
				settings.parameters[settings.searchParameter]=terms;
				$.get(settings.searchUrl,settings.parameters,function(data){
					var results=$('#'+id+'SuggestChoices');
					results.html('<ul class="livesearch-resultlist"></ul>');
					results=results.find('ul');
					data=$.parseJSON(data);
					if ( data['success'] === true ) {
						
						$(data['results']).each(function(){
							var img=this.icon;
							if ( img!=='' ){
								img=' style="background-image: url(&quot;'+img+'&quot;);"';
							}
							results.append('<li'+img+'><span class="suggest-label">'+this.title+'</span><span class="suggest-info">['+this.info+']</span></li>');
							results.find('li:last').data('data',this);
						});
						$('#'+id+'SuggestChoices').show();
					}
					$('#'+id+'SuggestIndicator').hide();
					
				});
				
			}
			var OrgVal=$('#'+id+'Suggest').val();
			if ( $this.val() != '' ) { $('#'+id+'Suggest').val($this.val()); }
			$('#'+id+'Suggest')
				.click(function(){
					if ( $(this).val()===OrgVal ){
						$(this).val('');
					}
					if ( $('#'+id+'SuggestChoices ul li').length ) {
						$('#'+id+'SuggestChoices').show();
					}
				})
				.blur(function(){
					if ( $(this).val()==='' ){
						$(this).val(OrgVal);
					}
					if ( !$('#'+id+'SuggestChoices ul li:hover').length ){
						$('#'+id+'SuggestChoices').hide();
					}
				})
				.keyup(function(){
					$this.val($(this).val());
					if ( $(this).val().length > 2 ) {
						if ( $(this).data('isSearching') != undefined ){
							window.clearTimeout($(this).data('isSearching'));
						}
						var val=$(this).val();
						$(this).data('isSearching', window.setTimeout(function(){search(val)},settings.typeGrace));
					}
				});
				
				$('#'+id+'SuggestChoices ul li')
					.live('click.tagsearch',function(){
						var data=$(this).data('data');
						settings.clickAction(data);
						$('#'+id+'SuggestChoices').hide();
					})
					.live('mouseover.tagsearch mouseout.tagsearch',function(event){
						if (event.type == 'mouseover') {
							$(this).addClass('selected');
							if ( $('#'+id+'SuggestChoices').data('mousetimer') != undefined ){
								window.clearTimeout($('#'+id+'SuggestChoices').data('mousetimer'));
							}
						}else{
							$(this).removeClass('selected');
							$('#'+id+'SuggestChoices').data('mousetimer', window.setTimeout(function(){$('#'+id+'SuggestChoices').hide();},600));							
						}
					});
		});
	}
})( jQuery );

