// Returns the element in the Site Explorer that matches the specified path.
// The path should be relative from the site root (e.g. location.pathname from
// the DOM).
function getSiteExplorerActiveAnchor(path) {
	// TODO When all pages in the site have been republished, remove
	// the check here for isSiteExplorerAtBottom -- at that point
	// it will always be.  I only added this temporarily after moving
	// the Site Explorer content to the bottom so that pages which had
	// not yet been republished would still select the active link.
	var isSiteExplorerAtBottom = $j("#site-explorer-content").length == 1;
	var siteExplorerSelector = "#site-explorer-layer li a";
	if (isSiteExplorerAtBottom) {
		siteExplorerSelector = "#site-explorer-content li a";
	}
	return $j(siteExplorerSelector).filter(function() {
		return $j(this).attr("href").endsWith(path); 
	});
}


function siteExplorerDataLoaded(html) {
                        
    $j("#site-explorer-layer").append(html);
    $j("#se-explorer-wait").remove();
     // move to html page
    var navToHighlight = getSiteExplorerActiveAnchor(location.pathname);
    $j(navToHighlight).parent().addClass("active");
    $j(navToHighlight).parents('#site-explorer-layer li').removeClass("collapsed").addClass("expanded"); // take all 'li' parents of navToHighlight and remove class "collapsed" and add "extended"
    $j(navToHighlight).replaceWith("<span class='active'>" + $j(navToHighlight).text() + "</span>"); // switch out anchor tag with span tag
   
    SiteExplorer.registerEvents($("site-explorer-layer")); // notify menu engine to parse newly filled DOM
}

function siteExplorerDataError(XMLHttpRequest, textStatus, errorThrown) {
    // typically only one of textStatus or errorThrown 
    $j("#site-explorer-layer").append("<ul><li class='error'>An Error has occurred - Status: " + textStatus + ". Error thrown: " + errorThrown + "</li></ul>");
    $j("#se-explorer-wait").remove();
}   

SiteExplorer.expandSubtree = function(linkNode) {
	linkNode.addClassName("expanded");
	linkNode.up().removeClassName("collapsed");
	linkNode.up().addClassName("expanded");
	SiteExplorer.layer.afterOpen();
}

SiteExplorer.collapseSubtree = function(linkNode) {
	linkNode.removeClassName("expanded");
	linkNode.up().addClassName("collapsed");
	linkNode.up().removeClassName("expanded");
	SiteExplorer.layer.afterOpen();
}

SiteExplorer.followLink = function(linkNode) {
	//if (linkNode.innerHTML != "Home") {
	//	alert("clicked link: " + linkNode.innerHTML + "\nThe Site Explorer will be closed.");
	//	Layer.closeCurrent();
	//}
}

SiteExplorer.getContent = function() {
	var navToHighlight = getSiteExplorerActiveAnchor(location.pathname);
	$j(navToHighlight).parent().addClass('active');
	$j(navToHighlight).parents('#site-explorer-content li').removeClass('collapsed').addClass('expanded');
	$j(navToHighlight).replaceWith('<span class=active>' + $j(navToHighlight).text() + '</span>');
	SiteExplorer.registerEvents($('site-explorer-layer'));
	return $j('#site-explorer-content').html();
}


