var visibleMenu = "";
var hc; st = "";
var highlightedMenuHeader = "";
var highlightedSubmenuHeader = "";
var navBar = "navigationBar";

var delayedSection = "";


function initializeMenus()
{
	if (!document.all)
	{
		document.onmouseover = delayedHideCancel;
	}
	
	initializeMenuHeaders();
	initializeMenuLists();
	initializeMyChartsMenu();
}


function initializeMenuHeaders()
{
	var ps = document.getElementsByTagName('p');
	var theHeader, theClass;
	var menuHeaderPattern = /menuHeader/;
	
	for (var i = 0; i < ps.length; i++)
	{
		theHeader = ps[i];
		
		if (document.all)
		{
			theClass = theHeader.className;
			if (menuHeaderPattern.test(theClass))
			{
				theHeader.style.marginTop = "1px";
			}
		}
			
		theHeader.onmouseover = delayedShowMenu;
		if (!document.all)
			theHeader.addEventListener("mouseover", stopProp, false);
		else
			theHeader.onmouseleave = delayedHideCancel;
	}
}


function initializeMenuLists()
{
	var uls = document.getElementsByTagName('ul');
	var theMenuList, theClass;
	var menuListPattern = /menuList/;
	var submenuListPattern = /submenu/;
	
	for (var i = 0; i < uls.length; i++)
	{
		theMenuList = uls[i];
		theClass = theMenuList.className;
		
		if (menuListPattern.test(theClass))
		{
			theMenuList.onmouseover = keepVisible;
			if (!document.all)
				theMenuList.addEventListener("mouseover", stopProp, false);
			else
			{
				theMenuList.onmouseleave = delayedHideCancel;
				if (submenuListPattern.test(theClass))
					theMenuList.style.top = "-.45em";
				else
					theMenuList.style.top = "-.2em";
					
			}
		}
	}
}


function initializeMyChartsMenu()
{
	var theCookieData = getaCookie("myCharts");
	var chartOrder = getaCookie("myChartsOrder");
	
	var theMenu = document.getElementById("myChartsMenu");
	var genericLines;
	
	if (theCookieData == "")
	{
		if (chartOrder == "")
			genericLines = new Array("chartsIntro", "chartsUsing", "chartsDash", "chartsAdd", "chartsDelete");
		else
			genericLines = new Array("chartsIntro", "chartsUsing", "chartsDash", "chartsAdd", "chartsDelete");
		
		for (var i = 0; i < genericLines.length; i++)
		{
			theMenu.appendChild(document.getElementById(genericLines[i]).cloneNode(true));
		}
	}
	
	else
	{
		var cookieDataArray = theCookieData.split("/");
		var numberOfCharts = cookieDataArray.length
		var theCookie, chartName, j;
	
		for (var i = 0; i < numberOfCharts; i++)
		{
			theCookie = cookieDataArray[i];
			j = theCookie.lastIndexOf(":");
			chartName = theCookie.substring(0, j);
			
			var chartLine = document.getElementById("chart" + i);
			var newText = document.createTextNode(chartName + "'s Chart");
			chartLine.appendChild(newText);
			theMenu.appendChild(chartLine.cloneNode(true));
		}
		
		if (chartOrder == "")
			genericLines = new Array("chartsDash", "chartsAdd", "chartsDelete", "chartsDash", "chartsIntro", "chartsUsing");
		else
			genericLines = new Array("chartsDash", "chartsAdd", "chartsDelete", "chartsDash", "chartsIntro", "chartsUsing");
			
		for (var i = 0; i < genericLines.length; i++)
		{
			theMenu.appendChild(document.getElementById(genericLines[i]).cloneNode(true));
		}
	}
}


function delayedHideCancel()
{
	hc = setTimeout("hideCancel()",50);
}


function hideCancel()
{
	clearTimeout(st);
	hideMenu();
	st=""; hc="";
}


function stopProp(e)
{
	e.stopPropagation();
}


function delayedShowMenu(e)
{
	var section;
	
	if (e)
	{
		theEvent = e;
		section = theEvent.currentTarget.firstChild.firstChild.nodeValue;
	}
	else if (event)
	{
		theEvent = event;
		section = theEvent.srcElement.id;
		section = section.substring(0, (section.length - 6));
		event.cancelBubble = true;
	}
	
	if (section == "My Charts")
	{
		section = "myCharts";
	}
	
	section = uncapWord(section);
	
	var shiftDown = theEvent.shiftKey;
	
	var menusCookie = getaCookie("astroMenus");
	
	if ((shiftDown && menusCookie == "") || (shiftDown == false && menusCookie == "on"))
	{
		hideCancel();
		return;
	}
	
	if (st == "")
	{
		delayedSection = section;
		st = setTimeout("showMenu()",50);
	}
}


function showMenu()
{
	section = delayedSection;
	delayedSection = "";
	st = "";
	
	if (section == "")
		return;
	
	if (visibleMenu != section)
	{
		hideMenu(visibleMenu);
	}

	var header = headerObject(section);
	var menu = menuObject(section);
	
	parent.document.getElementById(navBar).style.height = "32em";
	
	var headerName = section + "Header";
	highlightedMenuHeader = hMenuHeader();
	highlightedSubmenuHeader = hSubmenuHeader();
	
	if (headerName == highlightedMenuHeader || headerName == highlightedSubmenuHeader)
	{
		menu.style.backgroundColor = "#06CEC2";
	}
	else
	{
		header.style.backgroundColor = "#F5EF50";
		menu.style.backgroundColor = "#F5EF50";
	}

	menu.style.visibility = "visible";
	visibleMenu = section;
}

		
function hideMenu()
{
	if (visibleMenu == "")
	{
		return "";
	}
	else
	{
		var header = headerObject(visibleMenu);
		var menu = menuObject(visibleMenu);
		
		menu.style.visibility = "hidden";
		
		var headerName = visibleMenu + "Header";

		if (headerName != highlightedMenuHeader && headerName != highlightedSubmenuHeader)
		{
			if (headerName.length == 7)
			{
				header.style.backgroundColor = "#F6F6EC";
			}
			else
			{
				header.style.backgroundColor = "#9DD15F";
			}
		}

		if (document.getElementById("div2") == null)
		{
			var theHeight = "8em";
		}
		else
		{
			var theHeight = "6em";
		}
		
		parent.document.getElementById(navBar).style.height = theHeight;
		visibleMenu = "";
	}
}
		
function keepVisible()
{
	clearTimeout(hc)
	hc="";
	
	if (document.all)
		event.cancelBubble = true;
}

function headerObject(section)
{
	return document.getElementById(section + "Header");
}

function menuObject(section)
{
	return document.getElementById(section + "Menu");
}

function hMenuHeader()
{
	return parent.highlightedMenuHeader;
}

function hSubmenuHeader()
{
	return parent.highlightedSubmenuHeader;
}


function goPage(theLocation)
{
	if (theLocation != "")
	{
		theLocation = "../" + theLocation;
		top.location = theLocation;
	}
}


function goChart(x)
{
	var theCookieData = getaCookie("myCharts");
	var cookieDataArray = theCookieData.split("/");
	var theCookie = cookieDataArray[x];
	var j = theCookie.lastIndexOf(":");
	
	var theChartName = theCookie.substring(0, j);
	var theChartCode = theCookie.substring(j+1, theCookie.length);
	
	var namesCookie = getaCookie("myChartsNames");
	var codesCookie = getaCookie("myChartsCodes");
	var k;
	
	if (namesCookie == "")
	{
		namesCookie = theChartName;
		codesCookie = theChartCode;
		k = 0;
	}
	else
	{
		var namesCookieArray = namesCookie.split("/");
		k = namesCookieArray.length;
		
		if (k == 10)
		{
			var m = namesCookie.indexOf("/");
			namesCookie = theChartName + "/" + namesCookie.substring(m+1, namesCookie.length);
			
			m = codesCookie.indexOf("/");
			codesCookie = theChartCode + "/" + codesCookie.substring(m+1, codesCookie.length);
			
			k = 0;
		}
		else
		{
			namesCookie += "/" + theChartName;
			codesCookie += "/" + theChartCode;
		}
	}
	
	var newCookie = "myChartsNames=" + namesCookie + "; domain=.astromusings.com; path=/";
	document.cookie = newCookie;

	var newCookie = "myChartsCodes=" + codesCookie + "; domain=.astromusings.com; path=/";
	document.cookie = newCookie;
	
	top.location = "../my-charts/mycharts-" + k + ".html";
}


function uncapWord(theWord)
{
	var firstChar = theWord.charAt(0);
	firstChar = firstChar.toLowerCase();
	theWord = firstChar + theWord.substring(1,theWord.length);
	return theWord;
}


function openSeparateWindow(theURL, theWindowName, theWidth, theHeight)
{
	if (theWidth == "")
	{
		theWidth = "570";
	}
	
	if (theHeight == "")
	{
		theHeight = "570";
	}
	
	if (window.outerWidth)
	{
		var theLeft = window.screenY + window.outerWidth - theWidth + 20;
		var chartWindow = window.open(theURL, theWindowName, "height=" + theHeight + ",width=" + theWidth + ",scrollbars=no,directories=no,location=no,menubar=no,screenx=" + theLeft);
	}
	else
	{
		var chartWindow = window.open(theURL, theWindowName, "height=" + theHeight + ",width=" + theWidth + ",scrollbars=nodirectories=no,location=no,menubar=no,status=no,left=500");
	}
}


function showTheChart(chartName)
{
/* 	document.getElementById("navigationBar").document.getElementById("chartsMenu").style.visibility = "hidden"; */
	document.getElementById("chartsMenu").style.visibility = "hidden";
	openSeparateWindow('../charts/public/' + chartName + '-chart.html', 'theChart', "", "");
}


function getaCookie(searchName)
{
	var theCookies = document.cookie.split(";");
	var theCookie, j, theCookieName, theCookieData;
	
	for (var i = 0; i < theCookies.length; i++)
	{
		theCookie = unescape(theCookies[i]);
		j = theCookie.indexOf("=")
		theCookieName = theCookie.substring(0, j);
		
		if (theCookieName.charAt(0) == " ")
		{
			theCookieName = theCookieName.substring(1, theCookieName.length);
		}
		
		if (theCookieName == searchName)
		{
			return theCookie.substring(j+1, theCookie.length);
		}
	}
	return "";
}


function createBV()
{
	var userAgent = navigator.userAgent;
	var cookieValue;
	var pattern = /MSIE/;
	var result = pattern.exec(userAgent);
	
	if (result)
	{
		var index = result.index + 5;
		var theVersion = userAgent.charAt(index);
		cookieValue = "ie," + theVersion;
	}
	else
	{
		cookieValue = "x,x";
	}
	
	var theCookie = "bv=" + cookieValue + "; expires=1-Jan-2012 00:00:00 GMT; domain=.astromusings.com; path=/";
	document.cookie = theCookie;
	
	return cookieValue;
}


function bv()
{
	var cookieValue = getaCookie("bv");
	
	if (cookieValue == "")
	{
		cookieValue = createBV();
		var bvSections = cookieValue.split(",");
		var theBrowser = bvSections[0];
		var theVersion = bvSections[1];
		
		if (theBrowser == "ie" && theVersion < 7)
		{
			window.location = "http://www.astromusings.com/browsers.html";
		}
	}
		
	return cookieValue;
}


function goHome()
{
	var currentLoc = top.location;
	
	if (currentLoc == "http://www.astromusings.com/index.html" || currentLoc == "http://www.astromusings.com/")
		top.location = "http://astromusings.com/cgi-bin/home.pl";
	else
		top.location = "http://www.astromusings.com/index.html";
}
