// JavaScript Document

function chooseStyle(newstyle) {
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + 1800000);
	setCookie("corvidaetext",newstyle,expdate,false,false,false)
	window.location.reload();
}

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;
	}
}

function sizeAlert() {
	var frameDoc = frames['qtscripter'].location.href.indexOf('show');
	if(frameDoc != -1) {
		var shownPanel = "Panel" + frames['qtscripter'].location.href.substring(frames['qtscripter'].location.href.indexOf('show') +4, frames['qtscripter'].location.href.indexOf('.htm'));
		getStyleObject(shownPanel).visibility='hidden';
	}
	frames['qtscripter'].location.href="scriptcaller.html";
	showIntro('Panel0');
	if((navigator.appName=="Opera") || ((navigator.appName=="Microsoft Internet Explorer") && (navigator.appVersion.indexOf("Mac") !=-1))) {
		suggestString = "";
		if(navigator.appVersion.indexOf("Mac") !=-1) {
			suggestString = " Please try using Safari or Firefox for the best experience.";
		}
		if(navigator.platform.indexOf("Win") !=-1) {
			suggestString = " For the best experience, please try using Internet Explorer 6 or later, or Firefox 2 or later.";
		}
		alert("Unfortunately, this browser doesn't let us send commands to the QuickTime plug-in, so some features such as the sound will not work properly." + suggestString);
	}
	qtVer = document.panorama.GetQuickTimeVersion();
	if (qtVer.indexOf(".") != -1) {
		intEnd = qtVer.indexOf(".");
		qtVerInt = parseInt(qtVer.substring(0,intEnd));
		qtVerDecs = qtVer.substring(intEnd + 1, qtVer.length);
		if (qtVerDecs.indexOf(".") != -1) {
			decLargeEnd = qtVerDecs.indexOf(".");
			qtVerDecLarge = parseInt(qtVerDecs.substring(0, decLargeEnd));
			qtVerDecSmall = parseInt(qtVerDecs.substring(decLargeEnd +1, qtVerDecs.length));
		} else {
			qtVerDecLarge = parseInt(qtVerDecs);
			qtVerDecSmall =  0;
		}
		if (qtVerInt >= 7 && ((qtVerDecLarge > 1) || (qtVerDecLarge == 1 && qtVerDecSmall >= 5))) {
		}
	}
}

function playSound() {
	theLayer = window.currentlyVisiblePopup;
	theIndex=theLayer.substr(5);
	theSound = theRoom + theIndex + ".mp3";
	document.QTsound.SetURL(theSound);
	document.QTsound.Play();
}

function writeStyle() {
	if(document.cookie.indexOf('corvidaeAllText') == -1)
		document.write('<link rel="stylesheet" type="text/css" href="file:///Macintosh%20HD/Users/richardcrowest/Sites/css/interactive_on.css" media="screen" \/>');
}

function writeAllLink() {
	if(!document.layers) {
		if(document.cookie.indexOf('corvidaeAllText') != -1) {
			document.write('<a href="javascript:chooseStyle(\'corvidaeIntText\');">Show interactive product details<\/a>');
		}
		else {
			document.write('<a href="javascript:chooseStyle(\'corvidaeAllText\');">Show all product details<\/a>');
		}
	}
}

function writeAnchor(anchorName) {
	if((document.layers) || (document.cookie.indexOf('corvidaeIntText') == -1)) {
		document.write('<a name=\"' + anchorName + '\" id=\"' + anchorName + '\"\><\/a>');
	}
}

function setCookie(name, value, expires, path, domain, secure) { 
	 var curCookie = name + "=" + escape(value) + 
			((expires) ? "; expires=" + expires.toGMTString() : "") + 
			((path) ? "; path=" + path : "") + 
			((domain) ? "; domain=" + domain : "") + 
			((secure) ? "; secure" : ""); 
	 document.cookie = curCookie; 
}

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	obj.visibility=v; }
}

function showIntro(theItem) {
//This makes the layer specified by theItem  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).visibility='visible';
	window.currentlyVisiblePopup = theItem;
}

function showPanel(theItem) {
	getStyleObject(window.currentlyVisiblePopup).visibility='hidden';
	getStyleObject(theItem).visibility='visible';
	window.currentlyVisiblePopup = theItem;
}

function tempShowPanel(theItem) {
	if(document.cookie.indexOf('corvidaeAllText') == -1) {
	getStyleObject(window.currentlyVisiblePopup).visibility='hidden';
	getStyleObject(theItem).visibility='visible';
	}
}

function tempHidePanel(theItem) {
	if(document.cookie.indexOf('corvidaeAllText') == -1) {
	getStyleObject(window.currentlyVisiblePopup).visibility='visible';
	getStyleObject(theItem).visibility='hidden';
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
	document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

/* Key press detector for new keyboard-operated sound player and show room introduction */
var mykey;

if (window.Event){
	document.captureEvents(Event.KEYDOWN);
}

document.onkeydown = myKeyDown;

function myKeyDown(e){
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	mykey = String.fromCharCode(code);
	if ((mykey == "a") || (mykey == "A")){
		playSound();
	return false;
	}
	if ((mykey == "s") || (mykey == "S")){
		showPanel('Panel0');
	return false;
	}
}

function panorama_DoFSCommand(command, arg1) {
	if (command == "showPanel") {
		showPanel(arg1);
	}
}

function spincontrolled_DoFSCommand(command, arg1) {
	if (command == "showPanel") {
		showPanel(arg1);
	}
}


function checkKeyForClick(evt, func, param1, param2) {
	funcString = func.toString();
	toDo = funcString.substring(funcString.indexOf("{") + 2, funcString.indexOf(";") + 1);
	evt = (evt) ? evt : (window.event) ? event : null;
	if (!evt) {var evt = window.event;}
	if (evt.keyCode) {
		code = evt.keyCode;
	}else {
		if (evt.which) { code = evt.which;}
	}
	if (code == 13) {
		if(!(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Mac") != -1)) {
			return false
		}
		eval(toDo); return false		
	}
}

function checkKeyForClick(evt, func, param1, param2) {
	funcString = func.toString();
	toDo = funcString.substring(funcString.indexOf("{") + 2, funcString.indexOf(";") + 1);
	evt = (evt) ? evt : (window.event) ? event : null;
	if (!evt) {var evt = window.event;}
	if (evt.keyCode) {
		code = evt.keyCode;
	}else {
		if (evt.which) { code = evt.which;}
	}
	if (code == 13) {
		if(!(navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Mac") != -1)) {
			return false
		}
		eval(toDo);
		return false		
	}
}

function nextPanel(panAngle, tiltAngle, zoomAngle) {
	var panelNo = window.currentlyVisiblePopup.substring(5, window.currentlyVisiblePopup.length);
	var nextPanelNo = parseInt(panelNo) + 1;
	var nextPanel = "Panel" + nextPanelNo;
	var nextAnchor = "#" + nextPanel
	showPanel(nextPanel);
	var usrAgt = navigator.userAgent.toLowerCase();
		location.hash = nextPanel;
	if(usrAgt.indexOf("applewebkit") == -1) {
	}
	if (typeof(panAngle) != "undefined") {
		panTo(panAngle, tiltAngle, zoomAngle);
	}
}

function firstPanel() {
	var panelNo = window.currentlyVisiblePopup.substring(5, window.currentlyVisiblePopup.length);
	showPanel ("Panel0");
	var usrAgt = navigator.userAgent.toLowerCase();
		location.hash = "Panel0";
	if(usrAgt.indexOf("applewebkit") == -1) {
	}
}

function panTo(panAngle, tiltAngle, zoomAngle) {
	if(typeof(window.panInterval) != "undefined") {
		window.clearInterval(panInterval);
	}
	// Uncomment the following line if a sprite-track based auto rotate is added to the main panoramas:
	//	document.panorama.SetSpriteTrackVariable(5,353,1);

	// Get the start and end points for pan, tilt and zoom
	thePan = document.panorama.GetPanAngle();
	theTilt = document.panorama.GetTiltAngle();
	theZoom = document.panorama.GetFieldOfView();
	targetPan = panAngle;
	targetTilt = tiltAngle;
	targetZoom = zoomAngle;
	// Calculate how many degrees to pan from where we are to where we're going, taking into account the wrap-around
	if (Math.abs(targetPan-thePan) >180) panDistance = 360 - Math.abs(targetPan-thePan); else panDistance = Math.abs(targetPan-thePan);
	tiltDistance = Math.abs(targetTilt-theTilt);
	zoomDistance = Math.abs(targetZoom-theZoom);
	// Set the number of steps for the animation based on the largest change out of pan, tilt or zoom
	if (panDistance > tiltDistance) stepCount = panDistance/2; else stepCount = tiltDistance/2;
	if (zoomDistance > stepCount*2) stepCount = zoomDistance/2;
	// Choose whether to increase or decrease the pan angle, taking into account the wrap-around
	if (targetPan > thePan && (Math.abs(targetPan-thePan) <180) || targetPan < thePan && (Math.abs(targetPan-thePan) >180)) {
		panDelta = panDistance/stepCount;
	} else {
		panDelta = -1 * panDistance/stepCount;
	}
	// Choose whether to increase or decrease the tilt and FOV angles
	if (targetTilt > theTilt) tiltDelta = tiltDistance/stepCount; else tiltDelta = -1 * tiltDistance/stepCount;
	if (targetZoom > theZoom) zoomDelta = zoomDistance/stepCount; else zoomDelta = -1 * zoomDistance/stepCount;
	// Set the iteration count to 0 and call panIncrement periodically to move the pano
	stepIter = 0;
	panInterval = window.setInterval("panIncrement()",40);
}

function panIncrement() {
	// Stop if we've reached the calculated step count
	if (stepIter+1 >= stepCount) {
		window.clearInterval(panInterval);
		document.panorama.SetPanAngle(targetPan);
		document.panorama.SetTiltAngle(targetTilt);
		document.panorama.SetFieldOfView(targetZoom);
	} else {
		// Set the new pan value
		var newPan = thePan + panDelta;
		document.panorama.SetPanAngle(newPan);
		thePan = newPan;
		// Set the new tilt value
		var newTilt = theTilt + tiltDelta;
		document.panorama.SetTiltAngle(newTilt);
		theTilt = newTilt;
		// Only change the zoom if the FOV is more than 20 and we're zooming OUT, otherwise errors can result
		if(theZoom > 20 || zoomDelta > 0.01) {
			var newZoom = theZoom + zoomDelta;
			document.panorama.SetFieldOfView(newZoom);
			theZoom = newZoom;
		}
	}
	// Stop the controller from moving the pano
	stepIter ++;
}

