//  file: expo.js
//  desc: MediaDX Java-based client JavaScript helper functions
//  date: 11/13/2002

//  global settings

var _author = 0;

//  set browser-based variables

var _info = navigator.userAgent;
var _obj = (_info.indexOf("MSIE") > 0) &&
           (_info.indexOf("Win") > 0) &&
           (_info.indexOf("Windows 3.1") < 0) &&
           (_info.indexOf("Mac_") < 0);

//  parse arguments in URL string

function expoArgs() {
  var result = new Object();
  //  get querystring
  var querystring = location.search.substring(1);
  //  use window name for file-based parameter passing
  if (!querystring) {
    querystring = window.name;
    //  switch characters back from '_' to '%'
    for (var i = 0;  i < querystring.length;  i++)
      if (querystring.charAt(i) == '_')
        querystring = ((i > 0) ? querystring.substring(0, i) : "") + "%" +
                      ((i < querystring.length - 1) ? querystring.substring(i + 1) : "");
    querystring = unescape(querystring);
  }
  //  divide up string into name=value pairs
  var pairs = querystring.split("&");
  for (var i = 0;  i < pairs.length;  i++) {
    var pos = pairs[i].indexOf('=');
    if (pos > 0)
      //  store value as property of object
      result[pairs[i].substring(0, pos)] = unescape(pairs[i].substring(pos + 1));
  }
  return result;
}

//  tool width and height

function expoWidth(tool) {
  var result = 400;
  if (tool == "central")  result = 400;
  if (tool == "code")  result = 440;
  if (tool == "cond")  result = 400;
  if (tool == "density")  result = 600;
  if (tool == "dotplot")  result = 400;
  if (tool == "expo2")  result = 600;
  if (tool == "guess")  result = 400;
  if (tool == "histo")  result = 400;
  if (tool == "least")  result = 400;
  if (tool == "line")  result = 400;
  if (tool == "prob")  result = 400;
  if (tool == "process")  result = 400;
  if (tool == "random")  result = 400;
  if (tool == "scatter")  result = 400;
  if (tool == "table")  result = 400;
  if (tool == "target")  result = 500;
  return result;
}
function expoHeight(tool) {
  var result = 300;
  if (tool == "central")  result = 438;
  if (tool == "code")  result = 438;
  if (tool == "cond")  result = 438;
  if (tool == "density")  result = 472;
  if (tool == "dotplot")  result = 394;
  if (tool == "expo2")  result = 440;
  if (tool == "guess")  result = 408;
  if (tool == "histo")  result = 438;
  if (tool == "least")  result = 430;
  if (tool == "line")  result = 448;
  if (tool == "prob")  result = 438;
  if (tool == "process")  result = 434;
  if (tool == "random")  result = 438;
  if (tool == "scatter")  result = 418;
  if (tool == "table")  result = 438;
  if (tool == "target")  result = 436;
  return result;
}

//  launch new browser window

function expoLaunch(script, audio, tool, decode) {
  //  build parameter list
  var querystring = "script=" + script + "&audio=" + audio + "&tool=" + tool + "&decode=" + decode
  if (_author) {
    querystring = escape(querystring);
    //  window names can only use [A-Za-z0-9_] characters
    for (var i = 0;  i < querystring.length;  i++) {
      var ch = querystring.charAt(i);
      if (ch == '/')
        ch = "_2F";
      else if (ch == '.')
        ch = "_2E";
      else if (ch == '%')
        ch = "_";
      if (querystring.charAt(i) != ch)
        querystring = ((i > 0) ? querystring.substring(0, i) : "") + ch +
                    ((i < querystring.length - 1) ? querystring.substring(i + 1) : "");
    }
    window.open("expo.html", querystring,
                "width=" + expoWidth(tool) + "," +
                "height=" + expoHeight(tool) + "," +
                "scrollbars=0,resizable=0,toolbar=0,status=1");
  } else {
    //  encode slash characters
    for (var i = 0;  i < querystring.length;  i++)
      if (querystring.charAt(i) == '/')
        querystring = ((i > 0) ? querystring.substring(0, i) : "") + "%2F" +
                      ((i < querystring.length - 1) ? querystring.substring(i + 1) : "");
    window.open("expo.html?" + querystring, "MediaDX_" + tool,
                "width=" + expoWidth(tool) + "," +
                "height=" + expoHeight(tool) + "," +
                "scrollbars=0,resizable=0,toolbar=0,status=0");
  }
  return false;
}

//  generate applet code

function expoApplet(script, audio, tool, decode) {

  //  check arguments

  if ((tool != null) && ((typeof tool == "undefined") || (tool == "undefined")))
    tool = null;

  //  determine width, height, tool jar file

  var width = expoWidth(tool);
  var height = expoHeight(tool);
  var jar = "expo.jar";
  if ((tool != null) && (tool != "expo") && (tool != "expo2"))
    jar += "," + tool + ".jar";

  //  <object..> tag

  if (_obj) {
    document.writeln("<object ");
    document.writeln("  classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" ");
    document.writeln("  width=\"" + width + "\" height=\"" + height + "\" align=\"baseline\" ");
    document.writeln("  standby=\"Loading...\" ");
    document.writeln("  codebase=\"http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,1,mn\">");
    document.writeln("  <xmp>");
  }

  //  <applet..> tag

  document.writeln("<applet");
  document.writeln("  archive=\"" + jar + "\" ");
  document.writeln("  code=\"com.datadesk.expo.Expo.class\" ");
  document.writeln("  width=\"" + width + "\" height=\"" + height + "\">");
  document.writeln("  </xmp>");

  //  main parameters

  document.writeln("  <param name=\"type\" value=\"application/x-java-applet;version=1.3.1\">");
  document.writeln("  <param name=\"java_code\" value=\"com.datadesk.expo.Expo.class\">");
  document.writeln("  <param name=\"java_type\" value=\"application/x-java-applet;jpi-version=1.3.1\">");
  if (_obj)
    document.writeln("  <param name=\"archive\" value=\"" + jar + "\">");
  document.writeln("  <param name=\"frame\" value=\"0\">");
  document.writeln("  <param name=\"script\" value=\"" + script + "\">");
  if (audio != null)
    document.writeln("  <param name=\"audio\" value=\"" + audio + "\">");
  if (decode != null)
    document.writeln("  <param name=\"decode\" value=\"" + decode + "\">");
  if (tool != null)
    document.writeln("  <param name=\"tool\" + value=\"" + tool + "\">");
  //if (_author)
  //  document.writeln("  <param name=\"author\" value=\"true\">");

  //  not supported message

  document.writeln("No Java 2 SDK, Standard Edition v 1.3 support for APPLET.");

  //  clean up

  if (_obj)
    document.writeln("  </applet></object>");
  else
    document.writeln("  </applet>");
}

