/*
 * Author:      Marco Kuiper (http://www.marcofolio.net/)
 */
$(document).ready(function()
		{
	// Safely inject CSS3 and give the search results a shadow
	var cssObj = { 'box-shadow' : '#aaa 5px 5px 5px', // Added when CSS3 is standard
			'-webkit-box-shadow' : '#aaa 5px 5px 5px', // Safari
			'-moz-box-shadow' : '#aaa 5px 5px 5px'}; // Firefox 3.5+	
	
	$("#suggestions").css(cssObj);

	// Fade out the suggestions box when not active
	$("input").blur(function(){
		$('#suggestions').fadeOut();
	});
		});

$(document).ready(function() {
	/*$('#searchform input#inputString').delayedObserver(function(value, object) {
		spotlight();
	}, 0.1); */
});

function spotlight() {   
	var q = $('#searchform input#inputString').val();

	if(q.length < 2) {
		$('#suggestions').fadeOut(); // Hide the suggestions box
       
	} else {    
		$.post("/spotlight", {query: q}, function(data) {  
			$('#suggestions').fadeIn();
			$('#suggestions').html('');
			      
			var mainP = $('<p></p>');
			mainP.attr('id', 'searchresults');
            
            
            var data = eval("(" + data + ")");
		

			if (data.length == 0) {
				var notfound = $('<a></a>');
				notfound.html('<span class="searchheading">' + spotlighttext['sorry'] + '</span>\n<span>' + spotlighttext['please'] + '</span>');
				mainP.append(notfound);
			}
                       
			for (var i = 0; i < data.length; i++) {
				var result = data[i];
                  
				var cat = $('<span></span>');
				cat.addClass('category');
				cat.text(result[0]);
				mainP.append(cat);

				for (var j = 0; j < result[1].length; j++) {
					var item = result[1][j]; 

					var a = $('<a></a>');
					a.attr('href', item[0]);

					if (i == 0 && j == 0) { //only add images to the products
						var thumb = $('<img>');
						thumb.attr({height: "100", width: "50", src: item[1]});
						
						a.append(thumb);
						a.addClass("product");
					}

					var sh = $('<span></span>');
					sh.addClass('searchheading');
					sh.text(item[2]);

					a.append(sh);

					var desc = $('<span></span>');
					desc.html(item[3]);

					a.append(desc);

					mainP.append(a);
				}

				if (result[2] != 'none' && i == 0) {
					var a = $('<a></a>');
					a.addClass("display-all");
					a.attr('href', result[2]);
					a.html('<strong>'+ spotlighttext['display'] +'</strong>');
					mainP.append(a);
				}
			}

			mainP.append('<span class="seperator"><a href="/view/index/'+escape(q)+'" title="Search">'+ spotlighttext['viewall'] +'</a></span><br class="break" />');
			$('#suggestions').append(mainP);   
		});
	}
}
