// JavaScript Document

// Bits from the deconcept SWFObject scripts, just to detect if Flash is present:


if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof deconcept.util == "undefined") deconcept.util = new Object();
if(typeof deconcept.SWFObjectUtil == "undefined") deconcept.SWFObjectUtil = new Object();


deconcept.SWFObjectUtil.getPlayerVersion = function(){
	var PlayerVersion = new deconcept.PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			PlayerVersion = new deconcept.PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else{
		// do minor version lookup in IE, but avoid fp6 crashing issues
		// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}catch(e){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				PlayerVersion = new deconcept.PlayerVersion([6,0,21]);
				axo.AllowScriptAccess = "always"; // throws if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
			} catch(e) {
				if (PlayerVersion.major == 6) {
					return PlayerVersion;
				}
			}
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			} catch(e) {}
		}
		if (axo != null) {
			PlayerVersion = new deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
	return PlayerVersion;
}

deconcept.PlayerVersion = function(arrVersion){
	this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
	this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
	this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
}

// Functions using the above to detect and write Flash content or an image alternative

function detectFlash() {
	flashVersion = deconcept.SWFObjectUtil.getPlayerVersion();
}

function writeFlash(theMovie, theWidth, theHeight, theImage, altText, linkURL, linkTarget) {
	// This script inserts a Flash movie or an alternative image. The paths that are passed to this function should be relative to the folder level that's specified in the "countSubFolders" function below - the script then works out how many folders to go up first if necessary.
	var str='';
	countSubFolders();
	if (flashVersion.major >= 4) {
		str+='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=9,0,0,0" width="';
		str+=theWidth;
		str+='" height="';
		str+=theHeight;
		str+='">';
		str+='<param name="movie" value="';
		//str+=pathString;
		str+=theMovie;
		str+='">';
		str+='<param name="quality" value="high">';
		str+='<embed src="';
		//str+=pathString;
		str+=theMovie;
		str+='" quality="high" pluginspage="http:\/\/www.macromedia.com\/shockwave\/download\/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application\/x-shockwave-flash"';
		str+='width="';
		str+=theWidth;
		str+='" height="';
		str+=theHeight;
		str+='"><\/embed>';
		str+='<\/object>';
	} else {
		if(theImage) {
			if(linkURL) {
				str+='<a href="';
				str+=linkURL;
				str+='"';
				if(linkTarget) {
					str+=' target="';
					str+=linkTarget;
					str+='"';
				}
				str+='>';
			}
			str+='<img src="';
			//str+=pathString;
			str+=theImage;
			str+='" width="';
			str+=theWidth;
			str+='" height="';
			str+=theHeight;
			str+='" alt="';
			if(altText) {
				str+=altText;
			}
			str+='" title="">';
			if(linkURL) {
				str+='<\/a>';
			}
		}
	}
	document.write(str);
}

// Code for interactive mouse-overs, etd

function getStyleObject(objectId) {
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
	} else {
	return false;
	}
} // getStyleObject

function countSubFolders() {
	//This function counts the number of sub-folders that the page is nested within the 'trusty' subfolder, so that it can find its way back up to folders for CSS and images, etc.
	pathString = "";
	var thePath = window.location.pathname;
	var innerPath = thePath.substring(thePath.indexOf("trusty")+7, thePath.length);
	if (innerPath.indexOf("\/") != -1) {
		var theCounter = innerPath.split("/");
		theIter = 1
		while (theIter < theCounter.length) {
			pathString+="../"
			theIter++;
		}
	}
}

function writeStyle() {
	// This function writes a link to the interactive style sheet, which is assumed to be in a folder named 'css' which is in the same subfolder as that specified in the countSubFolders function above.
	turnOnInteraction = true;
	//This part writes in the links for the interactive style sheets.
	//countSubFolders();
	link1Str='<link rel="alternate stylesheet" type="text/css" href="';
	//link1Str+=pathString;
	link1Str+='/main/library/w-trusty-interactive_off.css" title="corvidaeAllText" media="screen">';
	document.write(link1Str);
	link2Str='<link rel="alternate stylesheet" type="text/css" href="';
	//link2Str+=pathString;
	link2Str+='/main/library/w-trusty-interactive_on_new.css" title="corvidaeIntText" media="screen">';
	document.write(link2Str);
	//This part checks whether there is currently a corvidaetext cookie. If not, it creates one and sets it to show interactive text.
	if(!readCookie('corvidaetext') || readCookie('corvidaetext') == "null") {
		createCookie("corvidaetext", "corvidaeIntText", 365);
	}
	if(readCookie('corvidaetext')) {
	  var cookie = readCookie("corvidaetext");
	  theTitle = cookie ? cookie : getPreferredStyleSheet();
	} else {
	  var theTitle = "corvidaeAllText";
	}
	setActiveStyleSheet(theTitle);
	window.currentlyVisiblePopup = "Panel0";

}


function writeAllLink() {
	if(turnOnInteraction) {
				var _link = document.location.href;
				if (_link.indexOf("#") == -1)
					_link += "#";
				document.write('<span class="interactivetext"><p class="smaller">Explore things by moving your mouse over links and pictures! Click to make things stay on screen. <a href="' + _link + '" onClick="swapStyle()";" accesskey="9">Use this link to see everything at once<\/a>.<\/p><\/span>');
				document.write('<span class="alltext"><p class="smaller">This setting lets you see everything at once. <a href="' + _link + '" onClick="swapStyle();" accesskey="9">Use this link to make things appear when you mouse-over and click on them<\/a>.<\/p><\/span>');
	}
}

function showIntro(theItem) {
//This checks whether the corvidaeText cookie is set to corvidaeIntText: if it is, it sets the display property of the layer specified by theItem to 'block', i.e. it makes it visible. It also sets a global variable saying that this is the currently visible item, so that it can be hidden when another one is shown.
	getStyleObject(theItem).display='block';
	window.currentlyVisiblePopup = theItem;
}

function showPanel(theItem, theAnchor) {
	if(getActiveStyleSheet() == "corvidaeIntText") {
		getStyleObject(window.currentlyVisiblePopup).display='none';
		getStyleObject(theItem).display='block';
		window.currentlyVisiblePopup = theItem;
	} else {
		window.location.href = theAnchor;
	}
}

function tempShowPanel(theItem) {
	if(getActiveStyleSheet() == "corvidaeIntText") {
		getStyleObject(window.currentlyVisiblePopup).display='none';
		getStyleObject(theItem).display='block';
	}
}

function tempHidePanel(theItem) {
	if(getActiveStyleSheet() == "corvidaeIntText") {
		getStyleObject(theItem).display='none';
		getStyleObject(window.currentlyVisiblePopup).display='block';
	}
}

function showPanelFocus(theItem) {
	if(getActiveStyleSheet() == "corvidaeIntText") {
		getStyleObject(window.currentlyVisiblePopup).display='none';
		getStyleObject(theItem).display='block';
		window.currentlyVisiblePopup = theItem;
	}
}


// Old code used to open pop-up windows

function RC_openBRWindow(theURL,winName,features) { //v2.0
	var theFuncString = "window.open('"+theURL+"','"+winName+"','"+features+"');"+winName+".focus();"
	try {eval(winName + " = " + theFuncString);} catch (e) {}
}

// ALA alternative style sheet code

function setActiveStyleSheet(theTitle) {
window.setStyle = theTitle;
var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("alternate") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == theTitle) a.disabled = false;
    }
  }
}

function showPanels() {
	var contRep = 1;
	panelNo = 0;
	while(contRep == 1) {
		thePanel = "Panel" + panelNo;
		if(getStyleObject(thePanel)) {
			getStyleObject(thePanel).display='block';
		}
		if(getStyleObject(thePanel) == false) {
			var contRep = 0;
		}
		panelNo++
	}
}

function hidePanels() {
	var contRep = 1;
	panelNo = 1;
	while(contRep == 1) {
		thePanel = "Panel" + panelNo;
		if(getStyleObject(thePanel)) {
			getStyleObject(thePanel).display='none';
		}
		if(getStyleObject(thePanel) == false) {
			var contRep = 0;
		}
		panelNo++
	}
}

function swapStyle() {
	if(getActiveStyleSheet() == "corvidaeIntText") {
		setActiveStyleSheet("corvidaeAllText")
		showPanels();
	} else {
		setActiveStyleSheet("corvidaeIntText")
		hidePanels();
		showIntro("Panel0");
	}
}

function alertStyle() {
	var currentStyle = getActiveStyleSheet(); 
	alert(currentStyle);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("alternate") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("alternate") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
if(readCookie('corvidaetext')) {
  var cookie = readCookie("corvidaetext");
  theTitle = cookie ? cookie : getPreferredStyleSheet();
} else {
  var theTitle = "corvidaeAllText";
}
setActiveStyleSheet(theTitle);
}

window.onunload = function(e) {
  var theTitle = getActiveStyleSheet();
  createCookie("corvidaetext", theTitle, 365);
}

