// Set up the breadcrumbs and highlight the right tab.

//Parts of the current URL.
var currentCategoryName = null;
var currentSubcategoryName = null;
var currentLessonName = null;
var currentFileName = null;

//Object for subcategory data.
function Subcategory(internalName, displayName) {
	this.internalName = internalName;
	this.displayName = displayName;
}

//Object for category data, including array of subcategories.
function Category(internalName, displayName) {
	this.internalName = internalName;
	this.displayName = displayName;
	this.subcategories = new Array();
	this.addSubcategory = function (newSubcategory) {
		this.subcategories[this.subcategories.length] = newSubcategory;
	}
}

//Main array for category objects.
var categoryData = new Array();

//Add a category to the main collection of category objects.
function addCategory(category) {
	categoryData[categoryData.length] = category;
}

//Create the category/subcategory structure.
var category = new Category("basic_lessons", "Basic lessons");
category.addSubcategory(new Subcategory("big_picture", "The big picture"));
category.addSubcategory(new Subcategory("introducing_html", "Introducing HTML"));
category.addSubcategory(new Subcategory("styling", "Styling with CSS"));
category.addSubcategory(new Subcategory("pit1", "Put it together 1: A simple site"));
category.addSubcategory(new Subcategory("management", "Managing a site"));
category.addSubcategory(new Subcategory("pit2", "Put it together 1: A managable site"));
addCategory(category);

category = new Category("javascript", "JavaScript");
category.addSubcategory(new Subcategory("basic_js", "Basic JavaScript"));
category.addSubcategory(new Subcategory("pit3", "Put it together 3: Navigation"));
category.addSubcategory(new Subcategory("more_js", "More JavaScript"));
category.addSubcategory(new Subcategory("pit4", "Put it together 4: Cool tools"));
addCategory(category);

category = new Category("php_mysql", "PHP/MySQL");
category.addSubcategory(new Subcategory("intro_php", "Introducing PHP"));
category.addSubcategory(new Subcategory("pit5", "Put it together 5: To the server"));
category.addSubcategory(new Subcategory("database", "Using a database server"));
category.addSubcategory(new Subcategory("pit6", "Put it together 6: All done"));
addCategory(category);

//Compute the category and subcategory for the current URL.
computeCategoryAndSubcategory();

function computeCategoryAndSubcategory() {
	var baseOfName = "webweb";
	var url = new String(document.location);
	//Add a file part if it isn't there.
	if ( url.substr(url.length-1, 1) == "/" ) {
		url += "index.html";
	} 
	//Split - last component is always a file name.
	var parts = url.split("/");
	//Find the component that is the logical root of the site.
	var siteRootComponent = -1;
	for ( i = 0; i < parts.length; i++ ) {
		if ( parts[i].indexOf(baseOfName) != -1 ) {
			siteRootComponent = i;
			break;
		}
	}
	if ( siteRootComponent == -1 ) {
		return;
	}
	if ( parts.length == siteRootComponent + 2 ) {
		//At root of site
		currentFileName = parts[parts.length - 1];
		return;
	}
	if ( parts.length == siteRootComponent + 3 ) {
		//Category only, no subcat.
		currentCategoryName = parts[siteRootComponent + 1];
		currentFileName = parts[parts.length - 1];
		return;
	}
	if ( parts.length == siteRootComponent + 4 ) {
		//Category only and subcat, no lesson.
		currentCategoryName = parts[siteRootComponent + 1];
		currentSubcategoryName = parts[siteRootComponent + 2];
		currentFileName = parts[parts.length - 1];
		return;
	}
	//Category, subcat, and lesson.
	currentCategoryName = parts[siteRootComponent + 1];
	currentSubcategoryName = parts[siteRootComponent + 2];
	currentLessonName = parts[siteRootComponent + 3];
	currentFileName = parts[parts.length - 1];
}	

//Set up the breadcrumbs on the page.
function setupBreadcrumbs() {
	if ( currentCategoryName == null ) {
		document.getElementById("breadcrumbs").style.hidden = true;
		return;
	}
	var html = "You are here: ";
	//Find the category
	var category = null;
	for ( i = 0; i < categoryData.length; i++ ) {
		if ( categoryData[i].internalName == currentCategoryName ) {
			//Found it
			category = categoryData[i];
			html += "<a href='/" + category.internalName + "/'>"
					+ category.displayName + "</a>";
		}
	}
	if ( category == null ) {
		return;
	}
	//Find the sub within the category.
	var subcategory = null;
	for ( i = 0; i < category.subcategories.length; i++ ) {
		if ( category.subcategories[i].internalName == currentSubcategoryName ) {
			//Found it
			subcategory = category.subcategories[i];
			html += " &gt; <a href='/" + category.internalName + "/"
					+ subcategory.internalName + "/'>"
					+ subcategory.displayName + "</a>";
		}
	}
	document.getElementById("breadcrumbs").innerHTML = html;
}

//Highlight the right tab.
function highlightTab() {
	var tabId = null;
	if ( currentCategoryName == null ) {
		//One of the top level pages.
		if ( currentFileName.indexOf("index") != -1 ) {
			tabId = "home_tab";
		}
		if ( currentFileName.indexOf("about") != -1 ) {
			tabId = "about_tab";
		}
	}
	else {
		tabId = currentCategoryName + "_tab";
	}
	if ( tabId != null ) {
		document.getElementById(tabId).className = "active";
	}
}