/* Copyright (c) 2011 Gregg A Dale. All rights reserved.
 * You may copy and modify this script as long as the above copyright notice,
 * this condition and the following disclaimer is left intact.
 * This software is provided by the author "AS IS" and no warranties are
 * implied, including fitness for a particular purpose. In no event shall
 * the author be liable for any damages arising in any way out of the use
 * of this software, even if advised of the possibility of such damage.
 * $Date: 2011-07-15 19:08:15 -0700 (Thursday, 15 July 2011) $
 */

function LoadMenu() 
{
	var menuItems = getMenu().split(";");
	var menuElement = document.getElementById("menu");
	cleanElement(menuElement);
	var index = ParseMenu(menuItems,menuElement,1,0);
}


function ParseMenu(Items, Element, level, index)
{
    var nextElement = document.createElement("ul");
    Element.appendChild(nextElement);
    nextElement.setAttribute("class","navigation-" + level);
    
    while(index < Items.length)
    {
    	if(Items[index].indexOf(level)==0)
	{
		var newLi = document.createElement("li");
		if(Items[index].indexOf(":") > -1)
		{
			var newLink = document.createElement("a");
			newLink.setAttribute("href",Items[index].substring(Items[index].indexOf(":")+1));
			newLink.appendChild(document.createTextNode(Items[index].substring(1, Items[index].indexOf(":"))));
			newLi.appendChild(newLink);
		}
		else
		{
			var newLink = document.createElement("a");
			newLink.setAttribute("href","#");
			newLink.appendChild(document.createTextNode(Items[index].substring(1)));
			newLi.appendChild(newLink);
		}	
		index++;
		if(Items[index].indexOf(level+1)==0)
		{
		    index = ParseMenu(Items, newLi, level + 1, index);		
		}
		nextElement.appendChild(newLi);
	}
	else
	{
		return index;	 	
	}
    }
    
    return index;
}

function GetParentElement(ParentElementID)
{
        return document.getElementById(ParentElementID);
}

function cleanElement(element)
{
	while(element.childNodes.length > 0)
	{
		element.removeChild(element.firstChild);
	}
}

function getMenu()
{
	var menu = "1Home:index.html;" +
                      "1Slip Rental;2Packages:packages.html;2Rates:rates.html;2Slip Map:slipmap.html;2Getting Started:startup.html;" +
                      "1News;" +
                      "1For Sale;2Boats:boats.html;2Parts:parts.html;2Supplies:supplies.html;" +
                      "1Useful Info;" +
                      "1Photo Galleries;2The Marina:marina.html;2Floods:floods.html;2The Fire:fire.html;2Wildlife:wildlife.html;"+
                      "1About Us;2Our Location:location.html;2History:history.html;2Contact Us:contact.html;" +
                      "1The River:river.html;";
	return menu;
}
