jQuery(document).ready(function(){
	
    var runningRequest = false;
    var request;

   //Identify the typing action
    $('input#s').keyup(function(e){
	$('div#fast').show();
        e.preventDefault();
        var $q = $(this);
	//	var $l = $('input#s').length;

        if($q.val() == ''){
            $('div#fast').html('');
            return false;
        }

        //Abort opened requests to speed it up
        if(runningRequest){
            request.abort();
        }
		
		$("body").click(function(){
		$('div#fast').hide();
		
		});
		
	//	if($l > 1) {
		
			runningRequest=true;
			request = $.getJSON('holla4_suche.php',{
				q:$q.val()
			},function(data){           
				showResults(data,$q.val());
				runningRequest=false;
			});

	//	}
		
		
		//Create HTML structure for the results and insert it on the result div
		function showResults(data, highlight){
			var resultHtml = '';
       
		     $.each(data, function(i,item){
			 
				var img = ''
				if(item.typ == 'dual') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/da.png" /></p>'}
				if(item.typ == 'abitur') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/da.png" /></p>'}
				if(item.dual_studium_bo == '1') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/da.png" /><img src="i/ds.png" /></p>'}
				if(item.typ == 'bfs') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/sa.png" /></p>'}
				if(item.typ == 'sonstige ausbildungsberufe') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/s.png" /></p>'}
				if(item.typ == 'beamte einfacher Dienst') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/ba.png" /></p>'}
				if(item.typ == 'beamte mittlerer Dienst') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/ba.png" /></p>'}
				if(item.typ == 'Studienberufe - Beamte geh. Dienst') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/ba.png" /></p>'}
				if(item.typ == 'Studienberufe - Beamte höherer Dienst') {img = '<p style="float:left;width:40px;margin-right:5px;"><img src="i/ba.png" /></p>'}
				
                resultHtml+='<a class="afast" href="'+item.title_slug+'"><div id="fastresult" class="fastresult">';
				resultHtml+= img+'<p style="float:left;padding-top:3px;">'+item.title+'</p>';
				resultHtml+='<p style="clear:both;"></p>';
                resultHtml+='</div></a>';
	
            });
			
            $('div#fast').html(resultHtml);
			var n = $(".fastresult").length;
			//$("span").text("There are " + n + " divs." + "Click to add more.");

			//$('div#counts').text(n+" Treffer");

			}

        $('form').submit(function(e){
            e.preventDefault();
        });
    });
	
});
