var hero_delay = 5;
var hero_speed = 2000;
var hero_timer = null;
var hero_end   = $('#home ul li').length;
var hero_index = 0;
function next_slide()
{
	if (hero_index > hero_end-2)
	{
		$('#home ul li').not(':eq('+hero_index+')').hide();
		$('#home ul li:eq(0)').show();
		$('#home ul li:eq('+hero_index+')').fadeOut(hero_speed);
		hero_index=0;
	}
	else
	{
		hero_index++;
		$('#home ul li:eq('+hero_index+')').fadeIn(hero_speed);
	}
	hero_timer = setTimeout(next_slide, hero_delay*1000);
}
function doSearch(t, q)
{
	clearTimeout(search_timer);
	$.getJSON('main/search/'+t, {q:q}, function(data) {
		if (data.rsp == 'ok')
		{
			if (data.users != null)
			{
				$('#user_list').fadeOut('fast', function() {
					$(this).html(data.users).fadeIn('fast');
				});
			}
			else if (t != 'blitz')
			{
				$('#user_list').fadeOut('fast', function() {
					$(this).html('<\li>No results. Try a different search term.<\/li>').fadeIn('fast');
				});
			}
			
			if (data.blitzes != null)
			{
				$('#blitz_list').fadeOut('fast', function() {
					$(this).html(data.blitzes).fadeIn('fast');
				});
			}
			else if (t != 'user')
			{
				$('#blitz_list').fadeOut('fast', function() {
					$(this).html('<\li>No results. Try a different search term.<\/li>').fadeIn('fast');
				});
			}
		}
	});
}
var search_timer = null;
var search_prev  = '';
var search_type_prev = '';
$(function() {
	next_slide();
	doSearch('both', '');
	$('#content input').keyup(function() {
		var $t = $(this);
		var type = $t.attr('id').split('_')[1];
		var q = $t.val();
		if ((q != search_prev || type != search_type_prev ) && (q == '' || q.length >= 3))
		{
			clearTimeout(search_timer);
			$('#'+type+'_list').html('<\li><\img src="public/images/ajax-loader.gif" alt="Loading..." /><\/li>');
			search_timer = setTimeout(function() {
				doSearch(type, q);
			}, 500);
			search_prev = q;
			search_type_prev  = type;
		}
	}).focus(function() {
		//this.select();
	});
	setTimeout(fade_in, 1000);
	$('#steps a').hover(function() {
		$(this).stop().addClass('hover').fadeTo(500, 1);
	}, function() {
		$(this).removeClass('hover');
		fade_out();
	}).css({ opacity: 0, display: 'block'});
});
function fade_in()
{
	$('#steps a:not(.hover)').fadeTo(500, 1, fade_out);
}
function fade_out()
{
	$('#steps a:not(.hover)').fadeTo(1500, 0, fade_in);
}