/*******************************************************
*                 B R E A D C R U M B S                *
*                                                      *
* Modified and implimented for the DMOHP by David Mart *
*                                                      *
*                                                      *
*******************************************************/

// ====================
// ===== Features =====
// ====================
// This script allows you to:
//    * Rename folders in the breadcrumb        see Folder Names
//    * Exclude folders from the breadcrumb     see Folders to Exclude
//    * Add extra links to the breadcrumb       see Extra Crumbs
//    * Add extra links after a crumb           see Extra Crumbs After
//    * Change the link of breadcrumb element   see Change a Crumb's Link


// ====================
// ==== Parameters ====
// ====================
var documentLocation = 'breadcrumb'; // SPAN or DIV ID for breadcrumb location
var folderNameCount = 0; // Increments the place counter for the folderNames list
var excludedFolderCount = 0; // Increments the place counter for the excludedFolders list
var includedFolderBeforeCount = 0; // Increments the place counter for the includedFolders... lists
var includedFolderAfterCount = 0; // Increments the place coutner for the includeFoldersAfter... lists

folderNames = new Object(); // List containing the exact folder names to be replaced and the desired text representation
textNames = new Object(); // List containing the desired text to be displayed in the breadcrumb for the related folder in folderNames

excludedFolders = new Object(); // List containing folders names to exclude from the breadcrumb
includeFoldersName = new Object(); // List containing extra crumbs (links) to include in the breadcrumb
includeFoldersLink = new Object(); // List containing the link for the extra crumb
includeFoldersBefore = new Object(); // List containing the element with the extra link should occur before

includeFoldersAfter = new Object(); // List containing extra crumbs (links) to include after the specified crumb
includeFoldersAfterName = new Object();  // List containing the desired text to be displayed for the specified crumb
includeFoldersAfterLink = new Object(); // List containing the element link for the extra crumb


// ================
// = Folder Names =
// ================
// This section allows you to enter the name of a folder in the breadcrumb that you would like replaced with text
// Entries are made in the form: addFolderName('FOLDER_NAME','TEXT_NAME');
//
// where FOLDER_NAME is replaced by the name of the folder and
// TEXT_NAME is replaced by the desired text

addFolderName('grhc','Home');
addFolderName('recipe','Recipes');

// ======================
// = Folders to Exclude =
// ======================
// This section allows you to specify folders you would like omitted from the breadcrumb
// Entries are made in the form: addExcludedFolder('FOLDER_NAME');
//
// where FOLDER_NAME is replaced by the name of the folder to be omitted

addExcludedFolder('images');
addExcludedFolder('files');


// ================
// = Extra Crumbs =
// ================
// This section allows you to enter crumbs into the breadcrumb that aren't represented by a folder
// Entries are made in the form: addIncludedFolder('TEXT_NAME','LINK_TO_PAGE','CRUMB_TO_INSERT_BEFORE');
// where TEXT_NAME is replaced by the desired text and
// LINK_TO_PAGE is replaced by the link the inserted crumb should go to and
// CRUMB_TO_INSERT_BEFORE is replaced by the name of the folder this link should occur before in the breadcrumb
//
// NOTE: Only one crumb can be specified for each CRUMB_TO_INSERT_BEFORE, if more than one is specified only the first will appear
//
// Example: To insert a link to the "archive.html" page before the "Profile" link in the following breadcrumb you would use:
//          Home > Dakota Memories > Profile
//     addIncludedFolder('Narrator Profiles','/grhc/dakotamemories/archive.html','2005');




// ======================
// = Extra Crumbs After =
// ======================
// This section allows you to enter crumbs into the breadcrumb that you would like to appear after another crumb
// Entries are made in the form: addIncludedFolderAfter('TEXT_NAME','LINK_TO_PAGE','CRUMB_TO_INSERT_AFTER');
// where TEXT_NAME is replaced by the desired text and
// LINK_TO_PAGE is replaced by the link the inserted crumb should go to and
// CRUMB_TO_INSERT_AFTER is replaced by the name of the folder this link should occur after in the breadcrumb
//
// NOTE: Only one crumb can be specified for each CRUMB_TO_INSERT_AFTER, if more than one is specified only the first will appear
//
// Example: To insert a link to the "audio.html" page before the "Profile" link in the following breadcrumb you would use:
//          Home > Dakota Memories > Profile
//addIncludedFolderAfter('About Us','/grhc/dakotamemories/comments.html','dakotamemories');






// =========================
// = Change a Crumb's Link =
// =========================
// This section allows you to change the link of a specific crumb.  This should be done for all breadcrumbs that point to
// a folder that does not contain the file 'index.html'.
// Entries are made in the form: addLinkChange('FOLDER_NAME','TEXT_NAME','LINK_TO_PAGE');
// where FOLDER_NAME is replaced by the name of the folder to be omitted and
// TEXT_NAME is replaced by the desired text and
// LINK_TO_PAGE is replaced by the link the inserted crumb should go to
//
// Example: To change the link of the "Itinerary" in the following breadcrumb you would use:
//          Home > Dakota Memories > Heritage Tour > itinerary
//     addLinkChange('itinerary','Itinerary','/grhc/dakotamemories/heritage/itinerary/index.html');


  // --- Oral History Project folders --- //
addLinkChange('foodways','Foodways','/grhc/foods/foodways/index.htm');
addLinkChange('foods','Foods & Recipes','/grhc/foods/index.htm');

// ==========================
// ========== Code ==========
// ==========================

function addFolderName(folderName, textName){ // Generates folderNames and textNames lists
	folderNames[folderNameCount] = folderName;
	textNames[folderNameCount] = textName;
	folderNameCount++;
}
	
function addExcludedFolder(folderName){ // Generates excludedFolders list
	excludedFolders[excludedFolderCount] = folderName;
	excludedFolderCount++;
}

function addIncludedFolder(textName, linkURL, beforeFolder){
	includeFoldersName[includedFolderBeforeCount] = textName;
	includeFoldersLink[includedFolderBeforeCount] = linkURL;
	includeFoldersBefore[includedFolderBeforeCount] = beforeFolder;
	includedFolderBeforeCount++;
}

function addIncludedFolderAfter(textName, linkURL, afterFolder){
	includeFoldersAfterName[includedFolderAfterCount] = textName;
	includeFoldersAfterLink[includedFolderAfterCount] = linkURL;
	includeFoldersAfter[includedFolderAfterCount] = afterFolder;
	includedFolderAfterCount++;
}

function addLinkChange(folderName, textName, linkURL){
	addIncludedFolder(textName, linkURL, folderName);
	addExcludedFolder(folderName);
}
// ==========================
// ==== Third Party Code ====
// ==========================
/*
Photo Viewer 2.30
Language: JavaScript 1.0

Source: Web 1 Marketing, Inc.
(http://www.web1marketing.com)

Author: Josh Freedman
*/
function getObjectByID(id) {
  // Cross-browser function to return the object with the specific id

  if (document.all) { // IE
    return document.all[id];
  } else { // Netscape
    return document.getElementById(id);
  }
}


/*
Count the number of occurrences of a specific character in a string
Language: JavaScript 1.0

Source: Code Codex
(http://www.codecodex.com)
*/
String.prototype.count=function(s1) { 
	return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}


/*
Breadcrumbs in JavaScript
Language: JavaScript 1.0

Source: Webreference.com
(http://www.webreference.com/js/scripts/breadcrumbs/)

Author: Justin Whitford
Modified by: David Mart
*/
function discover_breadcrumbs() {
  sURL = new String;
  bits = new Object;
  links = new Object;
  var x = 0;
  var depth = 0;
  var stop = 0;
  var output = '<font size="2"><strong>';
  var tempOutput = '';
  sURL = location.href;
  sURL = sURL.slice(8,sURL.length);
  chunkStart = sURL.indexOf("/");
  sURL = sURL.slice(chunkStart+1,sURL.length);
  
  while(!stop){
    chunkStart = sURL.indexOf("/");
	depth = sURL.count('/');
    
	if (chunkStart != -1){
      bits[x] = sURL.slice(0,chunkStart);
	  links[x] = '';
	  var done = false;
	  
	  var insertionsMade = 0;
	  var len = x + 1;
	  while(!done){ 
		  for(var m in includeFoldersBefore){ // Scan to see if this element needs to have a crumb inserted before it
			  done = true;
			  if(bits[x] == includeFoldersBefore[m]){ // Check if the current bit should have a crumb before it
				  done = false;
				  while(len > x){
					  bits[len] = bits[len-1].toString(); // Move each element below the insertion point down one space
					  links[len] = links[len-1].toString();  // Move each element below the insertion point down one space
					  len--; // Move to the previous element in the list
				  }
				  bits[len] = includeFoldersName[m].toString(); // Insert the included folder name into the list
				  links[len] = includeFoldersLink[m].toString(); // Insert the included folder link into the list
				  len = len + 2; // Return the len counter to the actual length of the lists
				  insertionsMade++;
			  }
		  }
	  }
	  x = x + insertionsMade; // Move x to the last element in the list
	  var excluded = false;
	  var replaced = false;
	  
	  for(var p in excludedFolders){ // Scan excludedFolders for the current folder name
		  if(bits[x] == excludedFolders[p]){ // Check if the current bit should be excluded from the breadcrumb
			  excluded = true; // Signify that the current bit should in fact be excluded
			  for(var h in includeFoldersAfter){ // Scan to see if this element needs to have a crumb inserted after it
				  if(bits[x] == includeFoldersAfter[h]){ // Check if the current bit should have a crumb after it
					  bits[x] = includeFoldersAfterName[h].toString();
					  links[x] = includeFoldersAfterLink[h].toString();
					  replaced = true;
				  }
			  }
			  if(!replaced){ // If the bit has not been replaced by a crumb inserted after it then delete it
				  delete bits[x];  // Erase the excluded bit from the list
				  x--; // Set the counter back to the previous bit because the current one is supposed to be excluded
			  }
		  }
	  }
	  
	  if(!excluded){ // If the current folder is not excluded begin creating the breadcrumb element
		  var includedAfter = false;
		  for(var h in includeFoldersAfter){ // Scan to see if this bit needs to have a crumb inserted after it
			  if(bits[x] == includeFoldersAfter[h]){ // Check if the current bit should have a crumb after it
				  bits[x+1] = includeFoldersAfterName[h].toString();
				  links[x+1] = includeFoldersAfterLink[h].toString();
				  includedAfter = true; // A bit has been inserted after the current one
			  }
		  }
		  
		  tempOutput = 'index.html';  // In the event that no link '../' is required, index.html will be used
		  
		  for(y = 1; y < depth; y++){ // Create the link string for the current bit
			  if (tempOutput == 'index.html'){ // The link '../' is required so remove index.html from tempOutput
				  tempOutput = '';
			  }
			  tempOutput += "../";
		  }
		  
		  links[x] = tempOutput; // Store the link for this crumb in the links list
		  
		  for(var j in folderNames){ // Scan folderNames for a text version of the current folder name
			  if(bits[x] == folderNames[j]){ // Check if the current bit should has a text version name
				  bits[x] = textNames[j].toString(); // Replace the folder name with the text version
			  }
		  }
		  
		  if(includedAfter){ // If a bit has been included after this one increment the counter to point to the included (next) bit
			  x++;
		  }
	  }
	  
	  for(var h in includeFoldersAfter){ // Scan to see if this element needs to have a crumb inserted after it
		  if(bits[x] == includeFoldersAfter[h]){ // Check if the current bit should have a crumb after it
			  bits[x+1] = includeFoldersAfterName[h].toString();
			  links[x+1] = includeFoldersAfterLink[h].toString();
			  x++;
		  }
	  }
	  
      sURL = sURL.slice(chunkStart+1,sURL.length); // Remove the leading character ('/') from sURL
    }
	else{ // If there are no more folders in the URL then stop
      stop = 1;
	}
	
    x++;
  }
  
  for(var x in bits){ // Create the output string for the breadcrumb
	  output += "<a href=\"" + links[x] + "\">" + bits[x] + "</a> <img src='/grhc/images/template-images/arrow.gif' width='8' height='7'> "
  }
  
  var breadcrumb = getObjectByID(documentLocation); // Find the location on the page to place the breadcrumb
  var z = output.lastIndexOf(" <img src"); // Remove the last arrow image from the output string
  output = output.slice(0,z) + "</strong></font>"; // Finish closing the <strong> and <font> tags in the string
  breadcrumb.innerHTML = output;
}