// ## performance.js
// 	Contains the dynamic functionality for the display of the two information columns that inform the user about various performance features.

// FUNCTION LIST:
// 	articleObject();
// 	fillArray();
// 	preLoadImages();
//	displayColumn();
// 	buildColumn1();
// 	buildColumn2();
// 	displayAll();
// 	getParams();
//	loadDefaultAttributes();

// TEMPLATE(S) USED:
// 	performance.html


var isDisplayAll = getParams();	// Called when user selects "Display all"
var newAddress;
var URLString = location.href.split('cfm');
var strURL = URLString[0] + 'cfm?display#midpageAnchor';
var isAnchored = false;

var params;

// Creates the array to be used for storing informational text.
function articleObject(imageName, articleName, text, ftNote) {
	this.imageName = imageName;
	this.articleName = articleName;
	this.text = text;
	this.linkName = makeLinkName(articleName);
	this.footNote = ftNote;
}

// Fills the array to be used for storing the informational text. This is done in the body of the HTML document (performance.html).
function fillArray(imageName, articleName, text, ftNote) {
    articleArray[objectArrayIndex++] = new articleObject(imageName, articleName, text, ftNote);
}

// Starts array at "1".  
var objectArrayIndex = 1;
var articleArray = new Array();

// Preloads images so dynamic functionality appears to be instantaneous.
function preLoadImages() {
	for (intIndex=1;intIndex<=articleArray.length-1;intIndex++) {
		if(articleArray[intIndex].imageName!="")
		{
			var newImage = new Image();
			newImage.src = articleArray[intIndex].imageName;
		}
	}
}

// Fills the appropriate <div> with information from the array. Outputs the appropriate HTML code and array variables.
function displayColumn(startVal,endVal) {
	var articleText = "";
	var i;

	if (!isDisplayAll) {
		for (i=startVal;i<=endVal;i++) {
			articleText += '<div id="col' + i + 'Div">';
			articleText += '&nbsp;<table border="0" cellpadding="0" cellspacing="0" valign="top" width="196">';
			articleText += '<tr><td valign="top" width="196" class="EmphasisText"><b>' + articleArray[i].articleName + '</b><br />' + articleArray[i].text + '</td></tr>';
			articleText += '<tr><td height="20"></td></tr>';
			articleText += '</table>';
			articleText += '<table border="0" cellpadding="0" cellspacing="0" valign="top" width="196">';
			articleText += '<tr><td class="footnote">' + articleArray[i].footNote + '</td></tr>';
			articleText += '<tr><td height="20"></td></tr>';
			articleText += '</table></div>';
		}
		return articleText;
	} else {
		return '';
	}
}

// Controls the visibility of column one (1) and swaps the image source with the corresponding image.
function buildColumn1(numVal){
	if (isDisplayAll) {
		window.location.hash = articleArray[numVal].linkName;
	} else if (doneLoadingAttrib) {
		if(articleArray[numVal].imageName!="")
		{
			document.images["colImage1"].src = articleArray[numVal].imageName;
		}
		divObj = eval('col' + numVal);
		oldDivObj = eval('col' + oldColumn1);
		oldDivObj.hide();
		divObj.show();
		oldColumn1 = numVal;
		//if (isAnchored) location.hash = "midpageAnchor";
	}
}

// Controls the visibility of column two (2) and swaps the image source with the corresponding image.
function buildColumn2(numVal){
	if (isDisplayAll) {
		window.location.hash = articleArray[numVal].linkName;
	} else if (doneLoadingAttrib) {
		if(articleArray[numVal].imageName!="")
		{
			document.images["colImage2"].src = articleArray[numVal].imageName;
		}
		divObj = eval('col' + numVal);
		oldDivObj = eval('col' + oldColumn2);
		oldDivObj.hide();
		divObj.show();
		oldColumn2 = numVal;
		//if (isAnchored) location.hash = "midpageAnchor";
	}
}

// Passes in start and end identifiers of the pre-filled array for "display all" option.
function displayAll(start, end){

	if (isDisplayAll) {
		var displayAllText = '<table width="196" border="0" cellpadding="0" cellspacing="0">';
		for (var i = start; i <= end; i++) {
			if(articleArray[i].imageName!="")
			{			
				displayAllText += '<tr><td height="90"><a name="' + articleArray[i].linkName + '"><img src="' + articleArray[i].imageName + '" alt="Photo" width="193" height="90" border="0"></a></td></tr>';
				displayAllText += '<tr><td height="15" width="174" nowrap></td></tr>';			
			}
			displayAllText += '<tr><td class="EmphasisText" valign="top"><b>' + articleArray[i].articleName + '</b></span></td></tr>';
			displayAllText += '<tr><td height="4"></td></tr>';
			displayAllText += '<tr><td class="EmphasisText" valign="top">'+ articleArray[i].text + '</td></tr>';
			displayAllText += '<tr><td height="10"></td></tr>';
			displayAllText += '<tr><td class="footnote">' + articleArray[i].footNote + '</td></tr>';
			displayAllText += '<tr><td height="18" nowrap></td></tr>';
		}
		displayAllText += '</table>';		
		return displayAllText;
	}
	else return '';
}

// checks to see if "display" is in the url.
function getParams() {
	var NewPage = window.location.href;
	var idx = NewPage.indexOf("?");
	params = new Array();
	if (idx != -1) {
		var pairs = NewPage.substring(idx+1, NewPage.length).split("#");
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split("=");
			params[nameVal[0]] = nameVal[1];
			var result = pairs[0];	
		}
	}
	if (result == "display") return true;
	else return false;	
}

// Creates the link name that will be stored in the information array (articleArray).
function makeLinkName(linkNames) {
	var i;
 	for (var text = '',i=0;i<=linkNames.length;i++){
		if (linkNames.charAt(i) != ' ') text += linkNames.charAt(i);	
	}
	return text;
}

var oldColumn1;
var oldColumn2;
var doneLoadingAttrib = false;

// Loads the default information to be displayed when the user first arrives to the page.
function loadDefaultAttributes() {
	oldColumn1 = 1;
	oldColumn2 = inColDiv;

	preLoadImages();

	if (!isDisplayAll) {
		relLeftColumn.show();
		relRightColumn.show();

		doneLoadingAttrib = true;
		buildColumn1(oldColumn1);
		buildColumn2(oldColumn2);

	}

}