// Title: Bread Crumb Maker version 2.01
// Author: Daniel Keller 32/7221
// Created: Oct 23, 2001

// Modified by: Kimberly Buckley
// Modified: May 29, 2001
    /* See comments below for the changes */

// Modified by: Dan Keller
// Modified: Feb 20, 2002
    /* Minor changes made to fix NN problem. NN was removing the spaces in the crumbs. */

// Modified by: David Shaw
// Modified: Apr 19, 2002
    /* Found out that Mac's thought that "undefined" was a variable. Changed to "null" and Mac's are fine. */ 


var url;
var splits;
var ROOT_FOLDER;
var ROOT_CONSTANT;
var ROOT_CODE;
var ROOT;
var ROOT_FOLDER_NAME;
var DEBUG = "";
var blnDEBUG = false;

/*  
    Enter in any words that you want that must keep a certain
    capitolization when viewed in the bread crumbs. For example:
    If you had B2E in your URL and you didn't put it in the array
    below, your crumb would print out "B2e" not "B2E" like you'd
    want it.
*/

SPECIAL_LIST = new Array("Japan Module");

/*  This grabs the pathname and strips away everything - kb*/

url = location.pathname; 

/*  Remove the last slash on the end if it's there. */

if( (url.length - 1) == (url.lastIndexOf("/") - 0) )

{

  url = url.substring(0, (url.length - 1));

}



/*  except the site root directory  - kb */

splits = url.split("/");



if (splits[1] == "public")

{

  ROOT_FOLDER = splits[2];

  ROOT_CONSTANT = "http://" + this.location.host + "/public/" + ROOT_FOLDER;

  ROOT_CODE = 2;

}

else

{

  ROOT_FOLDER = splits[1];

  ROOT_CONSTANT = "http://" + this.location.host + "/" + ROOT_FOLDER;

  ROOT_CODE = 1;

}



/*  This is the website root URL or HomePage - kb */

ROOT_FOLDER_NAME = cleanNameUp(ROOT_FOLDER) + " Home";

document.write("<SPAN class='breadcrumbs'>");

DEBUG += "<SPAN class='breadcrumbs'>";



/*  check to validate url and make sure we have a root folder */

if ((url == null) || (splits[ROOT_CODE] == null))

{ //test shows this is ok

writeBASE(); 

}

else 

	if (splits[ROOT_CODE + 1] == null)

	{	// test shows this is ok

  	writeBASE();

  	writeROOT();

	}

else 

	{ 	/*  We have some bread to bake: */

  	writeBASE();

  	writeROOT();

  	createBread();

	}

document.write("</SPAN>");

DEBUG += "</SPAN>";

if(blnDEBUG)alert(DEBUG);

 
/*

  This function writes the Bread Crumbs, if there are any.

*/

function createBread()

{

  totalCrumbs = splits.length - ROOT_CODE - 1;

  newURL = ROOT_CONSTANT;

  for (i=0 ; i < totalCrumbs ; i++)

  {

    crumb = splits[ROOT_CODE + 1 + i];

    if (crumb.indexOf(".") > -1)

    {

      /*  remove the file's extension */

      crumb = crumb.substring(0, crumb.indexOf("."));

      /*  If the file is a home page forget about it */

      if (crumb == "index" || crumb == "default" || crumb == "home")

      {

        break;

      }

    }

    newURL = newURL + "/" + splits[ROOT_CODE + 1 + i];

    strCrumb = "<SPAN class='breadcrumbs'>&nbsp;&gt;&nbsp;</SPAN><A HREF='" +

                   newURL +

                   "' class='breadcrumbs'>" +

                   cleanNameUp(crumb) + "</A>";

    document.write(strCrumb);

    DEBUG += strCrumb;

    }

}

/*

    This function takes in a string and makes it first caps. 
    For example, "thisSitESName" is converted to: Thissitesname

*/

function cleanNameUp(string)

{

  string = unescape(string);

  string = string.toLowerCase();



  /*split string into array*/

  names = string.split("_");

  string = "";

  /*first cap each word*/

  for(f=0; f < names.length; f++) 

  {

    /*rebuild name*/

    if(f == 0)

    {

      string += firstCap(names[f]);

    }

      else

    {

      string += "&nbsp;" + firstCap(names[f]);

    }

  }

  string = forceCase(SPECIAL_LIST, string);

  return string;

}



/*

  This function takes in a string and returns a string.

  It capitolizes the first letter in the word.

*/

function firstCap(c)

{

  c = c.toUpperCase().charAt(0) + c.substring(1 , c.length);

  return c;

}


/*
  This function will write the <A> tag for the BASE_URL.
*/

function writeBASE()

{

  var strBase = "&nbsp;<A HREF='http://www.rikkinyman.com/training' class='breadcrumbs'>Japan Module Home</A>";

  document.write(strBase);

  DEBUG += strBase;

  if(blnDEBUG)alert("Tell me you saw 'writeBase()'");

}

/*

  This function will write the <A> tag for the ROOT.

*/

function writeROOT()

{

  var strRoot = "&nbsp;&gt;&nbsp;<A HREF='" + ROOT_CONSTANT + "' class='breadcrumbs'>" +

                 ROOT_FOLDER_NAME +

                 "</A>";

  document.write(strRoot);

  DEBUG += strRoot;

}

  

/*

  This function takes in an Array of Strings and a String.

  The function returns a String. Strings that match will 

  change case guided by the strings in the Array.



  Example:

  if your array contained this: 

  "NIKE", "NIKETOWN", ...



  and your String contained this:

  "nike should always be in all caps."



  this function will return this:

  "NIKE should always be in all caps."

  

*/

function forceCase(arr, str)

{

  for(j = 0; j < arr.length; j++)

  {

    re = new RegExp(arr[j], "gi");

    str = str.replace(re, arr[j]);

  }

  return str;

}

