// -----------------------------------------------------------
// JS Module with functions for loading SpatialEdit into a div
// This version corresponds to SpatialEdit ver. 4.x
// Usage example:
//      var sp_plugin_appletParams = {
//      "callBack": "onAppletLoaded",
//      "configUrl": "" ,
//      "jnlpFile":"" ,
//      "licenseUrl":"" ,
//      "log4jFile":"" ,
//      "mainClass": ,
//      "namedUrlData": ,
//      "width":"" ,
//      "height":"" ,
//      "tokenXml":"",
//      "tokenUrl" : null
//    };
//
//    var sp_plugin_applet = new SP.applet();
//    sp_plugin_applet.initTarget(sp_plugin_appletParams,'applet_div');
//
//    function onAppletLoaded()
//    {
//        //This function is called from the applet when it has loaded
//        //Do something useful
//        //setTimeout("sp_plugin_getTokenAsync()", 30* 60 * 1000);
//    }
//
//
// -----------------------------------------------------------

var spatialedit_staticGlobalAppletInstance = null;

function spatialedit_staticGlobalInitCallback(){
    //Static function which is ALWAYS given to the applet as callback on init
    //Causes callback actions to execute on another thread
//    setTimeout("spatialedit_staticGlobalDoCallback()", 200);
	spatialedit_staticGlobalDoCallback();
    return;
}

function spatialedit_staticGlobalDoCallback(){
    spatialedit_staticGlobalAppletInstance.hasLoaded = true;
    //Did the caller specify a callback on init?
    if (spatialedit_staticGlobalAppletInstance.callBack != null){
        //Yes: Call it
        eval(spatialedit_staticGlobalAppletInstance.callBack + "()");
    } else {
        //No: Just paint
        spatialedit_staticGlobalAppletInstance.repaintFull();
    }
    return;
}

var SP = (function() {
    //SP defines namespace for the applet object

    //initParams (To be set by the caller)
//    var initParams = {
//            "callBack": name_of_js-function_to_call_after_load,
//            "configUrl": ,
//            "jnlpFile": ,
//            "licenseUrl": ,
//            "log4jFile": ,
//            "mainClass": ,
//            "namedUrlData": ,
//            "width": ,
//            "height": ,
//            "tokenXml":"",
//            "tokenUrl":
//            "ticketData":"",
//            "ticketUrl":
//            "ticketUser":"",
//            "ticketPwd":
//          };


function applet() {
      //Defaults
      this.mainClass = "com.carlbro.spatialedit.SpatialEdit";
      this.width     = "100%";
      this.height    = "100%";
      this.log4jFile = "log4j_info.properties";
      this.name      = "spatialedit";
      this.appletInstance = null;
      this.callBack   = null;
      this.hasLoaded  = false;
      this.tokenUrl   = null;
      this.tokenXml   = null;
      this.ticketUrl  = null;
      this.ticketData  = null;
      this.ticketUser = null;
      this.ticketPwd  = null;
}
applet.prototype.init = function(initParams) {
    //Assumes, and checks for, htmlelement in initParams
    if ((initParams.htmlElement != undefined) && (initParams.htmlElement  != '') ) this.htmlElement = initParams.htmlElement;
    if ((this.htmlElement == undefined) || (this.htmlElement == '')) {alert('There is no HTML target element defined.');return false;}  
    this.initTarget(initParams, this.htmlElement)
}
,

 applet.prototype.initTarget = function(initParams, AHtmlElement) {
 
      if ((initParams.configUrl   != undefined) && (initParams.configUrl    != '') ) this.configUrl = initParams.configUrl;
      if ((initParams.jnlpFile     != undefined) && (initParams.jnlpFile    != '') ) this.jnlpFile = initParams.jnlpFile;
      if ((initParams.licenseUrl  != undefined) && (initParams.licenseUrl   != '') ) this.licenseUrl = initParams.licenseUrl;
      if ((initParams.callBack    != undefined) && (initParams.callBack     != '') ) this.callBack = initParams.callBack;
      if ((initParams.log4jFile   != undefined) && (initParams.log4jFile    != '') ) this.log4jFile = initParams.log4jFile;
      if ((initParams.mainClass   != undefined) && (initParams.mainClass    != '') ) this.mainClass = initParams.mainClass;
      if ((initParams.namedUrlData!= undefined) && (initParams.namedUrlData != '') ) this.namedUrlData = initParams.namedUrlData;
      if ((initParams.width       != undefined) && (initParams.width        != '') ) this.width = initParams.width;
      if ((initParams.height      != undefined) && (initParams.height       != '') ) this.height = initParams.height;
      if ((initParams.tokenXml    != undefined) && (initParams.tokenXml     != '') ) this.tokenXml   = initParams.tokenXml;
      if ((initParams.tokenUrl    != undefined) && (initParams.tokenUrl     != '') ) this.tokenUrl   = initParams.tokenUrl;
      if ((initParams.ticketData   != undefined) && (initParams.tokenXml     != '') ) this.ticketData  = initParams.ticketData;
      if ((initParams.ticketUrl   != undefined) && (initParams.tokenUrl     != '') ) this.ticketUrl  = initParams.ticketUrl;
      if ((initParams.ticketUser  != undefined) && (initParams.tokenXml     != '') ) this.ticketUser = initParams.ticketUser;
      if ((initParams.ticketPwd   != undefined) && (initParams.tokenUrl     != '') ) this.ticketPwd  = initParams.ticketPwd;
      if (AHtmlElement != undefined) this.htmlElement = AHtmlElement;
      
      // Check required 
      if ((this.configUrl == undefined) || (this.configUrl == '')) {alert('configUrl is required!. Check your initscript');return false;}
      if ((this.jnlpFile   == undefined) || (this.jnlpFile   == '')) {alert('jnlpFile is required!. Check your initscript');return false;}

      //Set the global instance var
      spatialedit_staticGlobalAppletInstance = this;
      this.renderToHtmlElement();
}
,
      
applet.prototype.renderToHtmlElement = function() {
  
  var isFireFox = navigator.userAgent.indexOf("Firefox") != -1;
  var isIE      = navigator.userAgent.indexOf("MSIE") != -1;
  
  if (isFireFox)
    h = this.renderFF2();
  else
    h = this.renderIE();
    
    var d = document.getElementById( this.htmlElement );
    d.innerHTML = h;
    this.appletInstance = document.getElementById(this.htmlElement).firstChild;
}
,

applet.prototype.renderIE = function() {
                 
           var h = '<OBJECT' +
         ' classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"' + 	// Highest version
         ' name="' + this.name + '"' + 
         ' width="' + this.width + '"' +
         ' height="' + this.height + '"' +
         ' codebase="http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,5">' +
         //' <PARAM name="java_code" value="' + this.mainClass + '"/>' +
         ' <PARAM name="java_codebase" value="."/>' + 
         ' <PARAM name="jnlp_href" value="' + this.jnlpFile + '"/>' + 
         ' <PARAM name="type" value="application/x-java-applet;version=1.5"/>' +
         ' <PARAM name="bgcolor" value="FFFFFF"/>' + 
         ' <PARAM name="mayscript" value="true"/>' + 
         ' <PARAM name="scriptable" value="true"/>' + 
         ' <PARAM name="config" value="' + this.configUrl + '"/>';
  if (this.licenseUrl != null) h += ' <PARAM name="license" value="' + this.licenseUrl + '"/>';
  h +=    ' <PARAM name="log4j.configuration" value=" ' + this.log4jFile + '"/>';
  
  h += ' <PARAM name="runningmethod" value="spatialedit_staticGlobalInitCallback"/>';
  if (this.tokenUrl != null) h += ' <PARAM name="tokenurl" value="' + this.tokenUrl + '"/>';  
  if (this.tokenXml != null) h += ' <PARAM name="tokenxml" value="' + this.tokenXml + '"/>';  
  if (this.ticketUrl != null) h += ' <PARAM name="ticketurl" value="' + this.ticketUrl + '"/>';  
  if (this.ticketData != null) h += ' <PARAM name="ticketdata" value="' + this.ticketData + '"/>';  
  if (this.ticketUser != null) h += ' <PARAM name="ticketuser" value="' + this.ticketUser + '"/>';  
  if (this.ticketPwd  != null) h += ' <PARAM name="ticketpwd" value="' + this.ticketPwd + '"/>';  
  h +=    ' <COMMENT>' +
         '  <EMBED type="application/x-java-applet;version=1.5"' + 
         '   name="' + this.name + '"' +
         '   width="' + this.width + '"' +
         '   height="' + this.height + '"' + 
         '   pluginspage="http://www.java.com/en/download/manual.jsp"' +  
         //'   java_code="' + this.mainClass + '"' + 
         '   java_codebase="."' + 
         '   jnlp_href="' + this.jnlpFile + '"' +
         '   bgcolor="FFFFFF"' + 
         '   mayscript="true"' +
         '   scriptable="true"' +
         '   config="' + this.configUrl + '"';
  if (this.licenseUrl != null) h += '   license="' + this.licenseUrl + "'";
  h +=    '   log4j.configuration="' + this.log4jFile + '"';
  //if (this.callBack != null)     h += '   runningmethod="' + this.callBack + '"';
  if (this.tokenUrl != null) h += '   tokenurl="' + this.tokenUrl + '"';  
  if (this.tokenXml != null) h += '   tokenxml="' + this.tokenXml + '"';  
  if (this.ticketUrl != null) h += '   ticketurl="' + this.ticketUrl + '"';  
  if (this.ticketData != null) h += '   ticketdata="' + this.ticketData + '"';  
  if (this.ticketUser != null) h += '   ticketuser="' + this.ticketUser + '"';  
  if (this.ticketPwd  != null) h += '   ticketpwd="' + this.ticketPwd + '"';  
  h += '  />' +
      '  <NOEMBED>' + 
      '   No Java support in browser !' +
      '  </NOEMBED>' + 
      ' </COMMENT>' + 
      '</OBJECT>';

  return h;
        }
        
applet.prototype.renderFF2 = function() {
    
  //var h = '<applet code="' + this.mainClass + '"';
  var h = '<applet ';
  h += ' name="' + this.name + '"';
  h += ' jnlp_href="' + this.jnlpFile + '"';
  h += ' width="' + this.width + '"';
  h += ' height="' + this.height + '"';
  h += ' scriptable="TRUE"';
  h += ' MAYSCRIPT="TRUE">';
  h += ' <PARAM name="config" value="' + this.configUrl + '"/>';
  h += ' <PARAM name="log4j.configuration" value="' + this.log4jFile + '"/>';
  if (this.licenseUrl != null) h += ' <PARAM name="license" value="' + this.licenseUrl + '"/>';
  h += ' <PARAM name="runningmethod" value="spatialedit_staticGlobalInitCallback"/>';
  if (this.tokenUrl != null) h += ' <PARAM name="tokenurl" value="' + this.tokenUrl + '"/>';  
  if (this.tokenXml != null) h += ' <PARAM name="tokenxml" value="' + this.tokenXml + '"/>';  
  if (this.ticketUrl != null) h += ' <PARAM name="ticketurl" value="' + this.ticketUrl + '"/>';  
  if (this.ticketData != null) h += ' <PARAM name="ticketdata" value="' + this.ticketData + '"/>';  
  if (this.ticketUser != null) h += ' <PARAM name="ticketuser" value="' + this.ticketUser + '"/>';  
  if (this.ticketPwd  != null) h += ' <PARAM name="ticketpwd" value="' + this.ticketPwd + '"/>';  
  h += '</applet>';
                 
  return h;
}

,
//Proxy methods for corresponding methods on applet (See javaDoc for SpatialEditInterface)

applet.prototype.asyncCopyFeature = function(ACallBack, AName, Id, ADefValues) {
    if (this.isRunning()){
        this.appletInstance.asyncCopyFeature(ACallBack, AName, Id, ADefValues);     
    } else {
        throw ("asyncCopyFeature: There is no Applet running, stopping");
    }
  }
,

applet.prototype.asyncCreateFeature = function(ACallBack, AName, ADefValues) {
    if (this.isRunning()){
        this.appletInstance.asyncCreateFeature(ACallBack, AName, ADefValues);     
    } else {
        throw ("asyncCreateFeature: There is no Applet running, stopping");
    }
  }
,

applet.prototype.asyncDeleteFeature = function(ACallBack,AName,AID) {
    if (this.isRunning()){
        this.appletInstance.asyncDeleteFeature(ACallBack,AName,AID);      
    } else {
        throw ("asyncDeleteFeatur: There is no Applet running, stopping");
    }
  }
,

  applet.prototype.asyncEditFeature = function(ACallBack,AName,AID,ADefValues) {
    if (this.isRunning()){
        this.appletInstance.asyncEditFeature(ACallBack,AName,AID,ADefValues);     
    } else {
        throw ("asyncEditFeature: There is no Applet running, stopping");
    }
  }
,
/*
applet.prototype.asyncRepaintFull = function(ACallBack) {
    //Deprecated
    this.repaintFull();       
    eval(ACallBack + "('asyncRepaintFull', 'WARNING','asyncRepaintFull is deprecated. repaintFull() was called instead.')");
  }
,

applet.prototype.asyncSetExtent = function(ACallBack, maxx, maxy, minx, miny) {
    //Deprecated
    this.setExtent(maxx,maxy,minx,miny);
    eval(ACallBack + "('asyncSetExtent', 'WARNING','asyncSetExtent is deprecated. setExtent() was called instead.')");
    }
,

applet.prototype.asyncSetVisibleThemes = function(ACallBack, AThemeIDList) {
    //Deprecated
    this.setVisibleThemes(AThemeIDList);      
    eval(ACallBack + "('asyncSetVisibleThemes', 'WARNING','asyncSetVisibleThemes is deprecated. setVisibleThemes() was called instead.')");
}

,
*/
applet.prototype.getExtent = function() {
    if (this.isRunning()){
        return this.appletInstance.getExtent();       
    } else {
        throw ("getExtent: There is no Applet running, stopping");
    }
}
,

applet.prototype.getVisibleThemes = function() {
    if (this.isRunning()){
        return this.appletInstance.getVisibleThemes();        
    } else {
        throw ("getVisibleThemes: There is no Applet running, stopping");
    }
}
,

applet.prototype.repaintFull = function() {
    if (this.isRunning()){
        this.appletInstance.invalidate();
        this.appletInstance.repaint();
        this.appletInstance.repaintFull();
    } else {
        throw ("asyncRepaintFull: There is no Applet running");
    }
}
,

applet.prototype.setExtent = function(maxx, maxy, minx, miny) {
    if (this.isRunning()){
      this.appletInstance.setExtent(maxx,maxy,minx,miny);
  } else {
      throw ("asyncSetExtent: There is no applet to set extent on!");
  }
}
,

applet.prototype.setTokenUrl = function(tokenURL) {
    if (this.isRunning()){
        this.appletInstance.setTokenUrl(tokenURL);
    } else {
        throw ("setTokenUrl: There is no applet to set extent on!");
    }
  }
,

applet.prototype.setTokenXml = function(tokenXML) {
    if (this.isRunning()){
        this.appletInstance.setTokenXml(tokenXML);
    } else {
        throw ("setTokenXml: There is no applet to set extent on!");
    }
  }
,

applet.prototype.setVisibleThemes = function(AThemeIDList) {
    if (this.isRunning()){
        this.appletInstance.setVisibleThemes(AThemeIDList);       
    } else {
        throw ("asyncSetVisibleThemes: There is no Applet running, stopping");
    }
}
,

applet.prototype.isRunning = function() {
	return this.hasLoaded;
//    if ((this.appletInstance == null) || (this.appletInstance == undefined) || (this.hasLoaded == false)){
//        return false;
//    } else {
//        return true;
//    }
}
,

applet.prototype.returnNull_delete = function(Astring) {
    if (Astring=='') return null;
    return Astring;
}

return {
applet : applet
}
})();
