/************** Begin Initialization ****************/

function initializeNavigation()
{
	preloadNavButtons();

	var navList
	
	if (navList = document.getElementById('navigation_list'))
	{	
		var childNodes = navList.childNodes;
		
		var n = childNodes.length;
		var i;
		var e;
		
		var j, m, cn, ee;
		
		for (i=0; i<n; i++)
		{
			e = childNodes[i];
			
			if ((e.nodeType == 1) && (e.tagName == 'DIV'))
			{
				if (sectionShouldCollapse(e))
				{
					collapseSection(e);
				}
			}
		}
	}
}

// Preload the navigation buttons. Relies on them using a certain naming convention
function preloadNavButtons()
{
	var navButtonNames = Array
	(
		'accounts',
		'billing',
		'calcpayment',
		'contact',
		'help_support',
		'home',
		'profiles_account',
		'update_alert',
		'userprofile'
	)

	var n = navButtonNames.length;
	
	var ext = '.gif';
	
	var i;
	
	for (i=0; i<n; i++)
	{
		new Image().src = './gifs/navigation/' + navButtonNames[i] + '_1_hov' + ext;
		new Image().src = './gifs/navigation/' + navButtonNames[i] + '_2_hov' + ext;
		new Image().src = './gifs/navigation/' + navButtonNames[i] + '_2_reg' + ext;
	}
}

/************** End Initialization ****************/


/************** Begin URI Functions ****************/

// Gets the uri of an address without the query string or document fragment
function baseHref(uri)
{
	uri = uri.replace(/^(.*)[\?\#](.*)$/, "$1");
	return uri;
}

// Gets the page name of an address 
function pageName(uri)
{
	uri = uri.replace(/^.*\/([a-zA-Z0-9\._\-]+)$/, "$1");
	return uri;
}

// Gets the uri of an address without the page name, query string, or document fragment
function dirUri(uri)
{
	uri = baseHref(uri)
	return uri.replace(/^(.*)\/[a-zA-Z0-9\._\-]+$/, "$1/");
	return uri;
}

// Used to pop the navigation list open for pages that are not in the navigation list
function pageAlias(uri)
{
	uri = baseHref(uri);
	var uriStart = dirUri(uri);
	var pName = pageName(uri);

	switch (pName)
	{
		case 'index.html':
			pName = 'cust_profile.a4d';
			break;
			
		case 'cust_accountEdit.a4d':
			pName = 'cust_profile.a4d';
			break;

		case 'cust_accountpay.a4d':
			pName = 'cust_billing.a4d';
			break;
			
		case 'cust_calcpayment2.a4d':
			pName = 'cust_calcpayment.a4d';
			break;
			
		default:
			break;
	}

	return (uriStart + pName);
}

// Tells whether a uri targets the current web page or not
function targetsSelf(uri)
{
	if (pageAlias(window.location.href) == uri)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/************** End URI Functions ****************/


/************** Begin DOM Functions ****************/

function previousTagNode(aNode)
{
	while (aNode = aNode.previousSibling)
	{
		if (aNode.nodeType == 1)
		{
			return aNode;
		}
	}
	
	return null;
}

function nextTagNode(aNode)
{
	while (aNode = aNode.nextSibling)
	{
		if (aNode.nodeType == 1)
		{
			return aNode;
		}
	}
	
	return null;
}

function firstChildTag(aNode)
{
	var childNodes = aNode.childNodes;
	
	var n = childNodes.length;
	
	var i;
	
	for (i=0; i<n; i++)
	{
		if (childNodes[i].nodeType == 1)
		{
			return childNodes[i];
		}
	}
	
	return null;
}

/************** End DOM Functions ****************/

// Removes javascript event attributes from items that we no longer want clickable
function deLink(aLink)
{
	var obj;

	while (aLink.childNodes.length > 0)
	{
		try
		{
			if (obj = aLink.parentNode.insertBefore(aLink.removeChild(aLink.childNodes[0]), aLink))
			{
				if (obj.hasAttribute('onmouseover'))
					obj.removeAttribute('onmouseover');

				if (obj.hasAttribute('onmouseout'))
					obj.removeAttribute('onmouseout');
					
				if (obj.hasAttribute('onclick'))
					obj.removeAttribute('onclick');
			}
		}
		catch (err)
		{

		}
	}
	
	aLink.parentNode.removeChild(aLink);
}

// Determines if a navigation section should start out collapsed or expanded
function sectionShouldCollapse(aSection)
{
	var aNode = firstChildTag(aSection);
	
	while (aNode = nextTagNode(aNode))
	{
		if (listShouldCollapse(aNode))
		{
			return true;
		}
	}
	
	return false;
}

// Determines if a list contains a link to the current page
function listShouldCollapse(aList)
{
	var shouldCollapse = true;

	var i, e, ee, j, m, cn, imObj, linkURI;
	
	var childNodes = aList.childNodes;
	
	var n = childNodes.length;
	
	for (i=0; i<n; i++)
	{
		e = childNodes[i];
		
		if ((e.nodeType == Node.ELEMENT_NODE) && (e.tagName == 'DIV'))
		{
			cn = e.childNodes;
			
			m = cn.length;
			
			for (j=0; j<m; j++)
			{
				ee = cn[j];
				
				if ((ee.nodeType == Node.ELEMENT_NODE) && (ee.tagName == 'A'))
				{
					if (window.location.href == ee.href)
					{
						deLink(ee);
					}
					if (targetsSelf(ee.href))
					{
						shouldCollapse = false;
						break;
					}
				}
			}
		}
	}

	return shouldCollapse;
}

function highlight(buttonElement)
{
	var oldSrc			= buttonElement.src;	
	var components		= oldSrc.split('_reg.');
	var newSrc			= components[0];

	var ext				= components[1];
	
	newSrc				= newSrc + '_hov' + '.' + ext;
	
	buttonElement.src	= newSrc;
}

function unHighlight(buttonElement)
{
	var oldSrc			= buttonElement.src;	
	var components		= oldSrc.split('_hov.');
	var newSrc			= components[0];

	var ext				= components[1];
	
	newSrc				= newSrc + '_reg' + '.' + ext;
	
	buttonElement.src	= newSrc;
}

function handleNavClick(buttonElement)
{
	var parentNode = buttonElement.parentNode;
	
	toggleSectionDisclosure(parentNode);
}

function toggleSectionDisclosure(aSection)
{
	var disclosed = sectionIsDisclosed(aSection);
	
	if (disclosed)
	{
		collapseSection(aSection);
	}
	else
	{
		expandSection(aSection);
	}
}

function setDiscloseState(buttonElement, disclose)
{
	var buttonSrc = buttonElement.src;
	var newSrc;
	
	if (disclose)
	{
		newSrc = buttonSrc.replace(/(.*)(_2_)(.*)/, "$1_1_$3");
	}
	else
	{
		newSrc = buttonSrc.replace(/(.*)(_1_)(.*)/, "$1_2_$3");
	}
	
	buttonElement.src = newSrc;
}

function sectionIsDisclosed(aSection)
{
	var buttonElement = firstChildTag(aSection);
	
	var buttonSrc = buttonElement.src;
	
	if (buttonSrc.search(/(.*)(_2_)(.*)/))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function collapseSection(aSection)
{
	var buttonElement = firstChildTag(aSection);
	
	setDiscloseState(buttonElement, false);
	
	var aNode = buttonElement;
	
	while (aNode = nextTagNode(aNode))
	{
		collapseList(aNode);
	}
}

function expandSection(aSection)
{
	var buttonElement = firstChildTag(aSection);
	
	setDiscloseState(buttonElement, true);

	var aNode = buttonElement;
	
	while (aNode = nextTagNode(aNode))
	{
		expandList(aNode);
	}
}

function collapseList(aList)
{
	aList.style.display = 'none';
}

function expandList(aList)
{
	var e = aList;
	
	aList.style.display = '';	
}