 $j(document).ready(function(){
	 	// Ensure all download links are styled as such
		var styleDownloadLinks = function() {
			var links = $j('ul.download-list li a, ul.mixed-list li a');
			for (var i =0; i< links.length; i++) {
				var thisLink = $j(links[i]);
				var linkHref = links[i].href;
				if (linkHref.endsWith('pdf')) {
					thisLink.addClass('pdf');
				}
				else if (linkHref.endsWith('zip')) {
					thisLink.addClass('zip');
				}
				else if (linkHref.endsWith('exe')) {
					thisLink.addClass('download');
				}
				else if (linkHref.endsWith('flv') || 
						linkHref.endsWith('swf') ||
						linkHref.endsWith('avi') ||
						linkHref.endsWith('mpg') ||
						linkHref.endsWith('wmv')) {
					thisLink.addClass('movie');
				}
				else if (linkHref.indexOf('mailto:')>-1) {
					thisLink.addClass('email');
				}
				else
				{
					thisLink.addClass('link');
				}
				if (thisLink.hasClass("link") && thisLink.attr("class").match("pdf|zip|download|video|multimedia|movie") != null)
				{
					thisLink.removeClass("link");
				}
			}
		};
		styleDownloadLinks();
		
		var BASEPATH = location.pathname.substring(0,6);
		var SITE_EXPLORER_URL = BASEPATH + "/includes/html/site_explorer.cfm";
		
		function retrieveBreadcrumbMenu(breadcrumbHeaderClass, addRelatedLinks) {
			var breadcrumbID = "#" + breadcrumbHeaderClass;
			var childLinks = "a." + breadcrumbHeaderClass;
			$j.ajax({
				type: 'GET',
				url: SITE_EXPLORER_URL,
				dataType: 'html',
				success: function(html) {
					if (html != undefined && html.length > 0) {
						var breadcrumb = $j("#breadcrumb dd." + breadcrumbHeaderClass)
						breadcrumb.append("<div><ul>");
						breadcrumb.find("div ul").append(html);
						if (addRelatedLinks) {
							addRelatedLinksFromBreadcrumbNavigation();
						}
					}
				},
				dataFilter: function(data, type) {
					var theChildren = $j(data).find(breadcrumbID).closest("li").siblings().find(childLinks).closest("li");
					theChildren.find("a").attr("id", "");
					theChildren.each(function() {
						// Remove the text "Main Page" from any children that have it
						var theChild = $j(this);
						theChild.html(theChild.html().replace("Main Page", ""));
					});
					return theChildren;
				} 
			});
		}
		
		function getBreadcrumbMenu() {
			var breadcrumbHeaderClass = $j(this).attr("class");
			if (breadcrumbHeaderClass != "") {
				retrieveBreadcrumbMenu(breadcrumbHeaderClass, false);
			}
		}
		
		// 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();		

		if ($j.browser.msie && $j.browser.version == "6.0") {
			// Load the Site Explorer on document ready into its own container
			// for IE 6 to speed up access to Site Explorer and breadcrumbs.
			$j("#site-explorer-content").load(SITE_EXPLORER_URL, addBreadcrumbMenusAndRelatedLinks);
		} else {
			// This binds a one-time event handler to each breadcrumb on mouseover. The event handler
			// calls the Site Explorer and gets each of the children of the breadcrumb the first time
			// that breadcrumb is moused over.
			$j("#breadcrumb dd").one("mouseover", getBreadcrumbMenu);
			// The last breadcrumb menu is obtained in addRelatedLinksFromBreadcrumbNavigation.
			$j("#breadcrumb dd:last").unbind("mouseover", getBreadcrumbMenu);
			retrieveBreadcrumbMenu($j("#breadcrumb dd:last").attr("class"), true);
		}
		
		// This function is designed for IE6 and should not be called in other browsers.
		function addBreadcrumbMenusAndRelatedLinks(responseText, textStatus, XMLHttpRequest) {
			if (textStatus == "success") {
				// Adds the breadcrumb drop-down menus from the Site Explorer in the DOM
				$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", "");
						theChildren.each(function() {
							// Remove the text "Main Page" from any children that have it
							var theChild = $j(this);
							theChild.html(theChild.html().replace("Main Page", ""));
						});
						if (theChildren != undefined && theChildren.length > 0) {
							var breadcrumb = $j("#breadcrumb dd." + breadcrumbHeaderClass)
							breadcrumb.append("<div><ul>");
							breadcrumb.find("div ul").append(theChildren);
							// This is needed for IE6 only.
							breadcrumb.hover(
								function() {
									var curBreadcrumbLayer = $j(this).find("div");
									curBreadcrumbLayer.addClass("active").show();
								},
								function() {
									var curBreadcrumbLayer = $j(this).find("div");
									curBreadcrumbLayer.removeClass("active").hide();
								}
							);
						}
					}
					$j(this).css("visibility", "visible");
				});
				addRelatedLinksFromBreadcrumbNavigation();
			}
		}
				
		function addRelatedLinksFromBreadcrumbNavigation() {
			var numberOfColumns = $j("#right-content .column").length;
			var isPageTypeContent = $j("body").attr("class") == "page-type-content";
			var isAnyChildrenOfCurrentPage = $j("#breadcrumb dd:last div").length > 0;
			// If there are any children of the current page (i.e. the last breadcrumb)
			if (isAnyChildrenOfCurrentPage && isPageTypeContent) {
				var columnIndex = 0;
				var relatedLinksLabel = "Related Links";
				var relatedLinksColumnHtml = "<div class=column><div class=sifr><h3 id='related-links-label'>" + relatedLinksLabel + "</h3></div>";
				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:first div.sifr:first").before("<div class=sifr><h3 id='related-links-label'>" + relatedLinksLabel + "</h3></div>");
						} else {
							$j("#right-content .column:first").append("<div class=hruler/><div class=sifr><h3 id='related-links-label'>" + relatedLinksLabel + "</h3></div>");
						}
					} else {
						$j("#right-content .column:first").prepend("<div class=sifr><h3 id='related-links-label'>" + relatedLinksLabel + "</h3></div>");
					}
				}
				$j("#right-content .column div.sifr").eq(columnIndex).after($j("#breadcrumb dd:last div").clone().find("ul").addClass("link-list"));
			}
			// Get the translated value for the related links label
			$j.get(BASEPATH + "/includes/xml/global-labels.xml", function(xml) { 
				$j("#related-links-label").text($j(xml).find("RELATED_LINKS").text());
			});
		}
		
		var relocatePagetoolsZone = function() {
			if ($j("#right-content #pagetools-zone").size() < 1) {
				$j("#right-content").prepend($j("#pagetools-zone"));
			}
		};
		relocatePagetoolsZone();
		
		function fixDownloadLists() {
			var previousList;
			var downloadLists = $j("ul.download-list");
			var downloadListCount = downloadLists.size();
			for (var i=0; i< downloadListCount; i++) {
				var isAnyDownloads = false;
				var isAnyLinks = false;
				var thisList = downloadLists.eq(i);
				var thisListClass = thisList.attr("class");
				downloadLists.eq(i).find("li a").each(function() {					
					var thisAnchor = $j(this);
					if (thisAnchor.hasClass("link")) {
						isAnyLinks = true;
					}
					if (thisAnchor.attr("class").match("pdf|zip|download|video|multimedia|movie") != null) {
						isAnyDownloads = true;
					}
				});
				
				if (previousList != thisList) {
					if (isAnyDownloads) {
						if (isAnyLinks) {
							thisList.attr("class", "mixed-list");
						}
					} else {
						if (isAnyLinks) {
							thisList.attr("class", "link-list");
						}
					}
				}
				previousList = thisList;
			}
		}
		fixDownloadLists();
 });