// Generates breadcrumbs for the current page based on the directory hierarchy.

// For the current page, the script displays the title of the page as the last breadcrumb but without a hyperlink.
// If the current page is an index page, the page title is not included in the breadcrumbs.

/*
Possible Options:
	homeLinkSelector
	separator
	breadcrumbsSelector
*/

(function($){

// Because we are not using index pages for each section, we need to tell the script where to find the section overview pages.
var dirTitles = {
	'dirName' : 'overview-page.htm',
	'products' : 'products/graphicarts.htm',
	'technicalinfo' : 'technicalinfo/index.htm',
};

function breadcrumbs() {
	var opts = {
			homeLinkSelector : '#home-link a',
			indexFile : 'index.htm',
			separator : ' &gt; ',
			breadcrumbsSelector : '.breadcrumbs',
			pageTitleSelector : '#content h5:first'
		},
		indexurl = $(opts.homeLinkSelector).get(0).href,
		siteroot = indexurl.substr(0, indexurl.length - opts.indexFile.length),
		breadcrumbs = $(opts.breadcrumbsSelector),
		currenturl = window.location.href,
		fromIndex = siteroot.length,
		toIndex = -1,
		subdirList = '',
		pagetitle = '',
		filename = '';
		
		function makePretty(str) {
		return str.replace(/-/g, ' ')
				  .replace(/\b(\w)/g, function(s) { return s.toUpperCase(); })
				  .replace(/Dst /, 'DST-')
				  .replace(/ A /g, ' a ')
				  .replace(/ An /g, ' an ')
				  .replace(/ The /g, ' the ')
				  .replace(/ Of /g, ' of ')
				  .replace(/ In /g, ' in ')
				  .replace(/ And /g, ' and ')
				  .replace(/ To /g, ' to ')
				  .replace(/About/, 'About Us')
				  .replace(/Contact/, 'Contact Us')
				  .replace(/Technicalinfo/, 'Technical Info');
				  
	}
	
	// print "Home"
	breadcrumbs.html(''); // clear out any existing content
	breadcrumbs.append('<a href="' + indexurl + '">Home</a>');
	
	// print " > subdir" for all subdirectories
	toIndex = currenturl.indexOf('/', fromIndex);
	while (toIndex != -1) {
		var subdir = currenturl.substr(fromIndex, toIndex - fromIndex);
		subdirList += subdir + '/';
		
		if (dirTitles[subdir] != null)		
			breadcrumbs.append(opts.separator + '<a href="' + siteroot + dirTitles[subdir] + '">' + makePretty(subdir) + '</a>');
		else
			breadcrumbs.append(opts.separator + '<a href="' + siteroot + subdirList + '">' + makePretty(subdir) + '</a>');
		
		fromIndex = toIndex + 1;
		toIndex = currenturl.indexOf('/', fromIndex);
	}
	
	// print " > page title"
	filename = currenturl.substr(fromIndex);
	if (filename.indexOf('index') == -1) { // Don't show page title on index pages
		pagetitle = makePretty($(opts.pageTitleSelector).text());
		//pagetitle = $(opts.pageTitleSelector).text();
		breadcrumbs.append(opts.separator + pagetitle);
	}
}
breadcrumbs();
// $(document).ready(breadcrumbs);
})(jQuery);

