// 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] = "The remaining four teams arrived at the Waldorf Astoria in New York City, one of the world's great hotels. In the morning, Laird instructed them to use a Susan B. Anthony dollar at the Hall of Fame for Great Americans to help them find the secret code to unlock the cryptex.";
	story[2] = "The teams arrived and gathered around a statue of Susan B. Anthony, which led them to a bust of Lafayette. A nearby mailbox contained a key for each team and a message sending them to the Gould Library. At the library, Laird explained that their next clue was hidden in a painting and that \"art imitates life.\"";
	story[3] = "The Geniuses were first to put a series of clues together that pointed them to the Land Title Building in Philadelphia. Working together, the other teams figured out the puzzle and were on their way just as the Geniuses hit the gas pedal.";
	story[4] = "At the Land Title Building, Laird told the Geniuses that the information they needed was on the 22nd floor, but only one of them could go up. When Charles reached the floor, his partners were told that the clue was on a plaque on the building ledge. Charles stepped out onto the ledge and shimmied his way around the building to find the plaque, which said, \"Founder's Hall, Girard.\" Being from Philly himself, Francis knew that it meant Girard Avenue and not a person.";
	story[5] = "Air Force was second to find the clue, followed by the Southie Boys and Ex-CIA, who fell into last place because they didn't have their credit card, which was needed to access the room leading to the ledge.";
	story[6] = "The Geniuses may have jumped the gun. They were lost and had no idea where they were supposed to go as the other three teams arrived at Girard College. There, Laird told them about shipping magnate Steven Girard, who played a crucial role in the War of 1812 by bailing out the government from financial problems. In the building, there were multiple clues that would reveal the code to the cryptex. The last team to open their cryptex would be going home.";
	story[7] = "In the basement, Air Force found a cache of relics, including Girard's record book. Grabbing the book, they left the basement to search for additional clues. Outside on the college grounds, the Southie Boys yelled and screamed at each other as they began their search.";
	story[8] = "Now in last place, the Geniuses finally found Girard College, joining the hunt as the Southie Boys continued to squabble. Inside, Air Force became the first team to open their cryptex by using the word \"liberty.\" Inside their cryptex was a parchment that sent them to the Library of Congress, the location of the seventh and final artifact.";
	story[9] = "Southie Boys were next to find the right clue, leaving only one slot left and a battle for life between the Geniuses and Ex-CIA.";
	story[10] = "Francis found a painting that swung open revealing a secret room containing model ships. Just as the Geniuses found a ship with the name \"Liberty,\" Ex-CIA also found a clue leading them to the same word. It was a photo finish, but the Geniuses beat Ex-CIA to the punch. For Ex-CIA, the hunt was over.";
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/recap/108/th_108_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/recap/108/th_108_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/recap/108/th_108_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/recap/108/th_108_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/recap/108/th_108_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/recap/108/th_108_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/recap/108/th_108_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/recap/108/th_108_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/recap/108/th_108_09.jpg";
	imgs[10] = "/Treasure_Hunters/images/recap/108/th_108_10.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];
		}
		
	

}