/*******************************************************************************
	AUTHOR:		Timothy Higgins
	CONTACT:	timothymichaelhiggins@gmail.com
	DATE:		11/5/2010
	
	Photo viewing methods.
*******************************************************************************/

	var preloaded=false;

	/***************************************************************************
		View the passed photo.
		
		PARAMETERS: String		The image ID to view.
	***************************************************************************/
	function openPhoto(id){
	
		//Close any open photos and allow close onmouseout.
		closePhoto();
		makeDark();
		var close=1;

		//Create a non-thumbnail Image.
		img=new Image();
		img.src=document.getElementById(id).src+"&size="+_DEFAULT_PHOTO_SIZE;
		img.className="photo";
		img.style.marginTop=-document.getElementById(id).height*
			(_DEFAULT_PHOTO_SIZE/_DEFAULT_THUMBNAIL_SIZE)/2+"px";
		img.style.marginLeft=-document.getElementById(id).width*
			(_DEFAULT_PHOTO_SIZE/_DEFAULT_THUMBNAIL_SIZE)/2+"px";
		img.onclick=function(){

			//Find the key of the current and next Image in the document.
			for(i=0;i<document.images.length;i++){
				if(document.images[i].id==id){
					for(i++;i<document.images.length;i++){
						if(document.images[i].id.substring(0,6)=="photo_"){
							close=0;
							self.location.hash=document.images[i].id;
							openPhoto(document.images[i].id);
							return true;
						}
					}
				}
			}
			
			//No more photos.
			closePhoto();
			makeLight();
			return false;
		};
		img.onmouseout=function(){
			if(close){
				closePhoto();
				makeLight();
			}
		}
		document.getElementById("photo").appendChild(img);
		
		//If not already started, begin preloading images.
		if(!preloaded)
			preloadPhotos();
	}
	
	/***************************************************************************
		Close the open photo in the photo container.
	***************************************************************************/
	function closePhoto(){
		var photos=document.getElementById("photo");
		if(photos.childNodes.length==1)
			photos.removeChild(photos.firstChild );
	}
	
	/***************************************************************************
		Blackout the screen by setting the photo container's class.
	***************************************************************************/
	function makeDark(){
		document.getElementById("photo").className="photo";
	}
	
	/***************************************************************************
		Resume normal display by setting the photo container's class to null.
	***************************************************************************/
	function makeLight(){
		document.getElementById("photo").className=null;
	}
	
	/***************************************************************************
		Preload photo sizes of all photos on page.
	***************************************************************************/
	function preloadPhotos(){
		for(i=0;i<document.images.length;i++){
			if(document.images[i].id.substring(0,6)=="photo_"){
				img=new Image();
				img.src=document.getElementById(document.images[i].id).src+
					"&size="+_DEFAULT_PHOTO_SIZE
			}
		}
	}
