function updatePage(html)
 {
	window.setTimeout(function() {
		$('#ajax-content').html(html);
	},
	750);
}

function showBusy() {
	$('#ajax-content').block({
		message: '<img src="' + CI_ROOT + '/www/img/ajax-loader.gif" style="border: none" />',
		css: {
			border: 'none',
			background: 'none'
		},
		overlayCSS: {
			backgroundColor: '#FFFFFF',
			opacity: 0.6
		}
	});
}

function showCompleted() {
	window.setTimeout(function() {
		$('#ajax-content').unblock();
	},
	750);
}

function loadGoogleMaps() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById('googleMaps'));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		var latlng = new GLatLng(51.070122, 4.032082);
		var marker = new GMarker(latlng);
		map.setCenter(latlng, 14);
		map.addOverlay(marker);
		GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml('<b>Atletiekclub Zele</b>'); });
	}
}

function replaceEmptyTables()
{
	$('table').each(function() {
		var rows = $(this).find('tbody').find('tr').size();
		var hasFoot = $(this).find('tfoot').find('tr').size() > 0;
		
		if (rows == 0 && !hasFoot) {
			$(this).replaceWith("<p class='note'>Geen gegevens beschikbaar</p>");
		}
	});
}

function centerSponsorImages()
{
	var totalHeight = $('.sponsorImage').height();
	
	$('.sponsor').each(function() {
		var image = $(this).find('.sponsorImage').find('img');
		var height = image.height();
		image.css('margin-top', (totalHeight - height) / 2);
	});
}

/** On DOM-loaded */

$(function() {
	$.backstretch('/www/img/backgrounds/5.jpg');
	
	replaceEmptyTables();
	
	$('.paginate a, #infoSeason').live('click',
	function(event) {
		event.preventDefault();
		var link = $(this).attr('href');
		if (link != '#') {
			$.ajax({
				url: link,
				type: 'GET',
				dataType: 'html',
				beforeSend: function() {
					showBusy();
				},
				success: function(html) {
					updatePage(html);
				}
			});
		}
	});
	
	$('.tabContent').hide();
	$('.tabContent:first').show();
	
	$('a.tab').click(function (event) {
		event.preventDefault();
		
		tab = $(this);
		
		if (!tab.hasClass('active')) {
			previous = $('a.active').attr('title');
			$('.active').removeClass('active');
		
			// switch this tab on
			tab.addClass('active');
		
			// slide all content up
			$('#' + previous).fadeOut(100, function() {
				var content_show = $(tab).attr('title');
				$('#' + content_show).fadeIn(100);
			});
		}
	});
	
	$('table.news > tbody > tr').live('click', function() {
		var href = $(this).find('a').attr('href');
		if (href) {
			window.location = href;
		}
	});
	
	$('table.records > tbody > tr').hover(
		function() {
			place = $(this).find('.place').hide();
			report = $(this).find('.report').show();
		}, function() {
			report = $(this).find('.report').hide();
			place = $(this).find('.place').show();
		}
	);
	
	if($('#googleMaps').length) {
		loadGoogleMaps();
	}
	
	if(jQuery().cycle) {
		$('#images img:first').fadeIn(800, function() {
			$('#images').cycle({
				fx: 'fade',
				speed: 800, 
		 		timeout: 5000 
			});
		});
		
		$('#headerImages').cycle({
			fx: 'fade',
			speed: 800, 
			timeout: 5000 
		});
	}
	
	if(jQuery().fancybox) {
		$('a.fancybox').livequery(function(){
			$(this).fancybox({
				'titlePosition': 'outside',
				'overlayColor': '#1D1D1D',
				'overlayOpacity': 0.95
			});
		});
		
		$('a.fancybox_yt').livequery(function(){
			$(this).fancybox({
				'titlePosition': 'outside',
				'overlayColor': '#1D1D1D',
				'overlayOpacity': 0.95,
				'padding': 0,
				'autoScale': false,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'title': this.title,
				'width': 680,
				'height': 495,
				'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type': 'swf',
				'swf': {
					'wmode': 'transparent',
					'allowfullscreen': 'true'
				}
			});
		});
	}
});

$(window).load(function() {
	centerSponsorImages();
});
