//belenusHome = current.belenusHome;
//belenusHome.registerModule("Tree", self);

var newGUIDef = belenusHome.getQueryField(location.search,"newGUIDef");
if(newGUIDef == "null" ||  newGUIDef == "") newGUIDef = null;

function messageHandler(message, params) {
	if(message == "SetDataSourceByReloading") {
		var URL = belenusHome.getQueryField(params,"URL");
		location.href= belenusHome.getPath("tree.html")+"?newGUIDef="+URL;
	} else
		document.TreeApplet.sendMessage("Tree", message, params);
}
function getProperty(property, params) {
  if(document.TreeApplet) {
    try {
      var r = document.getElementById("TreeApplet");
      try {
        return r.getProperty("Tree", property, params);
      } catch (e) {
        return null;
      }
      //return document.TreeApplet.getProperty("Tree", property, params);
    } catch (e) {
      return null;
    }
    return null;
  }
}

function addStartupTreePathX(msgCount,appletNode) {
	var treePath = "";
	if(belenusHome.tp("MODE_FLAG") != null && belenusHome.tp("MODE_FLAG") == "0") {
	if(belenusHome.tp("KNOTEN_ID") != null)
		addAppletParam(appletNode, 'Message'+msgCount++, 'Tree,SelectNode,ItemPath='+belenusHome.tp("KNOTEN_ID"));
	} else {
		if(belenusHome.rememberLastVisited && belenusHome.getXCookie('treepath') != null && belenusHome.getXCookie('treepath') != "") 
			treePath = belenusHome.getXCookie('treepath');
		if(belenusHome.tp("treepath") != null && belenusHome.tp("treepath") != "") 
			treePath = belenusHome.getQueryField(location.search,"treepath");
		if (treePath != "") 
			addAppletParam(appletNode, 'Message'+msgCount++, 'Tree,SelectNode,ItemPath='+treePath);
	}
	addAppletParam(appletNode, 'Message'+msgCount++, 'Tree,SetGUIParam,Name=DisplayRoot&Value='+belenusHome.displayRoot);
}

function addStartupTreePath(msgCount) {
var treePath = "";
if(belenusHome.tp("MODE_FLAG") != null && belenusHome.tp("MODE_FLAG") == "0") {
  if(belenusHome.tp("KNOTEN_ID") != null)
  document.writeln('<param name="Message'+msgCount++ +'" value="Tree,SelectNode,ItemPath='+belenusHome.tp("KNOTEN_ID")+'">');
} else {
  if(belenusHome.rememberLastVisited && belenusHome.getXCookie('treepath') != null && belenusHome.getXCookie('treepath') != "") 
    treePath = belenusHome.getXCookie('treepath');
      if(belenusHome.tp("treepath") != null && belenusHome.tp("treepath") != "") 
    treePath = belenusHome.getQueryField(location.search,"treepath");
  if (treePath != "") 
  document.writeln('<param name="Message'+msgCount++ +'" value="Tree,SelectNode,ItemPath='+treePath+'">');
}
document.writeln('<param name="Message' + msgCount++ + '" value="Tree,SetGUIParam,Name=DisplayRoot&Value='+belenusHome.displayRoot+'"/>');
}

function addAppletParam(appletNode, key, val) {
  var elem = document.createElement('param');
  elem.setAttribute("name", key);
  elem.setAttribute("value", val);
  appletNode.appendChild(elem);
}	

function embedTreeViaDOM() {
    var tc = document.getElementById("TreeCanvas");
    var aSpan = document.createElement("span");
    var appletNode = document.createElement('applet');
    appletNode.setAttribute('id', 'TreeApplet');
    appletNode.setAttribute('code', 'com.belenus.interparts.PartsApplet');
    appletNode.setAttribute('mayscript', 'true');
    appletNode.setAttribute('width', '100%');
    appletNode.setAttribute('height', '100%');
    appletNode.setAttribute('vspace', '0');
    appletNode.setAttribute('hspace', '0');
    addAppletParam(appletNode, 'scriptable', 'true');
    addAppletParam(appletNode, 'archive', 'core.jar,xml.jar,tree.jar');
	if(newGUIDef != null)
		addAppletParam(appletNode, 'GUIDef', newGUIDef);
	else if(belenusHome.interpartsVersion == "static") 
		addAppletParam(appletNode, 'GUIDef', belenusHome.getPath('tree_guidef.xml'));
	else 
		addAppletParam(appletNode, 'GUIDef', belenusHome.getPath('tree_guidef.jsp'));
    addAppletParam(appletNode, 'DataLocales', belenusHome.getLanguagesCurrentFirst('DATA'));
    addAppletParam(appletNode, 'LocaleDef', belenusHome.getPath('lang/'+belenusHome.currentLanguage+'/localedef.txt'));
    addAppletParam(appletNode, 'GUILocale', belenusHome.currentLanguage);
    var msgCount = 0;
    var treeStyles = belenusHome.getTreeStyleAppletParams();
    for(styles in treeStyles) addAppletParam(appletNode, "Message"+msgCount++, treeStyles[styles]);
   	addStartupTreePathX(msgCount,appletNode);
    tc.appendChild(appletNode);
}