$(document).ready(function(){
	$('body').css({'background-color':'#000'});/**/

	$('#search_field').focus(function(){
		if($('#search_field').val() == '')
		{
			$('#onsearch').hide();
			$('#onfocus').show();
		}

		else
		{
		    $('#onfocus').hide();
		    $('#onsearch').show();
		}
	});

	$('#search_field').blur(function(){
		$('#onfocus').hide();
		setTimeout("document.getElementById('onsearch').style.display='none'", 200);
	});

	$('#search_field').typeWatch({
		callback: function()
		{
			$('#result').html('');
			$('#onsearch').hide();

			$.ajax({type: 'POST', url: '/AjaxSearchHandler.ashx', data: "search=" + $('#search_field').val(), dataType: 'json', 
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$('#error').append('Fel!');
				}, 
				success: function(data){
					var result = '';
					var count = 0;

					$.each(data, function(i,item){
						var title = '';
						var objects = '';

						$.each(item, function(i2,item2){
							if(i2 == 'GroupName')
							{
								if(item2 == null)
								{
									item2 = "";
								}

								title = item2;
							}
							
							else
							{
								$.each(item2, function(i3,item3){
									if(true || count < 20)
									{
										$.each(item3, function(i4,item4){

											if(i4 == 'Href')
											{
												objects += "<a href='" + item4 + "'>";
											}

											else if (i4 == "Text")
											{
												objects += item4 + "</a><br/>";
											}
										});

										count++;
									}
								});
							}
						});

						if(title != '' && objects != '')
						{
							result += "<div class='sr_row'><div class='sr_left'>" + title + "</div><div class='sr_right'>" + objects + "</div></div>";
						}
					});

					if(result != '')
					{
						$('#result').html(result);
						$('#onfocus').hide();
						$('#onsearch').show();
					}
				}
			});
		},
		wait: 500,
		highlight: true,
		enterkey: true
	});
});