// Tabbed Menu
// James Barberousse
var default_menu = null;
var menu_delay = null;
var current_menu = null;
var current_tab = null;

function initMenu()
{
	// make sure these methods are suported first
	if (!document.getElementById || !document.getElementsByTagName) {return false;}
	if (!document.getElementById("primarynavigation")) {return false;}
	if (!document.getElementById("secondarynavigationwrapper")) {return false;}

	// find default_menu for the current page
	var s = "sn-home";
	var path = window.location.pathname;
	if (path.indexOf("/shows/") != -1) {s = "sn-shows";}
	if (path.indexOf("/events/") != -1) {s = "sn-events";}
	if (path.indexOf("/schools/") != -1) {s = "sn-schools";}
	if (path.indexOf("/parties/") != -1) {s = "sn-parties";}
	if (path.indexOf("/show-book/") != -1) {s = "sn-show-book";}
	if (path.indexOf("/e-newsletter/") != -1) {s = "sn-e-newsletter";}
	if (path.indexOf("/membership/") != -1) {s = "sn-membership";}
	if (path.indexOf("/gift-shop/") != -1) {s = "sn-gift-shop";}
	if (path.indexOf("/astronomy-101/") != -1) {s = "sn-astronomy-101";}
	if (path.indexOf("/about/") != -1) {s = "sn-about";}

	// set and display the default menu
	default_menu = document.getElementById(s);
	default_menu.style.visibility = "visible";

	// set event handlers for primary navigation menu to open secondary navigation menu
	var primary_navigation_links = document.getElementById("primarynavigation").getElementsByTagName("a");
	var a, li, section, secondary_navigation_menu;

	for (var i = 0; i < primary_navigation_links.length; i++)
	{
		a = primary_navigation_links[i];
		li = a.parentNode;
		section = li.id.substring(2);
		a.onmouseover = openMenu;
		a.onmouseout = setMenuDelay;
		secondary_navigation_menu = document.getElementById("sn-" + section);
		secondary_navigation_menu.onmouseover = clearMenuDelay;
		secondary_navigation_menu.onmouseout = setMenuDelay;
	}
}

function openMenu()
{
	var a = this;
	var li = a.parentNode;
	var section = li.id.substring(2);
	var secondary_navigation_menu = document.getElementById("sn-" + section);

	if (current_menu != null) {closeMenu();}

	if (secondary_navigation_menu != default_menu)
	{
		secondary_navigation_menu.style.zIndex = "1000";
		secondary_navigation_menu.style.visibility = "visible";
		default_menu.style.visibility = "hidden";
		current_menu = secondary_navigation_menu;
		a.className = "hovering";
		current_tab = a;
	}
}

function closeMenu()
{
	clearMenuDelay();
	default_menu.style.visibility = "visible";
	current_menu.style.visibility = "hidden";
	current_tab.className = "";
	current_tab = null;
	current_menu = null;
}

function setMenuDelay()
{
	var li = this.parentNode;
	var section = li.id.substring(2);
	var secondary_navigation_menu = document.getElementById("sn-" + section);

	if (secondary_navigation_menu != default_menu && current_menu != null)
	{
		menu_delay = setTimeout("closeMenu();", 500);
	}
}

function clearMenuDelay()
{
	if (menu_delay) {clearTimeout(menu_delay);}
}




// Planet Popup Info
// James Barberousse
var planetPopup = null;
function initPlanetPopup()
{
	if (!document.getElementById) return false;

	if (!document.getElementById("weight-age-calculate-button")) return false;

	var weightagecalculate = document.getElementById("weight-age-calculate-button");
	weightagecalculate.onclick = calculate_weight_and_age;

	var mercury = document.getElementById("mercury");
	mercury.onclick = mercury_popup;

	var venus = document.getElementById("venus");
	venus.onclick = venus_popup;

	var earth = document.getElementById("earth");
	earth.onclick = earth_popup;

	var mars = document.getElementById("mars");
	mars.onclick = mars_popup;

	var jupiter = document.getElementById("jupiter");
	jupiter.onclick = jupiter_popup;

	var saturn = document.getElementById("saturn");
	saturn.onclick = saturn_popup;

	var uranus = document.getElementById("uranus");
	uranus.onclick = uranus_popup;

	var neptune = document.getElementById("neptune");
	neptune.onclick = neptune_popup;
}

function mercury_popup()
{
	var mercury_info = document.getElementById("mercury-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	mercury_info.style.display = "block";
	planetPopup = mercury_info;
}

function venus_popup()
{
	var venus_info = document.getElementById("venus-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	venus_info.style.display = "block";
	planetPopup = venus_info;
}

function earth_popup()
{
	var earth_info = document.getElementById("earth-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	earth_info.style.display = "block";
	planetPopup = earth_info;
}

function mars_popup()
{
	var mars_info = document.getElementById("mars-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	mars_info.style.display = "block";
	planetPopup = mars_info;
}

function jupiter_popup()
{
	var jupiter_info = document.getElementById("jupiter-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	jupiter_info.style.display = "block";
	planetPopup = jupiter_info;
}

function saturn_popup()
{
	var saturn_info = document.getElementById("saturn-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	saturn_info.style.display = "block";
	planetPopup = saturn_info;
}

function uranus_popup()
{
	var uranus_info = document.getElementById("uranus-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	uranus_info.style.display = "block";
	planetPopup = uranus_info;
}

function neptune_popup()
{
	var neptune_info = document.getElementById("neptune-info");
	if (planetPopup)
	{
		planetPopup.style.display = "none";
	}
	neptune_info.style.display = "block";
	planetPopup = neptune_info;
}




// Weight and Age Calculator
// James Barberousse with formulas from Levent Gurdemir
function calculate_weight_and_age()
{
	f = document.getElementById("planetary-weight-and-age-calculator");

	// set conversion factors and regexp for error checking
	var weight_factors = {"Mercury":0.38, "Venus":0.91, "Earth":1.0, "Mars":0.38, "Jupiter":2.54, "Saturn":0.93, "Uranus":0.8, "Neptune":1.2};
	var age_factors = {"Mercury":87.9, "Venus":224.7, "Earth":365.24, "Mars":686.9, "Jupiter":4331.7464, "Saturn":10759.9704, "Uranus":30683.8124, "Neptune":60187.8996};
	var pattern = /^\d*\.?\d*$/;

	// check weight input
	var enteredWeight = f.weight.value;
	if ((enteredWeight == "") || (enteredWeight == "."))
	{
		enteredWeight = 0;
		f.weight.value = enteredWeight;
	}

	// check age input
	var enteredAge = f.age.value;
	if ((enteredAge == "") || (enteredAge == ""))
	{
		enteredAge = 0;
		f.age.value = enteredAge;
	}

	// find entered planet
	var enteredPlanet = null;
	for (i = 0; i < f.planet.length; i++)
	{
		if (f.planet[i].checked)
			enteredPlanet = f.planet[i].value;
	}

	if ((enteredWeight.search(pattern) == -1) || (enteredAge.search(pattern) == -1) || (enteredPlanet == null))
	{
		alert("Please enter a valid weight and age and select a planet.");
	}
	else
	{
		// perform calculations
		calculatedWeight = Math.round(100 * enteredWeight * weight_factors[enteredPlanet]) / 100;
		calculatedAge = Math.round(100 * enteredAge * 365.24 / age_factors[enteredPlanet]) / 100; 

		// if there are previous results, remove them first
		var theForm = document.getElementById("planetary-weight-and-age-calculator");
		if (document.getElementById("results"))
		{
			theForm.removeChild(document.getElementById("results"));
		}

		// insert results into a <p> element into the DOM tree after theForm element
		var p = document.createElement("p");
		p.setAttribute("id", "results");
		var result = "On the planet " + enteredPlanet + ", your weight is " + calculatedWeight + " pounds, and your age is " + calculatedAge + " years.";
		var resultText = document.createTextNode(result);
		p.appendChild(resultText);
		theForm.appendChild(p);
	}

	document.getElementById("weight-age-calculate-button").blur();
}




// Open new windows
// James Barberousse
function initOpenNewWindows()
{
	if (!document.getElementsByTagName) return false;
	if (document.all) return false; // don't do this in IE since there seems to be problems in IE6

	var anchorTags = document.getElementsByTagName("a");
	var linkObj;

	for (var i = 0; i < anchorTags.length; i++)
	{
		linkObj = anchorTags[i];
		if (linkObj.className == "newwindow") // linkObj.getAttribute("class", "popuplink") doesn't work in IE
		{
			linkObj.onclick = open_new_window;
		}
	}
}

function open_new_window()
{
	var filename = this.getAttribute("href");
	var description = this.getAttribute("rel");

	var width = 300;
	var height = 400;
	var features = "width=" + width + ",height=" + height + ",left=200,screenX=200,resizable=yes,scrollbars=no";
	var title = "Star Store Gift Shop at The Planetarium at UT Arlington";

	cleanupWindows();

	wind1 = window.open("", "wind", features);

	if (wind1)
	{
		wind1.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
		wind1.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">');
		wind1.document.writeln('');
		wind1.document.writeln('<head>');
		wind1.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
		wind1.document.writeln('<meta http-equiv="Content-Language" content="en-us" />');
		wind1.document.writeln('<meta http-equiv="Content-Script-Type" content="text/javascript" />');
		wind1.document.writeln('<title>' + title + '</title>');
		wind1.document.writeln('<style type="text/css">');
		wind1.document.writeln('* {padding:0; margin:0;}');
		wind1.document.writeln('a {color:#0000FF;}');
		wind1.document.writeln('a:hover {color:#FF0000;}');
		wind1.document.writeln('body {font-family:verdana,sans-serif; font-size:100%; background-color:#FFF; padding:0 10px 10px 10px;}');
		wind1.document.writeln('p#close {float:right; margin:5px;}');
		wind1.document.writeln('p#close a {text-decoration:none; color:#0000FF;}');
		wind1.document.writeln('p#close a:hover {text-decoration:none; color:#FF0000;}');
		wind1.document.writeln('h2 {font-size:0.7em; padding:10px 0; clear:both;}');
		wind1.document.writeln('p {font-size:0.7em; padding:5px 0;}');
		wind1.document.writeln('ul {list-style-type:disc; margin-left:15px;}');
		wind1.document.writeln('ol {list-style-type:upper-alpha; margin-left:15px;}');
		wind1.document.writeln('li {font-size:0.7em;}');
		wind1.document.writeln('</style>');
		wind1.document.writeln('</head>');
		wind1.document.writeln('');
		wind1.document.writeln('<body>');
		wind1.document.writeln('<p id="close"><a href="javascript:window.close();" title="close window">close window</a></p>');
		wind1.document.writeln('<h2>' + description + '</h2>');
		wind1.document.writeln('<img src="' + filename + '" alt="' + description + '" />');
		wind1.document.writeln('</body>');
		wind1.document.writeln('</html>');

		wind1.document.close();
		wind1.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function cleanupWindows()
{
	if (window.wind1 && window.wind1.open)
	{
		window.wind1.close();
		wind1 = null;
	}
}