// main menu img array
menu = new Array()
menu[0] = new Image()
menu[0].src = "images/profileOut.png"
menu[1] = new Image()
menu[1].src = "images/profileOver.png"
menu[2] = new Image()
menu[2].src = "images/processOut.png"
menu[3] = new Image()
menu[3].src = "images/processOver.png"
menu[4] = new Image()
menu[4].src = "images/portfolioOut.png"
menu[5] = new Image()
menu[5].src = "images/portfolioOver.png"
menu[6] = new Image()
menu[6].src = "images/pricingOut.png"
menu[7] = new Image()
menu[7].src = "images/pricingOver.png"

// sub menu img array
subMenu = new Array()
subMenu[0] = new Image()
subMenu[0].src = "images/subMenuImgAOut.jpg"
subMenu[1] = new Image()
subMenu[1].src = "images/subMenuImgAOver.png"
subMenu[2] = new Image()
subMenu[2].src = "images/subMenuImgBOut.jpg"
subMenu[3] = new Image()
subMenu[3].src = "images/subMenuImgBOver.png"
subMenu[4] = new Image()
subMenu[4].src = "images/subMenuImgCOut.jpg"
subMenu[5] = new Image()
subMenu[5].src = "images/subMenuImgCOver.png"
subMenu[6] = new Image()
subMenu[6].src = "images/subMenuImgDOut.jpg"
subMenu[7] = new Image()
subMenu[7].src = "images/subMenuImgDOver.png"
subMenu[8] = new Image()
subMenu[8].src = "images/subMenuImgEOut.jpg"
subMenu[9] = new Image()
subMenu[9].src = "images/subMenuImgEOver.png"
subMenu[10] = new Image()
subMenu[10].src = "images/subMenuImgFOut.jpg"
subMenu[11] = new Image()
subMenu[11].src = "images/subMenuImgFOver.png"


function rollOut(btn){
		switch(btn){
		case "profile":
			document.profile.src = menu[0].src;
			break;
		case "process":
			document.process.src = menu[2].src;
			break;
		case "portfolio":
			document.portfolio.src = menu[4].src;
			break;
		case "pricing":
			document.pricing.src = menu[6].src;
			break;
		case "subMenuImgA":
			document.subMenuImgA.src = subMenu[0].src;
			break;
		case "subMenuImgB":
			document.subMenuImgB.src = subMenu[2].src;
			break;
		case "subMenuImgC":
			document.subMenuImgC.src = subMenu[4].src;
			break;
		case "subMenuImgD":
			document.subMenuImgD.src = subMenu[6].src;
			break;
		case "subMenuImgE":
			document.subMenuImgE.src = subMenu[8].src;
			break;
		case "subMenuImgF":
			document.subMenuImgF.src = subMenu[10].src;
			break;	
		default:
			break;
		}
		return true;
}

function rollOver(btn){
		switch(btn){
		case "profile":
			document.profile.src = menu[1].src;
			break;
		case "process":
			document.process.src = menu[3].src;
			break;
		case "portfolio":
			document.portfolio.src = menu[5].src;
			break;
		case "pricing":
			document.pricing.src = menu[7].src;
			break;
		case "subMenuImgA":
			document.subMenuImgA.src = subMenu[1].src;
			break;
		case "subMenuImgB":
			document.subMenuImgB.src = subMenu[3].src;
			break;
		case "subMenuImgC":
			document.subMenuImgC.src = subMenu[5].src;
			break;
		case "subMenuImgD":
			document.subMenuImgD.src = subMenu[7].src;
			break;
		case "subMenuImgE":
			document.subMenuImgE.src = subMenu[9].src;
			break;
		case "subMenuImgF":
			document.subMenuImgF.src = subMenu[11].src;
			break;
		default:
			break;
		}
		return true;
}

function resetDropdownList(list, i){
	list.selectedIndex=i;
}

//this is triggered from the portfolio landing page, creating cookies with the celebrity choice of the user and navigating to the portfolioCelebrity page
function sendContentValuesAndNav(nameAndQty){
	//split the values apart
	var valuesArray = nameAndQty.split(',');
	//values extracted in preparation for the cookies, using reverse order for Safari since it places the split variables into the array in reverse of all other browsers
	//further, the if condition below is a result of the word Safari being present in the navigator.appVersion string of the chrome browser as well
	//so since the or operator will evaluate to true if the equasion on either side is true, the statment works perfect for all conditions and reads
	//as follows: if the user is not using safari OR if the user is using chrome
	if ((navigator.appVersion.indexOf("Safari") == -1) || (navigator.appVersion.indexOf("Chrome") != -1)){
		var name = valuesArray[0];
		var qty = valuesArray[1];
	} else {
		var name = valuesArray[1];
		var qty = valuesArray[0];
	}
	document.cookie ='name=' + name + '; path=/'
	document.cookie ='qty=' + qty + '; path=/'
	window.location.href= "portfolioCelebrity.html";
}

var name;
var qty;

//this is triggered from the portfolioCelebrity page, extracting the values of the cookies and calling the changeImg function with the initialize parameter
function getCookiesAndIniContent(){
	var allCookies = document.cookie;
	//this separates the cookies
	var tempArray  = allCookies.split('; ');
	//this extracts the values and applies them to the global variables declared above
	name = tempArray[0].split('=')[1];
    qty = tempArray[1].split('=')[1];
	changeImg("initialize");
}

//this is triggered from the other portfolio pages, initializing the data //////////////////////////////////////////////////////////////////////////////////////////
function iniContent(){
	updateText(1, "initialize");
}

//this is triggered from the portfolio celebrity page on selecting a different option from the dropdown list, it therefore can bypass the above two functions which rely on cookies
function sendContentValuesAndReload(nameAndQty){
	//split the values apart
	var valuesArray = nameAndQty.split(',');
	//values extracted in preparation for the cookies
	name = valuesArray[0];
	qty = valuesArray[1];
	changeImg("re-load");
}

//this is triggered from the rest of the portfolio pages, it doesn't need cookies because the initialization is static
function sendContentValues(nameAndQty){
	//split the values apart
	var valuesArray = nameAndQty.split(',');
	//values extracted in preparation for the cookies
	name = valuesArray[0];
	qty = valuesArray[1];
}

//this function prepares the variables to be used by updateImage and updateText
function changeImg(direction){
	//variables below parse the current src property of the contentImg object in preparation for the folder name to be inserted or it's number to be incremented/decremented 
	//and pass the rest of it to the updateImage and updateText functions for re-assembly
	var imageSrcString = document.getElementById('contentImg').src;		//get src property of contentImg
	var numIndex = imageSrcString.indexOf(".jpg") - 2;					//find index of .jpg portion of string and subtract 2 to be at number position (or the folder name position for initializing)
	var firstHalfOfSrcString = imageSrcString.substring(0, numIndex);	//extract and store first half of src string for re-assbembly
	var currentImg = imageSrcString.substring(numIndex, numIndex + 2);	//extract that number for incrementing/decrementing
	var imgNumber = parseInt(currentImg, 10);					//convert it from a string to a number 
																//had to include the second argument (the radix) because parseInt assumes you are using an octal base in which case "08" and "09" are 0
	//determines whether to initialize, re-load, increment or decrement
	switch(direction){
	case "initialize":
		imgNumber = 1;
		updateImage(imgNumber, firstHalfOfSrcString, direction);		//sending the initialize variable as a flag
		break;
	case "re-load":
		imgNumber = 1;
		updateImage(imgNumber, firstHalfOfSrcString, direction);		//sending the re-load variable as a flag
		break;
	case "back":
		if (imgNumber == 1){
			imgNumber = qty;
			updateImage(imgNumber, firstHalfOfSrcString);
		}else{
			imgNumber = --imgNumber;
			updateImage(imgNumber, firstHalfOfSrcString);
		}
		break;
	case "forward":
		if (imgNumber == qty){
			imgNumber = 1;
			updateImage(imgNumber, firstHalfOfSrcString);
		}else{
			imgNumber = ++imgNumber;
			updateImage(imgNumber, firstHalfOfSrcString);
		}
		break;
	default:break;
	}
}

//this function updates the image
function updateImage(next, srcString, flag){
	//prep src string based on flag if needed
	switch(flag){
	case "initialize":
		srcString = srcString + name + "/";	
		break;
	case "re-load":
		//loop backwards through the srcString counting the backslashes until arriving at two, then store the index in preparation for placing a new name directory
		var backslashCounter = 0;
		var secondBackslashIndex;
		var i;
		for (i=srcString.length;backslashCounter<2;i--){
			if(srcString.charAt(i) == "/"){
				backslashCounter++;
			}
		}
		secondBackslashIndex = i + 2;	//for some reason the for loop above results in an index that is two characters too far
		srcString = srcString.substring(0, secondBackslashIndex) + name + "/";
		break;
	default:break;
	}
	//update image
	//re-assembling the src string, if condition to add leading zero if needed
	if(String(next).length == 1) {
		document.getElementById('contentImg').src = srcString + "0" + next + ".jpg";
	}else{
		document.getElementById('contentImg').src = srcString + next + ".jpg";
	}
	updateText(next, flag);
}		

//this function updates the text
function updateText(next, flag){
	//set the current image number text to 1 when initializing or re-loading and set the ending image number when initializing or re-loading
	if (flag == "initialize" || flag == "re-load") {
		document.getElementById('currentImgNum').innerHTML = "1";
		document.getElementById('endingImgNum').innerHTML = qty;
	} else {
		document.getElementById('currentImgNum').innerHTML = next;
	}
	var catNum = eval(name + "CatNum[" + next + "]");
	var currentCatNum = document.getElementById('currentCatNum');
	var year = eval(name + "Year[" + next + "]");
	var currentYear = document.getElementById('currentYear');
	var loc = eval(name + "Loc[" + next + "]");
	var currentLocation = document.getElementById('currentLocation');
	//writing the prepared values
	//huge if else condition to account for the differnces in writing HTML text for the celebrity page vs the other sections, specifically regarding the intercap celebrity names
	if (window.location.href.indexOf("Celebrity") != -1){
		//variables and for loop to parse the intercap name and prepare the values
		var formattedName;
		var currentName = document.getElementById('currentName');
		var numOfWords = 1;
		var newWordIndex = new Array();
		//determining the total number of words in the name based on how many capital letters it encounters, starting at one since the first letter is never capitalized
		for (i=0;i<name.length;i++){
			if(name.charCodeAt(i) > 64 && name.charCodeAt(i) < 91){
				newWordIndex[numOfWords] = i;
				numOfWords++;
			}
		}
		//adding spaces between the words depending on how many words are present
		for (i=1;i<numOfWords;i++){
			switch(i){
			case 1:
				formattedName = name.substring(0, newWordIndex[1]) + " ";
				if (formattedName == "tennessee "){
					formattedName = '"tenessee" ';	
				}
				break;
			case 2:
				formattedName += name.substring(newWordIndex[1], newWordIndex[2]) + " ";
				break;
			default:break;
			}
		}
		//this adds the end of the name if it consisted of more than one word, or just adds the whole name if it was only one word
		if (numOfWords > 1){
			formattedName += name.substring(newWordIndex[numOfWords-1]);
			if (formattedName == "nelson Rockefeller"){
				formattedName += "<br>& walter keane";
			}
		} else {
			formattedName = name;	
		}
		//writing the formatted name
		currentName.innerHTML = formattedName;
	} else {
		//writing the subject, this is much simpler for the other sections since the string doesn't have to be parsed from intercap and is just pulled from an array
		//this used to be an else if and have the following code but I think it might just need to be an else instead so that it can work for the other sections as well: if(window.location.href.indexOf("Motorsport") != -1)
		var subject = eval(name + "Subject[" + next + "]");
		var currentSubject = document.getElementById('currentSubject');
		currentSubject.innerHTML = subject;
	}
	//writing the rest of the values
	currentCatNum.innerHTML = catNum;
	currentYear.innerHTML = year;
	currentLocation.innerHTML = loc;
	//set dropdown list
	var list = document.userInput.celebritiesList
	var l = list.length;
	for (i=0;i<l;i++){
		if (document.userInput.celebritiesList[i].value.indexOf(name) != -1){
			document.userInput.celebritiesList[i].selected = "1";
		}
	}
}
