Shadowbox.init();

$(document).ready(function() {
	
	// attention: needs also to be changed in teamsite.js
	$('.hiddenNavigation').hide();
	
	$('.parent').click(function() {
		if ($(this).is('#activeItem')) {
			// the already active item was clicked, don't do anything
		} else {
			// close the other active item 
			$('#activeItem ul').slideToggle('slow');
			$('#activeItem a').first().toggleClass('active');
			$('#activeItem a').first().css('cursor', 'pointer');
			$('#activeItem').removeAttr('id', 'activeItem');

			// open
			$(this).find('ul').slideToggle('slow');
			$(this).find('a').first().toggleClass('active');
			$(this).find('a').first().css('cursor', 'default');
			$(this).attr('id', 'activeItem');
		}
	});

	$("#archiveFiltersSubmit").click(function(event) {
		event.preventDefault();

		$('#archiveFilterResults').html('<img src="/images/loader.gif" alt="" />');

		$.post("/index.helper.php?f=getFilteredFiles", $('#archiveFiltersForm').serialize(), function(data) {
			if (data.success) {
				$('#archiveFilterResults').html(data.html);
			} else {

			}			
		}, "json"); 
	});

	$("#archiveFiltersReset").click(function(event) {
		event.preventDefault();
		$('.filterSelection').attr("checked", false).button("refresh");
		$('#archiveFilterResults').html("");
		
	});
	
	var options = {
			callback : function(){
				$('#searchTeamStatus').html('<img src="/images/loading.gif" alt="" class="icon" />');
				$.post("/index.helper.php?f=searchTeams", $('#searchTeamForm').serialize(), function(data) {
					if (data.success) {
						$('#teamListing').html(data.content);
						$('#searchTeamStatus').html('<img src="/images/loading_spacer.gif" alt="" class="icon" />');
					}
				}, "json");
			},
			wait : 500,
			highlight : true,
			captureLength : 2
	};
	
	$('#searchTeam').typeWatch(options);
	$('#searchTeamForm').submit(function(event) {
		event.preventDefault();
	});

	$('.filterSelection, #archiveFiltersSubmit, #archiveFiltersReset, #teamLoginButton, #loginSubmit, .overviewLetter, #searchTeamReset').button();
	
	// load the results and upcoming games
	if ($('#gamesContent').length > 0) {
		$.post("/upcomingGames.helper.php", function(data) {
			$('#gamesContent').hide().html(data.html).fadeIn();
		}, "json"); 
	}

	if ($('#resultsContent').length > 0) {
		$.post("/latestResults.helper.php", function(data) {
			$('#resultsContent').hide().html(data.html).fadeIn();
		}, "json"); 
	}
			
});
