$('document').ready(function() {
	clearForms();
	
	tabSwitcher();
	//$('#olympics').click();
});


function tabSwitcher()
{
	$(".tabswitch").live('click', function() {
		
		var tab_id;
		var id_name = this.id;
		
		switch(id_name)
		{
			case 'interests':
				tab_id = 1;
				break;
			case 'cities':
				tab_id = 2;
				break;
			case 'olympics':
				tab_id = 3;
				break;
		}
		
		$.ajax(
			{
				url: 'ui/category/' + tab_id,
				cache: false,
				success: function (data)
					{
						$('#johnny').hide(); //hide("slide", {direction: "left"}, 1000);
						var urli = 'url(http://followformation.com/images/1' + ((tab_id == 1) ? '' : '-' + tab_id) + '.png)';
						$('#one').css('background-image', urli);
						
						$('#johnny').html(data);
						$('#johnny').show(); //show("slide", {direction: "left"}, 1000);
						
						if (id_name != 'interests')
						{
							$("#custom_category").hide();
						}
						else
						{
							$("#custom_category").show();
						}
						
						init_tooltip();
					}
			}
		);
		
	});
}

function followFormation()
{
	
	var categories = new Array();
	
	$('#johnny input:checked').each( function() {
		categories.push(this.value);
	});
	
	if (categories.length)
	{
		if (categories.length * $('#num_people').val() <= 150)
		{
			$('#infobox').html('<img src="images/loader.gif" /> Getting into formation...');
			$('#submit_button').css({disabled:true});
			_follow(categories);
		}
		else
		{
			$('#infobox').html('Try to follow under 100 people!');
		}
	}
	else
		$('#infobox').html('Please select at least one category.');
}

function _follow(categories)
{
	var tag = categories.pop();
	
	$.post(
		'index.php/app/follow/' + tag,
		{ username: $('#username').val(), password: $('#password').val(), to_follow: $('#num_people').val() },
		function (data) {
			if (data.indexOf('incorrect') != -1)
			{
				$('#infobox').html('Oops! Your username or password is incorrect.');
				$('#submit_button').css({disabled:false});
			}
			else
			{
				$('#infobox').html('<img src="images/loader.gif" /> Followed the category: ' + tag);
				
				if (categories.length)
					_follow(categories);
				else
				{
					/*var left = 4;
					$('#one').slideUp('slow', function() { left--; if (left == 0) { getSuccess(); } });
					$('#two').slideUp('slow', function() { left--; if (left == 0) { getSuccess(); } });
					$('#three').slideUp('slow', function() { left--; if (left == 0) { getSuccess(); } });
					$('#final').slideUp('slow', function() { left--; if (left == 0) { getSuccess(); } });*/
					
					$('#bodycontain').fadeOut('slow', function() { getSuccess(); });
				}
					
			}
		}
	);
}

function getSuccess()
{
	$.ajax(
		{
			url: 'app/success/' + $('#username').val(),
			cache: false,
			success: function (data)
				{
					$('#bodycontain').html(data).fadeIn();//slideDown();
				}
		}
	);
}

function clearForms()
{
	$('#johnny input:checked').each(function() {
		this.attr('checked', false);
	});
}