$(function(){
	var GFMF = {
		check:function(){
			if($('#container #content img.loading').length == 0){
				GFMF.resize();
				$('#container #content').addClass('hidden').fadeIn(500, function(){		
					$(this).removeClass('hidden');
				});
			}
		},
		initialize:function(location){
			$('#container #content img').load(function(){
				$(this).removeClass('loading');
				GFMF.check();
			});
			$('#container #footer ol li a').click(function(){
				if($(this).parent().hasClass('prev') || $(this).parent().hasClass('next')){
					GFMF.showImage($(this).attr('alt'));
				}else{
					$('#container #footer a').removeClass('selected');
					$(this).addClass('selected');
					var p = $(this).parent().attr('class');
					$('#container #content').fadeOut(500, function(){
						$('#footer h5 span').html('&nbsp;');
						$('#container #content div').not('.column').addClass('hidden').parent().find(('#'+p)).removeClass('hidden');	
						$('#container #content').addClass('baseline').fadeIn(500);
					});					
				}
			});
			
			$('#index ol li a').click(function(){
				var i = $(this).attr('alt');
				GFMF.showImage(i);
				return false;
			});

			$(window).resize(function(){
				GFMF.resize();
			});
			
			$('.prev a').attr('alt', 0);
			$('.next a').attr('alt', $('#index ol li').not('.divider').length-1);
			
			GFMF.check();
		},
		resize:function(){
			//$('h1').append('<br/>'+$('#content').height());
			if(!$('#content').hasClass('baseline')){
				var top =  ((($(window).height() / 2) - ($('#content').height() / 2)) - 15);
				$('#content').css({margin: top + 'px auto 0px auto'});				
			}
		},
		showImage:function(i){
			$('#container #content').fadeOut(500, function(){
				var image = $('#index ol li').not('.divider').eq(i);
				var t = $(image).find('a').attr('title');
				$('#footer h4 span').html(t.substr(0, t.indexOf('/'))+'&nbsp;');
				$('#footer h5 span').html(t.substr(t.indexOf('/')+1)+'&nbsp;');
				$('.prev a').attr('alt', parseInt(i)-1 < 0 ? ($('#index ol li').not('.divider').not('.clear').length-1) : parseInt(i)-1);
				$('.next a').attr('alt', parseInt(i)+1 > ($('#index ol li').not('.divider').not('.clear').length-1) ? 0 : parseInt(i)+1);
				$('#container #content div').not('.column').addClass('hidden').parent().find(('#canvas')).html('<img src="" />').removeClass('hidden').parent().find('img').unbind().load(function(){
					$('#container #content').removeClass('baseline');
					GFMF.resize();
					$('#container #content').fadeIn(500);
				});	
				$('#container #content #canvas img').attr('src', $(image).find('img').attr('src'));
			});
		}
	}
	GFMF.initialize(window.location.hash);
});