$(function()
{
	
	$('form#searchform a').bind('click',search.kick);
	
	$("#searchform").submit(function()
		{
			if ($("#searchform input:first").val() != '')
				{
					$('form#searchform a').trigger('click');
					return false;
				}
			return false;
		});
	
});


search =
{
	kick: function()
		{
			var term = $('input#searchinput').val();
			if (term.length >= 3)
				{
					search.query(term);
				}
			return false;
		},
		
	query: function(term)
		{
			
			search.req = $.ajax({
				url: "content/home/suchenAjax.php",
				data: "term="+term,
				dataType: "json",
				beforeSend: function()
					{
						$('div#results').html('<img src="lib/images/layout/loader.gif" id="loading" />');
						$('img#loading').css('display','block');
					},
				success: function(json)
					{
						$.each(json.results, function(i, item)
							{
								$('div#results').append('<a href="'+item["fid"]+'.html"><div class="img"><img src="files/thum/'+item["fid"]+'.jpg" /></div></a>');
								
								
								
								//console.log(json.results[i]);
								
							});
						
							$('div#results').append('<div class="clearboth"></div>');
						
							var newImages = $('div#results div.img');
							$.each(newImages, function(i, singleImage)
								{
										setTimeout(function()
											{
												$(newImages).eq(i).fadeIn(600)
											},150*i);
											
								});
					},
				complete: function()
					{
						$('img#loading').remove();
						if ($('div#results').html() == '')
							{
								$('div#results').html('Keine Treffer');
							}
					}
			});
		}
};