$("document").ready(function() {
	$("#do_search").click(search);
	$(".unixdate").each(changeCommentDate);
});
function search()
{
	var search_word = $("#search_word").val();
	
	if(search_word.length < 3) 
	{
		if(search_word.length == 0)
		{
			$('#blog').html('<h1>Поиск:</h1><h1>Введите слово для поиска</h1>');
		}
		else
		{ 
			$('#blog').html('<h1>Поиск:</h1><h1>Поисковый запрос слишком короткий</h1>');
		}
	}
	else {
	$.ajax({
        url: '/ajax/blogsearch',
        crossDomain: true,
        dataType: 'json',
        data: {search_word: search_word},
        type: 'POST',
        beforeSend: function () { ajaxLoader();},
        complete: function () { ajaxLoaderHide(); },
        success: function(data)
        {	
			var num = data.length;      
			if(num == 0)
			{
				$('#blog').html('<h1>Поиск:</h1><h1>Ничего не найдено</h1>');
			}
			else
			{ 
				var result = '';
				for(i=0; i<num; i++)
				{
					result += '<div class="article"><h1><a href="/article/' + data[i].blog_id + '">' + data[i].title;
					result += '</a></h1><div class="date"><a href="/id' + data[i].user_id + '" >' + data[i].first_name;
					result += ' ' + data[i].last_name + '</a> <span class="unixdate">' + data[i].date + '</span></div>';
					if(data[i].article_img)
						result += '<img src="/foto/upload/article/' + data[i].article_img + '" />';
					result += data[i].short_text + '<div class="clear"></div><div class="date"><a href="/article/'
					result += data[i].blog_id + '">Подробнее... </a><a href="/article/' + data[i].blog_id;
					result += '/#comments">Комментарии </a></div></div>';
					
					
				}
				$('#blog_comments').empty();
				$('#blog').html(result);
				$(".unixdate").each(changeCommentDate);
			}    
		},
		error: function()
		{
			alert('fail');
		}
	});
	}
	return false;
}
function ajaxLoader()
{
	$("#loader").show();
}
function ajaxLoaderHide()
{
	$("#loader").hide();
}

