// 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] = "Almost two hours had passed and the Geniuses still hadn't cracked the code that would unlock the secrets of their cryptex and thus the treasure. Meanwhile, Air Force and the Southie Boys struggled to find the entrance to the hidden chamber.";
	story[2] = "Finally, the Southies found their way into the staircase that wound down to the secret chamber. Soon after, Air Force also found their way to the stairs, joining the Southies at the chamber's entrance. Team Air Force figured out how to open the door, reuniting all three teams in their battle for the gold.";
	story[3] = "The playing field was equal as all three teams worked to solve the code, but after six and a half hours since the Geniuses entered the chamber, not one team was close to cracking the code.";
	story[4] = "And then it happened. Francis entered a five-letter sequence into the cryptex - FSKEY - and startled himself when it popped open, revealing a symbol-covered metallic arrow! Putting the arrow on a dial, they gave it a spin and it landed on the symbol for a star.";
	story[5] = "Pounding a star symbol embedded in the wall opened a secret chamber where a mound of gold awaited the winning team...the Geniuses!";
	story[6] = "All season long, thousands of people have played along online in their own virtual hunt for a $100,000 prize! And now, the ten finalists gathered in Washington, D.C. outside the Museum of American History where Laird stood in front of their first clue. After this stage, five of them would be eliminated.";
	story[7] = "The clue told them to find the castle that once housed the original star spangled banner. Now, the flag that fluttered high above its tower held the code to the next clue. The teams raced off to locate the castle, finding five lockboxes waiting for them outside. Scott was the first to crack the code that opened the lockbox and the clue it contained: The statue of Joseph Henry will show you the way. Luann, Karla, John, and Helen were next to open the boxes - the other five were going home.";
	story[8] = "The clue led them to the National Archives building where three lockboxes awaited. Scott, Karla, and Luann were the first three to find them, which meant that John and Helen were out of the challenge. Inside the boxes was a cryptic stencil leading to the Old Post Office.";
	story[9] = "Scott reached the Old Post Office's tower were he held the stencil over a map that revealed a large \"X\" marking the spot: the National Building Museum. With Karla and Luann hot on his heels, Scott made his way to the building where he became the winner of the Genworth Financial Treasure Challenge!";
	
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/recap/110/th_110_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/recap/110/th_110_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/recap/110/th_110_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/recap/110/th_110_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/recap/110/th_110_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/recap/110/th_110_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/recap/110/th_110_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/recap/110/th_110_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/recap/110/th_110_09.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.mainImg2.src=imgs[cCopy]; 
		
		//Writes out the story copy
		if(pCopy){
			pCopy.innerHTML=story[sText];
		}
		
	

}