/*
	BreadCrumbs.js
	Errol Sayre
	esayre@olemiss.edu
	
	Provided by the Office of Research and Sponsored Programs
	The University of Mississippi
	
	This script is free to be used but provides no warranty of function or quality.
*/

// BreadCrumb configuration
// set up the site prefix
var sitePrefix = "/fr/";
	// Please note that we don't use the whole prefix, just the important
	//	part that we need for the server so as to make this script easy for
	//	us to migrate between testing and live server. You can use whatever you
	//	like here.

// set up the bread crumb separator text
var breadCrumbSeparator = " > ";

// set up our list of crumb items
	// The bread crumb items refer to items on your site that require special
	//	labels. You can specify a label specifically using the URI of the
	//	file/folder or generically for all occurences of a file/folder.
var breadCrumbLabels = new Array();
//generic labels
//breadCrumbLabels["staff.html"] = "Staff List";
//specific labels
//breadCrumbLabels["/"] = "My Website";
//breadCrumbLabels["images/"] = "Images Folder";

function displayBreadCrumbs(attempts)
{
	// locate the breadcrumb container
	var theBreadCrumbBar = null;
	if (document.all)
	{
		theBreadCrumbBar = document.all.BreadCrumbBar;
	}
	else
	{
		theBreadCrumbBar = document.getElementById("BreadCrumbBar");
	}
	
	// check to make sure that we have our breadcrumb bar
	if (theBreadCrumbBar != null)
	{
		// get the current url
		// we'll want to ensure that we get the start of our site so, we'll
		//	ignore everything up to and including our site prefix
		var thePath = location.href;
		var theProtocol = "";
		var theSite = "";
		
		// strip out the protocol from the path
		theProtocol = thePath.substring(0, thePath.indexOf("://") + 3);
		thePath = thePath.substring(thePath.indexOf("://") + 3);
		
		// strip out the site name
		theSite = thePath.substring(0, thePath.indexOf(sitePrefix));
		thePath = thePath.substring(thePath.indexOf(sitePrefix));
		
		// strip out the site prefix
		thePath = thePath.substring(thePath.indexOf(sitePrefix) + sitePrefix.length);
		
		// remove hash links
		var theHash = "";
		if (thePath.indexOf("#") > -1)
		{
			theHash = thePath.substring(thePath.indexOf("#"));
			thePath = thePath.substring(0, thePath.indexOf("#"));
		}
		
		// break out the individual pieces of the location
		var crumbs = thePath.split("/");
		var currentPath = sitePrefix;
		var crumbCount = 0;
		// add a "home" link
		// create a bread crumb container
		var breadCrumb = document.createElement("span");
		breadCrumb.setAttribute("class", "breadCrumb");
		
		// determine the crumb label
		// first use the default
		var crumbLabel = "Home";
		
		// second look for a generic label
		if ((breadCrumbLabels[sitePrefix] != null))
		{
			crumbLabel = breadCrumbLabels[sitePrefix];
		}
		
		// third look for a specific label
		if ((breadCrumbLabels[currentPath] != null))
		{
			crumbLabel = breadCrumbLabels[currentPath];
		}
		
		// add the text
		// check to see if there are any crumbs after this one
		if ((0 < crumbs.length) &&
			(crumbs[0] != "index.html") &&
			(crumbs[0] != ""))
		{
			// create a new link
			var linkTag = document.createElement("a");
			
			linkTag.href = theProtocol + theSite + currentPath;
			linkTag.appendChild(document.createTextNode(crumbLabel));
			breadCrumb.appendChild(linkTag);
		}
		else
		{
			// add the text together
			breadCrumb.appendChild(document.createTextNode(crumbLabel));
		}
		theBreadCrumbBar.appendChild(breadCrumb);
		
		// increment our count of crumbs
		crumbCount++;
		
		// loop through the crumbs
		for (var crumbIndex = 0; crumbIndex < crumbs.length; crumbIndex++)
		{
			
			// setup the current path
			if(crumbs[crumbIndex]=="societe"){
			currentPath += "societe/aramark-dans-le-monde.php";
			} else if(crumbs[crumbIndex]=="catering"){
			currentPath += "catering/principes-fonctionnement.php";
			}else if(crumbs[crumbIndex]=="vending"){
			currentPath += "vending/gamme-complete.php";
			}else if(crumbs[crumbIndex]=="facility-services"){
			currentPath += "facility-services/facility-services.php";
			}else if(crumbs[crumbIndex]=="jobs"){
			currentPath += "jobs/offres-emploi.php";
			}else if(crumbs[crumbIndex]=="contact"){
			currentPath += "contact/comment-nous-contacter.php";
			}else if(crumbs[crumbIndex]=="rech"){
			currentPath += "rech/frmRech.php";
			}else {currentPath += crumbs[crumbIndex];
			
			if (crumbIndex + 1 < crumbs.length)
			{
				currentPath += "/";
			}
			}
			if ((crumbs[crumbIndex] != "") &&
				(crumbs[crumbIndex].indexOf("index.html") == -1))
			{
				// add this crumb to the list
				// create a bread crumb container
				var breadCrumb = document.createElement("span");
				breadCrumb.setAttribute("class", "breadCrumb");
				
				// add a greater than to the left hand side
				if (crumbCount > 0)
				{
					breadCrumb.appendChild(document.createTextNode(breadCrumbSeparator));
				}
				
				// determine the crumb label
				// first use the crumb itself
				var crumbLabel = crumbs[crumbIndex].replace(/_/g, " ").replace(".html", "").capitalize();
				if(crumbs[crumbIndex].indexOf("frmRech.php") != -1){
				var crumbLabel = crumbs[crumbIndex].replace("frmRech.php", "Aidez moi à choisir").capitalize();
			   	} else if(crumbs[crumbIndex].indexOf("resRech.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("resRech.php", "Resultats").capitalize();
				}

				if(crumbs[crumbIndex].indexOf("offres-emploi.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("offres-emploi.php", "Offres d'emploi");
				}
				if(crumbs[crumbIndex].indexOf("aramark-dans-le-monde.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("aramark-dans-le-monde.php", "Aramark dans le monde");
				}	
				if(crumbs[crumbIndex].indexOf("principes-fonctionnement.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("principes-fonctionnement.php", "Principes de fonctionnement");
				}
				if(crumbs[crumbIndex].indexOf("gamme-complete.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("gamme-complete.php", "Gamme complete");
				}	
				if(crumbs[crumbIndex].indexOf("online-vending.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("online-vending.php", "Vente en ligne");
				}	
				if(crumbs[crumbIndex].indexOf("online-products.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("online-products.php", "Liste des consommables et accessoires commandables en ligne.");
				}	
				if(crumbs[crumbIndex].indexOf("confirm_cmd.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("confirm_cmd.php", "Confirmation de votre commande en ligne.");
				}
				if(crumbs[crumbIndex].indexOf("facility-services.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("facility-services.php", "Facility Services.");
				}
				if(crumbs[crumbIndex].indexOf("candidature-spontanee.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("candidature-spontanee.php", "Candidature spontanée.");
				}		
				if(crumbs[crumbIndex].indexOf("form-confirm.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("form-confirm.php", "Confirmation d'envoi de la candidature spontanée.");
				}					
				if(crumbs[crumbIndex].indexOf("jobs.php?cat=1") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("jobs.php?cat=1", "Offres d'emploi - Catering.");
				}	
				if(crumbs[crumbIndex].indexOf("jobs.php?cat=2") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("jobs.php?cat=2", "Offres d'emploi - Vending.");
				}	
				if(crumbs[crumbIndex].indexOf("jobs.php?cat=3") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("jobs.php?cat=3", "Offres d'emploi - Facility Services.");
				}	
				if(crumbs[crumbIndex].indexOf("jobs.php?cat=4") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("jobs.php?cat=4", "Offres d'emploi - Office Support. ");
				}	
				if(crumbs[crumbIndex].indexOf("detail.php?cat=1") != -1){
					var crumbLabel =  "Offres d'emploi - Catering.  ";
				}	
				if(crumbs[crumbIndex].indexOf("detail.php?cat=2") != -1){
					var crumbLabel = "Offres d'emploi - Vending.  ";
				}	
				if(crumbs[crumbIndex].indexOf("detail.php?cat=3") != -1){
					var crumbLabel = "Offres d'emploi - Facility Services. ";
				}	
				if(crumbs[crumbIndex].indexOf("detail.php?cat=4") != -1){
					var crumbLabel =  "Offres d'emploi - Office Support.  ";
				}
				if(crumbs[crumbIndex].indexOf("form-full.php?cat=1") != -1){
					var crumbLabel = "Offres d'emploi - Catering.  ";
				}	
				if(crumbs[crumbIndex].indexOf("form-full.php?cat=2") != -1){
					var crumbLabel = "Offres d'emploi - Vending.";
				}	
				if(crumbs[crumbIndex].indexOf("form-full.php?cat=3") != -1){
					var crumbLabel =  "Offres d'emploi - Facility Services. ";
				}	
				if(crumbs[crumbIndex].indexOf("form-full.php?cat=4") != -1){
					var crumbLabel = "Offres d'emploi - Office Support.";
				}					
				if(crumbs[crumbIndex].indexOf("vending-catalogue.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("vending-catalogue.php", "Catalogue en ligne.");
				}
				if(crumbs[crumbIndex].indexOf("formmail.php") != -1){
					var crumbLabel = "Envoi de la demande.";
				}
				if(crumbs[crumbIndex].indexOf("liste_distrib.php?type=1") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("liste_distrib.php?type=1", "Boissons chaudes - Café Soluble.");
				}	
				if(crumbs[crumbIndex].indexOf("liste_distrib.php?type=2") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("liste_distrib.php?type=2", "Boissons chaudes - Café en grain.");
				}	
				if(crumbs[crumbIndex].indexOf("liste_distrib.php?type=3") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("liste_distrib.php?type=3", "Boissons chaudes - Café moulu.");
				}	
				if(crumbs[crumbIndex].indexOf("liste_distrib.php?type=4") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("liste_distrib.php?type=4", "Snacks et boissons froides.");
				}	
				if(crumbs[crumbIndex].indexOf("liste_distrib.php?type=5") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("liste_distrib.php?type=5", "Fontaines d'eau");
				}
				
				if(crumbs[crumbIndex].indexOf("frmRech_p2.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("frmRech_p2.php", "Aidez moi à choisir");
				}
				if(crumbs[crumbIndex].indexOf("frmRech_p3.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("frmRech_p3.php", "Aidez moi à choisir");
				}
				if(crumbs[crumbIndex].indexOf("frmRech_p4.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("frmRech_p4.php", "Aidez moi à choisir");
				}
				if(crumbs[crumbIndex].indexOf("frmRech_p5.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("frmRech_p5.php", "Aidez moi à choisir");
				}
				if(crumbs[crumbIndex].indexOf("frmRech_p6.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("frmRech_p6.php", "Aidez moi à choisir");
				}
				if(crumbs[crumbIndex].indexOf("comment-nous-contacter.php") != -1){
					var crumbLabel = crumbs[crumbIndex].replace("comment-nous-contacter.php", "Contactez nous.");
				}				
		
								
				// second look for a generic label
				if ((breadCrumbLabels[crumbs[crumbIndex]] != null))
				{
					crumbLabel = breadCrumbLabels[crumbs[crumbIndex]];
				}
				
				// third look for a specific label
				if ((breadCrumbLabels[currentPath] != null))
				{
					crumbLabel = breadCrumbLabels[currentPath];
				}
				
				// add the text
				// check to see if there are any crumbs after this one
				if ((crumbIndex + 1 < crumbs.length) &&
					(crumbs[crumbIndex + 1] != "index.html") &&
					(crumbs[crumbIndex + 1] != ""))
				{
					// create a new link
					var linkTag = document.createElement("a");
					linkTag.href = theProtocol + theSite + currentPath;
					linkTag.appendChild(document.createTextNode(crumbLabel));
					breadCrumb.appendChild(linkTag);
				}
				else
				{
					// add the text together
					breadCrumb.appendChild(document.createTextNode(crumbLabel));
				}
				theBreadCrumbBar.appendChild(breadCrumb);
				
				// increment our count of crumbs
				crumbCount++;
			}
		}
	}
	else if (attempts < 5)
	{
		// try again in a few seconds
		attempts++;
		setTimeout("displayBreadCrumbs(" + attempts + ");", 120);
	}
}

// add a handy function to the string class
String.prototype.capitalize = function()
{
	return this.replace(/\w+/g, function(a)
	{
		return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
	});
};

// set us up to display bread crumbs
displayBreadCrumbs(1);

