//////////////////////////////////////////////////////////////////////////////
//                         Nifty Breadcrumbs Script                         //
//////////////////////////////////////////////////////////////////////////////
// 
// This is a neat little script that takes your site's file/folder structure
// and converts it into breadcrumbs. As an added bonus, it forces you to
// create a structure that is optimal for search engines to index and rank!
//
// CONTRIBUTORS:
//
// Original Creator:
//     Paul Hirsch
//     www.paulhirsch.com - personal site
//
// Other Contributors:
//
//     [INSERT YOUR NAME AND BRIEF DESCRIPTION OF YOUR CONTRIBUTION HERE]
//
// VERSIONS:
//
// 3.0
//     - Complete new markup and instructions.  Previous versions no longer
//       apply.
//
// 1.0, 1.1, 2.0
//     - Deprecated
//
// INSTRUCTIONS:
//
// 1.  Create your site structure using folders and files with useful names.
//     You may choose any character you'd like to replace spaces in your
//     URLs.  I recommend using underscores (as in the example below), but
//     this script will allow you to specify any single character you'd like
//     to be the replacement for spaces.  If you use something other than
//     underscores, you will need to make a change in the variables section.
//
//     EXAMPLE: http://www.squid.com/My_Pet_Squid/Meet_Rocky.html shows good
//     name choices to describe an area of content and the contents of a page.
//    
//     Alternatively, you can setup mod_rewrite via your .htaccess file to
//     create friendly URLs such as the one above.
//
// 2.  Fill in the settings for the variables in the next section of this
//     script.  They should be pretty self-explanatory.
//
// 3.  Add the following to your site wherever you want your breadcrumbs
//     to appear (change the path to point to wherever you put this script):
//
//     <div id="breadcrumbs"></div>
//     <script type="text/javascript" src="path/to/breadcrumbs.js"></script>
//
// LICENSE:
//
// This script is protected under General Public License (GPL).  Feel free to
// redistribute this script, so long as you do not alter any of the contents
// specifying authorship.  If you add to or modify this script, you may add
// your name to the "Other Contributors" list at the top of this script.  As
// a courtesy, please email me and let me know how you've improved my script!
// You may not profit from the direct sale of this script.  You may use this
// script in commercial endeavors however (i.e. as part of a commercial site).
//
// Email me here: http://www.paulhirsch.com/contact_me.php
//
// Copyright 2006, Paul Hirsch. All rights specified herein and within GPL
// documentation: http://www.gnu.org/licenses/gpl.txt
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// Change the following variables as instructed                             //
//////////////////////////////////////////////////////////////////////////////

// Enter the top level of your site here (non-root homepages are acceptable!)
var homeURL = 'visitcantonstark.com';

// Display breadcrumbs on your home page? 'Y' or 'N'
var homeDisplay = 'N';

// Enter the word you want to use to describe the home page of your site.
var home = 'Home';

// Enter the character(s) you are using for spaces in your URLs.
var space = '-';

// Enter the character(s) you want to use to separate your breadcrumbs.
var divide = '&raquo;';

// Enter text you'd like to see.  You can make this blank as well.
var pre = 'You are here: ';

// Add any file extensions you use to this list if you don't see them.
ext = new Array();
ext[0] = '.html';
ext[1] = '.htm';
ext[2] = '.php';
ext[3] = '.aspx';
ext[4] = '.asp';
ext[5] = '.cfm';
ext[6] = '.cgi';

// Want to replace any file names with custom names of your own?  Create
// replacement pairs in the following format: swap[i] = 'Old Words|New Words';
// Remove comments from swap array variables below to put them into use.
// Create additional replacement items by increasing the array number [i] as
// you add more lines. WARNING: all files on all levels will be affected!

swap = new Array(); // Uncomment below and add as many as you need
//swap[0] = 'Old Text|New Text';
//swap[1] = 'Old Text|New Text';

//////////////////////////////////////////////////////////////////////////////
// DO NOT TOUCH ANYTHING BELOW THIS LINE                                    //
// unless you know damned well what you're doing!                           //
//////////////////////////////////////////////////////////////////////////////

// Reformats some of the items from above.
home = '<span class="breadcrumbsHome">' + home + '</span>';
pre = '<span class="breadcrumbsPre">' + pre + '</span>';
divide = '<span class="breadcrumbDivide">' + divide + '</span>';

// Creates a few variables we're going to need later.
var path = '/';
var code = pre + '<a href="/">' + home + '</a> ' + divide;
var URL = window.location + '';
var oldwords;
var newwords;

// This set of instructions calls in the page URL and scrubs it down to the
// components we want to split into an array.
if (URL.search('#') !== -1) { URL = URL.substring(0,URL.indexOf('#')); }

URL = URL.substring(URL.indexOf(homeURL));
URL = URL.substring(URL.indexOf('/')+1);

// Splits the scrubbed URL into an array, based on folder level
folders = new Array();
folders = URL.split('/');

// Creates each piece of the path, names it accordingly and build out the
// breadcrumbs with this information.

for (i=0 ; i<folders.length ; i++) {
	if (folders[i] !== '') {
	
		// Sets up the path for the next link to be inserted.
		path += folders[i] + '/';
		if (i === (folders.length)-1) { path = path.substring(0,(path.length)-1); }
		
		// Removes file extensions from the text that will be displayed.
		for (j=0 ; j<ext.length ; j++) {
			if (folders[i].search(ext[j]) !== -1) {
				if (folders[i].search('To=') !== -1) { folders[i] = folders[i].substring(0,folders[i].indexOf('To=')-1); }
				if (folders[i].search('Subject=') !== -1) { folders[i] = folders[i].substring(0,folders[i].indexOf('Subject=')-1); }
				folders[i] = folders[i].substring(0,(folders[i].length-ext[j].length));
			}
		}
		
		// Replaces words specified by the user above.
		for (k=0 ; k<swap.length ; k++) {
			oldwords = swap[k].substr(0,swap[k].lastIndexOf('|'));
			newwords = swap[k].substr(swap[k].lastIndexOf('|')+1);
			folders[i] = folders[i].replace(new RegExp(oldwords, "g"),newwords);
		}
		
		// Replaces your spacing characters with actual spaces.
		folders[i] = folders[i].replace(new RegExp(space, "g"),' ');
		
		// Updates the breadcrumbs code string, in preparation for insert.
		code += ' <a href="' + path + '"><span class="breadcrumbsLink">' + folders[i] + '</span></a>'
		if (i !== (folders.length)-1) { code += ' ' + divide; }
	}
}

// Ties up loose ends.
if (code.substring(code.lastIndexOf(divide)) === divide) { code = code.substring(0,code.lastIndexOf(divide)); }

// Inserts the breadcrumbs into the page, unless it's the home page and
// you choose not to show breadcrumbs.
if (folders[0] === '') {
	if (homeDisplay === 'Y') document.getElementById('breadcrumbs').innerHTML = code;
} else {
	document.getElementById('breadcrumbs').innerHTML = code;
}
