// 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] = "At Beneventum Plantation, the last-place Brown Family was held up further by Terry's torn hamstring. But his two brothers vowed to carry on with the hunt, accepting a six-hour penalty for not being able to finish the task. Meanwhile, Geniuses Francis and Charles debated whether or not to get rid of Sam, especially since they were in second-to-last place.";
	story[2] = "Ex-CIA was first to arrive at the last box where they uncovered a clue that directed them to the \"safe house with the American flag.\" Emerging from the swamp, they arrived at the safe house where they received a message from Laird: Look for clues to help see the way to freedom. Inside the safe house, they found stacks of quilts that possibly contained the next clue.";
	story[3] = "The second-place Fogals fought their way through the swamp despite Kayte's constant whining and screaming, \"I can't do this!\" When she slipped in the swamp, she screamed, \"My legs! Why doesn't anyone understand that my legs are hurting!\" as tears pour down her dirty face. Her parents ignored her cries for attention and carried on ahead of her.";
	story[4] = "The Fogals and the Southie Boys arrived at the last box at the same time, quickly followed by the Air Force team. Meanwhile, fifth-place Miss USA was struggling through swamp grass while Genius Sam waited eagerly for his teammates to return for him. At long last, his partners arrived to collect their teammate. \"You don't shaft your friends,\" Francis said.";
	story[5] = "At the safe house, Ex-CIA found a clue stuffed inside a quilt's pouch bearing a quotation about \"reflection\" and a line about \"Dock Street Theatre.\" With clue in hand, they took off to locate the theatre in Charleston, South Carolina, where they struggled to figure out their next step. Other teams started arriving while Ex-CIA was still trying to figure out the puzzle. Finally, team Air Force realized that blowing on one of the mirrors revealed their next destination: Fort Pulaski.";
	story[6] = "When the Fogals arrived at the theatre, the Southie Boys, Air Force, and Ex-CIA were already gone. Father Fogal wondered aloud why there were so many mirrors around, and Kayte assured him that it was just d&eacute;cor. Two hours passed, and Brad Fogal kept looking at the mirrors, even though Kayte continued to berate him for \"doing something that is making no sense.\"";
	story[7] = "With the Geniuses still in the swamp, Miss USA found the clue in the quilts and headed out to the theatre as the others arrived at Fort Pulaski, a key Civil War location. During the war, soldiers had to take special care to ensure that military intelligence wouldn't be stolen, and that meant hiding their secrets. The next clue was hidden in plain sight, in America's most famous symbol.";
	story[8] = "Miss USA met up with the Fogals in the theatre. Though Kayte had told her dad to stop looking at the mirror, he finally discovered that breathing on it revealed the next location.";
	story[9] = "At the fort, team Air Force discovered that their clue could be found screwed into the top of the flagpole. This new clue sent them 15 miles away to a location outside of Savannah, Georgia, where they received a message to dig for a piece of a hero's identity - although they wouldn't know where or how deep it had been buried. The teams had to hurry because there were only six artifacts and seven teams. The last-place team would be eliminated.";
	story[10] = "Air Force was first to retrieve the artifact, a death mask, impressing the Southie Boys and Ex-CIA with their speed. And as night fell, the Browns six-hour penalty was up and they were sent to the Dock Street theatre to look for the clue.";
	story[11] = "The Geniuses arrived at the theatre, running \"like chickens with our heads cut off\" to stay ahead of the Browns. It didn't take Sam long to blow on the mirror and figure out the next clue, reaffirming the other guys' decision to keep him on the team.";
	story[12] = "Ex-CIA and the Southie Boys continued to dig into the night. Close to 11 p.m., Ex-CIA became the second team to find the mysterious death mask. The Southies got back to work as the Fogals and Miss USA arrived to begin their own dig. After digging for almost 6 &frac12; hours, the Southies finally found an artifact, much to their relief.";
	story[13] = "Disregarding Kayte's digging advice, Brad led his team to a fourth-place finish in the quest to find the mask. As they headed to camp, Kayte apologized to her dad for not being more encouraging as Miss USA continued to dig on their own. Hungry, upset, and distraught, the three girls debated whether or not to quit the game.";
	story[14] = "The Geniuses arrived at the fort in the thick of night, using flashlights to guide the way. Finally, Francis found the clue in the flag. Meanwhile, the Browns raced toward the fort after discovering the clue hidden in the mirror at the theatre.";
	story[15] = "Miss USA was sitting around the dig site as the Geniuses arrived, which promoted them to get back on their feet and continue to hunt. Finally, after digging for five hours, Miss USA found a mask at 3 a.m., leaving the challenge down to the Geniuses and the Browns.";
	story[16] = "As the sun started to rise, the Geniuses finally found the last artifact, which meant that the Browns were out of the hunt.";
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/recap/105/th_105_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/recap/105/th_105_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/recap/105/th_105_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/recap/105/th_105_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/recap/105/th_105_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/recap/105/th_105_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/recap/105/th_105_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/recap/105/th_105_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/recap/105/th_105_09.jpg";
	imgs[10] = "/Treasure_Hunters/images/recap/105/th_105_10.jpg";
	imgs[11] = "/Treasure_Hunters/images/recap/105/th_105_11.jpg";
	imgs[12] = "/Treasure_Hunters/images/recap/105/th_105_12.jpg";
	imgs[13] = "/Treasure_Hunters/images/recap/105/th_105_13.jpg";
	imgs[14] = "/Treasure_Hunters/images/recap/105/th_105_14.jpg";
	imgs[15] = "/Treasure_Hunters/images/recap/105/th_105_15.jpg";
	imgs[16] = "/Treasure_Hunters/images/recap/105/th_105_16.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];
		}
		
	

}