
if (top != self) { top.location = self.document.location; } // break out of frames

var MSIE = (navigator.appName.indexOf("Internet Explorer") != -1) ? true : false;
var Opera = (navigator.appName.indexOf("Opera") != -1) ? true : false;

//	Multipurpose popup-window function. Width, height, and id are optional.
//	Accessible usage:	<a href="index.html" onClick="return pop(this);">link</a>
//	Alternate usage:	<a href="javascript:void(pop('index.html'));">link</a>
function pop(a, w, h, id) {
	var attr = (w && h) ? "width="+w+",height="+h+",location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no" : "";
	if (!id) { id = "_blank"; }
	window.open(a, id, attr);
	return false;
}

function alignMenu(strMenuID, strLinkID) {
	var linkObj = document.getElementById(strLinkID);
	var arrCoords = getOffset(linkObj);
	moveElement(strMenuID, arrCoords[0], arrCoords[1] + linkObj.offsetHeight);
}

function getOffset(hTarget) {
	var arrCoords = new Array(0,0);
	while (hTarget) {
		arrCoords[0] += !isNaN(hTarget.offsetLeft) ? hTarget.offsetLeft : 0;
		arrCoords[1] += !isNaN(hTarget.offsetTop) ? hTarget.offsetTop : 0;
		hTarget = hTarget.offsetParent;
	}
	return arrCoords;
}

//	Submit one of many target forms via selection of radio or selectbox items in another form
//	Target forms will receive the query value in the first input with [title="var"]
function submitMultiForm(hGroup, hQuery) {
	var sFormID = getSelectedValue(hGroup);
	var i, arrEl = document.forms[sFormID].elements;
	for (i=0; i < arrEl.length; i++) { if (arrEl[i].title == "var") { arrEl[i].value = hQuery.value; break; } }
	document.forms[sFormID].submit();
	return false;
}

//	Get the value of the selected item in a radio group or selectbox
function getSelectedValue(hGroup) {
	if (("type" in hGroup[0]) && (hGroup[0].type == "radio")) {
		for (var i=0; i < hGroup.length; i++) { if (hGroup[i].checked) { return hGroup[i].value; } }
	} else if (("type" in hGroup) && (hGroup.type == "select-one")) {
		return hGroup[hGroup.selectedIndex].value;
	}
	return "";
}

function setCheckedByValue(hTarget, sValue) {
	for (var i = 0; i < hTarget.length; i++) { if (hTarget[i].value == sValue) { hTarget[i].checked = true; return; } }
}

function getRandom(min, max) {
	return parseInt(min) + Math.floor(Math.random() * (parseInt(max) - parseInt(min) + 1));
}

function getRandomID() {
	var strID = "id-", strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	for (var i=0; i < 12; i++) { strID += strChars.charAt(getRandom(0, 61)); }
	return strID;
}

function moveElement(strID, intX, intY) {
	var hElement = document.getElementById(strID);
	intX = parseInt(intX); if (!isNaN(intX)) { hElement.style.left = intX + "px"; }
	intY = parseInt(intY); if (!isNaN(intY)) { hElement.style.top = intY + "px"; }
}

function showMenuFull(strMenuID, strLinkID) {
	alignMenu(strMenuID, strLinkID);
	showMenu(strMenuID);
}

function showMenu(strMenuID) {
	var hElement = document.getElementById(strMenuID);
	hElement.lastAction = "opened";
	hElement.style.display = "";
}

function waitMenu(strMenuID) {
	document.getElementById(strMenuID).lastAction = "closed";
	setTimeout("try { hideMenu('" + strMenuID + "'); } catch(ex) {}", 100);
}

function hideMenu(strMenuID) {
	var hElement = document.getElementById(strMenuID);
	if (hElement && hElement.lastAction == "closed") {
		hElement.style.display = "none";
	}
}

var SEARCH_MENUITEM = ["search", "search.html"];

var MAIL_POPUPMENU = [
					["GMail", "http://gmail.google.com/gmail"], ["Hotmail", "http://www.hotmail.com/"],
					["AOL WebMail", "http://aolmail.aol.com/"], ["Yahoo", "http://mail.yahoo.com/"]
				];
var MAIL_MENUITEM = ["mail", "mail.html", MAIL_POPUPMENU];

var CONVERT_POPUPMENU = [
					["strings", "stringmod.html"], ["measurement", "measure.html"], ["binary/hex", "binhex.html"],
					["language", "language.html"], ["MD5 hash", "md5.html"]
				];
var CONVERT_MENUITEM = ["convert", "convert.html", CONVERT_POPUPMENU];

var UTILITY_POPUPMENU = [
					["network query", "network.html"], ["markup validator", "validate.html"], ["color picker", "color.html"]
				];
var UTILITY_MENUITEM = ["utility", "utility.html", UTILITY_POPUPMENU];

var RESIZE_POPUPMENU = [
					["480x400", "javascript:void(top.resizeTo(480,400));"],
					["640x480", "javascript:void(top.resizeTo(640,480));"],
					["800x600", "javascript:void(top.resizeTo(800,600));"],
					["800x800", "javascript:void(top.resizeTo(800,800));"],
					["1024x768", "javascript:void(top.resizeTo(1024,768));"],
					["1152x864", "javascript:void(top.resizeTo(1152,864));"],
					["1280x960", "javascript:void(top.resizeTo(1280,960));"],
					["1280x1024", "javascript:void(top.resizeTo(1280,1024));"],
					["1600x1200", "javascript:void(top.resizeTo(1600,1200));"],
					["custom", "resize.html"],
					["floating", "javascript:void(pop('resize_float.html',200,150,'resizer'));"]
				];
var RESIZE_MENUITEM = ["resize", "resize.html", RESIZE_POPUPMENU];

function createNavBar() {
	document.write('<div class="nav">');
	document.write('<h1><a href="http://www.cosmicat.com/tool/">[ccc] Tools</a></h1>');
	document.write('<div class="menus"><ul>');
	addMenuItem(SEARCH_MENUITEM);
	addMenuItem(MAIL_MENUITEM);
	addMenuItem(CONVERT_MENUITEM);
	addMenuItem(UTILITY_MENUITEM);
	addMenuItem(RESIZE_MENUITEM);
	document.write('</ul></div></div>\n\n');
}

function addMenuItem(arrMenuItem) {
	if (arrMenuItem.length > 2) {
		var uID = getRandomID();
		document.write('<li><a href="' + arrMenuItem[1] + '" id="' + uID + '-a" onMouseOver="showMenuFull(\'' + uID + '\', \'' + uID + '-a\');" onMouseOut="waitMenu(\'' + uID + '\');">' + arrMenuItem[0] + '</a>');
		addPopupMenu(arrMenuItem[2], uID);
	} else {
		document.write('<li><a href="' + arrMenuItem[1] + '">' + arrMenuItem[0] + '</a>');
	}
	document.write('</li>');
}

function addPopupMenu(arrMenuItems, uID) {
	document.write('<div id="' + uID + '" class="popupmenu" style="display: none;" onMouseOver="showMenu(\'' + uID + '\');" onMouseOut="waitMenu(\'' + uID + '\');"><ul>');
	for (var i=0; i < arrMenuItems.length; i++) { addMenuItem(arrMenuItems[i]); }
	document.write('</ul></div>');	
}
