// JavaScript Document

window.onload=function(){
	document.getElementById('galleryInfo').innerHTML='\
	<form id="holder" name="holder" action="" method="post">\
	<input type="hidden" name="show">\
	</form>';
	
	// Get the url information
	var locate = window.location
	document.getElementById('holder').show.value = locate
	var text = document.holder.show.value
	
	// Parse out the url information
	theleft = text.indexOf("=") + 1;
	theright = text.length; // for 1 variable
	sText = (text.substring(theleft, theright));
	
	///// STORY COPY GOES HERE IN EACH VARIABLE /////
///// STORY COPY GOES HERE IN EACH VARIABLE /////
	var story = new Array();
	story[1] = "In the dark of early dawn, the remaining seven teams received their next communication: To find the fourth clue, they would have to unlock the code that protected America's most secret journey. The search began outside their hotel doors with an old newspaper article written by author Walt Whitman describing the Atlantic Avenue Tunnel in Brooklyn.";
	story[2] = "Team Miss USA figured out how to enter the tunnel and decided to tip off Ex-CIA since they had been so helpful in the past. While driving to the tunnels, the Brown Family became the first team to guess that this challenge was about the Underground Railroad.";
	story[3] = "Other teams weren't quite as united. While the Southie Boys bickered, the Geniuses were coming unhinged over Sam's asthma and other medical issues that prevented him from running or moving as fast as the others.";
	story[4] = "Miss USA made their descent into the murky tunnel where they found a cache of Civil War memorabilia and some words scrawled along the uneven wall. With the words memorized, they left the tunnel, leaving behind a book that they decided wasn't germane to the quest.";
	story[5] = "The Southie Boys and the Geniuses were next to the cache. When they opened the book, they discovered a clue leading to their next destination: a plantation in Georgetown. When they emerged back into the light of day, Miss USA realized that they'd left something behind and decided to return to the dark depths of the crumbling tunnel.";
	story[6] = "The Southie Boys headed out to the next location with the inspiring news that the first team to reach the next clue would receive a $30,000 prize. Meanwhile, Miss USA got the clues out of the tunnel, but their mistake put them into fourth place. Because of this, they decided to break their alliance with Ex-CIA.";
	story[7] = "Ex-CIA and the Fogal Family were fifth and sixth to enter and leave the tunnel. They were both on their way by the time the Brown Family made their initial descent. Knowing they were behind, tge Browns put some pepper in their step as they searched for the clue.";
	story[8] = "The Southie Boys were first to arrive at Georgetown's Beneventum Plantation where they learned that plantations like that one were departure points for slaves fleeing through the Underground Railroad to freedom. Sometimes, they embroidered maps onto quilts to hide their routes. The teams had to find such a quilt that would lead them though a swamp to a box containing little keys. The Southie Boys were off and running in no time to \"follow the drinking gourd\" to a hidden key that would unlock a box containing the $30,000.";
	story[9] = "The other teams were quick to head into the swamp in search of the elusive treasure box. The Brown Family, arriving last, knew they had to make up some time and threw themselves into the hunt with gusto.";
	story[10] = "The Southie Boys ran into the Fogals and they decided to throw in their lot together. Elsewhere, Miss USA and Air Force also decided to partner up. Forging ahead on their own, Ex-CIA struggled through the harsh terrain until Mark collapsed on the ground, spent and out of breath.";
	story[11] = "Kayte Fogal was having a meltdown of her own and \"couldn't go any farther.\" The others watched as she cried, whined, and complained. \"I don't want to do this anymore!\" she shouted hysterically.";
	story[12] = "Ex-CIA arrived at the \"old man\" box where a message from Laird informed them that they had reached a critical point: For the next leg of their journey, they would have to board a two-person boat and make the tough decision whether or not to return to pick up the third team member, which would cost them three hours of time.";
	story[13] = "Crying and on her knees, Kayte begged her parents to cut her loose from the hunt, which caused her mom to burst into tears as dad looked on in distress. Finally, Margie and Brad got on the boat and headed out, leaving their daughter behind.";
	story[14] = "The Browns struggled through the swamp until Keith tore a leg muscle. A team of medics arrived to put him on a stretcher and carry him out of the swamp. Meanwhile, the two Ex-CIA members who set off by boat returned to collect Todd from the shore. The Geniuses, however, were falling apart at the seams as Sam's asthma threatened to kick in and he struggled to keep up.";
	story[15] = "Kayte's tears dried up when she saw her parents returning for her. With newfound resolve, Kayte was ready to go on. Air Force also returned to collect their third member, as did Miss USA.";
	story[16] = "With all the clues assembled, the Southie Boys, Ex-CIA, and Air Force raced toward the box containing the gold with Ex-CIA beating them to the punch. With key in hand, they opened the box, and the treasure was theirs.";
	story[17] = "Francis and Charles of team Geniuses left Sam behind as they headed out on their boat. With the water gently lapping, they decided that it made more sense for them not to return for their struggling teammate. To be continued...";
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/recap/104/th_104_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/recap/104/th_104_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/recap/104/th_104_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/recap/104/th_104_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/recap/104/th_104_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/recap/104/th_104_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/recap/104/th_104_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/recap/104/th_104_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/recap/104/th_104_09.jpg";
	imgs[10] = "/Treasure_Hunters/images/recap/104/th_104_10.jpg";
	imgs[11] = "/Treasure_Hunters/images/recap/104/th_104_11.jpg";
	imgs[12] = "/Treasure_Hunters/images/recap/104/th_104_12.jpg";
	imgs[13] = "/Treasure_Hunters/images/recap/104/th_104_13.jpg";
	imgs[14] = "/Treasure_Hunters/images/recap/104/th_104_14.jpg";
	imgs[15] = "/Treasure_Hunters/images/recap/104/th_104_15.jpg";
	imgs[16] = "/Treasure_Hunters/images/recap/104/th_104_16.jpg";
	imgs[17] = "/Treasure_Hunters/images/recap/104/th_104_17.jpg";

	//alert(imgs[1].indexOf(".jpg"));
	
	
	///// STORY PROCESSES /////
	var sCount = imgs.length;
	var cPages = "";
	var	cNext = parseFloat(sText);
	var sTextNum = parseFloat(sText);
	
	///// FORCE AN ID IF NONE IS GIVEN /////
	if(isNaN(sTextNum)){
		sTextNum = 1;
		cNext = 1;
	}
	
	
	///// CHECKS THE SET VALUES FOR CORRECT VIEWING /////
	if(sText){
		cNext = cNext+1;
		cCopy = sTextNum;
		cBack = sTextNum - 1;
		
		if(sTextNum == 0){ // checks for 0 
			cBack = sCount - 1;		
			sText = 1;
		}
		if(sTextNum == 1){ // checks for 1
			cBack = sCount - 1;		
			sText = 1;
		}
		if(cNext > (sCount-1)){ // next turnaround
			cNext = 1;
			sText = cCopy;
			cBack = sCount-1;	
		}
		if(cBack == sText){ // back turnaround 
			cNext = 1;
			sText = cCopy;
			cBack = cBack-1;	
		}
	}else{
		cBack = sCount - 1;
		cNext = 2;
		sCount = 1
		sText = 1;
	}
	if(sCount ==  sTextNum){
		cNext = 1;
	}
	
	
	///// SET ID INFORMATION FOR HTML ELEMENTS /////
	pCopy = document.getElementById('copy');
	pNext = document.getElementById('goNext');
	pBack = document.getElementById('goBack');
	pPicList = document.getElementById('picList');
	pPicListNext = document.getElementById('picListNext');
	pPicListBack = document.getElementById('picListBack');
	
	
	// Checks and sets the setCount
	var setCount = 1;
	if(pPicList){
		setCount = parseFloat(pPicList.title);
	
		if(!setCount){
			setCount=1;
		}
	}
	
	///// THUMBNAILS /////
	if(pPicList){		
		// Set initial vairables for thumbnails
		var thumbNail = "";		
		id = cCopy - ((cCopy -1) % setCount);
		alert(cCopy);
		setLimit = id + setCount;
			
			// Write out the thumbnails
			for(id; id < setLimit; id++){
				if(imgs[id]){
					thumbNail = thumbNail + "\
						<a href='?id="+id+"'><img src='"+imgs[id].replace(".jpg" ,"_sm.jpg")+"' alt='"+story[id]+"' /></a>";
				}
			}
		
			// Writes Thumbnails to the page
			pPicList.innerHTML = thumbNail; 
		
			
			//tBack = (sCount-id);
			
			//alert(sCount);
			if(id >= sCount){
				id=setCount;
				//tBack = setCount;
			}
			
			if(id == sCount){
				//tBack = sCount - setCount;
				id=setCount;
			}
			
			if(setLimit > setCount){
				tBack = (sCount-1) - setCount;	
			}
			
			if(setLimit == (setCount+1)){
				tBack = (sCount+1) - setCount;	
			}
			
			if(setLimit < sCount){
				tBack = (id-setCount)-setCount;
			}
			if(tBack <= 1){
				tBack = sCount-1;
			}
		}
		
		
		////// CHECK AND BALANCE FOR HTML ELEMENTS /////
		for(i=0; i < document.anchors.length; i++){
		
			// Writes out the "Next" link
			if(document.anchors[i].name == "goNext"){
				document.anchors[i].href="?id="+cNext;
			}
			
			// Writes out the "Back" link
			if(document.anchors[i].name == "goBack"){
				document.anchors[i].href="?id="+cBack;	
			}
			if(pPicList){
				if(setCount != sCount-1){
					// Writes out the "Thumbnail Next" link
					if(document.anchors[i].name == "picListNext"){
						document.anchors[i].href="?id="+id;					
					}
					
					// Writes out the "Thumbnail Back" link
					if(document.anchors[i].name == "picListBack"){
						document.anchors[i].href="?id="+tBack;		
					}
				}
			}
							
		}
		
		
	
		// Writes in the Main Image Source
		document.images.mainImg.src=imgs[cCopy]; 
		
		//Writes out the story copy
		if(pCopy){
			pCopy.innerHTML=story[sText];
		}
		
	

}