		$(function(){
			var currentCategory = null;
			var photoIndex = 0;
			var BLURRED_PHOTO_OPACITY = 0.9;
			var displayingCategory = false;
			var displayingEnlarged = false;
			var hoveringCategory = false;
			var mouseOutTimeout = null;
			
			function init(){
				for(var i=0; i<galleryMap.categories.length; i++){
					var category = galleryMap.categories[i];
					var itemLink = $('<a href="'+(category.pageUrl?category.pageUrl:'#'+category.urlLabel)+'"><img src="'+category.imageUrl+'" width="240" height="240" alt="'+category.title+'" /></a>')
								.data('category', category)
								.mouseover(function(){
									if(displayingCategory){ return; }
									if(!$(this).hasClass('hover')){
										$('#home-categories a').removeClass('hover');
										$(this).addClass('hover');
										var title = $(this).data('category').title;
										$('#categoryTitle').html(title)
										hoveringCategory = true;
									}
								})
								.mouseout(function(){
									if(displayingCategory){ return; }
									hoveringCategory = false;
									mouseOutTimeout = setTimeout(function(){
										if(!hoveringCategory){
											$('#categoryTitle').html('Photographer');
											$('#home-categories a').removeClass('hover');
										}
									}, 100);
								});
					if(!category.pageUrl){
						itemLink.click(function(){
							loadCategory($(this).data('category'));
						});
						$('#masthead-categoryNav-links').append(
							$('<li><a href="/#'+category.urlLabel+'">'+category.title+'</a></li>')
								.data('category', category)
								.click(function(){
									loadCategory($(this).data('category'));
								})
								
						);
					}
					$('#home-categories').append(itemLink);
				}
				$('#masthead-categoryNav-links').append($('#masthead-categoryNav-links li.static'));
				
				if(window.location.hash.length){
					var categoryToLoad = getCategoryByUrlLabel(window.location.hash.substring(1));
					if(categoryToLoad){
						loadCategory(categoryToLoad);
					}else{ home(); }
				}else{
					if(typeof(preventHome) == 'undefined' || !preventHome){
						$('#masthead-categoryNav').hide();
						home();
					}
				}
				
				$('#masthead h1 a').click(function(){
					home();
				});
				
				$('#previous').click(function(){
					selectPhoto(photoIndex-1);
				});
				$('#next').click(function(){
					selectPhoto(photoIndex+1);
				});
				$('#enlarge').click(function(){
					enlargePhoto();
				});
				$('#enlarged-closeDialog').click(function(){
					closeDialog();
				});
				
				$(document).keydown(function(e){
					if(!displayingCategory){ return; }
					var code = (e.which || e.keyCode);
					switch(code){
						case 37:
							if(displayingEnlarged){ return true; }
							selectPhoto(photoIndex-1);
							break;
						case 39:
							if(displayingEnlarged){ return true; }
							selectPhoto(photoIndex+1);
							break;
						case 32:
							if(displayingEnlarged){ return false; }
							enlargePhoto();
							return false;
							break;
						case 27:
							if(!displayingEnlarged){ return true; }
							closeDialog();
							break;
					}
					return true;
				});
				
				try{
					document.getElementById('category-photosNav').onselectstart = function(){ return false; };
				}catch(e){}
			}
			
			function getCategoryByUrlLabel(label){
				for(var i=0; i<galleryMap.categories.length; i++){
					if(galleryMap.categories[i].urlLabel == label){ return galleryMap.categories[i]; }
				}
				return null;
			}
			
			function home(){
				displayingCategory = false;
				$('#masthead-categoryNav').slideUp('slow');
				$('#category').fadeOut('slow', function(){
					$('#home').fadeIn('slow');
				});
				$('body').animate({ 'background-color' : '#0d0d0d' }, 2000);
				$('#categoryTitle')
					.html('Photographer')
					.animate({ 'color' : '#797878' }, 2000);
			}
			function loadCategory(category){
				displayingCategory = true;
				$('#masthead-categoryNav').slideDown('slow');
				
				currentCategory = category;
				
				$('body').animate({ 'background-color' : category.bgColour }, 2000);
				$('#categoryTitle')
					.html(category.title)
					.animate({ 'color' : category.titleColour }, 2000);
				$('#masthead-categoryNav-links a').css({ 'color' : '#adadad' }).removeClass('selected');
				$('#masthead-categoryNav-links a[href=/#'+category.urlLabel+']')
					.addClass('selected')
					.css({ 'color' : category.titleColour });
				
				$('#home').fadeOut('slow', function(){
					var categoryElem = $('#category');
					categoryElem.fadeOut('slow', function(){
						$('#previous').css('opacity', 0);
						var pan = $('#pan');
						pan.find('*').remove();
						if(category.photos.length){
							for(var i=0; i<category.photos.length; i++){
								var photo = category.photos[i];
								pan.append(
									$('<td><div><img src="'+photo.smallUrl+'" alt="'+photo.title+'" title="'+photo.title+'" /></div></td>')
										.data('photo', photo)
										.data('index', i)
										.click(function(){
											var index = $(this).data('index');
											if(index == photoIndex){
												enlargePhoto();
											}else{
												selectPhoto(index);
											}
										})
								);
							}
							$('#pan td div img').hide();
							$('#pan td').css('opacity', BLURRED_PHOTO_OPACITY);
							
							$('#pan td div img').load(function(){
								$(this).parent().animate({ width : $(this).width()+'px' }, 1500);
								$(this).fadeIn('normal');
							});
							$('#pan td div img:eq(0)').load(function(){
								selectPhoto(0);
							});
						}
						categoryElem.fadeIn('slow');
					});
				});
			}
			function selectPhoto(index){
				if(!currentCategory){ return; }
				if(index < 0){ index = 0; }
				if(index > currentCategory.photos.length-1){ index = currentCategory.photos.length-1; }
				photoIndex = index;
				
				if(photoIndex == 0){ $('#previous').fadeTo(1000, 0); }else{ $('#previous').fadeTo(1000, 1); }
				if(photoIndex == currentCategory.photos.length-1){ $('#next').fadeTo(1000, 0); }else{ $('#next').fadeTo(1000, 1); }
				
				var pan = $('#pan');
				pan.stop();
				$('#pan td').fadeTo('slow', BLURRED_PHOTO_OPACITY);
				var photoCell = $('#pan td:eq('+index+')');
				
				// slide the photo into center
				var winWidth = $(window).width();
				var imgWidth = photoCell.find('img').width();

				var photoCellOffset = (photoCell.position().left * -1);
				pan.animate({
						'left' : (photoCellOffset+(winWidth/2)-(imgWidth / 2))+'px'
					}, 
					1000, function(){
						photoCell.fadeTo('slow', 1);
					});
			}
			function enlargePhoto(){
				displayingEnlarged = true;
				$('#enlarged-closeDialog').hide();
				$('#enlarged-blanket').show();
				var photo = currentCategory.photos[photoIndex];
				$('#enlarged-image').find('*').remove();
				$('#enlarged-image').append(
					$('<img src="'+photo.largeUrl+'" />')
						.load(function(){
							$(this).fadeTo(1000, 1);
							$('#enlarged-closeDialog').css({left:($(this).position().left+$(this).width()-15)+'px'}).fadeIn('fast');
						})
				);
				$('#enlarged-title').html(photo.title);
				$('#enlarged-description').html(photo.description);
				
				
				$('#enlarged').css({
						left : (($(window).width()/2) - ($('#enlarged').width()/2))+'px'
					}).fadeIn('normal').slideDown('normal');
			}
			function closeDialog(){
				displayingEnlarged = false;
				$('#enlarged').fadeOut('normal', function(){
					$('#enlarged-blanket').hide();
				});
			}
			
			init();
		});
