// Dynamic Breadcrumbs
// By Harry Love (http://harrylove.org/)
// License: http://creativecommons.org/licenses/by-sa/3.0/
// Updated: May 23, 2007
 
function Breadcrumb() {
	//////////////////////
	// Begin customization
	//////////////////////
	this.homeLinkText = 	'Home'; 	// Text for the home link
	this.separator = 		' > ';		// Character(s) to use between breadcrumbs
	this.homeLinkPosition = 0;			// Where is home? 0 = domain, 1 = 1st directory, and so on ...
	this.attachTo = 		'#breadcrumb';	// Attach breadcrumb to a tag or an ID: e.g., body, h1, div, #header, #breadcrumb
	this.replaceTheseCharacters = [		// You can replace any text with customized text
		["this text", "will be replaced with this text"],
		// ["_", " "], // Replace underscores with spaces
	['','']];
 
	////////////////////
	// End customization
	////////////////////
 
	this.output = writeHTML;
	var homeText = this.homeLinkText;
	var sep = this.separator;
	var position = this.homeLinkPosition;
	var tag = this.attachTo;
	var replacements = this.replaceTheseCharacters;
	var d = document, text = url = d.location.href.split('//')[1];
	url = url.split('/');
	if(url[url.length-1] == '') {url.pop();}
	url.pop();
	for (var i=0; i < replacements.length; i++) {
		rex = new RegExp(replacements[i][0], "g");
		text = text.replace(rex,replacements[i][1]);
	}
	text = text.split('/');
 
	function createBreadcrumbs() {
		var href = '', a, div = d.createElement('div');
		div.setAttribute('id', 'breadcrumbs');
		for (var i = 0; i < url.length; i++) {
			href += url[i] + '/';
			if (i < position) {continue;}
			if (i == position) {text[i] = homeText;}
			text[i] = text[i].charAt(0).toUpperCase() + text[i].substr(1);
			a = d.createElement('a');		
			a.setAttribute('href', 'http://' + href);
			a.appendChild(d.createTextNode(text[i]));
			div.appendChild(a);
			div.appendChild(d.createTextNode(sep));
		}
		div.appendChild(d.createTextNode(d.title));
		return div;
	}
 
	function writeHTML() {
		tag = tag.match('#') ? d.getElementById(tag.substring(1)) : d.getElementsByTagName(tag)[0];
		tag.appendChild(createBreadcrumbs());
	}
}
 
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
 
addLoadEvent(function() {
	var crumb = new Breadcrumb();
	crumb.output();					  
});
