$(document).ready(function() { // As soon as the document loads, do this...
	loadScripts();
});

function loadScripts() {
	// Load script and stylesheet files separate from the HTML, then run a callback function.
	// http://plugins.jquery.com/project/xLazyLoader
	$.xLazyLoader({
		css: ['/cu/ssw/includes/style-breadcrumb.css'],
		js: ['/cu/ssw/includes/jquery.url.packed.js','/cu/ssw/includes/breadcrumb-sitemap.js'],
		load: function() { modifyHtml(); }
	});
}

function modifyHtml() {
	displayBreadcrumb();
}

function displayBreadcrumb() {
	// http://plugins.jquery.com/project/url_parser
	// 'socialwork.columbia.edu/directory/subdirectory/' and 
		// 'www.columbia.edu/cu/ssw/directory/subdirectory/'
		// both become 'directory/subdirectory/'
	var directoryPath = jQuery.url.attr('directory'); // '/cu/ssw/directory/subdirectory/'
	directoryPath = directoryPath.replace('/cu/ssw',''); // '/directory/subdirectory/'
	directoryPath = directoryPath.slice(1);	// 'directory/subdirectory/'
	directoryPath = directoryPath.split('/'); // ['directory','subdirectory','']
	directoryPath.pop(); // ['directory','subdirectory']
	var buildDirectoryPath = '';
	var buildBreadcrumb = '<a href="/cu/ssw/" class="home"></a> &raquo; ';
	for(i=0; i<directoryPath.length; i++) {
		buildDirectoryPath += directoryPath[i] + '/'; // Build directory path iteratively.
		// Only build the breadcrumb if a path is defined in the sitemap. 
			// Otherwise, some link text would read "undefined."
		if(breadcrumbSitemap[buildDirectoryPath]) {
			buildBreadcrumb += '<a href="/cu/ssw/' + buildDirectoryPath + '">' + breadcrumbSitemap[buildDirectoryPath] + '</a> &raquo; '; // Build breadcrumb iteratively.
		} 
	}
	buildBreadcrumb += 'Here'; // full breadcrumb HTML
	
	// Find masthead image that is 710px wide and build structure around it like this:
	/* <div id="mastheadWrapper" style="position:relative; width:710px; height:70px;">
			<div id="mastheadImage" style="position:absolute; top:0; left:0;"><img /></div>
			<div id="breadcrumbTrail" style="position:absolute; bottom:0; right:0;"></div>
	   </div> */
	var mastheadImage = $('img[width="710"]');
	if(mastheadImage.length > 0) {
		mastheadImage.wrap('<div id="mastheadWrapper" style="width:' + mastheadImage.attr('width') + 'px; height:' + mastheadImage.attr('height') + 'px;"></div>').wrap('<div id="mastheadImage"></div>');
		$('#mastheadImage').after('<div id="breadcrumbTrail" style="display:none;"></div>');
		$('#breadcrumbTrail').append(buildBreadcrumb); // Insert the breadcrumb.
		var delayShow = setTimeout("$('#breadcrumbTrail').fadeIn('fast')",3000);
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}