// JavaScript Document

function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
	
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}


//-->

window.onload=function(){
	QueryString_Parse();	
	var sText = QueryString("id"); // Get's the value of ID.
	if (sText == null){
		var sText = 1;
	}else{
		var sText = QueryString("id"); // Get's the value of ID.
	}
	
	///// STORY COPY GOES HERE IN EACH VARIABLE /////
	var story = new Array();
	
	///// IMAGE LOCATIONS GO IN EACH VARIABLE /////
	var imgs = new Array();
	imgs[1] = "/Treasure_Hunters/images/photos/110/live/th_110_live_01.jpg";
	imgs[2] = "/Treasure_Hunters/images/photos/110/live/th_110_live_02.jpg";
	imgs[3] = "/Treasure_Hunters/images/photos/110/live/th_110_live_03.jpg";
	imgs[4] = "/Treasure_Hunters/images/photos/110/live/th_110_live_04.jpg";
	imgs[5] = "/Treasure_Hunters/images/photos/110/live/th_110_live_05.jpg";
	imgs[6] = "/Treasure_Hunters/images/photos/110/live/th_110_live_06.jpg";
	imgs[7] = "/Treasure_Hunters/images/photos/110/live/th_110_live_07.jpg";
	imgs[8] = "/Treasure_Hunters/images/photos/110/live/th_110_live_08.jpg";
	imgs[9] = "/Treasure_Hunters/images/photos/110/live/th_110_live_09.jpg";
	imgs[10] = "/Treasure_Hunters/images/photos/110/live/th_110_live_10.jpg";
	imgs[11] = "/Treasure_Hunters/images/photos/110/live/th_110_live_11.jpg";
	imgs[12] = "/Treasure_Hunters/images/photos/110/live/th_110_live_12.jpg";
	imgs[13] = "/Treasure_Hunters/images/photos/110/live/th_110_live_13.jpg";
	imgs[14] = "/Treasure_Hunters/images/photos/110/live/th_110_live_14.jpg";
	imgs[15] = "/Treasure_Hunters/images/photos/110/live/th_110_live_15.jpg";
	imgs[16] = "/Treasure_Hunters/images/photos/110/live/th_110_live_16.jpg";
	imgs[17] = "/Treasure_Hunters/images/photos/110/live/th_110_live_17.jpg";
	imgs[18] = "/Treasure_Hunters/images/photos/110/live/th_110_live_18.jpg";
	imgs[19] = "/Treasure_Hunters/images/photos/110/live/th_110_live_19.jpg";
	imgs[20] = "/Treasure_Hunters/images/photos/110/live/th_110_live_20.jpg";
	imgs[21] = "/Treasure_Hunters/images/photos/110/live/th_110_live_21.jpg";
	imgs[22] = "/Treasure_Hunters/images/photos/110/live/th_110_live_22.jpg";
	imgs[23] = "/Treasure_Hunters/images/photos/110/live/th_110_live_23.jpg";
	imgs[24] = "/Treasure_Hunters/images/photos/110/live/th_110_live_24.jpg";
	imgs[25] = "/Treasure_Hunters/images/photos/110/live/th_110_live_25.jpg";
	imgs[26] = "/Treasure_Hunters/images/photos/110/live/th_110_live_26.jpg";
	imgs[27] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_01.jpg";
	imgs[28] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_02.jpg";
	imgs[29] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_03.jpg";
	imgs[30] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_04.jpg";
	imgs[31] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_05.jpg";
	imgs[32] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_06.jpg";
	imgs[33] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_07.jpg";
	imgs[34] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_08.jpg";
	imgs[35] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_09.jpg";
	imgs[36] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_10.jpg";
	imgs[37] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_11.jpg";
	imgs[38] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_12.jpg";
	imgs[39] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_13.jpg";
	imgs[40] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_14.jpg";
	imgs[41] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_15.jpg";
	imgs[42] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_16.jpg";
	imgs[43] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_17.jpg";
	imgs[44] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_18.jpg";
	imgs[45] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_19.jpg";
	imgs[46] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_20.jpg";
	imgs[47] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_21.jpg";
	imgs[48] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_22.jpg";
	imgs[49] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_23.jpg";
	imgs[50] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_24.jpg";
	imgs[51] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_25.jpg";
	imgs[52] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_26.jpg";
	imgs[53] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_27.jpg";
	imgs[54] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_28.jpg";
	imgs[55] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_29.jpg";
	imgs[56] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_30.jpg";
	imgs[57] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_31.jpg";
	imgs[58] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_32.jpg";
	imgs[59] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_33.jpg";
	imgs[60] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_34.jpg";
	imgs[61] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_35.jpg";
	imgs[62] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_36.jpg";
	imgs[63] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_37.jpg";
	imgs[64] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_38.jpg";
	imgs[65] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_39.jpg";
	imgs[66] = "/Treasure_Hunters/images/photos/110/episode/th_110_ep_40.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
	setCount = parseFloat(pPicList.title);
	if(!setCount){
		setCount=1;
	}
	
	
	///// THUMBNAILS /////
	if(pPicList){		
		// Set initial vairables for thumbnails
		var thumbNail = "";		
		id = cCopy - ((cCopy -1) % setCount);
		setLimit = id + setCount;
			
			// Write out the thumbnails
			for(id; id < setLimit; id++){
				if(imgs[id]){
					thumbNail = thumbNail + "<div class='thumb'><a href='?id="+id+"'><img src='"+imgs[id].replace(".jpg" ,"_sm.jpg")+"' alt='"+story[id]+"' /></a></div>";
				}
			}
		
		// 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;
		}
		
		//alert(setLimit);
		//tBack = ((id-setCount)-setCount);
		
		//alert(tBack+" "+((setCount-id)+setCount));
		
		
		////// 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;	
			}
			
			// 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.getElementById('mainImg').style.backgroundImage="url("+imgs[sTextNum]+")"; 
		
		//Writes out the story copy
		if(pCopy){
			pCopy.innerHTML=story[sText];
		}
		
	

}