// common functions used throughout the toolbox.
// based on common.js from the hort6 Toolbox by Ian Minter.

var newWin = null;
function popWindow(url, windowType) {
	var xPos, yPos;
	switch (windowType) {
		case 'require':
			winFeatures='height=400,width=500,scrollbars=yes'; xPos = 20; yPos = 20;
		break;
		case 'credits':
			winFeatures='height=400,width=500,scrollbars=yes'; xPos = 20; yPos = 20;
		break;
		case 'disclaimer':
			winFeatures='height=400,width=500,scrollbars=yes'; xPos = 20; yPos = 20;
		break;		
		case 'criteria':
			winFeatures='height=450,width=668,menubar=yes,scrollbars=yes'; xPos = 20; yPos = 20;
		break;
		case 'resource':
			//if (screen.width > 810)
				//winFeatures='height=580,width=800, resizable=yes';
			//else
				winFeatures='height=497,width=720, resizable=yes';
			xPos = 20; yPos = 20;
		break;
		case 'document':
			winFeatures='height=480,width=680,menubar=yes,resizable=yes,scrollbars=yes,toolbar=yes';
			xPos = 20; yPos = 20;
		break;
		case 'ta': 		//text alternative
			winFeatures='height=500,width=700,menubar=yes,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;	
		case 'hint':	//hint
			winFeatures='height=180,width=350,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;
		case 'mag':		//magazine
			winFeatures='height=525,width=500,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 60; yPos = 60;
		break;	
		case 'manual':		// policy and standards manual
			winFeatures='height=525,width=720,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;	
		case 'getting_started':		// getting started - tour
			winFeatures='height=525,width=720,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;	
		case 'cdrom':		//health e-training cd rom
			winFeatures='height=525,width=720,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 40; yPos = 40;
		break;	
		case 'glossary':		//glossary
			winFeatures='height=200,width=300,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 50; yPos = 50;
		break;
		case 'glossary_with_index':		//glossary with index bar at the top
			winFeatures='height=500,width=700,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 50; yPos = 50;
		break;
		case 'comms':		//communications
			winFeatures='height=300,width=500,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;
		case 'gs':		//getting started
			winFeatures='height=525,width=720,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 20; yPos = 20;
		break;
		case 'weblink':
			winFeatures='height=525,width=720,menubar=yes,resizable=yes,scrollbars=yes,toolbar=yes'; xPos = 20; yPos = 20;
		break;
		case 'msds':		//material safety data sheet - used from mag: waste
			winFeatures='height=400,width=500,menubar=no,resizable=yes,scrollbars=yes,toolbar=no';
			xPos = 100; yPos = 100;
		break;
	}
	newWin=window.open(url, windowType, winFeatures);
	newWin.moveTo(xPos, yPos);
	newWin.focus();
}

function closeWin() {
	top.window.close();
}


// print a frame
function printFrame(fRef) {
	fRef.focus();
	fRef.print();
}

// checks whether mac is being used
function checkMac() {
	agent = navigator.userAgent.toLowerCase();
	isMac = agent.indexOf("mac") != -1;
}
	

// writes the print and close buttons onto the page. Doesn't show if JS isn't working
function write_print_and_close(resource) {
	document.write('<div align="right">');
	
	checkMac();
	
	if (top.location == self.location) { // if not in a frameset
		
		if (resource == "manual") {
			if (!isMac) {
				document.write('<a href="javascript:printFrame(this)" title="Print this page."><img src="images/icon_print.gif" width="60" height="33" alt="Print this page." /></a>&nbsp;&nbsp;');
			}
			document.write('<a href="javascript:top.window.close()" title="Close this window."><img src="images/icon_close.gif" width="60" height="33" alt="Close this window." /></a>');
		} else
		
		if (resource == "getting_started") {
			if (!isMac) {
				document.write('<a href="javascript:printFrame(this)" title="Print this page."><img src="images/print.gif" width="73" height="38" alt="Print this page." /></a>&nbsp;&nbsp;');
			}
			document.write('<a href="javascript:top.window.close()" title="Close this window."><img src="images/close.gif" width="73" height="38" alt="Close this window." /></a>');
		} else
		
		if (resource == "mag") {
			if (!isMac) {
				document.write('<a href="javascript:printFrame(this)" title="Print this page."><img src="images/icon_print.gif" width="60" height="20" alt="Print this page." /></a>&nbsp;&nbsp;');
			}
			document.write('<a href="javascript:top.window.close()" title="Close this window."><img src="images/icon_close.gif" width="60" height="20" alt="Close this window." /></a>');
		} else
		
		if (resource == "cd") {
			if (!isMac) {
				document.write('<a href="javascript:printFrame(this)" title="Print this page."><img src="images/icon_print.gif" width="77" height="29" alt="Print this page." /></a>&nbsp;&nbsp;');
			}
			document.write('<a href="javascript:top.window.close()" title="Close this window."><img src="images/icon_close.gif" width="77" height="29" alt="Close this window." /></a>');
		} 
		
	} 
	
	document.writeln('</div>');
	
}	
	

// writes the close button onto the page. Doesn't show if JS isn't working
function write_close() {
	document.writeln('<div align="right">');
	document.writeln('<a href="javascript:closeWin()" title="Close this window.">Close</a>');
	document.writeln('</div>');
}	



function loadGlossaryFrameset() {	
	if (top.location == self.location)  { // if not in a frameset
		
		page_title = document.title;  // get current page title
		current_word = location.hash.substring(1); // the current word - taken from after the # in the url
		
		newurl = "js_frameset.htm?" + current_word;
		top.location.replace(newurl); // replace the current doc with the realoader page
	} 
}	

function update_nav() {
	if (parent.navigation && parent.navigation.updateNav) { // make sure tb frame and function are there
		parent.navigation.updateNav(); // update navigation frame\
	}
}

///////// LMS functions

// modified from "SCORM 1.2 CONTENT - Content Developer's Guide"

var nFindAPITries = 0;
var objAPI = null;
var bFinishDone = false;

function FindAPI(win) {
	while ((win.API == null) && (win.parent != null) && (win.parent != win)) {
		nFindAPITries ++;
		if (nFindAPITries > 50) {
			alert("Error in finding LMS API -- too deeply nested.");
			return null;
		}
	win = win.parent;
	}
return win.API;
}

function APIOK() {
	return ((typeof(objAPI)!= "undefined") && (objAPI != null)) 
}

function init() {
	setTimeout("update_nav()",200); // update toolbox navigation frame after 1/5th second for slower machines

	objAPI = FindAPI(window);
	if ((objAPI == null) && (window.opener != null)){
		objAPI = FindAPI(window.opener);
	}
	if (!APIOK()) {
		if (!(parent.navigation && parent.navigation.updateNav) && (top.location != self.location)) { 
		// alert if not in toolbox frameset and not outside of a frameset
			//alert("Learning Management System interface not found.");
		}	
	} else {
		objAPI.LMSInitialize("");
	}
}

function finish() {
	if ((APIOK()) && (bFinishDone == false)) {
		bFinishDone = (objAPI.LMSFinish("") == "true");
	}
	return (bFinishDone);
}
