/* menu.js: accessory functions for pages generated from Freemind using our XSLT stylesheet.
These functions add style classes and modifications to the page. This keeps the XSLT simpler.
Copyright 2009, Attotron Biotechnologies
Author: Robert M. Horton, PhD (rmhorton@attoron.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

*/

function setupMenu(){	// onload handler
	markDemoLinks();
	markLoginLinks();
	markNotesLinks();
}

function markDemoLinks(){
	for (var i=0; i< document.links.length; i++){
		var link = document.links[i];
		if (link.pathname.match(/\/demo\//)){
			link.className = 'demoLink';
			var title = link.textContent;
			if ( ! title.match(/(lab|laboratory)$/i ))
				title = "'" + title + "' laboratory";
			else
				title = "'" + title + "'";
			link.title = '   Open the ' + title + ' in a new window, and the accompanying documentation in this window.';
			if (null == getCookie('CybertoryDemoVeteran'))
				link.onclick=demoWarning;
		}
	}
}

function demoWarning(){
	if (null == getCookie('CybertoryDemoVeteran')){
		var warning = "You are about to open the Virtual Laboratory in a new window, and load the accompanying documentation into this window. You may need to move and resize these windows so you can read both at once. If you have a small screen, you may want to print out the protocol, or have your lab partner read the protocol on another computer. Only this window will have a back button.";
		setCookie('CybertoryDemoVeteran');
		alert(warning);
	}
}

function markLoginLinks(){
	for (var i=0; i< document.links.length; i++){
		var link = document.links[i];
		if (link.pathname.match(/\/(simzymes||cms)\/$/)){
			link.className = 'loginLink';
			link.title = 'Go to the ' + link.textContent +  ' login page.';
		}
	}
}

function markNotesLinks(){
	for (var i=0; i< document.links.length; i++){
		var link = document.links[i];
		if ( 'notes page' == link.textContent ){	// link.textContent.match(/notes page/)
			link.className = 'notesLink';
			link.href = "javascript:bindNotesId('" + link.hash.substring(1) + "')";	// substring(1) removes hash mark
		}
	}
}

var NOTES_WINDOW;	// global
function bindNotesId(notesId){
	var notesDiv = document.getElementById(notesId).parentNode;
	// the element with this ID is a heading; we need it's parent
	if (!NOTES_WINDOW || NOTES_WINDOW.closed) {
		NOTES_WINDOW = window.open('',"notesWindow","HEIGHT=600,WIDTH=400,SCROLLBARS=yes");
	}
	if (NOTES_WINDOW){
		if ( (null == NOTES_WINDOW.opener.name) || ( 0 == NOTES_WINDOW.opener.name.length) )
			NOTES_WINDOW.opener.name = "mom"; // needs a name if we want to use it as a link target
		var msg = notesDiv.innerHTML;
		var html = '<html><head><title>Notes</title><body bgcolor="#FFFF88">';
		html += msg;
		html += '</body></html>'
		NOTES_WINDOW.document.open();
		NOTES_WINDOW.document.write(html);
		NOTES_WINDOW.document.close();
		for (var i=0; i< NOTES_WINDOW.document.links.length; i++){
			var link = NOTES_WINDOW.document.links[i];
			link.title = 'Link will open in other window';
			link.target = NOTES_WINDOW.opener.name;
		}

	}
	NOTES_WINDOW.focus();
}

function getObjectInfo(o){
	var msg = 'getObjectInfo: ';
	for (var e in o){
		msg += e + '=' + o[e] + "\n";
	}
	return msg;
}
