$(function() {
	var GFMF = {
		check : function() {
			GFMF.showImage(0);
		},
		initialize : function(location) {
			$('#content img').load(function() {
				$(this).removeClass('loading');
				// GFMF.check();
				});
			$('#image-navi span a').click(
					function() {
						if ($(this).parent().hasClass('prev')
								|| $(this).parent().hasClass('next')) {
							GFMF.showImage($(this).attr('alt'));
						} else {

							$('#content').fadeOut(500);
						}
					});

			$('#index ol li a').click(function() {
				var i = $(this).attr('alt');
				GFMF.showImage(i);
			});

			$('.back').click(function() {
				$('#image-navi').attr('class', 'hide');
				$('#content').fadeOut(2000);
			});

			$(window).resize(function() {
				GFMF.resize();
			});

			var anzahl = $('#index ol li').length;
			$('.prev a').attr('alt', 0);
			$('.next a').attr('alt', anzahl);

			GFMF.check();
		},
		resize : function() {
		},
		showImage : function(i) {
			var anzahl = $('#index ol li').length;
			var gesamt = String(anzahl);
			var o = parseInt(i) + 1;
			var offset = String(o);

			$('#content').fadeOut(
					500,
					function() {
						var image = $('#index ol li').eq(i);
						var groesse = $(image).find('img').attr('groesse');
						var praesentation = $(image).find('img').attr(
								'praesentation');
						var auflage = $(image).find('img').attr('auflage');
						var copyright = $(image).find('img').attr('copyright');
						// Klasse Abstandhalter vom Titel zu restlichen
						// Informationsabgaben
						var img_class = $(image).find('img').attr('img-class');

						$('.groesse')
								.html(groesse.substring(groesse.indexOf('/') + 1));
						$('.praesentation').html(
								praesentation.substring(praesentation
										.indexOf('/') + 1));
						$('.auflage').html(
								auflage.substring(auflage.indexOf('/') + 1));
						$('.copyright')
								.html(
										copyright.substring(copyright
												.indexOf('/') + 1));

						$('.offset').html(
								offset.substring(offset.indexOf('/') + 1));
						$('.anzahl').html(
								gesamt.substring(gesamt.indexOf('/') + 1));

						$('#image-navi').attr('class', 'show');

						var t = $(image).find('a').attr('title');

						$('.titel').html(t.substring(t.indexOf('/') + 1));
						$('.prev a').attr(
								'alt',
								parseInt(i) - 1 < 0 ? (anzahl - 1)
										: parseInt(i) - 1);
						$('.next a').attr(
								'alt',
								parseInt(i) + 1 > (anzahl - 1) ? 0
										: parseInt(i) + 1);
						$('.next a').attr('class', function() {
							if (parseInt(i) + 1 > (anzahl - 1)) {
								$('next-divider').attr('class', 'hide');
								return "hide";
							} else {
								$('next-divider').attr('class', 'show');
								return "show";
							}
						});
						$('.prev a').attr('class', function() {
							if (parseInt(i) - 1 < 0) {
								$('prev-divider').attr('class', 'hide');
								return "hide";
							} else {
								$('prev-divider').attr('class', 'show');
								return "show";
							}
						});

						$('#index').addClass('hidden').parent().find(('.bild'))
								.html('<img src="" />').removeClass('hidden')
								.parent().find('img').unbind().load(
										function() {

											$('#content #img-titel').attr(
													'class', img_class);
											GFMF.resize();
											$('#content').fadeIn(900);

										});
						$('#content .bild img').attr('src',
								$(image).find('img').attr('src'));
					});
		}
	}
	GFMF.initialize(window.location.hash);
});
