$(function(){
	// intro
	var href = $('#home a').attr('href');
	
	$('#home a').click(function(){
		$(this).fadeOut(700, function(){
			window.location = href;
		});
		return false;
	});
	
	
	// news
	$(window).load(function(){
		var width = $(window).width();
		var height = $(window).height();

		if(width > 1000)
		{
			var height = $(window).height();
			var newHeight = height - ($('header').outerHeight() + 10);
			$('#news li img').css('height', newHeight);
		}

		if($.browser.webkit || $.browser.opera)
		{
			setTimeout(function(){
				if(width > 1000)
				{
					var height = $(window).height();
					var newHeight = height - ($('header').outerHeight() + 10);
					$('#news li img').css('height', newHeight);

				}
			}, 500);
		}

		var resizeTimer;
		$(window).resize(function(){
			clearTimeout(resizeTimer);

			width = $(window).width();
			height = $(window).height();

			resizeTimer = setTimeout(function(){
				if(width > 1000)
				{
					var newHeight = height - ($('header').outerHeight() + 10);
					$('#news li img').css('height', newHeight);
				}
			}, 100);
		});
		
		
		// bio
		if(window.location.hash == '#bio')
		{
			var bioVisible = true;
			showBio();
		}
		else
		{
			var bioVisible = false;
		}
		
		$('#bio-btn').click(function(e){
			if(bioVisible) hideBio();
			else showBio();
			
			e.preventDefault();
		});
		
		$('#close-bio').click(function(e){
			hideBio();
			e.preventDefault();
		});
		
		function showBio()
		{
			bioVisible = true;
			window.location.hash = '#!bio';
			
			$('#bio').show().css('opacity', 0).animate({
				left: $('#news img').offset().left + 50,
				opacity: 1
			}, 500);
		}
		
		function hideBio()
		{
			bioVisible = false;
			
			window.location.hash = '';
			
			$('#bio').animate({
				left: '0%',
				opacity: 0
			}, 500);
		}
	});
	
	
	// everything else
	setTimeout(function(){
		var width = $(window).width();
		var height = $(window).height();
		var firstPhotoWidth = $('#photos-container img:eq(0)').innerWidth();

		if(width > 1000)
		{			
			setTimeout(function(){
				firstPhotoWidth = $('#photos-container img:eq(0)').width();
				var winCenter = (width / 2) - (firstPhotoWidth / 2);
				$('#photos-container').css('left', winCenter);
			}, 100);

			var newHeight = height - ($('header').outerHeight() + 10);
			$('#photos-container img').css('height', newHeight);
		}
		else
		{
			$('#photos-container').css('left', 0);
		}

		var current = 0;
		var num = $('#photos-container img').length;

		// left - right
		function goPrev()
		{
			if(current - 1 < 0)
			{
				var containerWidth = 0;
				$('#photos-container img:lt(' + (num - 1) + ')').each(function(){
					containerWidth += $(this).width();
				});
				var newPos = containerWidth;

				$('#photos-container').animate({
					'left': '-=' + newPos
				}, 1000);

				current = num - 1;
			}
			else
			{
				var oldWidth = $('#photos-container img:eq(' + current + ')').width();
				var newWidth = $('#photos-container img:eq(' + (current - 1) + ')').width();
				var newPos = (oldWidth / 2) + (newWidth / 2) + 3;

				$('#photos-container').animate({
					'left': '+=' + newPos
				}, 500);

				current--;
			}
		}
		
		function goNext()
		{
			if(current + 1 == num)
			{
				width = $(window).width();
				firstPhotoWidth = $('#photos-container img:eq(0)').width();
				var winCenter = (width / 2) - (firstPhotoWidth / 2);

				$('#photos-container').animate({
					'left': winCenter
				}, 1000);

				current = 0;
			}
			else
			{
				var oldWidth = $('#photos-container img:eq(' + current + ')').width();
				var newWidth = $('#photos-container img:eq(' + (current + 1) + ')').width();
				var newPos = (oldWidth / 2) + (newWidth / 2) + 3;

				$('#photos-container').animate({
					'left': '-=' + newPos
				}, 500);
				
				current++;
			}
		}
		
		$('#prev').click(function(){
			goPrev();

			return false;
		});

		$('#next').click(function(){
			goNext();

			return false;
		});
		
		$(document).keypress(function(e){
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 37) goPrev();
			else if(code == 39) goNext();
		});

		// resize
		var resizeTimer;
		$(window).resize(function(){
			clearTimeout(resizeTimer);

			width = $(window).width();
			height = $(window).height();

			resizeTimer = setTimeout(function(){
				if($(window).width() > 1000)
				{
					var firstPhotoWidth = $('#photos-container img:eq(0)').width();
					var winCenter = (width / 2) - (firstPhotoWidth / 2);
					$('#photos-container').css('left', winCenter);

					var newHeight = height - ($('header').outerHeight() + 10);
					$('#photos-container img').css('height', newHeight);

					current = 0;
				}
				else
				{
					$('#photos-container').css('left', 0);
					$('#photos-container img').css('height', 'auto');
				}
				
				if($('#bio').length > 0) $('#bio').css('left', $('#news img').offset().left + 50);
			}, 100);
		});
	}, 100);
	
	
	// infos
	if($('.infos').length > 0)
	{
		var bubble = $('<div id="bubble"/>');
		$(bubble).appendTo('#photos-container');
		
		$('#photos-container img').hover(
			function(){
				var pos = $(this).position();
				
				var text = $(this).attr('alt');
				$(bubble).css({
					width: $(this).width() - 30,
					left: pos.left + 10,
					top: $(this).height() - ($(bubble).height() + 20) 
				}).text(text).fadeIn(300);
			},
			function(){
				$(bubble).hide();
			}
		);
	}
	
	
	// no right click so you won't use these photos for a Nike campaign
	$('#news, #photos').bind('contextmenu', function(e){
		e.preventDefault();
	});
	
	
	// iPhone
	var agent = navigator.userAgent.toLowerCase();
	var isIphone = (agent.indexOf('iphone') != -1);

	if(isIphone)
	{
		$('a').click(function(){
			window.location = $(this).attr('href');
			return false;
		});
	}
});
