		var currentPage;
		
		
		
		/**
		 * Fades out the current page and then fades up the page passed.
		 * @param {Object} page
		 */
		function showPage(page, dataVO)
		{
			if (!page || typeof(page) == "string" ) return;
			
			if (!currentPage)
			{
					if (dataVO instanceof com.zero42.galleryAdmin.model.vo.PhotographerVO)
					{
						page.delay( 500 ).fadeIn( menuToggleSpeed );
						setTimeout( configurePage, 500, page, dataVO);
					}
					else
					{
						page.fadeIn(menuToggleSpeed);
						configurePage(page, dataVO);
					}
			}
			else if (page != currentPage)
			{
				var pdfInstruction = $("#pdfInstructions");
				
				if (currentPage.attr("id") == "pdfPage" )
					pdfInstruction.fadeOut(menuToggleSpeed);	
				
				currentPage.fadeOut(menuToggleSpeed, function()
				{
					if (dataVO instanceof com.zero42.galleryAdmin.model.vo.PhotographerVO)
					{
						page.delay( 500 ).fadeIn( menuToggleSpeed );
						setTimeout( function(){
								configurePage(page, dataVO);
								},
								500
								);
						
					}
					else
					{
						page.delay( 250 ).fadeIn( menuToggleSpeed );
						configurePage(page, dataVO);
					}
						
				})
					
			}
			currentPage = page;
		}
		
		
		function configurePage(page, dataVO)
		{
			var pdfInstruction = $("#pdfInstructions");
			
			if (page.attr("id") == "homePage" )
			{
				$('ul#homeGrid').innerfade(	{ speed: 3000, timeout: 4000, type: 'sequence',	containerheight: '560px'} )	
			}
			//If the page we are showing is the gallery, set the width once visible
			else if (page.attr("id") == "galleryPage") 
			{
				
				loadPhotographer(dataVO);
			
				setTimeout(function()
				{
					setScrollerWidth();
					scroll(0);
				} , 250);
				
			}
			
			else if (page.attr("id") == "gasPage" && dataVO) 
			{
					populatePage(dataVO);
			}	
			else if (page.attr("id") == "gasPage") 
			{
						populateNewsPage();
			}		
			else if (page.attr("id") == "pdfPage") 
			{
				pdfInstruction.fadeIn(menuToggleSpeed);
				populatePDFPage();
			}
			
			
		}
	

		var externalChangeInitalized = false;
		var lastPage="";
		var lastSubPage="";
		function showPageFromURL()
		{
			//We did do this when nav was construcuted, and the event would fire for the inital change even though it had already happened.
			//however, it didnt fire for FF, so we now manually call showPageFromURL when nav i sready, and then set up the watcher
			if (!externalChangeInitalized)
			{
				externalChangeInitalized=true;
				
				$.address.externalChange(function(event) 
				{  
					showPageFromURL(event.value);
				}); 
				
				
			}
		
		
		
			var path = window.location.href.split("#");
		//	alert(path);
			if (path.length>0 && path[1]=="")
				return;
			var path = window.location.href.split("/");
			
			if (path[path.length-1] == "")
				path.pop();
			
			
			var page = cleanPageName( path.pop() );
			var subPage;
			
			
			if (page =="pdf")
			{
				subPage="pdf";
				page= cleanPageName( path.pop() );
			}
				
			if (lastPage == page && lastSubPage==subPage)
			{
				return false;
			}
			else
			{
				lastPage = page;
				lastSubPage=subPage;
			}			
				
				
			var id;
		
			if (page)
			{
				for (var i=0; i<GAS.data.photographers.length; i++)
				{
					var photogVO = GAS.data.photographers[i];
					if (page == cleanPageName(photogVO.value))
					{
						if (subPage=="pdf")
						{
							currentPhotographer = photogVO;
							loadPage("pdf");
						}
						else
						{
							loadPage(photogVO);
						}
							
						return
					}
				}
				for (var i=0; i<GAS.data.pages.length; i++)
				{
					var pageVO = GAS.data.pages[i];
					if (page==cleanPageName(pageVO.title))
					{
						loadPage( pageVO );
						return
					}
				}
			}
			switch(page)
			{
				case "news" : loadPage('news');  break;
				//case "pdf" : loadPage('pdf');  break;
				default : 
					setTimeout(function(){ loadPage() } , 500); 
				break;
					
			}
			
		}
		
		function cleanPageName(str)
		{
			str = unescape(str.toLowerCase());
			str = str.replace("-","");
			str = str.replace(" ","");
			str = str.replace("_","");
			str = str.replace("#","");
			return str;
		}

		/**
		* Load up a GAS page vo to be displayed in the pages section.
		* If no VO is set, then load up the home page
		*/
		function loadPage(page)
		{
			deselectPhotographer();
			$("#gallerySubNavList").children().remove();
			
			if (page==null)
			{
				showNav();
				deselectAllNavItems();
				showPage( $("#homePage") );
				
			}
			else if (page=="pdf")
			{
				//configure the menu for the pdf veiw for the current photog
				if (currentPhotographer)
				{
					var photogBtn = $("a[photogID=" + currentPhotographer.id + "]");
					toggleItem( photogBtn );
					hideNav( photogBtn );
				
					showPDFButton( currentPhotographer );
				}
				
				toggleItem( $("a[href='pdf']") );
				showPage( $("#pdfPage") );
			}
			else if (page=="news")
			{
				showNav();
				toggleItem( $("a[href='news']") );
				showPage( $("#gasPage") );
			}
			else if (page instanceof com.zero42.galleryAdmin.model.vo.PhotographerVO)
			{
				var photog = page;
				var photogBtn = $("a[photogID=" + photog.id + "]");
				toggleItem( photogBtn );
				hideNav( photogBtn );
				
				showPage($("#galleryPage"), page); 
			}
			else
			{
				showNav();
				toggleItem( $("a[pageID=" + page.id + "]") );
				showPage( $("#gasPage"), page );
			}
			
			
		}


		/**
		 * Poplates the pdf with gas data (if it hasnt got any)
		 */
		function populatePDFPage()
		{
			if (currentPhotographer)
			{
				$("#pdfContent").children().remove();
					$("#pdfContent").append( createPDFListDiv( currentPhotographer ) );
				
			}
				
				/*
				if (!$("#pdfPage").attr("populated")) 
				{
					for (var i=0; i< GAS.data.photographers.length; i++) 
					{
						if (GAS.imageProxy.getImagesByPhotograpaher( GAS.data.photographers[i] ).length>0)
							$("#pdfPage").append( createPDFListDiv( GAS.data.photographers[i] ) );
					}
					
					$("#pdfPage").attr("populated", true);
				}
				*/
		}
		
		/**
		* Creates the news page
		*/
		function populateNewsPage( )
		{
			//Remove old page...
			$("#gasPage .mainContent").children().remove();
			
			for (var i=0; i<GAS.data.pages.length; i++)
			{
				//ignore id 1 and 2
				var pageVO = GAS.data.pages[i];
				if (pageVO.id>2)
					$("#gasPage .mainContent").append( createPageDiv( pageVO ) );
				
			}
			
		}
		
		/**
		* Populate the gas template with gas data
		*/
		function populatePage( pageVO )
		{
			//Remove old page...
			$("#gasPage .mainContent").children().remove();
			//Add new page...
			$("#gasPage .mainContent").append( createPageDiv(pageVO,false) );
			
			
		}


		/**** Gallery ****/
		var currentPhotographer;
		function loadPhotographer(photogrpaherVO)
		{
			//if (currentPhotographer != photogrpaherVO)
			//{
				currentPhotographer = photogrpaherVO;
				
				var photog = photogrpaherVO;
				var photogBtn = $("a[photogID=" + photog.id + "]");
				
			
				var imgs = GAS.imageProxy.getImagesByPhotograpaher( photog );
			
				$("#items").children().remove(); 
				
				//Add photog images
				for (var i=0; i<imgs.length; i++)
				{
					if (!imgs[i].isVideo())
					{
						$("#items").append( createImageTag( imgs[i], false, i ) );
					}
				}
				//Add the photog bio at the end - wait 3 secs so we dont appear before the image loads
				if (photog.description)
					$("#items").append( createPhotogBio( photog ) );										
				
				
				
				//Add tags links - a bit special for this site, we show a list of tags for this PHOTOG only
				var uniqueTags = new Array();
				var imageIndex = new Array();
				
				for (var i=0; i<imgs.length; i++)
				{
					var tags;
					
					tags = GAS.tagsProxy.getItemTags( imgs[i] );
						
					for (var j=0; j<tags.length; j++)
					{
						//this tag is unique and is not a reference tag (#)
						if (uniqueTags.indexOf(tags[j])==-1 && tags[j].label.indexOf("#")==-1 )	
						{
							uniqueTags.push(tags[j]);
							imageIndex.push(i);
						}
					}
				}
			
			
				
			
				//Fake a tag for the biography
				//Add the photog bio at the end
				if (photog.description && photog.description.length>0 && photog.description!="")
				{
					uniqueTags.push( new com.zero42.galleryAdmin.model.vo.TagVO(999, "Bio") );
					imageIndex.push(i);
				}
				
				$("#gallerySubNavList").children().remove();
				
				for (var k=0; k<uniqueTags.length; k++)
				{
					var a = createTagsTag( uniqueTags[k] );
					a.imgIndex=imageIndex[k];
					//Get the first item in the images list whoes tag is this
					
					
					a.onclick=function() {  scroll($(this).attr("imgIndex"))  }
					$("#gallerySubNavList").append( a );
				
				}
				
				showPDFButton( photog );
				
				
					
				setScrollerWidth();
				scroll(0);
				
				
			//}
		}
		
		

		function deselectPhotographer()
		{
			if (selectedItem)
			{
				currentPhotographer=null;
				deselectItem(selectedItem);
				selectedItem.attr("toggled" ,false);
				selectedItem=null;
			}
		}
		
		
		
