//
// LEFTNAV FUNCTIONS
//

/*-----[ Left Navigation Functions ]---------------*/

var id = "";
var name = "";
var url = "";
var navType = "";
var level = "";
var itemNum = "";
var hiding = "";

var currentNavType = "";
var currentNavLevel = "";

//  Default filename of index.html assigned if none specified in
// URL (which happens if href is just a directory)
if (!pageName ||  pageName == " "){
    pagePath=loc;
    pageName="index.html";
    loc=loc + pageName;
}else{
    pagePath=loc.replace(pageName, '');
}

/*-----[ FUNCTION navLink ]---------------*/

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 ]---------------*/

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

/*-----[ FUNCTION displayVarList ]---------------*/

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

/*-----[ FUNCTION isFirstItemType ]---------------*/

function isFirstItemType(arrayIndex, itemType, itemLevel){

  if (arrayIndex== 0){
    return true;
  }

  if (navArray){

    for (var i=0; i < navArray.length; i++){
      currentNavType = navArray[i].navType;
      currentNavLevel = navArray[i].level;
      if (currentNavType == itemType && currentNavLevel == itemLevel && i < arrayIndex){
        return false;
      }
    }
    return true;
  }

}// end function

/*-----[ FUNCTION isLastArrayItem ]---------------*/

function isLastArrayItem(arrayIndex){
  if (navArray){
    var arrayLength = navArray.length;
    if ((arrayIndex+1) == arrayLength){
      return true;
    } else {
      return false;
    }
  }
}// end function

/*-----[ FUNCTION displayNav ]---------------*/

//**********
// function displayNav();
//
// Object navLink has attributes id,name,url,level, itemNum, and subItemNum
//**********
function displayNav(){

//**********
// Initialize URL & Path Variables
//**********
var mainLinkID= "";
var pagePath=       "";

///**********
// 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";

if (!IMGPath){
	var IMGPath ="/images"; /* site-specific */
}
var LHNPath=        IMGPath + "/nav/";
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" alt="" border="0"><br><img src="' + IMGPath + '/spacer.gif" width="3" height="1" alt="" border="0"><img src="' + LHNIcon1 + '" width="4" height="7" alt="" border="0"></td>';
var leftColEmpty=   '<td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" alt="" border="0"></td>';
var midColIMG_ON=   '<td valign="top" align="left"><img src="' + IMGPath + '/spacer.gif" width="1" height="4" alt="" border="0"><br><img src="' + LHNIcon2_on + '" width="3" height="5" alt="" border="0"></td>';
var midColIMG_OFF= '<td valign="top" align="left"><img src="' + IMGPath + '/spacer.gif" width="1" height="4" alt="" border="0"><br><img src="' + LHNIcon2_off + '" width="3" height="5" alt="" border="0"></td>';
var headerSRC=  LHNPath;
var headerHREF, headerWidthHeight, 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;
        itemNum=navArray[i].itemNum;
        subItemNum=navArray[i].subItemNum;
        if(fullURL.indexOf(id)!=-1){
            mainLinkID=id;
            linkItemNum=itemNum;
            linkSubItemNum=subItemNum;
            idAlreadyFound = "true";
        }
    }
}

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

if (idAlreadyFound == "false"){
    for (var i=0; i < navArray.length; i++){
        id=navArray[i].id;
        itemNum=navArray[i].itemNum;
        subItemNum=navArray[i].subItemNum;
        if (loc==id){
            mainLinkID=id;
            linkItemNum=itemNum;
            linkSubItemNum=subItemNum;
            idAlreadyFound = "true";
        }
    }
}

// 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;
        itemNum=navArray[i].itemNum;
        subItemNum=navArray[i].subItemNum;
        if (pagePath==id){
            mainLinkID=id;
            linkItemNum=itemNum;
            linkSubItemNum=subItemNum;
            idAlreadyFound = "true";
        }
    }
}

// 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;
        itemNum=navArray[i].itemNum;
        subItemNum=navArray[i].subItemNum;
        if (fileName==id){
            mainLinkID=id;
            linkItemNum=itemNum;
            linkSubItemNum=subItemNum;
            idAlreadyFound = "true";
        }
    }
}

//**********
// Loop through array. 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=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 /* site-specific */
//**********

if (!isFullyQualifiedURL(url)){
    if (isSecureProtocol){
        serverNameAppend ="http://www.freddiemac.com"; /* site-specific */
        url =serverNameAppend+url;
    } else if (!onTS && !onCMS && !onDev && !onQA){
        serverNameAppend ="http://www.freddiemac.com"; /* site-specific */
        url =serverNameAppend+url;
    }
}

    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 (level=="3"){
            code_mid_column=midColIMG_ON;
        }

    }
    else if (pageName!=id) {linkClass="mL"+level;}

//**********
//  display
//**********

    // level = 0 indicates a header image
    if (level==0){
        if (i == 0){
            // add vertical space if item header image is first one
            code += '<tr><td colspan="4" class=""><img src="' + IMGPath + '/spacer.gif" width="1" height="7" alt="" border="0" /></td></tr>';
        }
        else {
            if (navArray[i-1].level != 0){
                // add vertical space if last item was not another header
                code += '<tr><td colspan="4" class=""><img src="' + IMGPath + '/spacer.gif" width="1" height="9" alt="" border="0" /></td></tr>';
            }
            // add divider image if not first header
            code += '<tr><td colspan="4" class="mLhdrDiv"><img src="' + IMGPath + '/nav/nav_divider_2solid.gif" alt="" border="0" /></td></tr>';
        }
        // add header image and link
        code += '<tr><td colspan="4" class=""><a href="' + url + '" class="mL0">'+name+'</a></td></tr>\n';
    }
    if (level==1){
    // if the row is not the first, add a row containing the image divider
        if (i != 0){
            if (navArray[i-1].level != 0){
                code += '<tr>' + leftColEmpty + '<td colspan="2" class="nDiv"><img \nsrc="' + LHNDivider1 + '" width="126" height="1" vspace="2" alt="" border="0"></td><td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" alt="" 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><td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" alt="" border="0"></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="4"><img \nsrc="' + IMGPath + '/spacer.gif" width="1" height="1" vspace="1" alt="" border="0"></td></tr>\n';
        }else{code += '<tr>' + leftColEmpty +  '<td colspan="2" class="nDiv"><img \nsrc="' + LHNDivider2 + '" width="119" height="1" vspace="2" alt="" border="0"></td><td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" alt="" border="0"></td></tr>\n';}
        code += '<tr>' + leftColEmpty + code_mid_column + '<td><a href="'+url+'" class="'+linkClass+'" class=&{ns4class};>'+name+'</a></td><td><img src="' + IMGPath + '/spacer.gif" width="1" height="1" alt="" border="0"></td></tr>\n';
    }
    else if (level==3 && linkItemNum==itemNum && linkSubItemNum==subItemNum){
        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" alt="" border="0"></td>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="7" height="1" alt="" border="0"></td>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="119" height="1" alt="" border="0"></td>';
code += '<td><img src="' + IMGPath + '/spacer.gif" width="5" height="1" alt="" border="0"></td>';
code +='</tr></table>\n<br>';
d.write(code);
// d.write('<span style="color:#DBE8F8">IMGPath=' + IMGPath + '</span>');

} // end function
