/***************
Left Navigation Functions
*/

var d = document;

var filePath ="";
var serverNameAppend ="";

var fullPath =location.href;
var fullPathElements =fullPath.split("/");
var serverProtocol =fullPathElements[0];
var serverName =fullPathElements[2];
var protocolAndHost =serverProtocol+"//"+serverName;

var onTS =serverName.indexOf('he2unx49') !=-1?true:false;
var onCMS =serverName.indexOf('cms') !=-1?true:false;
var onDev =(serverName.indexOf('extcdev') !=-1) || (serverName.indexOf('extcdev') !=-1)?true:false;
var onQA =(serverName.indexOf('extcstage') !=-1) || (serverName.indexOf('extcstage') !=-1)?true:false;
var isSecureProtocol=(serverProtocol.indexOf('https') !=-1)?true:false;

var loc=location.pathname;
var pathElements=loc.split("/");
var fileName=pathElements[(pathElements.length-1)];

/***************
function navLink(id,name,url,level,itemNum,subItemNum);
*/


function navLink(id,name,url,level,itemNum,subItemNum) {
	this.id=id;
	this.name=name;
	this.url=url;
	this.level=level;
	this.itemNum=itemNum;
    this.subItemNum=subItemNum;
}// end function


/***************
function isFullyQualifiedURL(urlString);
*/


function isFullyQualifiedURL(urlString){
	var booleanValue =(urlString.indexOf('http://') !=-1) || (urlString.indexOf('https://') !=-1) || (urlString.indexOf('mailto:') !=-1)?true:false;

	return booleanValue;
}


/***************
 function displayVarList();
*/


function displayVarList(){
	var code ="";
	code +=fullPath;
	code +='<br>serverProtocol: '+serverProtocol;
	code +='<br>serverName:'+serverName;
	code +='<br>protocolAndHost: '+protocolAndHost;
	code +='<br>onQA: '+onQA;
	code +='<br>onTS: '+onTS;
	code +='<br>onCMS: '+onCMS;
	code +='<br>onDev: '+onDev;
	code +='<br>onQA: '+onQA;
	code +='<br>isFullyQualifiedURL should be true: '+isFullyQualifiedURL('https://www.freddiemac.com');
	code +='<br>isFullyQualifiedURL should be false: '+isFullyQualifiedURL('/corporate/index.html');
	code +='<br>isSecureProtocol: '+isSecureProtocol;
	d.write(code);
}

/***************
 function displayNav();

 Object navLink has attributes id,name,url,level, and itemNum
*/


function displayNav(){

/***************
 Initialize URL & Path Variables
*/

var mainLinkID=	"";
var linkType=	"";
var pagePath= "";
var terminateNum= 999;
var ignoreNum= 999;

/***************
 Initialize Display Variables
*/

var code=""; // This variable will hold all of the code and be written out once its complete. This speeds up processing time.
var code_header="";
var code_left_column="";
var code_mid_column="";

var linkClass, linkItemNum, linkSubItemNum ="";
var id, name, url, level, itemNum, subItemNum = "";
var idAlreadyFound = "false";
var isFirstSubNavItem = "false";

var LHNPath=		IMGPath + "/leftnav/";
var LHNDivider1=	LHNPath + "nav_divider_solid.gif";
var LHNDivider2=	LHNPath + "nav_divider_dotted.gif";
var LHNIcon1=		LHNPath + "nav_arrow4x7_on.gif";
var LHNIcon2_on=	LHNPath + "nav_arrow3x5_on.gif";
var LHNIcon2_off=	LHNPath + "nav_arrow3x5.gif";

var leftColIMG=		'<td valign="top" align="left"><img src="' + IMGPath + '/spacer.gif" width="1" height="2" border="0"><br><img src="' + IMGPath + '/spacer.gif" width="3" height="1" border="0"><img src="' + LHNIcon1 + '" width="4" height="7" border="0"></td>';
var leftColEmpty= 	'<td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" border="0"></td>';

var midColIMG_ON=	'<td valign="top" align="left"><img src="' + IMGPath + '/spacer.gif" width="1" height="4" border="0"><br><img src="' + LHNIcon2_on + '" width="3" height="5" border="0"></td>';
var midColIMG_OFF= '<td valign="top" align="left"><img src="' + IMGPath + '/spacer.gif" width="1" height="4" border="0"><br><img src="' + LHNIcon2_off + '" width="3" height="5" border="0"></td>';

var headerSRC=	LHNPath;
var headerHREF = '';
var headerWidthHeight = '';
var headerALT="";

/***************
 Grab the page's URL info.
*/

var fullURL=location.href;
var loc=location.pathname;
var pathElements=loc.split("/");
var pageName=pathElements[(pathElements.length-1)];

// Check to see if filename isn't specified, which happens if an href is just a directory. The default filename will be index.html

if (!pageName ||  pageName == " ")
{
	pagePath=loc;
	pageName="index.html";
	loc=loc + pageName;
}
else
{
	pagePath=loc.replace(pageName, '');
}

/***************
 Search for leftnav header and splice from array.
 Then add to code. Currently there shouldn't be
 any headers, but the functionality IS supported!
*/

if (navArray[0].id=="leftnav_header")
{
	headerSRC+=navArray[0].level;
	headerHREF=navArray[0].url;
	headerWidthHeight=navArray[0].itemNum;
	headerALT=navArray[0].name;

	code_header='<img src="' + headerSRC + '" ' + headerWidthHeight + ' border="0" ' + headerALT + '>';
	if (navArray[0].id!="_____")
	{
		code_header='<a href="' + headerHREF + '">' + code_header + '</a>';
	}
	code_header+='<br>';

	navArray.splice(0, 1);

} // end if

code += code_header + '<table width="135" border="0" cellspacing="0" cellpadding="0">';

/***************
 Determine type of id (fullURL/page/directory/)
*/

// Search for the full file path, name, and querystring. If found, set idAlreadyFound to "true"

if(fullURL.indexOf("?")!=-1)
{
	for (var i=0; i < navArray.length; i++){
		id=navArray[i].id;
		name=navArray[i].name;
		itemNum=navArray[i].itemNum;
		if(fullURL == id)
		{
			if(name!="terminate" && name!="ignore")
			{
				mainLinkID=id;
				linkItemNum=itemNum;
				linkSubItemNum=subItemNum;
				idAlreadyFound = "true";
				linkType="fullURL/page/directory";
			}
			else if (name=="terminate")
			{
				terminateNum=itemNum;
			}
			else
			{
				ignoreNum=itemNum;
			}
		}
	}
}

// If the full path does not contain a querystring, search for the file name. If found, set idAlreadyFound to "true"

if (idAlreadyFound == "false")
{
	for (var i=0; i < navArray.length; i++)
	{
		id=navArray[i].id;
		name=navArray[i].name;
		itemNum=navArray[i].itemNum;
		if (loc == id)
		{
			if(name!="terminate" && name!="ignore")
			{
				mainLinkID=id;
				linkItemNum=itemNum;
				linkSubItemNum=subItemNum;
				idAlreadyFound = "true";
				linkType=	"page/directory";
			}
			else if (name=="terminate")
			{
				terminateNum=itemNum;
			}
			else
			{
				ignoreNum=itemNum;
			}
		}
	}
}

// If the file name was not found, search for a file path match

if (idAlreadyFound == "false")
{
	for (var i=0; i < navArray.length; i++)
	{
		id=navArray[i].id;
		name=navArray[i].name;
		itemNum=navArray[i].itemNum;
		subItemNum=navArray[i].subItemNum;
        if (pagePath.indexOf(id) !=-1)
		{
			if(name!="terminate" && name!="ignore")
			{
				mainLinkID=id;
				linkItemNum=itemNum;
	            linkSubItemNum=subItemNum;
				idAlreadyFound = "true";
				linkType=	"/directory";
			}
			else if (name=="terminate")
			{
				terminateNum=itemNum;
			}
			else
			{
				ignoreNum=itemNum;
			}
		}
	}
}

/***************
 Assign properties of page and assign classes.
*/

for (var i=0; i < navArray.length; i++){

	id=navArray[i].id;
	name=navArray[i].name;
	url=navArray[i].url;
	level=navArray[i].level;
	itemNum=parseInt(navArray[i].itemNum);
	subItemNum=navArray[i].subItemNum;

/***************
 Append protocol and host to url if current page is secured
 Appends www.freddiemac.com if host is not recognized
*/

if (!isFullyQualifiedURL(url)){
	if (isSecureProtocol){
		url =protocolAndHost+url;
	}
	else if (!onTS && !onCMS && !onDev && !onQA){
		serverNameAppend ="http://www.freddiemacfoundation.org";
	}
}

	code_left_column=leftColEmpty;
	code_mid_column=midColIMG_OFF;

// check to see if the link correlates to the page we are on
	if (mainLinkID==id)
	{
		linkClass="mLOn"+level;

		if (level=="1")
		{
			code_left_column=leftColIMG;
		}
		else if (level=="2")
		{
			code_mid_column=midColIMG_ON;
		}
	}
	else if (pageName!=id) {linkClass="mL"+level;}

/***************
 Loop through array and display.
*/

if (level==0){
    // level = 0 once indicated a header image, but it is no longer supported       
    }
if (level==1 && itemNum<terminateNum && name!="terminate" && itemNum!=ignoreNum && name!="ignore"){
	// if the row is not the first, add a row containing the image divider
		if (i != 0)
		{
			code += '<tr>' + leftColEmpty + '<td colspan="2" class="nDiv"><img \nsrc="' + LHNDivider1 + '" width="126" height="1" vspace="2" border="0"></td></tr>\n';
		}
		code += '<!-- Item Level 1 --><tr>' + code_left_column + '<td colspan="2"><a href="'+url+'" class="'+linkClass+'" class=&{ns4class};>'+name+'</a></td></tr>\n';
		isFirstSubNavItem="true";
	}
	else if (level==2 && linkItemNum==itemNum)
	{
	// if the subrow is not the first, add a row containing the image divider. Otherwise, set it to false and move on.
		if (isFirstSubNavItem=="true")
		{
			isFirstSubNavItem="false";
			code += '<tr>' + leftColEmpty +  '<td colspan="3"><img \nsrc="' + IMGPath + '/spacer.gif" width="1" height="1" vspace="1" border="0"></td></tr>\n';
		}
		else
		{
			code += '<tr>' + leftColEmpty +  '<td colspan="2" class="nDiv"><img \nsrc="' + LHNDivider2 + '" width="119" height="1" vspace="2" border="0"></td></tr>\n';
		}
		code += '<tr>' + leftColEmpty + code_mid_column + '<td><a href="'+url+'" class="'+linkClass+'" class=&{ns4class};>'+name+'</a></td></tr>\n';
	}
	else if (level==3 && linkItemNum==itemNum)
	{
	//	code += '<tr>' + emptyTD + midColIMG_OFF + '<td><a href="'+url+'" class="'+linkClass+'" class=&{ns4class};>'+name+'</a></td></tr>\n';
        code += '<tr><td colspan="5" class="navLevel3"><div><a href="'+url+'" class="'+linkClass+'" class=&{ns4class};>'+name+'</a></div></td></tr>\n';
	}
}
// divider row & spacer row
code += '<tr>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="9" height="1" border="0"></td>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="7" height="1" border="0"></td>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="119" height="1" border="0"></td>';
code +='</tr></table>\n<br><img src="' + IMGPath + '/spacer.gif" width="140" height="1" border="0">';
d.write(code);

// display variables
/*
d.write('<br /><br />');
d.write('isSecureProtocol = ' + isSecureProtocol + '<br />');
d.write('idAlreadyFound = ' + idAlreadyFound + '<br />');
d.write('linkType = ' + linkType + '<br />');
d.write('mainLinkID = ' + mainLinkID + '<br />');
d.write('linkItemNum = ' + linkItemNum + '<br />');
d.write('idAlreadyFound = ' + idAlreadyFound + '<br />');
*/

} // end function


