//  START JUMP PAGE FUNCTIONS
function initMenu() {
	var combinedId = "";
	for (var i=0; i<plays.length-1; i++) {
		combinedId += unitCode + officialCode + plays[i] + stringedArrayDelimiter;
	}
	combinedId += unitCode + officialCode + plays[plays.length-1];
	readThis (courseCode, combinedId);
	setTimeout(checkEm,1000);
}

function checkEm() {
	for (var j=0; j<plays.length; j++) {
		combinedId = unitCode + officialCode + plays[j];
		iconId = plays[j] + "Icon";
		if (soVars[combinedId] == "pass") {
			imageSrc = document.getElementById(iconId).src.substring(0,document.getElementById(iconId).src.lastIndexOf('.')) + "_done.jpg";
			document.getElementById(iconId).src = imageSrc;
		} else if (soVars[combinedId] == "visited") {
			imageSrc = document.getElementById(iconId).src.substring(0,document.getElementById(iconId).src.lastIndexOf('.')) + "_visit.jpg";
			document.getElementById(iconId).src = imageSrc;
		}
	}

}

function setAll(val) {
	var variableToSave = "";
	var valueToSave = "";
	for (var i=0; i<(plays.length-1); i++) {
		variableToSave += unitCode + officialCode + plays[i] + stringedArrayDelimiter;
		valueToSave += val + stringedArrayDelimiter;
	}
	variableToSave += unitCode + officialCode + plays[plays.length-1];
	valueToSave += val;
	saveThis (courseCode, variableToSave, valueToSave);
	setTimeout(refreshPage,1000);
}

function refreshPage() {
	window.location = window.location;
}

function changeSrc(el, newSrc) {
	el.src = newSrc;
	}

// END JUMP PAGE FUNCTIONS
// START SHARED OBJECT READER AND WRITER FUNCTIONS
var stringedArrayDelimiter = "||";

function saveThis (fileToSaveIn, variableToSave, valueToSave) {
	saveThisFlash = new FlashObject("flash/soSet.swf", "soSet", "50", "50");
	saveThisFlash.addVariable("soFileIn", fileToSaveIn);
	saveThisFlash.addVariable("soVarIn", variableToSave);
	saveThisFlash.addVariable("soValueIn", valueToSave);
	saveThisFlash.addVariable("delimiter", stringedArrayDelimiter);
	el = generateEl();
	el.className = "offLeft";
	saveThisFlash.writeDOM(el);
}

function readThis (fileToReadFrom, variablesToRead) {
	readThisFlash = new FlashObject("flash/soRead.swf", "soRead", "50", "50");
	readThisFlash.addVariable("soFileOut", fileToReadFrom);
	readThisFlash.addVariable("soVarOut", variablesToRead);
	readThisFlash.addVariable("delimiter", stringedArrayDelimiter);
	el = generateEl();
	el.className = "offLeft";
	readThisFlash.writeDOM(el);
}

var soVars = new Object();

function soReaderFromFlash(variablesRead, valuesRead, delimiterUsed) {
	var variablesReadArray = new Array();
	var valuesReadArray = new Array();
	variablesReadArray = variablesRead.split(delimiterUsed);
	valuesReadArray = valuesRead.split(delimiterUsed);
	for (var i=0; i<variablesReadArray.length; i++) {
		soVars[variablesReadArray[i]] = valuesReadArray[i];
	}
}

function generateEl(parentEl) {
	if (parentEl) {
		var theParent = document.getElementById(parentEl);
	} else {
		var theParent = document.getElementsByTagName("body").item(0);
	}
	var elToAdd = document.createElement("div");
	var theEl = theParent.appendChild(elToAdd);
	return theEl;
}

// END SHARED OBJECT FUNCTIONS
// Playball functions //

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//End playball functions //

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  sample=window.open(theURL,winName,features); sample.window.focus();
}



function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function construction () {
     alert("This feature is currently under construction.");
}

function actionNotice() {
     alert("Click the Submit button to see your feedback.");
}


function windowSize () {

var targetheight=460;
var targetwidth=775;

function winWid(){ return (ns4||ns6) ? window.innerWidth : document.body.clientWidth; }
function winHei(){ return (ns4||ns6) ? window.innerHeight : document.body.clientHeight; }
function centerX(){ return (ns4||ns6) ? parseInt((screen.width/2)-(width/2)) : parseInt((screen.availWidth/2)-(width/2)); }
function centerY(){ return (ns4||ns6) ? parseInt((screen.height/2)-(height/2)) : parseInt((screen.availHeight/2)-(height/2)); }
var ns4 = (document.layers) ? 1 : 0;
var ie4 = (document.all) ? 1 : 0;
var ns6 = (document.getElementById && !document.all) ? 1 : 0;
top.resizeTo(targetwidth,targetheight);
var width=winWid();
var height=winHei();
var difx=targetwidth-winWid();
var dify=targetheight-winHei();
width+=(difx*2);
height+=(dify*2);
top.resizeTo(width,height);
top.moveTo(centerX(),centerY());

}




function glossaryplatformDetect()
{
  if(navigator.appVersion.indexOf("Win") != -1)
  {
    MM_openBrWindow('glossary/index.cfm','whatever1','toolbar=no,location=no,status=no,menubar=no,resizable=no,width=550,height=375');
  }
  else if(navigator.appVersion.indexOf("Mac") != -1)
  {
    MM_openBrWindow('glossary/index.cfm','whatever1','toolbar=no,location=no,status=no,menubar=no,resizable=no,width=535 ,height=375');
  }
  else alert("Other");
}


function libraryplatformDetect()
{
  if(navigator.appVersion.indexOf("Win") != -1)
  {
    MM_openBrWindow('library/index.cfm','whatever1','toolbar=no,location=no,status=no,menubar=no,resizable=no,width=550,height=375');
  }
  else if(navigator.appVersion.indexOf("Mac") != -1)
  {
    MM_openBrWindow('library/index.cfm','whatever1','toolbar=no,location=no,status=no,menubar=no,resizable=no,width=535 ,height=375');
  }
  else alert("Other");
}

function dndFeedback(){
switch(FBLocation){
	case ("FBOnPage"):
		changeClassById("dndPosFb", "on");
		break;
	case ("FBNextPage"):
		myFileName = getFileName (window.location.toString()); // get the file name
		myFileExt = getFileExtension (window.location.toString()); // get the file extension
		pageAdvance("p");
		break;
	}
}// end dndFeedback

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external") {
	   	anchor.target = "_blank";
	} else {
	   	anchor.target = "_self";
	}
 }
}

// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}
addEvent(window, "load", externalLinks);



function changeClassOfParent(el, newClass) {
/*
+===========================================-===========================================+
|                                    VERSION HISTORY                                    |
| 1.0.00  2004.08.30  JEM  Initial version.                                             |
|                                        SUMMARY                                        |
| Given an element and the desired class, change the class of the element's parent.     |
|                                       PARAMETERS                                      |
| el - usually a 'this' reference; reference to the element that called this function.  |
| newClass - the desired class to assign to the element's parent.                       |
|                                  DEPENDANT FUNCTIONS                                  |
| changeClassById.                                                                      |
|                                     EXAMPLE USAGE                                     |
| <div id="els1"><a href="#" onclick="changeClassOfParent(this,'off'); return false;">  |
| Linked text or image</a></div>                                                        |
+===========================================-===========================================+
*/
            changeClassById(el.parentNode.id, newClass);
}
function changeClassById(id, newClass){
/* +===============================================================================================================+
   | Given the ID attribute of the desired HTML element and the newClass to which the element should change,       |
   | do just that and assign newClass to the class property of the element ID.                                     |
   +===============================================================================================================+ */

identity=document.getElementById(id);  // This is required for Mozilla-based browsers (including NS)
identity.className=newClass;  // Change the element's class to newClass
} // end change()



 
var ht, wd
ht = screen.height - 75;
wd = screen.width - 10;
var anatomywin
function anatomytv(start,res)
{
 var windopts;
 windopts = "fullscreen=0,top=0,left=0,width=" + wd + ",height=" + ht + ",resizable=0,status=1,maximised=1,toolbar=0,location=0";
 
 if ((!anatomywin) || (anatomywin.closed))
  anatomywin = window.open('http://www.anatomy.tv/default.asp?ref=hk&jumpthrough=yes&startapp='+start+'&startres='+escape(res),'anatomywin', windopts);
 else
 {  
  if (anatomywin.dummy)
   anatomywin.dummy.location='http://www.anatomy.tv/links.asp?jumpthrough=yes&res=' + escape(res);
  else
   anatomywin.top.location = 'http://www.anatomy.tv/default.asp?ref=hk&jumpthrough=yes&startapp='+start+'&startres='+escape(res);
 }
anatomywin.focus();
}
/*
 * FlashObject embed
 * http://blog.deconcept.com/2004/10/14/web-standards-compliant-javascript-flash-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 * modified by Jason Mock -- added writeDOM method.
 *
 * v1.0.7.1 - 12-15-2004 - jem
 *
 * Create and write a flash movie to the page, includes detection
 *
 * Usage:
 *
 *	myFlash = new FlashObject("path/to/swf.swf", "swfid", "width", "height", flashversion, "backgroundcolor");
 *	myFlash.altTxt = "Upgrade your Flash Player!";                // optional
 *	myFlash.addParam("wmode", "transparent");                     // optional
 *	myFlash.addVariable("varname1", "varvalue");                  // optional
 *	myFlash.addVariable("varname2", getQueryParamValue("myvar")); // optional
 *	myFlash.write();
 *
 */

FlashObject = function(swf, id, w, h, ver, c) {
	this.swf = swf;
	this.id = id;
	this.width = w;
	this.height = h;
	this.version = ver || 6; // default to 6
	this.align = "middle"; // default to middle
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	this.altTxt = "Please <a href='http://www.macromedia.com/go/getflashplayer'>upgrade your Flash Player</a>.";
	this.bypassTxt = "<p>Already have Flash Player? <a href='?detectflash=false&"+ this.sq +"'>Click here if you have Flash Player "+ this.version +" installed</a>.</p>";
	this.params = new Object();
	this.variables = new Object();
	if (c) this.color = this.addParam('bgcolor', c);
	this.addParam('quality', 'high'); // default to high
	this.doDetect = getQueryParamValue('detectflash');
}

FlashObject.prototype.addParam = function(name, value) {
	this.params[name] = value;
}

FlashObject.prototype.getParams = function() {
    return this.params;
}

FlashObject.prototype.getParam = function(name) {
    return this.params[name];
}

FlashObject.prototype.addVariable = function(name, value) {
	this.variables[name] = value;
}

FlashObject.prototype.getVariable = function(name) {
    return this.variables[name];
}

FlashObject.prototype.getVariables = function() {
    return this.variables;
}

FlashObject.prototype.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

FlashObject.prototype.getHTML = function() {
    var flashHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
        flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '">';
        flashHTML += '<param name="movie" value="' + this.swf + '" />';
        if (this.getParamTags() != null) {
            flashHTML += this.getParamTags();
        }
        if (this.getVariablePairs() != null) {
            flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
        }
        flashHTML += '</object>';
    }
    else { // Everyone else
        flashHTML += '<embed type="application/x-shockwave-flash" src="' + this.swf + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" align="' + this.align + '"';
        for (var param in this.getParams()) {
            flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        if (this.getVariablePairs() != null) {
            flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
        }
        flashHTML += '></embed>';
    }
    return flashHTML;	
}


FlashObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

FlashObject.prototype.write = function(elementId) {

	if(detectFlash(this.version) || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

FlashObject.prototype.writeDOM = function(el) {

	if(detectFlash(this.version) || this.doDetect=='false') {
		el.innerHTML = this.getHTML();
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			el.innerHTML = this.altTxt +""+ this.bypassTxt;
		}
	}		
}

function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.plugins.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x){
			if (x.description) {
				var y = x.description;
	   			flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
	} else {
		result = false;
	    for(var i = 15; i >= 3 && result != true; i--){
   			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
   			flashversion = i;
   		}
	}
	return flashversion;
}

function detectFlash(ver) {	
	if (getFlashVersion() >= ver) {
		return true;
	} else {
		return false;
	}
}

// get value of querystring param
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return true;
	}
}

/* add Array.push if needed */
if(Array.prototype.push == null){
	Array.prototype.push = function(item){
		this[this.length] = item;
		return this.length;
	}
}

function selectConflict (series, start, end, me) {
var range=end-start; 
	for (var i=0;i<=range;i++) {
		myNum=start+i; 
		idToGet = series+myNum;
		thisEl = document.getElementById(idToGet);
		meEl = document.getElementById(me);
		if ((meEl.value == thisEl.value) && (meEl != thisEl)) {
			thisEl.value = '';
		}
	}
}
