// -----------------------------------------------------------------------------
// header.js 
// This script will automatically populate the www.jhuapl.edu 
// navigation.  It uses the directory structure to determine 
// if a navigation link should be highlighted because it is the 
// current page.  Pages must be in the proper location for the 
// script to work correctly.  Directories must use lowercase letters
// for code comparison.
// 
// DIRECTORY STRUCTURE REQUIREMENTS:
// currentDirectory: is used to represent the left navigation bar 
// currentSubDirectory: is used to represent the burgundy sub-navigation 
// bar across the top of the page below JHUAPL 
// 
// IMAGE REQUIREMENTS:
// Images have three states. 
// directoryname.jpg = highlighted link/current page (gold text) 
// directoryname1.jpg = off state (white text) 
// directoryname2.jpg = over state  (white text - raised) 
// Left bar images should be stored in /navImages
// Top sub-navigation images should be stored in the /navImages/currentDirectory
//
// HTML PAGE REQUIREMENTS:
// 1.  Add the following line of code in html header
//     <script src="http://www.jhuapl.edu/header.js"></script>
//
// 2.  Make page margins set to 0 & header_init() call.
//     Calling the header_init() function in the onload event of  
//     the body tag will automatically set the navigation.  This 
//     function does not require parameters.
//     <body leftmargin="0" topmargin="0" >
// 3.  Add the following line of code after the <body> tag.
//     <script>header()</script>
// 4.  Add the following line of code before the </body> tag.
//     <script>footer()</script>
//
// COMMENTS:
// To make the code portable the following must be changed:
// 1.  imageDirectory = "http://www.jhuapl.edu/navImages/" should be set
//     to the proper navImages location.
// 2.  URLbase = "www.jhuapl.edu" should be set the proper URL. 
//     (occurs twice in this script
// 3.  chars = 19 should be set to the number of characters in the URLbase 
//     (occurs twice in this script)
// -----------------------------------------------------------------------------
document.write('<link href="http://www.jhuapl.edu/stylesheets/www.css" rel="stylesheet" type="text/css">');
function header_init(){
	imageDirectory = "http://www.jhuapl.edu/navImages/";
	
	currentDirectory = getCurrentDirectory();
	currentImage = imageDirectory + currentDirectory + "2.jpg";
	header_swapImage(currentDirectory,'',currentImage ,1);
	
	currentSubDirectory = getCurrentSubDirectory();
	if (currentSubDirectory.length == 0){
	    //&& (currentPage == "index" || currentPage.length == 0)) {
		return;
	}
	currentImage = imageDirectory + currentDirectory + "/" + currentSubDirectory + "2.jpg";
	subheader_swapImage(currentSubDirectory,'',currentImage ,1);
}

function header_swapImgRestore() { //v3.0
  var i,x,a=document.header_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function header_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.header_p) d.header_p=new Array();
    var i,j=d.header_p.length,a=header_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.header_p[j]=new Image; d.header_p[j++].src=a[i];}}
}

function header_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=header_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function header_swapImage() { //v3.0
  var n,i,j=0,x,a=header_swapImage.arguments; document.header_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=header_findObj(a[i]))!=null){
	   document.header_sr[j++]=x; 
	   if(checkDirectory(a[i])) {
			n=getActiveImage(a[i+2]);
			x.src=n;
			a[i+2]=n;}
	   if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
   }

function checkDirectory(imageName) {
	<!-- Check if image should be active because it is the current directory
	currentDirectory = getCurrentDirectory();
	linkDirectory = imageName;
	if (linkDirectory == currentDirectory) {
		return true;
	}  
}

function getCurrentDirectory(){
	<!-- Code to get the directory name -->
	URLbase = "jhuapl.edu";
	chars = 10; 

	currentDirectory = document.URL;
	currentDirectory = currentDirectory.toLowerCase();
	offset = currentDirectory.lastIndexOf(URLbase);
	/*alert(offset);
	alert(chars); */
	currentDirectory = currentDirectory.substr(offset + chars + 1);
	
	offset = currentDirectory.indexOf("\\");
	if (offset > 0) {
			currentDirectory = currentDirectory.substr(0,currentDirectory.indexOf("\\"));
	} else {
		currentDirectory = currentDirectory.substr(0,currentDirectory.indexOf("/")); 
	}
	return currentDirectory;
}

function WWW_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=WWW_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function WWW_swapImage() { //v3.0
  var i,j=0,x,a=WWW_swapImage.arguments; document.WWW_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=WWW_findObj(a[i]))!=null){document.WWW_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function WWW_swapImgRestore() { //v3.0
  var i,x,a=document.WWW_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function WWW_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.WWW_p) d.WWW_p=new Array();
   var i,j=d.WWW_p.length,a=WWW_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.WWW_p[j]=new Image; d.WWW_p[j++].src=a[i];}}
}

//-->
function subheader_swapImgRestore() { //v3.0
  var i,x,a=document.header_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function subheader_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.header_p) d.header_p=new Array();
    var i,j=d.header_p.length,a=header_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.header_p[j]=new Image; d.header_p[j++].src=a[i];}}
}

function subheader_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=header_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function subheader_swapImage() { //v3.0
  var n,i,j=0,x,a=subheader_swapImage.arguments; document.header_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=header_findObj(a[i]))!=null){
	   document.header_sr[j++]=x; 
	   if(checkSubDirectory(a[i])) {
			n=getActiveImage(a[i+2]);
			x.src=n;
			a[i+2]=n;}
	   if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
   }

function checkSubDirectory(imageName) {
	<!-- Check if image should be active because it is the current subdirectory
	currentSubDirectory = getCurrentSubDirectory();
	linkDirectory = imageName;
	if (linkDirectory == currentSubDirectory) {
		return true;
	}  
}

function getCurrentSubDirectory(){
	<!-- Code to get the directory name -->
	URLbase = "jhuapl.edu";
	chars = 10; 

	tempDirectory = document.URL;
	tempDirectory = tempDirectory.toLowerCase();
	<!-- Strip off base URL-->
	offset = tempDirectory.lastIndexOf(URLbase);
	tempDirectory = tempDirectory.substr(offset + chars + 1);
	<!-- Strip off 1 directory-->
	offset = tempDirectory.indexOf("\\");
	if (offset > 0) {
			tempDirectory = tempDirectory.substr(tempDirectory.indexOf("\\") + 1);
	} else {
		tempDirectory = tempDirectory.substr(tempDirectory.indexOf("/") + 1); 
	}
	<!-- Strip down to subdirectory-->
	offset = tempDirectory.indexOf("\\");
	if (offset > 0) {
			currentSubDirectory = tempDirectory.substr(0,tempDirectory.indexOf("\\"));
	} else {
		currentSubDirectory = tempDirectory.substr(0,tempDirectory.indexOf("/")); 
	}
	return currentSubDirectory;
}
function getPage() {
	var currentPage;
	var offset;
	
	currentPage = document.URL;
	currentPage = currentPage.toLowerCase();
	currentPage = currentPage.substr(currentPage.lastIndexOf("/") + 1); //strip URL down to page name
	currentPage = currentPage.substr(currentPage.lastIndexOf("\\") + 1); //strip URL down to page name when running locally
	currentPage = currentPage.substr(0,currentPage.lastIndexOf("."));  //strip off .htm or .html
	return currentPage;
	<!--window.alert(currentPage);-->
}
function getPageAndExt() {
	var currentPage;
	var offset;
	
	currentPage = document.URL;
	currentPage = currentPage.toLowerCase();
	currentPage = currentPage.substr(currentPage.lastIndexOf("/") + 1); //strip URL down to page name
	currentPage = currentPage.substr(currentPage.lastIndexOf("\\") + 1); //strip URL down to page name when running locally
	return currentPage;
	<!--window.alert(currentPage);-->
}

function getActiveImage(currentImage) {
	extension = currentImage.substr(currentImage.lastIndexOf(".") + 1);  //get extension
	currentImage = currentImage.substr(0,currentImage.lastIndexOf(".") - 1);  //strip off extension and the 1 or 2
	currentImage = currentImage + "." + extension;
	return currentImage;
}

function header(main) {

		document.write("<style type='text/css'><!--");
		// Background Color for entire Page
		document.write("body { background-color: #999999; }");
		document.write(".wwwnavblack { font-family: Arial, Helvetica, sans-serif;	font-size: 12px;	color: #000000;	font-weight: bold;	text-decoration: none; }");
		document.write(".wwwnavblack:hover  { font-family: Arial, Helvetica, sans-serif;	font-size: 12px;	color: #000000;	font-weight: bold;	text-decoration: none; }");
		document.write(".wwwnavblack:visited { font-family: Arial, Helvetica, sans-serif;	font-size: 12px;	color: #000000;	font-weight: bold;	text-decoration: none; }");
		document.write("}");
		document.write("--></style>");
		document.write("<table border='0' cellpadding='0' cellspacing='0' align='center' bgcolor='#FFFFFF'><tr><td><table border='0' cellpadding='0' cellspacing='0' width='780'>");
		
		document.write("<tr>");
		document.write("<td rowspan='2'><a href='http://www.jhuapl.edu/'><img src='http://www.jhuapl.edu/images/header/header_t1.jpg' width='561' height='140' alt='' border='0'></a></td>");
		document.write("<td><table width=100% height='91' cellpadding='0' cellspacing='0' border='0' background='http://www.jhuapl.edu/images/header/header_t2.jpg' ><tr><td><a href='http://www.jhuapl.edu/aplalert/cgi-bin/user/current_alert.pl'> <img src='http://www.jhuapl.edu/aplalert/cgi-bin/user/alert.pl' border=0 width='52' height='52'></a></td></tr></table></td></tr>");
		document.write("<tr>");
		document.write("<td valign='bottom'><a href='http://www.jhuapl.edu/'><img src='http://www.jhuapl.edu/images/header/header_t3.jpg' width='219' height='49' alt='' border='0'></a></td>");
		document.write("</tr>");
		
   		document.write("</table>");

		document.write("<table border='0' cellspacing='0' cellpadding='5' width='780'>");
 		document.write("<tr>");
    	document.write("<td valign='top' width='150' background='http://www.jhuapl.edu/images/header/linepx.jpg'><img src='http://www.jhuapl.edu//navImages/spacer.gif' width='150' height='1'><br><br>");
		
	<!--Skip subheaders for foyer pages-->
	currentPage = getPage();
	currentSubDirectory = getCurrentSubDirectory();
	currentDirectory = getCurrentDirectory();
	
	<!--Special logic for technical digest, it sits on a separate server & does -->
	<!--not follow the directory structure -->
	
	pageURL = document.URL;
	pageURL = pageURL.toLowerCase();
	if (pageURL.indexOf("techdigest") > -1) {
		subheader_technicaldigest();
		return;
	}
	if (pageURL.indexOf("weather") > -1) {
		subheader_weather();
		return;
	}
	if (currentDirectory == "newscenter" || currentDirectory == "weather") {
	<!-- Show navigation on newscenter; continue -->
	}
	else if (currentSubDirectory.length == 0 && (currentPage == "index" || currentPage.length == 0)) {
		return;
	}
	if (currentDirectory == "aboutapl" && currentSubDirectory =="diversity") {
		//no header
	}
	if (currentDirectory == "weather") {
		subheader_weather();
	}
}

function change_it() {
}

function subheader_technicaldigest() {
	document.write("<a href='http://techdigest.jhuapl.edu/' class='wwwnavblack'>Technical Digest</a><br>");
	
	document.write("</td>");
	document.write("<td width='635' valign='top' bgcolor='#FFFFFF'>");
}

function subheader_ott() {
}

function subheader_weather() {
	document.write("<a href='http://www.jhuapl.edu/weather/main/index.html' class='wwwnavblack'>Weather Home</a><br>");
	<!--APL-->
	document.write("<a href='http://www.jhuapl.edu/weather/apl/index.html' class='wwwnavblack'>APL</a><br>");
	<!--Local-->
	document.write("<a href='http://www.jhuapl.edu/weather/local/index.html' class='wwwnavblack'>Local Area</a><br>");
	<!--National-->
	document.write("<a href='http://www.jhuapl.edu/weather/national/index.html' class='wwwnavblack'>National</a><br>");
	<!--International-->
	document.write("<a href='http://www.jhuapl.edu/weather/inational/index.html' class='wwwnavblack'>International</a><br>");
	<!--Education-->
	document.write("<a href='http://www.jhuapl.edu/weather/education/index.html' class='wwwnavblack'>Education</a><br>");
	document.write("</td>");
	document.write("<td width='635' valign='top'>");
}

function footer(includefoot, keepStats, verify, verifyDate) {
	document.write("</td>");
	document.write("</tr>");
	currentPage = getPage();
	currentSubDirectory = getCurrentSubDirectory();
	if (typeof verifyDate == 'undefined') {
		verifyDate = "01/01/2008"
	}

	if (includefoot != "nofooter") {
		document.write("<style type='text/css'><!--");
		document.write(".wwwbodytxtfooter2 { font-family: Arial, Helvetica, sans-serif;   font-size: 10px;   color: #FFFFFF;   font-weight: bold; }");
    	document.write("--></style>");		
		document.write("</td>");
		document.write("</tr>");
		document.write("</table>");
		document.write("<table width='780' border='0' cellpadding=7' cellspacing='0' bgcolor='#00FF00'>");
		document.write("<tr>");
		document.write("<td bgcolor='#314871'><div class='wwwbodytxtfooter2'>The Johns Hopkins University Applied ");
		document.write("Physics Laboratory<br />11100 Johns Hopkins Road, Laurel, Maryland");
		document.write(" 20723 &#8226; 240-228-5000 (Washington, DC, area) &#8226; 443-778-5000 (Baltimore area)<br/>");
					   
		document.write("&#169;  2009 - <a href='http://www.jhuapl.edu/privacy/statement.asp' class='wwwbodytxtfooter2'");
		document.write("class='wwwbodytxtfooter2' style='text-decoration:none'><span style='color:#FFFFFF'>Privacy");
		document.write("Notice/Legal Disclaimer</span></a></div></td>");
		document.write("<td bgcolor='#314871'><div align='right'  class='wwwbodytxtfooter2'>Last verified:&nbsp;");
		document.write(verifyDate);
		document.write("</DIV></td>");
		document.write("</tr>");
		document.write("</table></td></tr></table>");
	}
	
	if (keepStats == "yesstats") {
	
		currentPageAndPath = "www.jhuapl.edu/"; 	

		currentDirName = getCurrentDirectory();
		if (currentDirName.length > 0) {
			currentPageAndPath = currentPageAndPath + currentDirName + "/";
		}

    	currentSubDirName = getCurrentSubDirectory();
		if (currentSubDirName.length > 0) {
			currentPageAndPath = currentPageAndPath + currentSubDirName + "/";
		}
	
		currentPageName = getPageAndExt();
		if (currentPageName.length > 0) {
			currentPageAndPath = currentPageAndPath + currentPageName
		}


		pageName = currentPageAndPath;
    	var code = ' '; 
		document.write("<script src='http://code.superstats.com/code/ss/jhuapl/0/30b'></script>");
		br = navigator.appName + parseInt(navigator.appVersion);
		if (code != ' ' || br == 'Netscape2') {
	   	document.write(code);
		}
    	else {
		document.write(''+ ' <im'+'g'+ ' src="http://stats.superstats.com/b/ss/jhuapl/1'+ '?pageName=' + escape(pageName) + '" border=0>');
		}		
	}
}
