document.observe("dom:loaded", function()
	{
		$('pfmenu').childElements().each(
			function(e)
			{
				e.observe( 'click', function(event)
					{
						var attr = e.getAttribute('rel');
						
						$$('.pfimages').each( function(a){a.hide();} );
						$('pfmenu').childElements().each( function(b){b.removeClassName('active');} );
						$('pf-' + attr).show();
						
						e.addClassName('active');
					}
				);
			}
		);
		
		$('pfmenu').childElements().first().addClassName('active');
		
		var images 		= new Array();
		var canGoAgain 	= 1;
		
		var maxPhotos = {
			'aph': 8,
			'eirwen': 14,
			'hostels': 4,
			'nature': 8,
			'ns': 11,
			'wlh': 6
		}
		
		$$('.img_viewer').each(
			function(e)
			{
				e.observe( 'click', function(event)
					{
						if ( canGoAgain == 0 ) return;
							canGoAgain = 0;
							
						var orientation = e.up('li').hasClassName('portrait');
						
						if ( orientation == true )
						{
							$('image_view').addClassName('portrait');
							$('image_view').removeClassName('landscape');
						}
						else
						{
							$('image_view').removeClassName('portrait');
							$('image_view').addClassName('landscape');
						}
						
						var source = e.getAttribute( 'src' ).replace( 'tiny', 'large' );
						
						var lsource 	= source.match( /images\/photos\/([a-zA-Z]+)\/([0-9]+)-large\.jpg/ ),
							lnewNumber	= parseInt(lsource[2])-1+'',
							lnewSource 	= lsource[0].replace( 'images/photos/' + lsource[1] + '/' + lsource[2], 'images/photos/' + lsource[1] + '/' + lnewNumber ),
						
							rsource 	= source.match( /images\/photos\/([a-zA-Z]+)\/([0-9]+)-large\.jpg/ ),
							rnewNumber	= parseInt(rsource[2])+1+'',
							rnewSource 	= rsource[0].replace( 'images/photos/' + rsource[1] + '/' + rsource[2], 'images/photos/' + rsource[1] + '/' + rnewNumber );
						
						var loader = new Image();
						loader.src = source;
						
						/* Show loading */
						showLoading();
						
						new Effect.SlideDown( $('image_view'),
									{
										afterFinish: function()
										{
											canGoAgain = 1;
											stopLoading(source);
											showSlideshow(lsource,rsource);
											images[source] = 1;
										}
									}
						);

						$('image_left_link').hide();
						$('image_right_link').hide();
						
						$('picnumber').update( 'Picture ' + lsource[2] + ' of ' + maxPhotos[lsource[1]] );
					}
				);
			}
		);
		
		function showSlideshow(lsource,rsource)
		{
			$('image_left_link').show();
			$('image_right_link').show();
			$('image_container_image').show();
			
			new Effect.Appear( $('image_outer_container'), { duration: 0.25 } );
			
			if ( ( parseInt(lsource[2]) - 1 ) < 1 )
			{
				$('image_left_link').hide();
			}
			
			if ( ( parseInt(rsource[2]) + 1 ) > maxPhotos[rsource[1]] )
			{
				$('image_right_link').hide();
			}
		}
		
		$('image_left_link').observe( 'click', goLeft );
		
		function goLeft(event)
			{
				$('image_left_link').show();
				$('image_right_link').show();
				
				var source 		= $('image_container_image').getAttribute( 'src' ).match( /images\/photos\/([a-zA-Z]+)\/([0-9]+)-large\.jpg/ );
				var newNumber	= parseInt(source[2])-1+'';
				var newSource 	= source[0].replace( 'images/photos/' + source[1] + '/' + source[2], 'images/photos/' + source[1] + '/' + newNumber );
				
				if ( (parseInt(newNumber)-1) < 1 )
				{
					$('image_left_link').hide();
				}
				
				if ( parseInt(newNumber) < 1 )
				{
					return;
				}	
							
				$('picnumber').update( 'Picture ' + newNumber + ' of ' + maxPhotos[source[1]] );
		
				var loader = new Image();
				loader.src = newSource;
				
				/* Show loading */
				showLoading();
				loader.onload = (function(){stopLoading(newSource);});
			}

		$('image_right_link').observe( 'click', goRight );
		Event.observe( document.body, 'keydown', goKey );
		
		function goRight(event)
			{
				$('image_left_link').show();
				$('image_right_link').show();
										
				var source 		= $('image_container_image').getAttribute( 'src' ).match( /images\/photos\/([a-zA-Z]+)\/([0-9]+)-large\.jpg/ );
				var newNumber	= parseInt(source[2])+1+'';
				var newSource 	= source[0].replace( 'images/photos/' + source[1] + '/' + source[2], 'images/photos/' + source[1] + '/' + newNumber );

				if ( ( parseInt(newNumber) + 1 ) > maxPhotos[source[1]] )
				{
					$('image_right_link').hide();
				}
				
				if ( parseInt(newNumber) > maxPhotos[source[1]] )
				{
					return;
				}	
							
				$('picnumber').update( 'Picture ' + newNumber + ' of ' + maxPhotos[source[1]] );
		
				var loader = new Image();
				loader.src = newSource;
				
				/* Show loading */
				showLoading();
				loader.onload = (function(){stopLoading(newSource);});
			}
			
		function goKey(event)
		{
			if ( ! $('image_view').visible() )
			{
				return;
			}
			
			if ( event.keyCode == Event.KEY_LEFT )
			{
				goLeft(event);
			}
			else if ( event.keyCode == Event.KEY_RIGHT )
			{
				goRight(event);
			}
			else if ( event.keyCode == Event.KEY_ESC )
			{
				doClose(event);
			}
		}

		
		$('image_view_close').observe('click', doClose );
		
		function doClose(event)
			{
				if ( canGoAgain == 0 )
				{
					return;
				}
				
				canGoAgain = 0;
					
				$('loading').hide();

				$('image_left_link').hide();
				$('image_right_link').hide();
				
				new Effect.Fade( $('image_outer_container'),
					{
						duration: 0.25,
						afterFinish: function()
						{
							new Effect.SlideUp( $('image_view'), { afterFinish: function(){ canGoAgain = 1;} } );
						}
					}
				);
			}
		
		var currentOpen = '';
		
		['about','portfolio','contact'].each(
			function(page)
			{
				$('a_'+page).observe( 'click', 
					function(e)
					{
						$('content_'+page).removeClassName('hide');
						revealPage('content_'+page);
		
						currentOpen = 'content_'+page;
					}
				);
			}
		);

		function revealPage(toOpen)
		{
			if ( currentOpen != '' && currentOpen != toOpen )
			{
				$(currentOpen).addClassName('hide');
			}
			
			$('content').removeClassName('hide');
			$('copyright_closed').hide();
		
			new Effect.ScrollTo( $('page') );
		}
		
		function showLoading()
		{
			$('loading').setStyle('display:block;z-index:99999999;');
		}
		
		function stopLoading(newSource)
		{
			$('loading').setStyle('z-index:1');
			$('image_container_image').setAttribute( 'src', newSource );
		}
	}
);
