﻿function createLayer(id,html,width,height,left,top) { 
 
 var newdiv = document.createElement('div');
 var xdif = document.body.getAttribute("leftmargin");
 var ydif = document.body.getAttribute("topmargin");
 newdiv.setAttribute("id",id);
 if(width)
 {
   newdiv.style.width = width;
 }
 if(height)
 {
   newdiv.style.height = height;
 }
 if((left || top)||(left && top))
 {
 newdiv.style.position = "absolute";
   if(left)
   {
     newdiv.style.left = left + Number(xdif) + 1 ; // gives accurate position in IE
   }
   if(top)
   {
     newdiv.style.top = top + Number(ydif);
   }
 }
 newdiv.style.background = "#00C";
 newdiv.style.border = "0px solid #000";
 //newdiv.style.visibility = "hidden";
 if(html)
 {
   newdiv.innerHTML = html;
 } else {
   newdiv.innerHTML = "nothing"
 }
 document.body.appendChild(newdiv);
} 

function showHideLayer(id,val){
	var mydiv = document.getElementById(id);
	if(mydiv){
		mydiv.style.visibility = val;
	}
}

function replaceX3D(id,scene){
	var mydiv = document.getElementById(id);
	var browser = mydiv.document.FLUX.getBrowser();
	var vers = browser.loadUrlS(scene);
}
function showString(str){
	alert(str);
}

// Add X3D to the current scene
function createNewWorld(id, str){
	var mydiv = document.getElementById(id);
	var browser = mydiv.document.FLUX.getBrowser();
	var scene = browser.createX3DFromString(unescape(str));
	

	var rootnodes = scene.getRootNodes();

	var i;
	var context = browser.getExecutionContext();
	// Do a bit of work to deal with the quirky X3D add/remove root node paradigm
	for (i = 0; i < rootnodes.length; i++)
	{
		node = rootnodes[i];

		scene.removeRootNode(node);

		context.addRootNode(node);
	}
}

function myParse(str){

	return str;	
}
function resetScene(id){
	// This code, though it works and follows the spec, makes IE crash sometimes
	/*
	var mydiv = document.getElementById(id);
	var browser = mydiv.document.FLUX.getBrowser();
	var context = browser.getExecutionContext();
	var scene = context.getRootNodes();
	var i = 0;
	for (i = 0; i < scene.length; i++)
	{
		node = scene[i];
		context.removeRootNode(node);
	}*/
// Load blank scene instead, seems to be a stable hack.
	var mydiv = document.getElementById(id);
	var browser = mydiv.document.FLUX.getBrowser();
	var vers = browser.loadUrlS("101.x3d");
	
}
function createX3DFromString(str)
{
	var mydiv = document.getElementById(id);
	var browser = mydiv.document.FLUX.getBrowser();
	var scene = browser.createX3DFromString(str);

	var rootnodes = scene.getRootNodes();

	var i;

	// Do a bit of work to deal with the quirky X3D add/remove root node paradigm
	for (i = 0; i < rootnodes.length; i++)
	{
		node = rootnodes[i];

		scene.removeRootNode(node);

		context.addRootNode(node);
	}
}

function setNavigationMode(id,new_mode)
{
	var context = getContext(id);
	//alert(new_mode);
	myNavInfoNode = context.getNode(new_mode);
	myNavInfoNode.set_bind = true; 
	//myNavInfoNode.set_type = new_mode; 
}	
function setCamera(id,cam)
{
	var context = getContext(id);
	var camera = context.getNode(cam);
	camera.set_bind = true;	
}
function getSomeData(flashId,id){
	flashMovie = document.getElementById(flashId);
	if(flashMovie == null)
	{
		alert("Movie not found. Check id/name of Flash movie");
		return;
	}
	var context = getContext(id);
	var scene = context.getRootNodes();
	var firstName = "";
	//var thenode = context.getNode('dad_Sphere1');
	var i;
	for(i=0;i<scene.length;i++)
	{
	firstName += scene[i].getTypeName() + "<br>";
	}
	flashMovie.SetVariable('tracer',firstName);
}
function changeColor(id,targ,r,g,b){
	//alert(id+","+targ+","+r+","+g+","+b);
	var context = getContext(id);
	var scene = context.getRootNodes();
	// loop through root nodes to find target node (a proto with unique name)
	var i;
	for(i=0;i<scene.length;i++){
		if(scene[i].getTypeName() == targ){
			//alert("tried changing"+prop+" to "+value);
			scene[i].getField("myColor").r = r;
			scene[i].getField("myColor").g = g;
			scene[i].getField("myColor").b = b;
			break;
		}
	}
}
function changeImage(id,targ,value){
	var context = getContext(id);
	var scene = context.getRootNodes();
	// loop through root nodes to find target node (a proto with unique name)
	var i;
	for(i=0;i<scene.length;i++){
		if(scene[i].getTypeName() == targ){
			//alert("tried changing"+prop+" to "+value);
			scene[i].getField("img").set1Value(0, value);
			break;
		}
	}
	
}
function changePosition(id,targ,x,y,z){
	//alert(id+","+targ+","+x+","+y);
	var context = getContext(id);
	//var scene = context.getRootNodes();
	// loop through root nodes to find target node (a proto with unique name)
	/*var i;
	for(i=0;i<scene.length;i++){
		if(scene[i].getTypeName() == targ){
			//alert("tried changing"+prop+" to "+value);
			scene[i].getField("myTrans").x = x;
			scene[i].getField("myTrans").y = y;
			scene[i].getField("myTrans").z = z;
			break;
		}
	}*/
	var thenode = context.getNode(targ);
	thenode.translation.x = x;
	thenode.translation.y = y;
	thenode.translation.z = z;
	
}
function getContext(id){
	var mydiv = document.getElementById(id);
	if (mydiv == null)
	{
		alert("Couldn't find X3D layer with this id");
		return;
	}
	var browser = mydiv.document.FLUX.getBrowser();
	if (browser == null)
	{
		alert("Couldn't get X3D browser object!");
		return;
	}
	var context = browser.getExecutionContext();
	if (context == null)
	{
		alert("Couldn't get executionContext object!");
		return;
	}
	return context;
}



