function switchFoto(showKat, showFoto, clickFoto)
{
	if(showLayer == 1)
	{
		hideLayer = 1;
		showLayer = 2;
	}
	else
	{
		hideLayer = 2;
		showLayer = 1;
	}
	$('#' + clickFoto).children().attr('src', '/img/' + clickFoto + '_ani.gif');
	$('#foto' + showLayer).load('/index.php/ajax/fotoloader/' + showKat + '/' + showFoto, '',
	function()
	{
		document.location.href = document.location.href.substr(0, document.location.href
		.indexOf('#'))
		+ '#' + showFoto;
		$('#foto' + hideLayer).fadeOut('slow');
		$('#foto' + showLayer).fadeIn('slow');
		$('#' + clickFoto).children().attr('src', '/img/' + clickFoto + '.gif');
	});
}

$(document).ready(function()
{
	$('a').click(function()
	{
		this.blur();
	});
	$('.hoverimg').hover(function()
	{
		$(this).parent().children('img').each(function()
		{
			if($(this).attr('src').indexOf('_') < 0)
				$(this).attr(
				'src',
				$(this).attr('src').substr(0, $(this).attr('src').lastIndexOf('.')) + '_on'
				+ $(this).attr('src').substr($(this).attr('src').lastIndexOf('.')));
		});
	}, function()
	{
		$(this).parent().children('img').each(function()
		{
			if(document.URL.indexOf($(this).parent().attr('href')) < 0
			|| $(this).parent().attr('href').indexOf('#') > 0)
				$(this).attr('src', $(this).attr('src').replace('_on', ''));
		});
	});
	if(document.location.href.indexOf('fotos') > 0)
	{
		$('#foto1')
		.before('<div id="back"><a href="#"><img src="/img/blank.gif" width="60" height="360" alt="Vorheriges Foto" /></a></div>');
		$('#foto1')
		.before('<div id="next"><a href="#"><img src="/img/blank.gif" width="60" height="360" alt="N&auml;chstes Foto" /></a></div>');

		fotTotal = parseInt($('#minus').attr('class'));
		fotHash = parseInt(document.location.hash.replace('#', ''));

		if(!document.location.hash)
		{
			fotNum = document.location.href.substr(document.location.href.lastIndexOf('/') + 1)
			.replace(document.location.hash, '');
			document.location.hash = parseInt(fotNum) > 0 ? '#' + fotNum : '#' + fotTotal;
			fotHash = parseInt(fotNum) > 0 ? fotNum : 1;
		}
		else
		{
			if(fotHash > fotTotal) fotHash = 1;
			$('#foto1').load('/index.php/ajax/fotoloader/' + $('#foto1').attr('class') + '/'
			+ fotHash);
			document.location.hash = fotHash;
		}
		fotNext = fotHash - 1;
		if(fotNext <= 0) fotNext = fotTotal;
		fotPrev = fotHash + 1;
		if(fotPrev > fotTotal) fotPrev = 1;

		fotKat = $('#foto1').attr('class');
		$('#foto1').after('<div id="foto2"></div>');
		$('#foto2').hide();
		showLayer = 1;

		$('#back').click(function()
		{
			fotHash = parseInt(document.location.hash.replace('#', ''));
			fotPrev = ((fotHash + 1) < fotTotal) ? (fotHash + 1) : 1;
			switchFoto(fotKat, fotPrev, 'minus');
			return false;
		});

		$('#next').click(function()
		{
			fotHash = parseInt(document.location.hash.replace('#', ''));
			fotNext = ((fotHash - 1) > 0) ? (fotHash - 1) : fotTotal;
			switchFoto(fotKat, fotNext, 'plus');
			return false;
		});

		$('#back').hover(function()
		{
			$('#minus').children().attr('src', '/img/minus_ani.gif');
		}, function()
		{
			$('#minus').children().attr('src', '/img/minus.gif');
		});

		$('#next').hover(function()
		{
			$('#plus').children().attr('src', '/img/plus_ani.gif');
		}, function()
		{
			$('#plus').children().attr('src', '/img/plus.gif');
		});

		$('#linkthumbs').click(function()
		{
			if(showLayer == 1)
			{
				hideLayer = 1;
				showLayer = 2;
			}
			else
			{
				hideLayer = 2;
				showLayer = 1;
			}
			$('#foto' + showLayer).load(
			'/index.php/ajax/thumbsloader/' + $('#foto1').attr('class') + '/', '', function()
			{
				$('#foto' + hideLayer).fadeOut('slow');
				$('#foto' + showLayer).fadeIn('slow');
				$('.thumblink').click(function()
				{
					fNum = $(this).attr('href').substr($(this).attr('href').lastIndexOf('/') + 1);
					switchFoto(fotKat, fNum, 'plus');
					fotNext = parseInt(fNum) + 1;
					if(fotNext > fotTotal) fotNext = 1;
					fotPrev = parseInt(fNum) - 1;
					if(fotPrev <= 0) fotPrev = fotTotal;
					return false;
				});
			});
			return false;
		});
	}
});

