﻿// JavaScript Document

//J. Riley 2010-11

//The following is AJAX code to test to see if a file exists on our server. If the right /NAME/NAME.html pattern exists then we add a breadcrumb to the breadcrumb navigation.
function AJ(){
var obj;
if (window.XMLHttpRequest){
	obj= new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
	try{
		obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
	catch(er){
		obj=false;
		}
	}
return obj;
}

function isFile(str){
var O= AJ();
if(!O) return false;
try{
	O.open("HEAD", str, false);
	O.send(null);
	return (O.status==200) ? true : false;
	}
catch(er){
	return false;
	}
}




// custom Hash (associative array) utility function so we can store key/value pairs nicely for our breadcrumb dir exclusion lists
function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	this.removeItem = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	}
}






/* The following code was co-opted by J. Riley 01-15-2011 and significantly altered from original work by the following: */
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Nate Baldwin :: http://www.mindpalette.com */

/* Breadcrumb Links Script
   NOTE: Be sure to check Web site for updates */

// ============================================================

/* ADDED 2011-02-10&16: The following variables allow you to prefix your breadcrumb trail with a custom URL, e.g., a meta home page for a parent company even though your site is on it's own host. Test if we are on our main home site and if not, add a "home" link prefix to the breadcrumbs*/
var currentHost = window.location.host; //alert(currentHost);

var metaHome = "home";
var metaUrl = "http://www.unavco.org";

if (currentHost != ("www.unavco.org" || "unavco.org")) {   //alert('We are not on www.unavco.org');
	if (metaHome && metaUrl) var metaLink = '<a href="'+metaUrl+'">'+metaHome+'</a>';
}
/* The variable below determines what the current site's home page, or the root directory,
of your site is named. The default is "Home". To use the address of your
site (www.yoursite.com), leave the value blank (nothing between the double quotes). */
var homePage = "";
switch (currentHost) {
   case 'www.unavco.org': homePage = metaHome; break;
   case 'facility.unavco.org': homePage="facility"; break;
   case 'pbo.unavco.org': homePage="pbo"; break;
   case 'binex.unavco.org': homePage="binex"; break;
   case 'facility-beta-web-server.win.int.unavco.org': homePage="facility BETA"; break;
   case 'hq-beta-web-server.win.int.unavco.org': homePage="BETA"; break;
	 case 'register.unavco.org': homePage="register"; break;
	 case 'achaia.unavco.org': homePage="achaia"; /*ADD EXCEPTIONS FOR ACHAIA HERE; */ break;
}

/* Dir Aliasing/Removal Funtionality by J. Riley
The variable below lets you create aliases for dir names, and exclude them entirely when the alias name is set to an empty string */
var changeDirNames = new Hash(
								//HQ Aliases
								'aboutus', 'about us',
								'crosscutting', '',
								'meetings-events', 'meetings  \&amp\;  events',
									'tls', 'Terrestrial Laser Scanning',
									'sciworkshop12', '2012 science workshop',
									'realtime', 'Real\&\#45\;time Data Workshop',
								'policies_forms', 'Policies\, Forms \&amp\; Procedures',													
								'edu_outreach', 'Education \&amp\; Outreach',
								'pubs_reports', 'Publications \&amp\; Reports',
									'comm_biblio', 'Community Bibliography',
									'reports', 'reports to sponsors',
									'meeting_events_pubs', 'Meeting \&amp\; Event Publications ',
								'research_science', 'Community Science',
									'science-apps', 'Geodesy Applications',
										'hydro', '',
									'science-support', 'Science Product Support',
										//'crustal_motion', '',
											'dxdt', 'Global Velocity',
											'dedt', 'Global Strain Rate',
										'geoid', '',
								'crumb-test-dummy-dir', 'My Dummy text',
								
								//FACILITY Aliases
								'aboutus', 'about us',
								//project support
								'engineer-equip', 'GNSS Engineering \&amp\; Equipment',
								'es', 'EarthScope Project Support',
									'pbo-camp', 'pbo gps campaigns',
								'nasa', 'NASA Geosciences Support',
								'polar', 'Polar Services',
									'remote', 'Remote Station Technology ',
									'whatnew', 'what\'s new',
								'dev_test', 'Development \&amp\; Testing',
								'download_transfer', 'Data Download \&amp\; Transfer'
								);

/* The variable below controls the character(s) that the script places between
the link levels. The default is set to be the > (greater than) HTML entity
with a space on either side. Change this to whatever you want, but if the
character has an HTML entity, be sure to use that. */
var sepChars = ' <img src="/lib/images/arrow-bullet.png" border="0" alt="" /> ';

/* In most cases, the value below should just be a / sign. Or, you can use the
fully qualified http://www.yoursite.com address if you'd rather. Basically,
it just tells the script what all the link addresses will start with. */
var linkHome = "/";

/* Leave the value below set to 'true' if you want the script to not list your
index page name (index.html) at the end of the links path. The values 'true' or
'false' should be used without quotes. */
var hideIndex = true;

/* Leave the value below set to 'true' if you want the script to convert any
underscores in directory or names to spaces. For instance, it would convert
"golive_actions" to "golive actions". The values 'true' or 'false' should be
used without quotes. */
var UToSpace = true;

/* Leave the value below set to 'true' if you want the script to convert any dashes
in directory or page names to spaces. For instance, it would convert "golive-actions"
to "golive actions". The values 'true' or 'false' should be used without quotes. */
var DToSpace = true;

/* The variable below allows you to change the capitalization of the directory and
pages names in your menu. The value should be a number between 0 and 3, without
quotation marks. Options are as follows: 0 = no change; 1 = initial caps;
2 = all upper case; 3 = all lower case */
var changeCaps = 3;

/* If set to true, the variable below will not display the file's extension for
any file names displayed in the menu. Otherwise, set to false to show extensions.
The values 'true' or 'false' should be used without quotes. */
var hideExt = true;

//-------------------------------------------------------------------------
// SCRIPT FUNCTIONS  (shouldn't need to edit code below)...
//-------------------------------------------------------------------------

// build breadcrumb links...
function MPJSBackLinks() {
var linkHTML = '';
var thisURL = window.location + '';
var urlPair = thisURL.split('//');
if (urlPair.length > 1) thisURL = urlPair[1];
var dirArray = thisURL.split('/');
var linkArray = dirArray.slice(1);
var linkDir = '/';
var currentPage = '';
if (linkHome != '' && linkHome != '/') {
  var thisTest = linkHome.split('//');
  if (thisTest.length > 1) linkHome = thisTest[1];
  startArray = linkHome.split('/');
  var backCount = 0;
  for (var n=0; n<startArray.length; n++) {
    if (startArray[n] == '..') backCount++;
      else break;
    }
  if (backCount > 0) {
    var part1 = dirArray.slice(0, (dirArray.length - backCount - 1));
    var part2 = startArray.slice(backCount);
    startArray = part1.concat(part2);
    } else {
    var newStart = new Array(dirArray[0]);
    for (var n=1; n<startArray.length; n++) {
      var thisTest = (typeof dirArray[n] != "undefined") ? dirArray[n] : false;
      if (thisTest && thisTest == startArray[n]) newStart[n] = startArray[n];
        else break;
      }
    startArray = newStart;
    }
  if (startArray.length > 1) {
    var lastOne = startArray[startArray.length - 1];
    if (lastOne != '') {
      var thisTest = lastOne.split('.');
      if (thisTest.length > 1) startArray[startArray.length - 1] = '';
        else startArray[startArray.length] = '';
      }
    if (homePage == '') homePage = startArray[startArray.length-2];
    linkArray = dirArray.slice(startArray.length - 1);
    if (startArray[0] != '') startArray[0] = "http://"+startArray[0];
    linkDir = startArray.join('/');
    } else linkArray = dirArray.slice(1);
  } else {
  linkArray = dirArray.slice(1);
  if (homePage == '') homePage = dirArray[0];
  }
// Added by J. Riley for dir aliases and exclusions
// Check to see if any dir names in our linkArray need to be changed or removed based on our exclussion list changeDirNames
var arrlen = linkArray.length;
var linkAliasArray = [];
var lookup = {}; //Index array for fast compare of link array and changeDirNames hash
for (var j=0; j < arrlen; j++) {
	lookup[linkArray[j]] = linkArray[j];
}
for (var x in changeDirNames.items) {
	//alert('key is: ' + x + ', value is: ' + changeDirNames.items[x]);
	if (typeof lookup[x] != 'undefined') { // we found a hit
		//alert('found ' + x + ' in both lists');
		for (var z=0; z < arrlen; z++) {
			if (linkArray[z] == x){
				if (changeDirNames.items[x] == '') {
					linkArray = linkArray.slice(0,z).concat(linkArray.slice(z+1, arrlen)); //remove the link from the linkarray
				} else { 
					linkAliasArray[z] = changeDirNames.items[x]; //create an alias for the link using our hash of key values
				}
			}
		}
	}
}
var xmlhttpArray = [];
var backTrack = 1;
if (linkArray[linkArray.length - 1] != '') {
  var lastOne = linkArray[linkArray.length - 1];
  var testName = lastOne.split('.');
  if (testName[0] == 'index' || testName[0] == 'default') {
    backTrack = 2;
    currentPage = linkArray[linkArray.length - 2];
    } else if (hideExt) currentPage = testName[0]
    else currentPage = lastOne;
  } else {
  backTrack = 2;
  currentPage = linkArray[linkArray.length - 2];
  }
var html = '';
if (linkArray.length >= backTrack) {
  linkArray = linkArray.slice(0, linkArray.length - backTrack);
  var links = new Array();
  if (homePage != '') {
    homePage = MPBCParseText(homePage, UToSpace, DToSpace, changeCaps);
    links[links.length] = '<a href="'+linkDir+'">'+homePage+'</a>';
    }  
	var baseDir = linkDir;
  for (var n=0; n<linkArray.length; n++) {
    baseDir += linkArray[n] + '/';
    if(linkAliasArray[n]) {
			var thisText = MPBCParseText(linkAliasArray[n], UToSpace, DToSpace, changeCaps);
			} else {
			var thisText = MPBCParseText(linkArray[n], UToSpace, DToSpace, changeCaps);
			}
			
  //File test added by J. Riley
	//Since we don't use index.html files... 
	//Use AJAX to test if a file [/dirname/dirname.html] is present, no sense in creating broken link breadcrumbs		
		var testurl = baseDir+linkArray[n]+'\.html';
		//alert('Array index: ' + n +'Testing url: ' + testurl);
		if(isFile(testurl)) {
			//alert(testurl+" URL Exists!");
			links[links.length] = '<a href="'+baseDir+linkArray[n]+'.html">'+thisText+'</a>';
			} 
		
    } //end of for loop of linkArray
		
  if ((!hideIndex) && (currentPage != '')) links[links.length] = MPBCParseText(currentPage, UToSpace, DToSpace, changeCaps);
  if (metaLink) {
		html = '<div class="crumb">'+metaLink+sepChars+links.join(sepChars)+'<\/div>';
		} else {
			html = '<div class="crumb">'+links.join(sepChars)+'<\/div>';
			}
  } //end of if (linkArray.length >= backTrack)
return html;
} //end function MPJSBackLinks()

// parse string through text filters
function MPBCParseText(thisText, UToSpace, DToSpace, changeCaps) {
if (typeof thisText != "undefined" && thisText) {
  if (DToSpace) thisText = MPBCReplaceChar('-', ' ', thisText);
  if (UToSpace) thisText = MPBCReplaceChar('_', ' ', thisText);
  if (changeCaps) thisText = MPBCFixCaps(thisText, changeCaps);
  } else thisText = '';
return thisText;
}
// find and replace single character in string...
function MPBCReplaceChar(oldChar, newChar, thisString) {
var newString = '';
for (var n=0; n<thisString.length; n++) {
  newString += (thisString.charAt(n) == oldChar) ? newChar : thisString.charAt(n);
  }
return newString;
}
// determine changes in capitalization...
function MPBCFixCaps(thisString, changeCaps) {
if (changeCaps == 1) thisString = MPBCUCWords(thisString);
  else if (changeCaps == 2) thisString = thisString.toUpperCase();
  else if (changeCaps == 3) thisString = thisString.toLowerCase();
return thisString;
}
// capitalize the first letter of every word...
function MPBCUCWords(thisString) {
var thisArray = thisString.split(' ');
var newString = '';
for (var n=0; n<thisArray.length; n++) {
  var firstChar = thisArray[n].charAt(0).toUpperCase();
  var theRest = thisArray[n].substring(1, thisArray[n].length);
  newString += firstChar+theRest+' ';
  }
return newString.substring(0, newString.length - 1);
}
document.write(MPJSBackLinks());


// Paste this code into the BODY section of your HTML document where you want the breadcrumbs to be displayed You may need to change the path of the file.
/* <script type="text/javascript" src="MPBackLinks.js"></script> */


/* You can also customize the appearance of the breadcrumbs 
You can customize the styling of your menu using CSS.
The menu is created inside a <div> with a class assigned
named "mp_backlinks". You can create a new CSS class style
named "mp_backlinks" and assign whatever style settings you want.
*/


