$(document).ready(function(){
	$('ul.categoryList li div.catName').click(function(){
		if ( $(this).hasClass('inactive') )
		{
			$('ul.categoryList li div.catName').removeClass('active').addClass('inactive')
				.siblings('ul.games').slideUp();
			$(this).removeClass('inactive').addClass('active')
				.siblings('ul.games').slideDown();
		}
		else // is already active, so collapse
		{
			$(this).removeClass('active').addClass('inactive').siblings('ul.games').slideUp();
		}
	});
	
	
	// top scores
	$('div.topScores .content .tab a').click(function(){
		if ( $(this).hasClass('active') )
		{
			return false;
		}
		
		var dataObj = {
			when:$(this).attr('class'),
			type:'',
			game_name:$(this).parents('div.topScores').children('.game_name').text()
		}
		
		if ( $(this).parents('.tab').hasClass('myFriendsTab') )
		{
			dataObj.type = 'friends';
		}
		else
		{
			dataObj.type = 'everyone';
		}
		
		$('div.topScores .content .tab a').removeClass('active');
		$('div.topScores .content .tab').removeClass('activeTab');
		$('div.topScores .content .section').removeClass('activeSection');
		$('div.topScores .content .scores').removeClass('activeScores');
		$(this).addClass('active');
		$(this).parents('.tab').addClass('activeTab');
		if ( $(this).parents('.tab').hasClass('everyoneTab') )
		{
			var requestedScoresDiv = $(this).parents('.tab').siblings('.everyone').addClass('activeSection').find('.scores.' + dataObj.when);;
		}
		else if ( $(this).parents('.tab').hasClass('myFriendsTab') )
		{
			var requestedScoresDiv = $(this).parents('.tab').siblings('.myFriends').addClass('activeSection').find('.scores.' + dataObj.when);
		}
		requestedScoresDiv.addClass('activeScores');
		
		if ( $.trim( requestedScoresDiv.html() ) == '' )
		{
			requestedScoresDiv.text('please wait...');
			
			$.ajax({
				url : '/ajax/landingTopScores',
				data : dataObj,
				type: 'post',
				dataType: 'json',
				success : function( data ) {
					$('div.topScores .content .activeScores').html( data.markup );
				}
			});
		}
		
		return false;
	});
	$('div.topScores .content .tab').click(function(){
		if ( !$(this).hasClass('activeTab') )
		{
			$(this).find('a.today:not(.active)').click();
		}
	});
});