// JavaScript Document © 2003-2008 Corvidae Ltd. All rights reserved.

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() {
	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 Flash plug-in, so some features such as the sound will not work properly." + suggestString);
	}
}

function playSound() {
	if (typeof(document.soundController) != "undefined") {
		loadSound();
		document.soundController.playIt();
	} else {
		alert("Please install or update Flash Player.");
	}
}

function loadSound() {
	theLayer = window.currentlyVisiblePopup;
	theIndex=theLayer.substr(5);
	theSound = "sounds\/" + theRoom + theIndex + ".mp3";
	if (typeof(document.soundController) != "undefined") {
		if (typeof(document.soundController.getSoundUrl()) != "undefined") {
			var theSoundUrl = document.soundController.getSoundUrl();
			if(theSoundUrl.indexOf(theSound) == -1) {
				document.soundController.loadSound(theSound);
			} else {
				document.soundController.rewindSound();
			}
		} else {
			document.soundController.loadSound(theSound);
		}
	} else {
		alert("Please install or update Flash Player.");
	}
}

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 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 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;
	loadTimeout = setTimeout("loadSound();", 1000);
}

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) {
	// Stop the controller from moving the pano
	window.document.pano.myidPanoExecute('pano.panKey=0;pano.tiltKey=0;pano.zoomKey=0');
	// Convert from QTVR angles to flashpanoramas angles
	if (panAngle > 180) {
			var fpPan = 360 - panAngle;
	} else {
			var fpPan = 0 - panAngle;
	}
	var thePan = window.document.pano.myidPanoGet('pano.pan');
	// Prevent sweeps > 180 degrees - first get absolute value of the current pan angle, and the remainders when divided by 360 and 180
	var interPan = Math.abs(thePan);
	var panModA = (thePan % 360);
	var panModB = (thePan % 180);
	// Get the absolute value of remainder by 180 so we can do all the maths on positive figures then convert to negative if needed.
	var absBit = Math.abs(panModB);
	// If we're past 180 degrees, change all values to 180-the angle, as flashpanoramas works from 0 to +- 180
	if (Math.abs(panModA) > 180) var interPan = (180 - absBit) * -1;
	// If we're not at 0 or 180, make the sign of the new angle negative if it was negative before
	if (absBit != 0) {
		var interPan = interPan * panModB/absBit;
	} else {
		// If we are at 0 or 180, set tha angle accordingly
		var interPan = 0;
		if (Math.abs(thePan) == 180) interPan = 180;
	}
	// If the calculated angle is different from the current angle, reset the angle to the calculated figure (which is the same, but less than  +- 180)
	if (interPan != thePan) {
		var interPanString="pano.pan=" + interPan + ";"
		window.document.pano.myidPanoExecute(interPanString);
		var thePan = interPan;
	}
	// Now, if the angle we're going to is more than 180 degrees away, prevent a long journey by going the other way, which may mean a journey over the 180 degree mark, which is why we had to do all the checking and resetting above.
	if (Math.abs(thePan - fpPan) > 180) {
		if (fpPan > 0) {
			var fpPan = 0 - panAngle;
		} else {
			var fpPan = 360 + fpPan;
		}
	}
	var fpPan = Math.round(fpPan * 100)/100;
	var panToString="pano.qualityStatic=low;pano.pan=" + fpPan + ",1500,smooth;pano.tilt=" + tiltAngle + ",1500;pano.vfov=" + zoomAngle + ",2000,smooth;"
	window.document.pano.myidPanoExecute(panToString);
	qualTimeout = setTimeout("restoreQual(" + fpPan + "," + tiltAngle + "," + zoomAngle+ ");", 1000);
}

function restoreQual(fpPan, tiltAngle, zoomAngle) {
	var thePan = window.document.pano.myidPanoGet('pano.pan');
	var theTilt = window.document.pano.myidPanoGet('pano.tilt');
	var theFov = window.document.pano.myidPanoGet('pano.vfov');
	if(thePan == fpPan && theTilt == tiltAngle && theFov == zoomAngle) {
		window.document.pano.myidPanoExecute("pano.qualityStatic = high;");
	} else {
		qualTimeout = setTimeout("restoreQual(" + thePan + "," + theTilt + "," + theFov+ ");", 1000);
	}
}