    $(document).ready(function(){
        $('#search').keyup(function() { 
            delay(function(){
              getResults();
            }, 400 );
        });
	$('#search').keyup(function(e) { if(e.keyCode == 13) { window.location = "/view/category/all/?searchTerm=" + $('#search').attr('value') + "&filterType=all"; } });
       $('#search').click(function() {  getResults(); if($('#search').attr('value')=="Wonach suchst du?" || $('#search').attr('value')=="What are you looking for?") { $('#search').attr('value',""); } });
	$('.btn-submit').click(function() { window.location = "/view/category/all/?searchTerm=" + $('#search').attr('value') + "&filterType=all"; });

	$('body').click(function() { $(".searchcontainer").addClass("hidden"); });
	$(".searchcontainer").click(function() {  getResults(); });
    });

function getResults() {
	$("#search").addClass("loader");

   $.getJSON('http://mars.moreboards.com/cakephp/ajaxsearch/index/' + top.location.host +'/' + $("#search").attr('value') + $.contextPath, function(data) {
	$(".searchcontainer").removeClass("hidden");
  	var items = [];

	if(!data['none']=="") {
		items.push('<table><tr><td class="first" colspan="4">Leider konnte kein exakter Treffer erzielt werden, aber vielleicht ist es ja eines der unten stehenden Produkte, eine der Kategorien oder eine Marke<br><br></td></td>');
		items.push('<table><tr><td></td><td class="first"><b>Produkt</b></td><td><b>Kategorie</b></td></tr>');
		$.each(data['none'], function(i,product) {
			items.push("<tr><td align='center' width='60px'><a href='" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'><img src='http://www.moreboards.com/medias/sys_master/" + product.Ajaxsearch.BildPreview + "/image.jpg' border='0' /></td><td><a href='http://www.moreboards.com" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'><b>" + product.Ajaxsearch.Brand + "</b><br>" + product.Ajaxsearch.Name + "</a></td><td>" + product.Ajaxsearch.Cat + "</td></tr>");
	  	});
	} else {
		items.push('<table><tr><td></td><td class="first"><b>Produkt</b></td><td><b>Kategorie</b></td></td>');
	}

	  if(!data['first']=="") {
	  	$.each(data.first, function(i,product) {
			items.push("<tr><td align='center' width='60px'><a href='" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'><img src='http://www.moreboards.com/medias/sys_master/" + product.Ajaxsearch.BildPreview + "/image.jpg' border='0' /></a></td><td><a href='" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'><b>" + product.Ajaxsearch.Brand + "</b><br>" + product.Ajaxsearch.Name + "</a></td><td>" + product.Ajaxsearch.Cat + "</td></tr>");
	  	});
	  }
	  
	  if(!data['more']=="") {
		  $.each(data['more'], function(i,product) {
				items.push("<tr><td align='center' width='60px'><img src='http://www.moreboards.com/medias/sys_master/" + product.Ajaxsearch.BildPreview + "/image.jpg' /></td><td><a href='" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'><b>" + product.Ajaxsearch.Brand + "</b><br>" + product.Ajaxsearch.Name + "</a></td><td>" + product.Ajaxsearch.Cat + "</td></tr>");
		  });
	  }
	  
	  if(!data['similar']=="") {
		  items.push("<tr><td colspan='4'><br><b>&auml;hnliche Produkte</b></td>");
		  $.each(data['similar'], function(i,product) {
				items.push("<tr><td colspan='2'><a href='" + $.contextPath + "/view/product/details/" + product.Ajaxsearch.Code + "'>" + product.Ajaxsearch.Brand + ": " + product.Ajaxsearch.Name + "</a></td><td>" + product.Ajaxsearch.Cat + "</td></tr>");
		  });
	  }
	  
	  items.push("<tr><td colspan='4'><br><b>Kategorien & Markenshops</b></td>");
	  $.each(data['Kategorie'], function(i,code) {
			items.push("<tr><td colspan='2'><a href='" + $.contextPath + "/view/category/all/" + code + "'>" + i + "</a></td><td>Kategorie</td></tr>");
	  });
	  
	  $.each(data['Marken'], function(i,product) {
			items.push("<tr><td colspan='2'><a href='/" + i + "'>" + i + "</a></td><td>Marke</td></tr>");
	  }); 
	  
	  
	items.push('</table>');
	$('.searchcontainer').empty();
  	$('<div/>', { 'class': 'result',  html: items.join('')}).appendTo('.searchcontainer');
	$("#search").removeClass("loader");

});

}

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();
