 $j(document).ready(function(){
	 	// Ensure all download links are styled as such
		var styleDownloadLinks = function() {
			var links = $j('ul.download-list li a');
			for (var i =0; i< links.length; i++) {
				var linkHref = links[i].href;
				if (linkHref.endsWith('pdf')) {
					$j(links[i]).addClass('pdf');
				}
				else if (linkHref.endsWith('zip')) {
					$j(links[i]).addClass('zip');
				}
				else if (linkHref.endsWith('exe')) {
					$j(links[i]).addClass('download');
				}
				else if (linkHref.endsWith('flv') || 
						linkHref.endsWith('swf') ||
						linkHref.endsWith('avi') ||
						linkHref.endsWith('mpg') ||
						linkHref.endsWith('wmv')) {
					$j(links[i]).addClass('video');
				}
				else if (linkHref.indexOf('mailto:')>-1) {
					$j(links[i]).addClass('email');
				}
				else
				{
					$j(links[i]).addClass('link');
				}
			}
		};
		styleDownloadLinks();
		
		// Adds the breadcrumb drop-down menus from the Site Explorer
		$j("#breadcrumb dd").each(function() {
			var breadcrumbHeaderClass = $j(this).attr("class");
			if (breadcrumbHeaderClass != "") {
				var breadcrumbID = "#" + breadcrumbHeaderClass;
				var childLinks = "a." + breadcrumbHeaderClass;
				var thisPage = $j(breadcrumbID).closest("li");
				var theChildren = $j(thisPage).siblings().find(childLinks).closest("li").clone();
				theChildren.find("a").attr("id", "");
				if (theChildren != undefined && theChildren.length > 0) {
					var breadcrumb = $j("#breadcrumb dd." + breadcrumbHeaderClass)
					breadcrumb.append("<div><ul>");
					breadcrumb.find("div ul").append(theChildren);
				}
			}
			$j(this).css("visibility", "visible");
		});
		
		// Adds an id of "right-content" to the element found with the class "right-content" (only
		// if there is exactly one such element).
		var addRightContentId = function() {
			if ($j(".right-content").length == 1) {
				$j(".right-content").attr("id", "right-content");
			}
		}

		addRightContentId();

		// Add a Related Links section in the right column from the contents
		// of the current page's breadcrumb navigation, if applicable.
		var addRelatedLinksFromBreadcrumbNavigation = function() {
			var relatedLinksLabel = "Related Links";
			var relatedLinksColumnHtml = "<div class=column><div class=sifr><h3>" + relatedLinksLabel + "</h3></div>";
			var isAnyChildrenOfCurrentPage = $j("#breadcrumb dd:last div").length > 0;
			var numberOfColumns = $j("#right-content .column").length;
			var isPageTypeContent = $j("body").attr("class") == "page-type-content";

			// If there are any children of the current page (i.e. the last breadcrumb)
			if (isAnyChildrenOfCurrentPage && isPageTypeContent) {
				var columnIndex = 0;
				var isAnyRightColumnImages = $j("#right-content .column img").length > 0 || $j("#right-content .column object").length > 0;
				// If there is no column in the right-content area
				if (numberOfColumns == 0) {
					// Create a new column in the right-content area
					$j("#right-content").append(relatedLinksColumnHtml);
				} else if (numberOfColumns == 1) {
					if (isAnyRightColumnImages) {
						$j("#right-content .column:first").after(relatedLinksColumnHtml);		
					} else {
						$j("#right-content .column:first").before(relatedLinksColumnHtml);			
					}
					columnIndex = 0;
				} else {
					columnIndex = 0;
					if (isAnyRightColumnImages) {
						if ($j("#right-content .column:first div.sifr").length > 0) {
                            $j("#right-content .column div.sifr").before("<div class=sifr><h3>" + relatedLinksLabel + "</h3></div>");
						} else {
                            $j("#right-content .column:first").append("<div class=hruler/><div class=sifr><h3>" + relatedLinksLabel + "</h3></div>");
						}
					} else {
						$j("#right-content .column:first").prepend("<div class=sifr><h3>" + relatedLinksLabel + "</h3></div>");
					}
				}
				$j("#right-content .column div.sifr").eq(columnIndex).after($j("#breadcrumb dd:last div").clone().find("ul").addClass("link-list"));
			}
		};
		addRelatedLinksFromBreadcrumbNavigation();
 });