$(document).ready(function(){

	$('.contacts tbody tr:even').css('background-color', '#dddddd');
	$('.contacts tbody tr:odd').css('color', '#1545a5');
 	
	$('.semitable tbody').hide();
	$('.semitable thead').click(function(){
		$(this).next().toggle();
	});

	$('#pics').cycle({
    fx: 'scrollLeft',
			speedIn: 500,
			speedOut: 200,
			timeout: 2500,
			next: '#pics',
			pause: 1
	});

	$('#stop').click(function(){
		$('#pics').cycle('stop');
	});

	$('#restart').click(function(){
		$('#pics').cycle({
    fx: 'scrollLeft',
			speedIn: 500,
			speedOut: 200,
			timeout: 2500,
			next: '#pics',
			pause: 1
	});
	});

	$('a.lightbox').click(function(e){
		$('body').css('overflow-y', 'hidden');

		$('<div id="overlay"></div>')
			.css('top', $(document).scrollTop())
			.css('opacity', '0')
			.animate({'opacity': '0.6'}, 'slow')
			.appendTo('body');

		$('<div id="lightbox"></div>')
      .hide()
      .appendTo('body');

		$('<img />')
			.attr('src', $(this).attr('href'))
			.load(function(){
				positionLightboxImage();
			})
			.click(function(){
				removeLightbox();
			})
			.appendTo('#lightbox');

		return false;
	});

	function positionLightboxImage(){
		var top = ($(window).height() - $('#lightbox').height()) / 2;
		var left = ($(window).width() - $('#lightbox').width()) / 2;
		$('#lightbox')
			.css({
				'top': top + $(document).scrollTop(),
				'left': left
			})
			.fadeIn(2000);
	}

	function removeLightbox(){
		$('#overlay, #lightbox')
			.fadeOut(2000, function() {
				$(this).remove();
				$('body').css('overflow-y', 'auto');
			});
}

$('a[rel=marwin]').colorbox({
		transition: 'fade',
		speed: 500,
		current: "{current} of {total} March Cup photos"
	});

	rotatePics(1);

});

function rotatePics(currentPhoto) {
  var numberOfPhotos = $('#photos img').length;
  currentPhoto = currentPhoto % numberOfPhotos;
	
  $('#photos img').eq(currentPhoto).fadeOut(function() {
		// re-order the z-index
    $('#photos img').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfPhotos - i) + currentPhoto) % numberOfPhotos
      );
    });
    $(this).show();
    setTimeout(function() {rotatePics(++currentPhoto);}, 4000);
  });
}
