// ©2003 ChannelVR. All rights reserved

var reloadContent=true;

// Global Variables
var xmlStr=""; // XML string to be loaded into Flash Player
var gStr=""; // Global string to render text to
var gActionsStr=""; // Global string to render actions text to

// which room are we in?
var currentRoom=0;
var currentStill=0;
var currentVideo=0;
var currentObject3D=0;

var startingObjectIndex = 0;
var objectType="";
var objectDesc="0";



// is this the first time this page is displayed
var firstTime=true;

// wait for viewer to fully load before allowing events
var appletLoaded = false;

// some useful paths
var rootPagePath = activePagePath.substring(activePagePath.lastIndexOf('jobs/',activePagePath.length));
var rootPortalPath = activePortal.substring(activePortal.lastIndexOf('portals/',activePortal.length));
var rootSharedPath = commonResourcesURL.substring(commonResourcesURL.lastIndexOf('shared-files/',commonResourcesURL.length));


// What browser are we viewing in?
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;


// Handle all the the FSCommand messages in a Flash movie
function panoviewerflash_DoFSCommand(command, args) {
  var panoviewerflashObj = InternetExplorer ? panoviewerflash : document.panoviewerflash;

  switch (command) {
	  case "onYawChange":
		document.all["yaw"].value=args
		break;
	  case "onPitchChange":
		document.all["pitch"].value=args
		break;
	  case "onZoomChange":
		document.all["zoom"].value=args
		break;
	  case "onTourLoaded":
		appletLoaded = true;

		break;
	  case "onRoomLoaded":

		var layerCount=args;
		if (layerCount <= rooms.length) {
			if (firstTime) {
				document.panoviewerflash.SetVariable('varaction','_vr_left');
				firstTime=false;
				//alert("onTourLoaded");
				testID = setTimeout("startWithObject()", 100);
			}
			p.setSecondaryDescription(rooms[layerCount].panoDescription);
			currentRoom = layerCount;
		}
		else if (layerCount > rooms.length && layerCount <= stills.length) {
			p.setSecondaryDescription(stills[layerCount-rooms.length].stillDescription);
			currentStill = layerCount;
		}
		else if (layerCount > rooms.length+stills.length && layerCount <= stills.length+videos.length) {
			p.setSecondaryDescription(videos[layerCount-rooms.length-stills.length].videoDescription);
			currentVideo = layerCount;
		}
		else if (layerCount > rooms.length+stills.length+videos.length && layerCount <= stills.length+videos.length+Objects3D.length) {
			p.setSecondaryDescription(stills[layerCount-rooms.length-stills.length-videos.length].object3DDescription);
			currentObject3D = layerCount;
		}
		break;
	  case "onViewerLoaded":
		document.panoviewerflash.SetVariable('xmlscript',xmlStr);
		break;
	}
}

// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
	  navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub panoviewerflash_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call panoviewerflash_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}

//
// ----------------------------------------------
// APPLET ACTIONS
// ----------------------------------------------


function startWithObject()
{

    // showing any object at startup instead of just panorama
    if (objectType != "")
    {
	if (objectType == "Panorama")
	{
	    if (startingObjectIndex == 0) return;
	    jsLoadRoom(startingObjectIndex);
	}

	else if (objectType == "Still")
	{
	    chImage(startingObjectIndex);
	}
	else if (objectType == "Video")
	{
	    chVideo(startingObjectIndex);
	}
	else if (objectType == "3DObject")
	{
	    chObject3D(startingObjectIndex);
	}
	else if (objectType == "Plan")
	{
	    if(fullscreen)
		{ p=window.opener; }
	    else { p=parent;}

	    p.chPlan(startingObjectIndex);
	}
    }
}



function jsGetActiveRoomID()
{
	return (currentRoom);
}

// display a room
function jsLoadRoom(iRoom)
{
	if (rooms.length&&appletLoaded) {
		document.panoviewerflash.SetVariable('varaction',iRoom+7);
		document.panoviewerflash.SetVariable('varaction2','_vr_left');
	}
}

// display a still image
function chImage(nImage)
{
	if (stills.length&&appletLoaded) {
		document.panoviewerflash.SetVariable('varaction',nImage+7+rooms.length);
	}
}

// display a video
function chVideo(nVideo)
{
	if (videos.length&&appletLoaded) {
		document.panoviewerflash.SetVariable('varaction',nVideo+7+rooms.length+stills.length);
	}
}

// display a 3D Object
function chObject3D(nObject3D)
{
	if (Objects3D.length&&appletLoaded) {
		document.panoviewerflash.SetVariable('varaction',nObject3D+7+rooms.length+stills.length+videos.length);
	}
}

// start an action
function jsSetAction(i)
{
	if (rooms.length&&appletLoaded) {
		if(i=="pause") i = '_vr_stop';
//		if(i=="zoomin") i = '_vr_zoomin';
//		if(i=="zoomout") i = '_vr_zoomout';
		if(i=="zoomin") i = '3';
		if(i=="zoomout") i = '4';
		if(i=="left") i = '_vr_left';
		if(i=="right") i = '_vr_right';
//		if(i=="left") i = 'left';
//		if(i=="right") i = 'right';
		if(i=="guidedtour") i = '6';
		if(i=="interrupt") {
			//if(fullscreen==1) {
			//	document.panoviewerflashFS.SetVariable('varaction','_vr_stop');
			//}
			//else {
				document.panoviewerflash.SetVariable('varaction','_vr_stop');
			//}
		}
		else {
			//if(fullscreen==1) {
			//	document.panoviewerflashFS.SetVariable('varaction',i);
			//}
			//else {
				document.panoviewerflash.SetVariable('varaction',i);
			//}
		}
	}
}

function pageChanged() {
	this.location.reload();
}


/***********LOGGING*/
var logging=false;
if (mode=="expert") logging=true;
/*******************/

if( logging )
{
	var win = window.open();
	var doc = win.document;
	doc.writeln("<HTML><HEAD><TITLE>Log</TITLE></HEAD><BODY><TEXTAREA name=\"log\" rows=40 cols=80></TEXTAREA></BODY></HTML>");
	doc.close();
	var log=doc.all.log;
}

// append a string to use later (and write out to log)
function writer (str) {
	if( logging )
	{
		log.value+=str+"\n";
	}
	gStr+=str;
}

// add Actions to a big string to add to xml later
function actions (str) {
	gActionsStr+=str+"\n";
}

/////////////////////////
// Preload and Swap Images
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_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_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&p.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 init() {
}

////////////////////////////////////////
// Get an index into the XML ids from the Objects number
function actionIDfromImageID (iID) {
	return ((iID)+(rooms.length)+7);
}

function actionIDfromVideoID (vID) {
	var v=7+(rooms.length)+(stills.length)+(vID);
	return v;
}

function actionIDfromObject3DID (oID) {
	var o=7+(rooms.length)+(stills.length)+(videos.length)+oID;
	return o;
}

function layerIDfromObject3DID (oID) {
	var o=stills.length+videos.length+oID;
	return o;
}

function actionIDfromPanoramaID (pID) {
	return ((pID)+ 7);
}

function hotspotActionIDfromPanoramaID (pID, hotspotID) {
	// for this room and all previous add up the number of hotspot actions so we can start at the current rooms hotspot actions
	k = 0;
	for(x=0;x<pID;x++) {
		for(j=0;j<rooms[x].hotspots.length;j++) {
			if (rooms[x].hotspots[j].type == "pano") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[x].hotspots[j].type == "video") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[x].hotspots[j].type == "Object3D") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[x].hotspots[j].type == "still") {
				k=k+2;
			}
		}
	}
	// optionally return the action ID of a specific hotspot in that room
	if(hotspotID!=null) {
		j=0;
		while(j<hotspotID) {
			if (rooms[pID].hotspots[j].type == "pano") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[pID].hotspots[j].type == "video") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[pID].hotspots[j].type == "Object3D") { // hotspot links to another room
				k=k+2;
			}
			if (rooms[pID].hotspots[j].type == "still") {
				k=k+2;
			}
			j++;
		}
	}
	return (k);
}
////////////////////////////////////////

////////////////////////////////////////
// Write out the Flash player Object into the webpage
////////////////////////////////////////
function writeViewer() {

	if (p.paramArray["objectType"])
	{
	    objectType = p.paramArray["objectType"];
	    if (p.paramArray["objectDesc"])
	    {
	        objectDesc = p.paramArray["objectDesc"];
		objectDesc = objectDesc.replace("%20", " ");
	    }
	}


	var activeItem;

	xmlStr = writeTourXML();
	//alert(xmlStr);
	writer('  </table>');
	gStr="";

	if(fullscreen==1) {
		writer('<table class="viewer_applet_fullscreen_table" cellpadding="0" cellspacing="0">');
		writer('  <tr>');
		writer('    <td class="viewer_applet_fullscreen_table_td">');
//		writer('<object valign="middle" align="center" MAYSCRIPT name="panoviewerflashFS" id="panoviewerflashFS" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="700" height="400">');
//		writer('<embed valign="middle" align="center" MAYSCRIPT name="panoviewerflashFS" id="panoviewerflashFS" src="vr.swf" quality=high bgcolor=#FFFFFF  width="700" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
		writer('<object valign="middle" align="center" MAYSCRIPT name="panoviewerflash" id="panoviewerflash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="700" height="400">');
		writer('<embed valign="middle" align="center" MAYSCRIPT name="panoviewerflash" id="panoviewerflash" src="vr.swf" quality=high bgcolor=#FFFFFF  width="700" height="400" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
	}
	else {
		writer('<table class="viewer_applet_table" cellpadding="0" cellspacing="0">');
		writer('  <tr>');
		writer('    <td class="viewer_applet_table_td">');
		writer('<object valign="middle" align="center" MAYSCRIPT name="panoviewerflash" id="panoviewerflash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="400" height="209">');
		writer('<embed valign="middle" align="center" MAYSCRIPT name="panoviewerflash" id="panoviewerflash" src="vr.swf" quality=high bgcolor=#FFFFFF  width="400" height="209" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
	}

//	writer('  <param name=base value="'+activePagePath+'../">');
//	writer('  <param name=movie value="'+activePagePath+'../vr.swf?tourname='+p.pageURL[p.activePage]+'tour.xml">');
	// root at page level
	//writer('  <param name=base value="'+activePagePath+'">');
	//writer('  <param name=movie value="'+activePagePath+'../vr.swf">');
	// root at root level

	writer('  <param name=base value="'+activePagePath+'../../../">');
	writer('  <param name=movie value="'+activePagePath+'../../../vr.swf">');
	writer('  <param name=menu value=false>');
	writer('  <param name=quality value=high>');
	writer('  <param name=bgcolor value=#FFFFFF>');
	//writer('  <a href="http://www.vrinspection.com/get_java.htm" target="download_plugin_window">');
	//writer('  <img src="'+commonResourcesURL+'images/getjava.gif" border="0" alt="Flash is Not Installed: Click to Install"></a>');
	writer('  </object>');
	writer('  </td>');
	writer('  </tr>');
	writer('  </table>');
	document.write(gStr);
}

////////////////////////////////////////
// anything that ends in .gif replace with .swf instead (flash dont read gifs)
////////////////////////////////////////
function swf(filename) {
	var swffilename=filename;
	//if (swffilename.substring(swffilename.lastIndexOf('.')+1,swffilename.length) == 'gif') {
		swffilename = filename.substring(0,filename.lastIndexOf('.')+1)+'swf';
	//}

	return swffilename;
}

////////////////////////////////////////
// generate XML file from page.js data
////////////////////////////////////////
function writeTourXML() {

	gStr="";
	gActionsStr="";
	// Portal Logo
	writer('<?xml version="1.0" encoding="iso-8859-1"?>');
	writer('<tour>');
	writer('	<logo>');
	writer('		<image>'+rootPortalPath+(p.portalViewerLogoURL)+'</image>');
	writer('		<bgcolor>#CCCCCC</bgcolor>');
	writer('	</logo>');

	// Which room to display first
	if (p.currentRoom!=null) writer('    <startroom>'+p.currentRoom+'</startroom>');

	// How to display Hotspots
	writer('	<reveal_hotspots>true</reveal_hotspots>');

	var numActions=-1;
	var numLayers=-1;
	// NAVIGATION ACTIONS

	// reset zoom
	actions('	<action type="position" id="reset_zoom">');
	actions('		<time>2000</time>');
	actions('		<relative>false</relative>');
	actions('		<zoom>1</zoom>');
	actions('	</action>');

	numActions++;
	// rotate 360 right ACTION 0
	actions('	<action type="position" id="0">');
	actions('		<time>3000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom >0</zoom>');
	actions('		<yaw >360</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	numActions++;
	// rotate 360 left ACTION 1
	actions('	<action type="position" id="1">');
	actions('		<time>3000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom >0</zoom>');
	actions('		<yaw >-360</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	numActions++;
	// stop for 3 seconds ACTION 2
	actions('	<action type="position" id="2">');
	actions('		<time>3000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom >0</zoom>');
	actions('		<yaw >0</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	numActions++;
	// zoom in ACTION 3
	actions('	<action type="position" id="3">');
	actions('		<time>2000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom >0.3</zoom>');
	actions('		<yaw >0</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	numActions++;
	// zoom out ACTION 4
	actions('	<action type="position" id="4">');
	actions('		<time>2000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom>-0.3</zoom>');
	actions('		<yaw >0</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	numActions++;
	// rotate 360 right twice as fast for guided tour ACTION 5
	actions('	<action type="position" id="5">');
	// the guided tour panos should spin for as long as it takes for the image to download
	actions('		<time>7000</time>');
	actions('		<relative>true</relative>');
	actions('		<zoom >0</zoom>');
	actions('		<yaw >180</yaw>');
	actions('		<pitch default="0">0</pitch>');
	actions('	</action>');

	//skip ACTION 6 - defined below for guided tour, it can only be determined after all actions are defined then we know what action numbers to use.
	numActions++;

	// add loadroom actions for each room
	for(i=0;i<rooms.length;i++) {
		numActions++;
		// Load Room Action to be called by thumbnails etc
		actions('	<action type="load_room" id="'+numActions+'">');
		actions('		<room>'+i+'</room>');
		actions('	</action>');

		if (objectType=="Panorama" && objectDesc==rooms[i].panoTitle)
		    startingObjectIndex = i;
	}

	// add loadstill actions for each still
	for(i=0;i<stills.length;i++) {
		numActions++;
		// Load Room Action to be called by thumbnails etc
		actions('	<action type="load_room" id="'+numActions+'">');
		actions('		<room>'+(i+rooms.length)+'</room>');
		actions('	</action>');

		if (objectType=="Still" && objectDesc==stills[i].stillTitle)
			startingObjectIndex = i;
	}

	// add loadvideo actions for each video
	for(i=0;i<videos.length;i++) {
		numActions++;
		// Load Room Action to be called by thumbnails etc
		actions('	<action type="load_room" id="'+numActions+'">');
		actions('		<room>'+(i+rooms.length+stills.length)+'</room>');
		actions('	</action>');

		if (objectType=="Video" && objectDesc==videos[i].videoTitle)
			startingObjectIndex = i;
	}

	// add loadstill actions for each still
	for(i=0;i<Objects3D.length;i++) {
		numActions++;
		// Load Room Action to be called by thumbnails etc
		actions('	<action type="load_room" id="'+numActions+'">');
		actions('		<room>'+(i+rooms.length+stills.length+videos.length)+'</room>');
		actions('	</action>');

		if (objectType=="3DObject" && objectDesc==Objects3D[i].object3DTitle)
			startingObjectIndex = i;
	}

	if (objectType && !isNaN(objectDesc))
	{
	    startingObjectIndex = parseInt(objectDesc);
	    if (objectType=="Panorama" && startingObjectIndex >= rooms.length)
		startingObjectIndex = 0;
	    if (objectType=="Still" && startingObjectIndex >= stills.length)
		startingObjectIndex = 0;
	    if (objectType=="Video" && startingObjectIndex >= videos.length)
		startingObjectIndex = 0;
	    if (objectType=="3DObject" && startingObjectIndex >= Objects3D.length)
		startingObjectIndex = 0;
	}

	// add all rooms
	for(i=0;i<rooms.length;i++) {

		// Type of room: Cylinder, Cube, Sphere etc
		writer('	<room id="'+i+'">');
		writer('		<type>pano</type>');
		writer('		<mesh style="flat"></mesh>');

		// Room Panorama Image
		writer('		<image>'+parent.escape(rootPagePath)+(rooms[i].panoImageURL)+'</image>');

		//  3D Hotspots within each room
		var k=0; // count of image layers

		// hotspots for jumping from pano to pano
		for(j=0;j<rooms[i].hotspots.length;j++) {
			writer('		<hotspot instances="true">');
			writer('			<pitch default="0">'+ rooms[i].hotspots[j].pitch +'</pitch>');
			writer('			<yaw default="0">'+ rooms[i].hotspots[j].yaw +'</yaw>');
			writer('			<zoom default="1">'+ rooms[i].hotspots[j].zoom +'</zoom>');
			writer('			<points>'+ rooms[i].hotspots[j].points +'</points>');

			if (rooms[i].hotspots[j].type == "pano") { // hotspot links to another room
				//if(p.portalPanoOverIcon) {
				//	writer('			<image>'+rootPortalPath+swf(p.portalPanoOverIcon)+'</image>');
				//}
				//else {
					//writer('			<image>'+rootPortalPath+swf(p.hotSpotLogoURL)+'</image>');
					writer('			<image>'+rootSharedPath+swf(panoImage)+'</image>');
				//}

				writer('			<action>'+(numActions+2)+'</action>');
				var nRoom = rooms[i].hotspots[j].action;
				var roomID = actionIDfromPanoramaID(nRoom);

				numActions++;
				actions('	<action type="position" id="'+numActions+'">');
				actions('		<relative>false</relative>');
				actions('		<time>3000</time>');
				actions('		<pitch default="0">'+ rooms[i].hotspots[j].pitch +'</pitch>');
				actions('		<yaw default="0">'+ rooms[i].hotspots[j].yaw +'</yaw>');
				actions('		<zoom default="1">'+ rooms[i].hotspots[j].zoom +'</zoom>');
				actions('	</action>');

				numActions++;
				actions('	<action type="script" id="'+numActions+'">');
				actions('		<actions>'+(numActions-1)+',3,'+(rooms[i].hotspots[j].action+7)+',reset_zoom,5</actions>');
				actions('	</action>');

				// add a label to the hotspot, by default we use the room title, but it can be overridden by the hotspot
				var lStr=('			<text>');
				if ((rooms[i].hotspots[j].desc == "") || (rooms[i].hotspots[j].desc == null)) {
					if(rooms[rooms[i].hotspots[j].action].panoTitle == "")
						lStr+=(rooms[rooms[i].hotspots[j].action].name);
					else
						lStr+=(rooms[rooms[i].hotspots[j].action].panoTitle);
				}
				else {
					lStr+=(rooms[i].hotspots[j].desc);
				}
				lStr+=('</text>');
				writer(lStr);
				k++;
			}
			else if (rooms[i].hotspots[j].type == "still") { // hotspot links to a still image
				//if(p.portalStillOverIcon) {
				//	writer('			<image>'+rootPortalPath+swf(p.portalStillOverIcon)+'</image>');
				//}
				//else {
					writer('			<image>'+rootSharedPath+swf(iconImage)+'</image>');
				//}

				writer('			<action>'+(numActions+2)+'</action>');
				var nImage = rooms[i].hotspots[j].action;
				var imageID = actionIDfromImageID(nImage);

				numActions++;
				actions('	<action type="position" id="'+numActions+'">');
				actions('		<relative>false</relative>');
				actions('		<time>3000</time>');
				actions('		<pitch default="0">'+ rooms[i].hotspots[j].pitch +'</pitch>');
				actions('		<yaw default="0">'+ rooms[i].hotspots[j].yaw +'</yaw>');
				actions('		<zoom default="1">'+ rooms[i].hotspots[j].zoom +'</zoom>');
				actions('	</action>');

				numActions++;
				actions('	<action type="script" id="'+numActions+'">');
				actions('		<actions>'+(numActions-1)+',3,'+(rooms[i].hotspots[j].action+7+rooms.length)+',2,'+(i+7)+',4</actions>');
				actions('	</action>');

				// add a label to the hotspot, by default we use the room title, but it can be overridden by the hotspot
				var lStr=('			<text>');
				if ((rooms[i].hotspots[j].desc == "") || (rooms[i].hotspots[j].desc == null)) {
					if(stills[rooms[i].hotspots[j].action].stillTitle == "")
						lStr+=(stills[rooms[i].hotspots[j].action].name);
					else
						lStr+=(stills[rooms[i].hotspots[j].action].stillTitle);
				}
				else {
					lStr+=(rooms[i].hotspots[j].desc);
				}
				lStr+=('</text>');
				writer(lStr);
				k++;
			}
			else if (rooms[i].hotspots[j].type == "video") { // hotspot links to a video
				//if(p.portalVideoOverIcon) {
				//	writer('			<image>'+rootPortalPath+swf(p.portalVideoOverIcon)+'</image>');
				//}
				//else {
					writer('			<image>'+rootSharedPath+swf(videoImage)+'</image>');
				//}

				writer('			<action>'+(numActions+2)+'</action>');
				var nImage = rooms[i].hotspots[j].action;
				var videoID = actionIDfromVideoID(nImage);

				numActions++;
				actions('	<action type="position" id="'+numActions+'">');
				actions('		<relative>false</relative>');
				actions('		<time>3000</time>');
				actions('		<pitch default="0">'+ rooms[i].hotspots[j].pitch +'</pitch>');
				actions('		<yaw default="0">'+ rooms[i].hotspots[j].yaw +'</yaw>');
				actions('		<zoom default="1">'+ rooms[i].hotspots[j].zoom +'</zoom>');
				actions('	</action>');

				numActions++;
				actions('	<action type="script" id="'+numActions+'">');
				actions('		<actions>'+(numActions-1)+',3,'+(rooms[i].hotspots[j].action+7+rooms.length+stills.length)+',2,'+(i+7)+',4</actions>');
				actions('	</action>');

				// add a label to the hotspot, by default we use the room title, but it can be overridden by the hotspot
				var lStr=('			<text>');
				if ((rooms[i].hotspots[j].desc == "") || (rooms[i].hotspots[j].desc == null)) {
					if(videos[rooms[i].hotspots[j].action].videoTitle == "")
						lStr+=(videos[rooms[i].hotspots[j].action].name);
					else
						lStr+=(videos[rooms[i].hotspots[j].action].videoTitle);
				}
				else {
					lStr+=(videos[i].hotspots[j].desc);
				}
				lStr+=('</text>');
				writer(lStr);
				k++;
			}
			else if (rooms[i].hotspots[j].type == "Object3D") { // hotspot links to a 3D Object
				//if(p.portalObject3DOverIcon) {
				//	writer('			<image>'+rootPortalPath+swf(p.portalObject3DOverIcon)+'</image>');
				//}
				//else {
					writer('			<image>'+rootSharedPath+swf(Object3DImage)+'</image>');
				//}

				writer('			<action>'+(numActions+2)+'</action>');
				var nImage = rooms[i].hotspots[j].action;
				var object3DID = actionIDfromObject3DID(nImage);

				numActions++;
				actions('	<action type="position" id="'+numActions+'">');
				actions('		<relative>false</relative>');
				actions('		<time>3000</time>');
				actions('		<pitch default="0">'+ rooms[i].hotspots[j].pitch +'</pitch>');
				actions('		<yaw default="0">'+ rooms[i].hotspots[j].yaw +'</yaw>');
				actions('		<zoom default="1">'+ rooms[i].hotspots[j].zoom +'</zoom>');
				actions('	</action>');

				numActions++;
				actions('	<action type="script" id="'+numActions+'">');
				actions('		<actions>'+(numActions-1)+',3,'+(rooms[i].hotspots[j].action+7+rooms.length+stills.length+videos.length)+'</actions>');
				actions('	</action>');

				// add a label to the hotspot, by default we use the room title, but it can be overridden by the hotspot
				var lStr=('			<text>');
				if ((rooms[i].hotspots[j].desc == "") || (rooms[i].hotspots[j].desc == null)) {
					if(Objects3D[rooms[i].hotspots[j].action].object3DTitle == "")
						lStr+=(Objects3D[rooms[i].hotspots[j].action].name);
					else
						lStr+=(Objects3D[rooms[i].hotspots[j].action].object3DTitle);
				}
				else {
					lStr+=(Objects3D[i].hotspots[j].desc);
				}
				lStr+=('</text>');
				writer(lStr);
				k++;
			}
			else {// hotspot links to nothig, just a label
				//if(p.portalInfoOverIcon) {
				//	writer('			<image>'+rootPortalPath+swf(p.portalInfoOverIcon)+'</image>');
				//}
				//else {
					writer('			<image>'+rootSharedPath+swf(infoImageAni)+'</image>');
				//}

				// add a label to the hotspot, by default we use the room title, but it can be overridden by the hotspot
				var lStr=('			<text>');
				if ((rooms[i].hotspots[j].desc == "") || (rooms[i].hotspots[j].desc == null)) {
					lStr+=("no link");
				}
				else {
					lStr+=(rooms[i].hotspots[j].desc);
				}
				lStr+=('</text>');
				writer(lStr);
				k++;
			}
			writer('		</hotspot>');
		}
		writer('	</room>');
	}

	// add all stills
	for(i=rooms.length;i<(stills.length+rooms.length);i++) {
		writer('	<room id="'+i+'">');
		writer('		<type>still</type>');
		writer('		<mesh style="flat"></mesh>');
		writer('		<image>'+parent.escape(rootPagePath)+(stills[i-rooms.length].stillImageURL)+'</image>');
		writer('	</room>');
	}


	// add all videos
	for(i=stills.length+rooms.length;i<videos.length+stills.length+rooms.length;i++) {

		// Type of room: Cylinder, Cube, Sphere etc
		writer('	<room id="'+i+'">');
		writer('		<type>slideshow</type>');

		// Video Image
		writer('		<image>'+parent.escape(rootPagePath)+(videos[i-stills.length-rooms.length].videoURL)+'</image>');
		writer('		<frames>35</frames>');
		writer('		<initial_action>3</initial_action>');
		writer('	</room>');
	}

	// add all 3d objects
	for(i=videos.length+stills.length+rooms.length;i<Objects3D.length+videos.length+stills.length+rooms.length;i++) {

		// Type of room: Cylinder, Cube, Sphere etc
		writer('	<room id="'+i+'">');
		writer('		<type>3dobject</type>');

		// 3D Object Image
		writer('		<image>'+parent.escape(rootPagePath)+(Objects3D[i-videos.length-stills.length-rooms.length].object3DURL)+'</image>');
		writer('		<frames>35</frames>');
		writer('	</room>');
	}

	// AUTOMATED GUIDED TOUR

	// script action to play many actions in sequence
	actions('	<action type="script" id="6">');
	if (mode=="tradeShow")
		actions('		<loop>true</loop>');
	else
		actions('		<loop>false</loop>');
	actions('		<interruptable>false</interruptable>');

	str='		<actions>';
	if (specificGuidedTour.length<=0) { // show each room, pan 360, show any images or 3Dobjects associated with that room
		index=0;
		tourActions=new Array();
		tourActions[index++]=7; // load first room
		for(i=0;i<rooms.length;i++) {
				// play first video if it exists
				for(j=0;j<rooms[i].hotspots.length;j++) {
					if (rooms[i].hotspots[j].type == "video") { // hotspot links to another room
						tourActions[index++]=actionIDfromVideoID(rooms[i].hotspots[j].action);
					}
				}

				tourActions[index++]='5'; // rotate 360

				for(j=0;j<rooms[i].hotspots.length;j++) {
					if (rooms[i].hotspots[j].type == "pano") { // hotspot links to another room
					}
					if (rooms[i].hotspots[j].type == "video") { // hotspot links to another room
					}
					if (rooms[i].hotspots[j].type == "still") {
						tourActions[index++]=1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(i,j); // show all still images associated with the room
					}
					if (rooms[i].hotspots[j].type == "Object3D") { // hotspot links to another room
						/*
						tourActions[index++]=actionIDfromObject3DID(rooms[i].hotspots[j].action); // show all 3D Objects associated with the room
						tourActions[index++]='2'; // pause between 3D Objects
						*/
					}
				}

				if (i != (rooms.length-1)) { // if not the last room (ie. we dont go anywhere from the last room cause its the end)
					hasNextPanoHotspot = 0;
					for(j=0;j<rooms[i].hotspots.length;j++) {
						if ((rooms[i].hotspots[j].type == "pano") & (rooms[i].hotspots[j].action == (i+1))) { // if there is a hotspot to the next room in the tour then use it
							tourActions[index++]=1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(i,j); // go to next pano
							hasNextPanoHotspot = 1;
						}
						if (rooms[i].hotspots[j].type == "pano") { // hotspot links to another room
						}
						if (rooms[i].hotspots[j].type == "still") {
						}
						if (rooms[i].hotspots[j].type == "video") {
						}
						if (rooms[i].hotspots[j].type == "Object3D") {
						}
					}
					if (hasNextPanoHotspot == 0) {
						tourActions[index++]=actionIDfromPanoramaID(i+1); // load next room without doing a hotspot zoom (as there is no hotspot joining the two)
					}
				}

		}
		str+=tourActions.toString();
	}
	else if (specificGuidedTour.length>0) { // play the specific guided tour defined in the page.js file
		var currentRoom=0;

		str += currentRoom+7;
		for (i=0;i<specificGuidedTour.length;i++) {
			if(specificGuidedTour[i].name == "room") {
				str += ',';
				if (currentRoom!=null) {
					str += (7+currentRoom)+',';
					// check if currentRoom has any hotspots
					if (rooms[currentRoom].hotspots.length >= 1) {
						// check if currentRoom has a hotspot to the new room
						var foundHotspot = null;
						for(j=0;j<rooms[currentRoom].hotspots.length;j++) {
							if (rooms[currentRoom].hotspots[j].type == "pano") { // hotspot links to a still

								// is it the particular still we are after, ie does a hotspot action equal the currentHotspot
								if  (rooms[currentRoom].hotspots[j].action == specificGuidedTour[i].index) {
									foundHotspot = j;
								}
							}
						}
						if (foundHotspot!=null) {
								// get hotspot action ID from hotspot in the current room
								str += 1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(currentRoom,foundHotspot);
						}
						else { // no hotspot found that matches, so just show it
							str += actionIDfromPanoramaID(specificGuidedTour[i].index);
						}
					}
					else { // no hotspots in this room so just show it
						str += actionIDfromPanoramaID(specificGuidedTour[i].index);
					}
				}
				else { // not preceeded by a room so just show it
					str += actionIDfromPanoramaID(specificGuidedTour[i].index);
				}
				str += ',5';
				currentRoom = specificGuidedTour[i].index;
			}
			else if (specificGuidedTour[i].name == "still") {
				str += ',';
				if (currentRoom!=null) {
					// check if currentRoom has any hotspots
					if (rooms[currentRoom].hotspots.length >= 1) {
						// check if currentRoom has a hotspot to the still
						var foundHotspot = null;
						for(j=0;j<rooms[currentRoom].hotspots.length;j++) {
							if (rooms[currentRoom].hotspots[j].type == "still") { // hotspot links to a still


								// is it the particular still we are after, ie does a hotspot action equal the currentHotspot
								if  (rooms[currentRoom].hotspots[j].action == specificGuidedTour[i].index) {
									foundHotspot = j;
								}
							}
						}
						if (foundHotspot!=null) {
								// get hotspot action ID from hotspot in the current room
								str += 1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(currentRoom,foundHotspot);
						}
						else { // no hotspot found that matches, so just show it
							str += actionIDfromImageID(specificGuidedTour[i].index);
						}
					}
					else { // no hotspots in this room so just show it
						str += actionIDfromImageID(specificGuidedTour[i].index);
					}
				}
				else { // not preceeded by a room so just show it
					str += actionIDfromImageID(specificGuidedTour[i].index);
				}
			}
			else if (specificGuidedTour[i].name == "video") {
				str += ',';
				if (currentRoom!=null) {
					// check if currentRoom has any hotspots
					if (rooms[currentRoom].hotspots.length >= 1) {
						// check if currentRoom has a hotspot to the video
						var foundHotspot = null;
						for(j=0;j<rooms[currentRoom].hotspots.length;j++) {
							if (rooms[currentRoom].hotspots[j].type == "video") { // hotspot links to a video

								// is it the particular video we are after, ie does a hotspot action equal the currentHotspot
								if  (rooms[currentRoom].hotspots[j].action == specificGuidedTour[i].index) {
									foundHotspot = j;
								}
							}
						}
						if (foundHotspot!=null) {
								// get hotspot action ID from hotspot in the current room
								str += 1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(currentRoom,foundHotspot);
						}
						else { // no hotspot found that matches, so just show it
							str += actionIDfromVideoID(specificGuidedTour[i].index);
						}
					}
					else { // no hotspots in this room so just show it
						str += actionIDfromVideoID(specificGuidedTour[i].index);
					}
				}
				else { // not preceeded by a room so just show it
					str += actionIDfromVideoID(specificGuidedTour[i].index);
				}
			}
			else if (specificGuidedTour[i].name == "Object3D") {
				/*
				str += ',';
				if (currentRoom!=null) {
					// check if currentRoom has any hotspots
					if (rooms[currentRoom].hotspots.length >= 1) {
						// check if currentRoom has a hotspot to the 3D Object
						var foundHotspot = null;
						for(j=0;j<rooms[currentRoom].hotspots.length;j++) {
							if (rooms[currentRoom].hotspots[j].type == "Object3D") { // hotspot links to a video

								// is it the particular 3D Object we are after, ie does a hotspot action equal the currentHotspot
								if  (rooms[currentRoom].hotspots[j].action == specificGuidedTour[i].index) {
									foundHotspot = j;
								}
							}
						}
						if (foundHotspot!=null) {
								// get hotspot action ID from hotspot in the current room
								str += 1+7+rooms.length+stills.length+videos.length+Objects3D.length+hotspotActionIDfromPanoramaID(currentRoom,foundHotspot);
						}
						else { // no hotspot found that matches, so just show it
							str += actionIDfromObject3DID(specificGuidedTour[i].index);
						}
					}
					else { // no hotspots in this room so just show it
						str += actionIDfromObject3DID(specificGuidedTour[i].index);
					}
				}
				else { // not preceeded by a room so just show it
					str += actionIDfromObject3DID(specificGuidedTour[i].index);
				}
				*/
			}
		}
	}
	else {
		alert("Error: Specified Guided Tour undefined");
	}

	str+='</actions>';
	actions(str);
	actions('	</action>');


	writer(gActionsStr);
	writer('</tour>');

	return (gStr);
}

