// The scrolling duration is calculated based on the distance from the current position to the top/bottom.

var distance = $('#main').height()-20;
	
function scrollUp(elem)
{
	if (elem.position().top < 0)
	{
		if ($('#gallery').length > 0)
		{
			distance = $('#gallery img:first').height();
			if ($('.inner').position().top >= 0)
				distance -= 70;
		}
		
		var goTo = '+='+distance;
		if (elem.position().top+distance > 0)
			if ($('#content.bs_news').length > 0)
				goTo = 100;
			else
				goTo = 0;
		
		elem.animate({
			top: goTo,
		}, 300, 'linear', function()
		{
			if ($('#gallery').length > 0)
				showInfo();
		});
	}
}

function scrollDown(elem, containerHeight)
{
	if (Math.abs(elem.position().top-containerHeight) <= Math.abs(elem.height()))
	{
		if ($('#gallery').length > 0)
		{
			distance = $('#gallery img:first').height();
			if ($('.inner').position().top >= 0)
				distance -= 70;
		}
		
		var goTo = '-='+distance;
		if (Math.abs(elem.position().top-distance-containerHeight) > elem.height())
			goTo = '-='+(elem.height()+elem.position().top-containerHeight);
		
		elem.stop().animate({
			top: goTo,
		}, 300, 'linear', function()
		{
			if ($('#gallery').length > 0)
				showInfo();
		});
	}
}

function showInfo()
{
	var currentPosition = Math.abs($('#main .inner').position().top);
	var containerHeightMiddle = Math.abs($('#main').height()/2);
	var measurePoint = currentPosition + containerHeightMiddle;
	
	// Check every element if it is the visible
	$('#gallery li').each(function()
	{
		var elemPosTop = Math.abs($(this).position().top)
		var elemHeight = $(this).height();
		// The current element is visible when its top has passed the middle of the gallery's viewport
		// an when its bottom position is not above the middle of the gallery's viewport
		// This is huge! :P
		if (elemPosTop < measurePoint && elemPosTop + elemHeight > measurePoint)
		{
			$('#info').empty().html($(this).children('aside').html());
			return false;
		}
	});
}

$(document).ready(function()
{
	// Newsletter
	$('#newsletter label').addClass('over');
	
	if ($('#newsletter_email').val() != '')
		$('#newsletter label').hide();
		
	$('#newsletter_email').focus(function()
	{
		$('#newsletter label').hide();
	});
	
	$('#newsletter_email').blur(function()
	{
		if(this.value == '')
			$('#newsletter label').show();
	});
	
	$('#newsletter form').submit(function(e)
	{
		e.preventDefault();
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = $('#newsletter_email').val();
		if(reg.test(address) == false)
		{
			alert('Please enter a valid email address.');
		}
		else
		{
			$.ajax({
				url: '/wp-content/themes/boessert-und-schorn/newsletter.php',
				type: "POST",
				data: ({address : address}),
				success: function(data){
					alert(data);
				}
			});
		}
	});
	
	// Content scrolling
	$('#content > .up').click(function(e)
	{
		e.preventDefault();
		scrollUp($('.inner'));
	});

	$('#content > .down').click(function(e)
	{
		e.preventDefault();
		scrollDown($('.inner'), $('#main').height());
	});
	
	// Gallery image info
	if ($('#content.archive, #content.current_collection').length > 0)
		$('#info').empty().html($('.inner li:first aside').html());
	
	// Gallery image navigation
	$('.gallery-navigation .thumbs a').click(function(e)
	{
		e.preventDefault();
		
		var imgID = $(this).attr('href').replace('#', '');
		var position = $('.inner li#'+imgID).position();
		var scrollToImg = (-position.top == 0) ? 0 : -position.top+70;

		var distance = Math.abs(Math.abs(position.top)-Math.abs($('#main .inner').position().top));
		var duration = distance/3;
				
		$('.inner').stop().animate({
			top: scrollToImg,
		}, duration, 'swing', function()
		{
			$('#info').empty().html($('.inner li#'+imgID+' aside').html());
		});
	});
	
	// Gallery image navigation scrolling
	$('.gallery-navigation > .up').click(function(e)
	{
		e.preventDefault();
		scrollUp($('.thumbs'));
	});
	
	$('.gallery-navigation > .down').click(function(e)
	{
		e.preventDefault();
		scrollDown($('.thumbs'), $('.gallery-navigation').height()-100);
	});
	
});
