// 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] = "It's out there somewhere, a treasure that will become the richest prize in television history, but its location is shrouded in mystery and guarded by a secret society with an ancient past.";
	story[2] = "Miles from the Arctic Circle, five teams prepared to embark on the treasure hunt of a lifetime, unaware that five additional teams were also preparing for their own journey in the deep blue waters of the South Pacific.";
	story[3] = "On board the USS Jefferson in the South Pacific, the teams listened to the telltale beeps of Morse code - it was their first clue. After scouring the ship, the Geniuses found a book to help them decode it, making them the first team to head out to the specified dive point.";
	story[4] = "In Alaska, the other five teams received their first clue, also in Morse code. Though the Wild Hanlons were off and running first, Ex-CIA cracked the code before them.";
	story[5] = "Back on board the Jefferson, the Fogal family was having a rather gut-wrenching problem - daughter Kayte was seasick. But father Fogal, who is a pastor when not hunting for treasure, refused to let this get in the family's way, and they managed to get to the dive point ahead of the last-placed Brown family.";
	story[6] = "When the Southie Boys, Young Professionals, and Ex-CIA teams stepped off their helicopters and onto Matanuska Glacier, they received a phone call instructing them to look for their next clue, frozen somewhere in the ice.";
	story[7] = "After rappelling down a glacier and swimming through a pool of ice-cold water, Ex-CIA became the first to find the clue: A map of Alaska frozen into a chunk of ice, etched with the cryptic words \"Stillwater Washington.\"";
	story[8] = "Getting to that point wasn't so easy for the Wild Hanlons and Miss USA teams, both of whom struggled with the altitude and cold. Miss USA managed to find the map first, but it was the Wild Hanlons who first guessed that \"Stillwater Washington\" was code for Lake George, Alaska.";
	story[9] = "58 miles away, Ex-CIA arrived first to Lake George where they were instructed to find a clue buried in the ground. Digging through the rocks, they found a cylinder engraved with a key to a code located at the state capitol building in Lincoln, Nebraska.";
	story[10] = "In another part of the world, the other five teams headed out to the dive site. The Geniuses arrived first with Charles diving into the depths to locate a steel box containing a pilot's logbook holding coordinates to the next location. The Fogals managed to move ahead when Brad snared a clue away from the Grad Students, who couldn't quite get a grip on the box.";
	story[11] = "A map inside the logbook led the Geniuses to the final resting place of the airplane Monticello, where their next clue waited among the wreckage. But their first-place status was threatened when they got lost in the jungle, paving an opening for the Air Force team to beat them to the crash site. ";
	story[12] = "Though Air Force struggled to figure out the puzzle, they managed to open the lock box and get the next clue: a painting with a coded map they would need at their next location, the state capitol in Lincoln, Nebraska.";
	story[13] = "Once all teams had solved their clues, they converged outside the Nebraska state capitol where they were shocked to discover that there were ten teams instead of five. Host Laire Macintosh met the startled teams with the instruction that they would be pairing off to solve the next clue. At the end of this stage, the last-place team would be eliminated.";
	story[14] = "The teams boarded buses that were actually heading in the wrong direction! The driver would not change course until the teams solved the clue and gave the right destination. The Browns and Miss USA edged into first place when they became the first to guess Mount Rushmore.";
	story[15] = "Arriving at Mount Rushmore, the Browns and Miss USA learned that it was every team for itself and to look for a clue locked inside a time capsule somewhere off the President's Trail.";
	story[16] = "As the other teams arrived at Mount Rushmore, the Geniuses and the Young Professionals were dropped off at Mount Theodore Roosevelt… the wrong location! A sneaky phone call to the Wild Hanlons clued them in that they were at the wrong place.";
	story[17] = "Team after team found the time capsules. Most of them even managed to extract the clue that was contained within.";
	story[18] = "The Geniuses and the Young Professionals finally arrived at Mount Rushmore, where they received the message that there were only nine time capsules and not ten. The last one to the clue would be out of the game for good.";
	story[19] = "When the Geniuses arrived at the site, they found the Wild Hanlons, who were fellow Texans, struggling to unlock their time capsule. They formed a brief alliance to help each other out, but the Hanlons got theirs open first and were on their way just as the Young Professionals arrived.";
	story[20] = "Tensions flared as the Geniuses and the Young Professionals struggled to be the first to the final remaining clue. With brains working overtime, the Geniuses beat their adversaries to the punch, making the Young Professionals the first team eliminated from the treasure hunt.";
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/recap/101/th_101_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/recap/101/th_101_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/recap/101/th_101_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/recap/101/th_101_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/recap/101/th_101_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/recap/101/th_101_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/recap/101/th_101_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/recap/101/th_101_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/recap/101/th_101_09.jpg";
	imgs[10] = "/Treasure_Hunters/images/recap/101/th_101_10.jpg";
	imgs[11] = "/Treasure_Hunters/images/recap/101/th_101_11.jpg";
	imgs[12] = "/Treasure_Hunters/images/recap/101/th_101_12.jpg";
	imgs[13] = "/Treasure_Hunters/images/recap/101/th_101_13.jpg";
	imgs[14] = "/Treasure_Hunters/images/recap/101/th_101_14.jpg";
	imgs[15] = "/Treasure_Hunters/images/recap/101/th_101_15.jpg";
	imgs[16] = "/Treasure_Hunters/images/recap/101/th_101_16.jpg";
	imgs[17] = "/Treasure_Hunters/images/recap/101/th_101_17.jpg";
	imgs[18] = "/Treasure_Hunters/images/recap/101/th_101_18.jpg";
	imgs[19] = "/Treasure_Hunters/images/recap/101/th_101_19.jpg";
	imgs[20] = "/Treasure_Hunters/images/recap/101/th_101_20.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];
		}
		
	

}