$(document).ready( function() {
	// TODO trim input value to prevent hit on whitespace input
	
	String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
	
	//search
	$v = $('.prvtsb_values');
	$sb = $('#prvttsuchbegriff');
	
	
	$sb.attr('autocomplete', 'off').blur();
	
	
	$t = $('#prvtthema');
	$sugs = $('.prvtsb_values li');
	function compare() {
		$val = $("#prvttsuchbegriff").val().toLowerCase();
		$sugs.each( function(i) {
				$suche = $(this).text().toLowerCase().indexOf($val);
				if ($suche == 0 && $val != '') {
					// show item and container if current value matches beginning characters				
				
				$('.prvtsb_values:hidden').fadeIn('fast');
				
				$(this).show().attr('class','pon');//.parent().parent().show();
				
			} else {
				$(this).hide().removeAttr('class');
			}
		});
		
		// hide container if current value is empty
		if ($val == '') {
			$(".prvtsb_values").hide();
		} else {
			
			if($('.prvtsb_values li.pon').eq(0).attr('class') == 'pon') $(".prvtsb_values").show();
			
		}
	};
	var timer = null;
	function suggest() {
		timer = window.setTimeout( function() {
			compare();
			window.clearTimeout(timer);
			timer = null;
			suggest();
		}, 50);
	};
	
	
	
	
	function doOnBlur(){
		window.clearTimeout(timer);
		timer = null;
		if ($pcl != 'c'){
		
		}
		
		if ($sb.val() == '') $sb.val('Begriff eingeben');
	}
	

	

	$sb.focus( function() {
		if ($(this).val() == 'Begriff eingeben') $(this).val('');
		suggest();
		
		$t.attr('class','prvt_passiv');
		$(this).removeAttr('class');
		$('.prvthelp').children('p').hide();
		
		
	});
	
	$sb.blur(function(){
		
		window.clearTimeout(timer);
		timer = null;
		if ($sb.val() == '') $sb.val('Begriff eingeben');

	});
	
	$('body').click(function(){
	
		
		
		//$('.prvtsb_values li:visible').hide();
		
		$('.prvtsb_values').hide();
		
	});
	
	$('#prvtsubmit_sb').focus(function(){
		$('.prvtsb_values').hide();
		
	})
	
	
	
	$t.focus(function(){
		$('.prvtsb_values').hide();
		$sb.attr('class','prvt_passiv');
		$(this).removeAttr('class');
		$('.prvthelp').children('p').hide();
	})


	
	
	
	
	$sugs.click(function(){
		
		$sbval = $(this).text();
		
		$sbval = $sbval.trim();
		
		
		
		
		$("#prvttsuchbegriff").val($sbval);
		//$sugs.hide();
		//$("#prvttsuchbegriff").val($(this).text());
	});
	
	//$(".prvtsb_values").hide();
	//$sugs.hide();
	
	
	$('#prvtsubmit_sb').click(function(){
		
		if($sb.attr('class')== 'prvt_passiv') $sb.val('');
		if($t.attr('class')== 'prvt_passiv') $t.val('');
		$('.prvthelp').children('p').hide();
		
		
		
	})
	
	
	
	
	
	
	
	
});