/* 
====================================================================
REQUIRED PAGE ELEMENTS:
====================================================================

--------------------------------------------------------------------
THE BREADCRUMB
--------------------------------------------------------------------
<div id="breadcrumbDIV"></div>
--------------------------------------------------------------------
- automatically inserts the breadcrumb trail with function gov_ShowBreadCrumbs();, called from the header javascript file.
--------------------------------------------------------------------


--------------------------------------------------------------------
THE SIDE MENU
--------------------------------------------------------------------
<div id="sidemenu">
<noscript>
<ul>
<li class="LeftHandMenus_l0_off"><a href="">Home</a><li>
<li class="LeftHandMenus_l1_off"><a href="">Level 2</a><li>
</ul>
</noscript>
</div>
--------------------------------------------------------------------
- this is the contents of the side menu that is displayed when javascript is disabled, also serves as a way for spiders to find pages in the site.
- for each level, use the classes  LeftHandMenus_l{level}_on and LeftHandMenus_l{level}_off.  The topmost level is 0.
- the two anchors listed are just examples

--------------------------------------------------------------------


--------------------------------------------------------------------
THE LANGUAGE TOGGLE
--------------------------------------------------------------------
<div id='language'></div>
--------------------------------------------------------------------
- this is included in the gov_showHeader() function
- the contents are automatically replaced with the language text 
  and link to the corresponding page in that language (if available)
--------------------------------------------------------------------


--------------------------------------------------------------------
THE HEADER LINKS
--------------------------------------------------------------------
<div id='headerlinks'></div>
--------------------------------------------------------------------
- this is included in the gov_showHeader() function
- automatically replaced with the contents of arrHeaderLink[] array
--------------------------------------------------------------------


*/




// ================================================
// gov_setupPage() 
// - called from the web pages' OnLoad() event
// - generates the header,footer, language toggle, 
//   side menu, breadcrumbs and header links
// ================================================
function gov_setupPage() {

	// set the proper resourcePath (determining between local and remote template)
	resourcePath = resourcePath+"";
	var protocolPrefix = "";
	if (resourcePath.indexOf("https://") > -1) {
		protocolPrefix = "https://";
		resourcePath = resourcePath.replace("https://", "");
	}
	if (resourcePath.indexOf("http://") > -1) {
		protocolPrefix = "http://";
		resourcePath = resourcePath.replace("http://", "");
		
	}
	
	if (protocolPrefix != "") {
		var theLoc = window.location + "";
		if (theLoc.indexOf("https://") > -1) {
			// we're on a secure server, so don't show non-secure items
			protocolPrefix = "https://";
		}
		if (theLoc.indexOf("http://") > -1) {
			// we're on a non-secure server, so don't show secure items
			protocolPrefix = "http://";
		}
		// the template is located locally, so let's prepend the rootPath to it
		resourcePath = protocolPrefix+resourcePath;
	} else {
		// the template is located locally, so let's prepend the rootPath to it
		resourcePath = protocolPrefix+rootPath+resourcePath;
	}

	gov_determineLanguage();
	gov_checkPrint();
	gov_showHeader();  // within the template.js file
	gov_showFooter();	 // within the template.js file
	if (REPLACE_IFR == 1){ 
		gov_replaceIFR();
	}
	if (REPLACE_LANG == 1) {
		gov_showLangToggle();
	}
	gov_setMenus();
	
	
	if (REPLACE_BREADCRUMB == 1) {
		gov_showBreadCrumbs();
	}
	
	if (REPLACE_LINKS == 1) {
		gov_showHeaderLinks();
	}
}








// =================================================================
// gov_stripSubPath(path, find)
// - removes all portions of the "path" prior to the "find" parameter
// e.g. path="http://www.gov.mb.ca/seeinggreen/index.html",
//      find="/seeinggreen/"
//      returns  "/seeinggreen/index.html"
// =================================================================
function gov_stripSubPath(path, find) {
	var ret;
	var mypath = path;
	ret = false;
	if (path.indexOf("\\")) {
		mypath = mypath.replace(/\\/g, "/");
	}
	if (mypath.indexOf(find) > -1) {
		ret = mypath.substr(mypath.indexOf(find) + find.length);
	}

	return ret;
}






// =================================================================
// gov_arrangeMenus()
// - determines which entries in arrMenu[] will be expanded and shown
//   based on the current page path
// =================================================================
  		function gov_arrangeMenus()
  		{
  		
  			var k=0;
  			var maxLevel = 0;
  			for( var i=0; i<arrMenu.length; i++)
  			{
				if (arrMenu[i]["level"] > maxLevel) {
					maxLevel = arrMenu[i]["level"];
				}
  				if( pagePath + "" == arrMenu[i]["url_"+langID] + "")
  				{
  					if (arrMenu[i]["alt_link_"+langID] + "" == "1") {
						arrMenu[i]["active"] = false;
						arrMenu[i]["show"] = false;
						arrMenu[i]["expand"] = false;
  					} else {
						arrMenu[i]["active"] = true;
						arrMenu[i]["show"] = true;
						arrMenu[i]["expand"] = true;

						// move up and set the parent to expand...
						var curdepth = arrMenu[i]["level"];
						for (ii = i; ii > 0; ii--) {
							if (curdepth > arrMenu[ii]["level"]) {
								arrMenu[ii]["expand"] = true;
								arrMenu[ii]["active"] = true;
								curdepth = arrMenu[ii]["level"];
							}
						}
						k  = i;
						kk = i;
  					}
  				}
  				else
  				{
  					arrMenu[i]["active"] = false;
  					arrMenu[i]["show"] = false;
  					arrMenu[i]["expand"] = false;
  				}

  			}

			
			// loop through the array "maxLevel" times to properly expand the entries
			// basically we only expand the immediate children of that entry... it will naturally expand...


			// always show top level menus
			for( var i=0; i<arrMenu.length; i++)
			{
				if (arrMenu[i]["level"] == 0) {
					arrMenu[i]["show"] = true;
				}
			}
			var expanding = false;
			for (var currentLevel = 0; currentLevel <= maxLevel; currentLevel++) {
				expanding = false;
				for( var i=0; i<arrMenu.length; i++)
				{
					if (arrMenu[i]["expand"] == true) {
						// show immediate children..
						for( var ii=i+1; ii<arrMenu.length; ii++)
						{
							if (arrMenu[ii]["level"] == currentLevel + 1) {
								arrMenu[ii]["show"] = true;
							}  else {
								if (arrMenu[ii]["level"] == currentLevel) {
									break;
								}
							}
						}
					}
				}
			}


  			return;
  		}



function gov_setExpandIcons (obj) {
	var xx,xxx;
	var hasChildren = false;
	for (xx=0; xx< obj.childNodes.length;xx++) {
		if (obj.childNodes[xx].nodeName == "UL") {
			var foundUL = obj.childNodes[xx];
			for (xxx=0; xxx< foundUL.childNodes.length;xxx++) {
				if (foundUL.childNodes[xxx].nodeName == "LI") {
					if (foundUL.childNodes[xxx].className != "altlink") {
						// foundUL.childNodes[xxx].className ="expand";
						obj.className ="expand";
						hasChildren = true;
					}
				}
			}
		}
	}
	
	return hasChildren;

}


// =================================================================
// gov_setMenus()
// - dynamically hides/shows the appropriate side menu links
// =================================================================
function gov_setMenus() {
var obj = document.getElementById("sidemenu");
var cntParents = 0;
var x,xx,xxx,xxxx,foundDIV;
var foundExactLink = -1; // if we've matched the current page to the side menu link
if (obj) {
	// retrieve the contents of the side menu
	var links = obj.getElementsByTagName("a");

	// hide them all first...
	for(x=0; x<links.length; x++) {
		if (links[x].parentNode.nodeName == "LI") {
			links[x].parentNode.style.display = "none";
			links[x].parentNode.parentNode.style.display = "none";
		}
	}
	obj.style.display = "block";

	// show first level links in this node (UL)...
	// just in case we can't match the current page to any side menu links
	for (xx=0; xx< obj.childNodes.length;xx++) {
		if (obj.childNodes[xx].nodeName == "UL") {
			var foundUL = obj.childNodes[xx];
			foundUL.style.display="block";
			for (xxx=0; xxx< foundUL.childNodes.length;xxx++) {
				if (foundUL.childNodes[xxx].nodeName == "LI") {
					foundUL.childNodes[xxx].style.display="block";
					var childUL = foundUL.childNodes[xxx].getElementsByTagName("ul");
					if (childUL.length > 0) {
						foundUL.childNodes[xxx].className ="expand";
						for (var y = 0; y < childUL.length; y++) {
							if (childUL[y].parentNode.nodeName == "LI") {
								childUL[y].parentNode.className ="expand";
								// causes IE to put extra gray under menu
								// childUL[y].parentNode.parentNode.style.display ="block";
							}
						}
					}
				}
			}
		}
	}

	

	var mypath = document.location.href;
	mypath = mypath.replace(/\\\\/g, "/");
	mypath = mypath.replace(/file:\/\//g, "");

	// see if we can match this page to a sidemenu link
	for(x=0; x<links.length; x++) {
		var testlink = links[x] + "";
		testlink = testlink.replace(/\\\\/g, "/");
		testlink = testlink.replace(/file:\/\//g, "");
		if (mypath == testlink) {
			if (links[x].className=="altlink") {
			} else {
				foundExactLink = x;
				x = links.length;
			}
		}
	}

	// see if we can match this page to a sidemenu link without parameters...
	if (foundExactLink  == -1) {
		mypath = document.location.href;
		mypath = mypath.replace(/\\\\/g, "/");
		mypath = mypath.replace(/file:\/\//g, "");
		
		for(x=0; x<links.length; x++) {
			var testlink = links[x] + "";
			testlink = testlink.replace(/\\\\/g, "/");
			testlink = testlink.replace(/file:\/\//g, "");

			if (mypath == testlink || testlink.indexOf(mypath) > -1 || mypath.indexOf(testlink) > -1)  {
				if (links[x].className=="altlink") {
				} else {
					foundExactLink = x;
				}
			}
		}
	}



	// see if we can match this page to a sidemenu link within the same folder...
	if (foundExactLink  == -1) {
		mypath = document.location.href;
		mypath = mypath.replace(/\\\\/g, "/");
		mypath = mypath.replace(/file:\/\//g, "");
		mypath = mypath.substring(0, mypath.lastIndexOf("/"));
		
		for(x=0; x<links.length; x++) {
			var testlink = links[x] + "";
			testlink = testlink.replace(/\\\\/g, "/");
			testlink = testlink.replace(/file:\/\//g, "");
			if (testlink.indexOf(mypath) > -1) {
				if (links[x].className=="altlink") {
				} else {
					foundExactLink = x;
					// get out...
					x = links.length;
				}
			}
		}
	}

	// see if we can match this page to a sidemenu link within a folder one level up
	if (foundExactLink  == -1) {
		mypath = mypath.substring(0, mypath.lastIndexOf("/"));
		for(x=0; x<links.length; x++) {
			var testlink = links[x] + "";
			testlink = testlink.replace(/\\\\/g, "/");
			testlink = testlink.replace(/file:\/\//g, "");
			if (testlink.indexOf(mypath) > -1) {
				if (links[x].className=="altlink") {
				} else {
					foundExactLink = x;
					// get out...
					x = links.length;
				}
			}
		}
	}

	// see if we can match this page to a sidemenu link within a folder two levels up
	if (foundExactLink  == -1) {
		mypath = mypath.substring(0, mypath.lastIndexOf("/"));
		for(x=0; x<links.length; x++) {
			var testlink = links[x] + "";
			testlink = testlink.replace(/\\\\/g, "/");
			testlink = testlink.replace(/file:\/\//g, "");
			if (testlink.indexOf(mypath) > -1) {
				if (links[x].className=="altlink") {
				} else {
					foundExactLink = x;
					// get out...
					x = links.length;
				}
			}
		}
	}


	for(x=0; x<links.length; x++) {

		var href = links[x];

		var match = 0;
		if (foundExactLink > -1) {
			if (x == foundExactLink) {
				match = 1;
			}
		} else {
			if (document.location.href.indexOf(href) >= 0) {
				match = 1;
				foundExactLink = x;
			}
		}

			if (match == 1) {
				if(links[x].className=="altlink") {
					// links[x].style.className = "";
					links[x].parentNode.style.display = "none";
				} else {

					// show the parents...
					cntParents = 0;
					// figure out how deep we are...
					foundDIV = links[x].parentNode;
					while(foundDIV.id != "sidemenu") {
						foundDIV = foundDIV.parentNode;
						if(foundDIV.nodeName == "UL") {
							cntParents++;
							foundDIV.style.display="block";
						}
					}

					// set classes and visibility of links
					var depth = cntParents - 1;

					// show first level child links in this node (UL)...
					foundDIV = links[x].parentNode;
					for (xx=0; xx< foundDIV.childNodes.length;xx++) {
						if (foundDIV.childNodes[xx].nodeName == "UL") {
							var foundUL = foundDIV.childNodes[xx];
							for (xxx=0; xxx< foundUL.childNodes.length;xxx++) {
								if (foundUL.childNodes[xxx].nodeName == "LI") {
									foundUL.childNodes[xxx].style.display="block";
									foundUL.childNodes[xxx].parentNode.style.display = "block";
								}
							}
						}
					}

					// proceed backwards up the tree, setting the visibility of the parent menus and siblings
					foundDIV = links[x].parentNode;
					if (foundDIV.className == "hidden") {
						foundDIV.style.display = "none";
					} else {
						if (foundDIV.className == "expand") {
							foundDIV.className ="collapse";
						} else {
							foundDIV.className ="on";
						}
					}
					while(foundDIV.id != "sidemenu") {
						foundDIV = foundDIV.parentNode;
						if(foundDIV.nodeName == "UL") {
							
							// show the siblings at this level...
							for (xx=0; xx< foundDIV.childNodes.length;xx++) {
								if (foundDIV.childNodes[xx].nodeName == "LI") {
									foundDIV.childNodes[xx].style.display="block";
									if (foundDIV.childNodes[xx].className == "hidden") {
										foundDIV.childNodes[xx].style.display = "none";
									}
								}
							}
							depth=depth - 1;
							foundDIV.parentNode.className ="collapse";
							foundDIV.style.display = "block";
						}
					}
				}
		}

	}

}

}

  		
// ================================================
// gov_generateSiteMap() 
// - creates a site map using the contents of the arrMenu
// - on the sitemap web page, you should 
// ================================================

function gov_generateSiteMap()
{


	// because the dreamweaver library object for the menu will be in /Library/, we make all the links relative to the root.
	var myRootPath = "../";
	
	
	var langID;

	var html = "<div id=\"sitemap\">\n";
	var lastLevel = -1;

	if (LANGUAGE_IDS) {
		if (LANGUAGE_IDS.length > 0) {
		} else {
			return false;
		}
	} else {
		return false;
	}


	for (langs = 0; langs < LANGUAGE_IDS.length; langs++) {
		langID = LANGUAGE_IDS[langs];
		html = "<div id=\"sitemap\">\n";
		lastLevel = -1;
		for( var i=0; i<arrMenu.length; i++)
		{
			if (arrMenu[i]["level"] != lastLevel) {
				if (lastLevel == -1) {
					// starting the menu, let's open up list
					html += "<ul>\n";
				} else {
					if (arrMenu[i]["level"] > lastLevel) {
						// moving down in menus... open the list
						html += "\n<ul>\n";
					}
					if (arrMenu[i]["level"] < lastLevel) {
						// moving up, close the list, and <li>
						html += "</li>\n";
						for (xx = lastLevel; xx > arrMenu[i]["level"]; xx--) {
							html += "</ul></li>\n";
						}
					}
				}
				lastLevel = arrMenu[i]["level"];
			} else {
				// close the <li> tag
				html += "</li>\n";
			}

			if (arrMenu[i]["hidden_"+langID] + "" == "1") {
				html += "<li class=\"hidden\">";
			} else {
				html += "<li>";
			}

			html += ("<a href=\"");
			var testHTTP = arrMenu[i]["url_"+langID];
			if (testHTTP.indexOf("http:") > -1 || testHTTP.indexOf("https:") > -1 || testHTTP.indexOf("ftp://") > -1 || testHTTP.indexOf("mailto:") > -1 || testHTTP.indexOf("javascript:") > -1 ) {
			} else {
				html += myRootPath+siteRootFolder;
			}
			html += (arrMenu[i]["url_"+langID]);
			html += ("\" title=\"");
			html += (arrMenu[i]["text_"+langID]);
			html += ("\"");


			// if this link is an alternate way of getting to a page, then set the class
			// so that the menu doesn't expand at this location too.
			if (arrMenu[i]["alt_link_"+langID] + "" == "1") {
				html += " class=\"altlink\"";
			}
			if (arrMenu[i]["attributes_"+langID]) {
				html += (" ");
				html += (arrMenu[i]["attributes_"+langID]);
			}

			html += (">");
			html += (arrMenu[i]["text_"+langID]);
			html += ("</a>");
		}

		html += ("</li>\n");
		// moving up, close the list, and <li>
		for (xx = lastLevel; xx > 0; xx--) {
			html += "</ul></li>\n";
		}		
		
		html += ("</ul>\n");
		html += ("</div>\n");
		
		html = html.replace(/&/g,"&amp;");
		document.write("<form><h4>"+LANGUAGE_LABELS[langs]+" Site Map</h4>Paste the following html into the site map content area.<br><textarea cols='60' rows='20'>"+html+"</textarea></form>");
		// document.write("<form><h4>"+LANGUAGE_LABELS[langs]+" Side Menu</h4>Paste the following code into the '"+langID+"_sidemenu.lbi' Dreamweaver Library file<br><textarea cols='60' rows='20'>"+html+"</textarea></form>");
	}
	document.close();
	return false;
}





// ================================================
// gov_showLangToggle() 
// - generates the link that will switch to the 
//   alternate langauges
// ================================================
function gov_showLangToggle() {
	// don't replace anything if there isn't a language array
	if (LANGUAGE_IDS) {
		if (LANGUAGE_IDS.length > 0) {
		} else {
			return;
		}
	} else {
		return;
	}

	var html = "";
	var sepChar = "";
	if (arrHeaderLink) {
		if (arrHeaderLink.length > 0) {
			sepChar = " | ";
		}
	}
	
	sepChar = " | ";
	
	for( var i=0; i<arrMenu.length; i++)
	{
		var is_alt_link = 0;
		if (arrMenu[i]["alt_link_"+langID] == undefined) {
		} else {
			if (arrMenu[i]["alt_link_"+langID]+"" == "1") {
				is_alt_link = 1;
			}
	
		}
		if (is_alt_link == 0) {
			if( pagePath + "" == arrMenu[i]["url_"+langID] + "") {
				var obj = document.getElementById("language");
				
				if (obj) {
					for (x = 0; x < LANGUAGE_IDS.length; x++) {
						var this_lang = LANGUAGE_IDS[x];
						if (LANGUAGE_IDS[x] == langID) {
						} else {
							if (""+arrMenu[i]["url_"+this_lang] != "") {
							html += ("<a href=\"");
							var testHTTP = arrMenu[i]["url_"+this_lang];
							if (testHTTP.indexOf("http:") > -1 || testHTTP.indexOf("https:") > -1 || testHTTP.indexOf("ftp://") > -1 || testHTTP.indexOf("mailto:") > -1 || testHTTP.indexOf("javascript:") > -1 ) {
							} else {
								html += rootPath;
							}
							html += (arrMenu[i]["url_"+this_lang]);
							html += ("\" title=\"");
							html += (arrMenu[i]["text_"+this_lang]);
							html += ("\"");


							// html += " class=\"headerlinks\"";
							if (arrMenu[i]["attributes_"+this_lang]) {
								html += (" ");
								html += (arrMenu[i]["attributes_"+this_lang]);
							}

							html += (">");
							html += LANGUAGE_LABELS[x];
							html += ("</a>\n");
							html += sepChar;
							}
						}
					}
					obj.innerHTML = html;

				}
				// get out of the french linking
				i = arrMenu.length;
			}
		}
	}	
}



// ================================================
// gov_showPrinterFriendly() 
// - reloads page with printer friendly version
// - usually called from an anchor in the web page
// - just redirects to the current page with 
//   ?print appended to it
// ================================================
function gov_showPrinterFriendly()
{
	var newLocation = windowURL;
	if (newLocation.indexOf("?") > -1) {
		newLocation = newLocation.substr(0,  newLocation.indexOf("?"));
	} else {
		if (newLocation.indexOf("#") > -1) {
			newLocation = newLocation.substr(0,  newLocation.indexOf("#"));
		}
	}
	if (urlParameters != "") {
		// automatically includes named anchor...
		newLocation = newLocation + "?print&" + urlParameters;
	} else {
		if (namedAnchor != "") {
			newLocation = newLocation + "?print#" + namedAnchor;
		} else {
			newLocation = newLocation + "?print";
		}
	}
	// launch printer version in new window...
	var newwin = window.open(newLocation);
	
	if (newwin) {
	} else {
		alert("Please enable popups for the printer friendly page to show.");
	}
	return false;
}


// ================================================
// gov_showPrinterFriendly(title) 
// - sets appropriate stylesheet by disabling all
//   stylesheets that have a title attribute, then
//   enables the stylesheet which matches "title"
// - this is only used for when toggling printer
//   friendly version
// ================================================
function gov_setActiveStyleSheet(title) 
{
	var i, a, main;
   
	for(i=0; (a = document.getElementsByTagName("style")[i]); i++) 
	{
   
		if (a.getAttribute("title")) {
			a.disabled = true;
		   if(a.getAttribute("title") == title) {
				a.disabled = false;
		   }
		}
	}   
   
}
		
		
		
// ================================================
// gov_checkPrint()
// - used to determine if which stylesheet to use 
//   when the page loads (print.css, screen.css)
// - common.css is always used regardless
// ================================================
function gov_checkPrint()
{
	if (windowURL.indexOf("?print") > -1 || windowURL.indexOf("#printerfriendly") > -1) {
		gov_setActiveStyleSheet("print");
	} else {
		gov_setActiveStyleSheet("screen");
	}
	
	// set the url of the print button...
	
	var newLocation = windowURL;
	if (newLocation.indexOf("?") > -1) {
		newLocation = newLocation.substr(0,  newLocation.indexOf("?"));
	} else {
		if (newLocation.indexOf("#") > -1) {
			newLocation = newLocation.substr(0,  newLocation.indexOf("#"));
		}
	}
	if (urlParameters != "") {
		// automatically includes named anchor...
		newLocation = newLocation + "?print&" + urlParameters;
	} else {
		if (namedAnchor != "") {
			newLocation = newLocation + "?print#" + namedAnchor;
		} else {
			newLocation = newLocation + "?print";
		}
	}
	var obj = document.getElementById("print"); 
	if (obj) {
		obj.href = newLocation;
	}

	
	return false;
}


// ================================================
// gov_showBreadCrumbs()
// - generates the breadcrumb trail using the array arrCrumb[] 
// ================================================
function gov_showBreadCrumbs()
{
	var x,xx;

	// don't replace anything if there isn't a breadcrumb array
	if (arrCrumb) {
		if (arrCrumb.length > 0) {
		} else {
			return;
		}
	} else {
		return;
	}
	
	var num_slashes = pagePath.split("/");
	var html = "";
	
	var sepChar = " &gt; ";
	if (num_slashes.length == 1) {
		// at the root!
		for (x = 0; x < arrCrumb.length; x++) {
			if (arrCrumb[x]["folder"] == "") {
				html += "<a href=\""+arrCrumb[x]["url_"+langID]+"\">"+arrCrumb[x]["text_"+langID]+"</a>"+sepChar;
			}
		}
	} else {
		
		// show the top-level breadcrumbs first
		for (x = 0; x < arrCrumb.length; x++) {
			if (arrCrumb[x]["folder"] == "") {
				html += "<a href=\""+arrCrumb[x]["url_"+langID]+"\">"+arrCrumb[x]["text_"+langID]+"</a>"+sepChar;
			}
		}

		// now show the others		
		var searchFolder = "";
		for (x = 0; x < num_slashes.length; x++) {
			if (num_slashes[x].indexOf(".html") == -1) {
				// it's a folder...
				searchFolder += num_slashes[x];
				for (xx = 0; xx < arrCrumb.length; xx++) {
					if (arrCrumb[xx]["folder"] == searchFolder) {
						html += "<a href=\""+arrCrumb[xx]["url_"+langID]+"\">"+arrCrumb[xx]["text_"+langID]+"</a> ";
						if (xx < arrCrumb.length - 1) {
							html += " &gt; ";
						}
					}
				}
				if (searchFolder == "") {
				} else {
					searchFolder += "/";
				}
			} else {
				// this is a web page... don't show on the breadcrumb trail
			}
		}
	}
	
	var obj = document.getElementById("breadcrumb");
	if (obj) {
		obj.innerHTML = html;
	}

}



// ================================================
// gov_showHeaderLinks()
// - generates the header links from the array arrHeaderLink[]
// - e.g. the "Site Map" and "Contact Us" header links 
// ================================================
function gov_showHeaderLinks()
{
	var html = "";
	
	// don't replace anything if there isn't a header link array
	if (arrHeaderLink) {
		if (arrHeaderLink.length > 0) {
		} else {
			return;
		}
	} else {
		return;
	}
	if (arrHeaderLink) {
		// show the top-level breadcrumbs first
		for (x = 0; x < arrHeaderLink.length; x++) {
			
			if (arrHeaderLink[x]["url_"+langID] != "") {
				html += " | ";
				html += "<a href=\""+arrHeaderLink[x]["url_"+langID]+"\">"+arrHeaderLink[x]["text_"+langID]+"</a>";
				/*
				if (x < arrHeaderLink.length - 1) {
				}
				*/
			}
		}
		var obj = document.getElementById("headerlinks");
		if (obj) {
			obj.innerHTML = html;
		}
	}

}












// ================================================
// gov_generateMenuLists()
// - generates the header links from the array arrHeaderLink[]
// - e.g. the "Site Map" and "Contact Us" header links 
// ================================================
function gov_generateMenuLists(){

	// because the dreamweaver library object for the menu will be in /Library/, we make all the links relative to the root.
	var myRootPath = "../";
	
	
	var langID;

	var html = "";
	var lastLevel = -1;

	if (LANGUAGE_IDS) {
		if (LANGUAGE_IDS.length > 0) {
		} else {
			return;
		}
	} else {
		return;
	}


	for (langs = 0; langs < LANGUAGE_IDS.length; langs++) {
		langID = LANGUAGE_IDS[langs];
		html = "";
		lastLevel = -1;
		for( var i=0; i<arrMenu.length; i++)
		{
			if (arrMenu[i]["level"] != lastLevel) {
				if (lastLevel == -1) {
					// starting the menu, let's open up list
					html += "<ul>\n";
				} else {
					if (arrMenu[i]["level"] > lastLevel) {
						// moving down in menus... open the list
						html += "\n<ul>\n";
					}
					if (arrMenu[i]["level"] < lastLevel) {
						// moving up, close the list, and <li>
						html += "</li>\n";
						for (xx = lastLevel; xx > arrMenu[i]["level"]; xx--) {
							html += "</ul></li>\n";
						}
					}
				}
				lastLevel = arrMenu[i]["level"];
			} else {
				// close the <li> tag
				html += "</li>\n";
			}

			if (arrMenu[i]["hidden_"+langID] + "" == "1") {
				html += "<li class=\"hidden\">";
			} else {
				html += "<li>";
			}

			html += ("<a href=\"");
			var testHTTP = arrMenu[i]["url_"+langID];
			if (testHTTP.indexOf("http:") > -1 || testHTTP.indexOf("https:") > -1 || testHTTP.indexOf("ftp://") > -1 || testHTTP.indexOf("mailto:") > -1 || testHTTP.indexOf("javascript:") > -1 ) {
			} else {
				html += myRootPath+siteRootFolder;
			}
			html += (arrMenu[i]["url_"+langID]);
			html += ("\" title=\"");
			html += (arrMenu[i]["text_"+langID]);
			html += ("\"");


			// if this link is an alternate way of getting to a page, then set the class
			// so that the menu doesn't expand at this location too.
			if (arrMenu[i]["alt_link_"+langID] + "" == "1") {
				html += " class=\"altlink\"";
			}
			if (arrMenu[i]["attributes_"+langID]) {
				html += (" ");
				html += (arrMenu[i]["attributes_"+langID]);
			}

			html += (">");
			html += (arrMenu[i]["text_"+langID]);
			html += ("</a>");
		}

		html += ("</li>\n");
		// moving up, close the list, and <li>
		for (xx = lastLevel; xx > 0; xx--) {
			html += "</ul></li>\n";
		}		
		
		html += ("</ul>\n");
		
		html = html.replace(/&/g,"&amp;");
		document.write("<form><h4>"+LANGUAGE_LABELS[langs]+" Side Menu</h4>Paste the following code into the '"+langID+"_sidemenu.lbi' Dreamweaver Library file<br><textarea cols='60' rows='20'>"+html+"</textarea></form>");
	}
	document.close();
}




// ================================================
// gov_determineLanguage()
// - based upon the current page being viewed, figures out what language we're viewing
// - loops through the arrMenu[] array and finds the currently viewed page
// - if page can't be found, if ".fr." is not in the filename, langID is 'en'
// ================================================
function gov_determineLanguage() {
	var mylang = "";
	var foundLang = 0;
	var xx,x;
	if (LANGUAGE_IDS) {
		if (LANGUAGE_IDS.length > 0) {
		} else {
			langID = templateLangID;
			return;
		}
	} else {
		langID = templateLangID;
		return;
	}


	for (x=0; x < arrMenu.length; x++) {
		for (xx=0; xx<LANGUAGE_IDS.length; xx++) {
				mylang=LANGUAGE_IDS[xx];	
  				if( pagePath + "" == arrMenu[x]["url_"+mylang] + "") {
  					langID = mylang;
  					foundLang = 1;
  				}
		}
	}
	
	if (foundLang == 0) {
		for (xx=0; xx<LANGUAGE_IDS.length; xx++) {
			mylang=LANGUAGE_IDS[xx];	
			if( windowURL.indexOf("."+mylang+".")>0)
			{
				langID = mylang;
				foundLang = 1;
			}
		}
	}
	if (foundLang == 0) {
		langID = templateLangID;
	}
	
}






function isDefined(variable)
{
return (!(!( variable||false )))
}



/* 
=============================================
MAIN LINE PROCESSING BEGINS
=============================================
*/

/* 
DETERMINE WHICH PAGE IS BEING VIEWED
=============================================
*/

var rootPath = "";
var langID = "en";   // by default, pages are english
var windowPath = window.location.pathname + "";
var windowURL = window.location.href + "";
if (windowURL.indexOf(".html") == -1) {
	// browsing a folder without a filename, append index.html
	windowURL = windowURL + "index.html";
}




var urlParameters = "";
if (windowURL.indexOf("?") > 0) {
	// we got a parameter in the url...
	urlParameters = gov_stripSubPath(windowURL, "?");
}

var namedAnchor = "";
if (windowURL.indexOf("#") > 0) {
	// we got a named anchor...
	namedAnchor = gov_stripSubPath(windowURL, "#");
}

// used in the page template...
var tempURL = windowURL;


var pagePath = "";

// if for some reason the browser can't figure out the page we're on, default to index.html
if (pagePath == false) {
	pagePath = "index.html";
}

// if there is no .html extension in the current URL, it means we're browsing the folder
// so append index.html.
if (pagePath.indexOf(".html") == -1) {
	pagePath = pagePath + "index.html";
}

// figure out where the root of the site is, based on number of slashes
var num_slashes = pagePath.split("/");
for (x = 1; x < num_slashes.length; x++) {
	rootPath = rootPath + "../";
}


// if the resources are within this web site, uncomment the line below
// rootPath is determined in functions.js above...
document.write('<style type="text/css" media="all" title="screen">\n@import url('+rootPath+resourcePath+'css/screen.css);\n</style>\n');
document.write('<style type="text/css" media="all" title="print">\n@import url('+rootPath+resourcePath+'css/print.css);\n</style>\n');
document.write('<style type="text/css" media="all">\n@import url('+rootPath+resourcePath+'css/hide.css);\n</style>\n');

// sIFR entries
document.write('<style type="text/css" media="screen">\n@import url('+rootPath+resourcePath+'css/sIFR-screen.css);\n</style>\n');
document.write('<style type="text/css" media="print">\n@import url('+rootPath+resourcePath+'css/sIFR-print.css);\n</style>\n');

gov_setActiveStyleSheet("screen");
