// the_treehouse.js

window.onload = function()
{
   var categories = getCategories() //
   var posts      = getPosts()     //

	createTreehouseMenu(getTreehouseList  (categories).sort(), posts)  //
	createWhoWeAreMenu (getWhoWeAreSubList(categories).sort(), posts) //

   return null
}


function createTreehouseMenu(treehouses, posts)
{
	for( var cx = 0, treehouse ; treehouse = treehouses[cx] ; cx++ ) // for each treehouse subcategory
	{
	   var menuItem = create("li", "treehouse"+cx, id('treehouseList'))
	   var menuText = create("span", "treehouse"+cx+"scroller", menuItem)
		 menuText.setAttribute("class", "scroller closed")
		 menuText.setAttribute("className", "scroller closed")

		listener(text(menuText, treehouse), "click", scrollMenu)

		createCollapseableMenu(menuItem, getPostsByCategories(posts, "Treehouse_Sub: "+treehouse)) // create submenu
	}

   return id('treehouseList').getElementsByTagName('li') // reference to newly created menu items
}

function createWhoWeAreMenu(whoweares, posts)
{
	create("ol", "whoweareList", clear(id('treehouse1MenuContainer'))) // clear current list of posts and insert menu of subcategories

   var whoweare = getPostsByCategories(posts, "Treehouse_Sub: Who We Are").sortObjectsByProperty('title')

	for( var cx = 0, post ; post = whoweare[cx] ; cx++ ) // for each post
	{
	   var isSubCategory = false

		for( var bx = 0, category ; category = post.categories[bx] ; bx++ ) // for each category of post
		{
			if( category.indexOf('Treehouse_WhoWeAre_Sub: ')!=-1 )
			{
			   isSubCategory = new Boolean(true)
				break
			}
		}

		if( !isSubCategory )
		{
			create("li", "whoweare"+cx+"Li"    , id('whoweareList'))
			create("a" , "whoweare"+cx+"Li"+"A", id("whoweare"+cx+"Li")).setAttribute("href", post.href)

			text(id("whoweare"+cx+"Li"+"A"), post.title)
		}
	}

	for( var cx = 0, whoweare ; whoweare = whoweares[cx] ; cx++ ) // for each treehouse subcategory
	{
	   var menuItem = create("li", "whoweares"+cx, id('whoweareList'))

		text(menuItem, whoweare)

	   var submenuItem = create("ol", "whoweare"+cx+"Submenu", menuItem)

	   var individualPosts = getPostsByCategories(posts, "Treehouse_WhoWeAre_Sub: "+whoweare)

		for( var bx = 0, post ; post = individualPosts[bx] ; bx++ )
		{
			create("li", "whoweare"+cx+"Submenu"+"MenuLi"+bx    , id("whoweare"+cx+"Submenu"))
			create("a" , "whoweare"+cx+"Submenu"+"MenuLi"+bx+"A", id("whoweare"+cx+"Submenu"+"MenuLi"+bx)).setAttribute("href", post.href)

			text(id("whoweare"+cx+"Submenu"+"MenuLi"+bx+"A"), post.title)
		}
	}

   return id('treehouse1MenuContainer').getElementsByTagName('li') // reference to newly created menu items
}

// extracts and returns array of all prefixed treehouse categories from passed 'categories' array
function getTreehouseList(categories)
{
   var treehouse = new Array()

	for( var cx = 0, category ; category = categories[cx] ; cx++ ) // for each category
	{
		if( category.indexOf('Treehouse_Sub: ')!=-1 ) // if prefixed with 'Treehouse_Sub: '
		{
			treehouse.push(category.substring(15, category.length))
		}
	}

   return treehouse
}

// extracts and returns array of all prefixed 'Who We Are' categories from passed 'categories' array
function getWhoWeAreSubList(categories)
{
   var whowearesub = new Array()

	for( var cx = 0, category ; category = categories[cx] ; cx++ ) // for each category
	{
		if( category.indexOf('Treehouse_WhoWeAre_Sub: ')!=-1 ) // if prefixed with 'Treehouse_WhoWeAre_Sub: '
		{
			whowearesub.push(category.substring(24, category.length))
		}
	}

   return whowearesub
}

// Treehouse_Sub: Who We Are, Treehouse_WhoWeAre_Sub: Mike
<!-- ph=1 -->

