jQuery(document).ready(function($)
{
    $('a[rel*=facebox]').facebox();


	// Puts the cursor in the first input field when there's no value there
	$(function()
	{
	    if($('.query').val() == "")
	    {
	        $('.query:first').focus();
	    }
	});

	$(".search-form input.query").autocomplete(
		"/search/autocomplete-terms/",
		{
			delay:10,
			minChars:3,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectSearchItem,
			onFindValue:findSearchValue,
			formatItem:formatSearchItem,
			autoFill:true
		}
	);
	
	$(".sort-show select").change(function() {
		var myform = $(this).parent();
		myform.submit();
	});
	
	$(".search-form .query:first").focus();
});

function findSearchValue(li) {
	if( li == null ) return alert("No match!");


	addTag(li.innerHTML);
}

function selectSearchItem(li) {
	window.location="/search/index/query/"+li.innerHTML;
}

function formatSearchItem(row) {
	return row[0];
}

function ilikethis(obj, type, type_id, type_source, target_user)
{
	$.ajax({
    	url: "/like/create",
      	type: "GET",
      	data: "type="+type+"&type_id="+type_id+"&type_source="+type_source+"&target_user="+target_user,
      	dataType: "json",
      	success: function(data){
			// If sucessful, update the like count on the sibling span
			if(data.message == 'success')
			{
				$(obj).css('background-position', '0 0');
				count = parseInt($(obj).siblings('span').html())+1;
				$(obj).siblings('span').html(count+'');
			}
			$(obj).attr('onclick', 'return false;');
			$(obj).css('cursor', 'default');
      	}
   });
}
