/*
 * Copyright (C) 2001-2007 Seagull Holding N.V. All rights reserved.
 */

var PRINTER_CLIENT_PACKAGE_NAME           = 'LegaSuite Printer Plugin 4.1038.1.664';
var PRINTER_CLIENT_XPI_FILENAME           = 'legasuiteprinterclient.xpi';
var PRINTER_CLIENT_CRX_FILENAME           = 'legasuiteprinterclient.crx';

var CRX_TIMER_TIMEOUT                     = 3000;
var CRX_TIMER_MAX                         = 40;

function PrinterClient()
{
    /*
     * Determine the User Agent.
     */
    var ua = navigator.userAgent.toLowerCase();
    this.isInternetExplorer_ = ua.indexOf("msie") != -1;
    this.isNavigator_ = ((ua.indexOf('mozilla')!=-1)
        && (ua.indexOf('spoofer')==-1)
        && (ua.indexOf('compatible') == -1)
        && (ua.indexOf('opera')==-1)
        && (ua.indexOf('webtv')==-1));
    this.isWindows_ = navigator.appVersion.indexOf("Win") != -1;

  /*
   * Find out if the printer client is already installed.
   */
  this.isInstalled_ = PrinterClient.isPluginInstalled();
}

/**
 * getRandomParam
 * Helper function used to prevent browser extension caching
 *
 * @ returns random number converted to hex string
 */
PrinterClient.getRandomParam = function()
{
  return Math.floor((Math.random() * 0xFFFFFFFF), 1).toString(16)
}

PrinterClient.isPluginInstalled = function()
{
  for (i = 0; i < navigator.plugins.length; ++i)
  {
    if (navigator.plugins[i].name == PRINTER_CLIENT_PACKAGE_NAME)
    {
      return true;
    }
  }  
  return false; 
}

PrinterClient.prototype.writeInstallPage = function()
{
    document.write('<div class="printerClientInstallBox">');
    document.write('<p class="header">The browser you are using does not have the <b>' + PRINTER_CLIENT_PACKAGE_NAME + '</b> ');
    document.write('installed.  To get the Printer Client please press button below.</p>');
    document.write('<p>Installing the <B>' + PRINTER_CLIENT_PACKAGE_NAME + '</b> takes approximately 4 minutes with a 33K modem.</b>');
    document.write('<p>The Printer Client will automatically be downloaded and installed by');
    document.write(' your browser. The only thing you have to do is to restart the browser when the installation procedure asks you to do so.</p>');
    document.write('<input id="printerClientInstallButton" type="button" class="button" value="Install Now" />');
    document.write('</div>');

    /*
     * Add the on-click handler to the printer client install button.
     */
    document.getElementById('printerClientInstallButton').onclick = PrinterClient.install;
}

PrinterClient.prototype.mustInstall = function()
{
    return this.isWindows_ && this.isNavigator_ && !this.isInstalled_;
}

PrinterClient.prototype.write = function()
{
	if (!this.isWindows_)
	    return;
	
    document.write('<iframe src="" width="1", height="1", scrolling="no" frameborder="0" name="printerClientFrame" ></iframe>');
    document.write('<div class="printerClientStartBox">');
    document.write('<p>Use this button to start the Printer Client, if it was accidentally stopped.</p>');
    document.write('<input id="printerClientStartButton" class="button" type="button" value="Start the Printer Client" />');
    document.write('</div>');

    /*
     * Store a reference to "this" object, so that we can use it in the anonymous function below.
     */
    var printerClient = this;
    document.getElementById('printerClientStartButton').onclick = function () {printerClient.load();};

    this.load();
}

PrinterClient.prototype.load = function()
{
    if (this.isInternetExplorer_)
        parent.printerClientFrame.document.location = 'startprinterclientie.html';
    else if (this.isNavigator_)
        parent.printerClientFrame.document.location = 'startprinterclientns.html';
}

PrinterClient.install = function ()
{
    var agentstr;
    var ABSOLUTE_URL;
    var fullpath = window.location.pathname;
    var pathname = fullpath.substring(0,fullpath.lastIndexOf("/"));

    ABSOLUTE_URL =  window.location.protocol + "//";
    ABSOLUTE_URL += window.location.hostname;
    
    if (window.location.port != "")
        ABSOLUTE_URL += ":" + window.location.port;

    agentstr = navigator.userAgent.toLowerCase();      
    if (agentstr.indexOf('firefox') != -1)
    { 
      var xpiInstallation = new Object();          
      ABSOLUTE_URL += pathname + "/" + PRINTER_CLIENT_XPI_FILENAME + "?" + PrinterClient.getRandomParam();
      xpiInstallation[PRINTER_CLIENT_PACKAGE_NAME] = ABSOLUTE_URL;
      InstallTrigger.install(
          xpiInstallation,
          function (url,status)
          {
            if (status == 999 )
             alert("Please, restart the browser to complete the installation process.");
           else if (status == 210 )
             alert("Install failed: User cancelled installation");
           else if (status != 0)
             alert("Install FAILED with error: " + status);
          });
    }
    else if (agentstr.indexOf('chrome') != -1)
    {  
      ABSOLUTE_URL += pathname + "/" + PRINTER_CLIENT_CRX_FILENAME + "?" + PrinterClient.getRandomParam();
      window.open(ABSOLUTE_URL, "Extension download");

      var printerClient = this;
      
      printerClient.crxTimerCount = 0;
      printerClient.crxTimerID    = window.setInterval(
          function()
          {
            if (PrinterClient.isPluginInstalled()) {
              window.clearInterval(printerClient.crxTimerID);
              window.location.reload(true);
            }
            else if (this.crxTimerCount >= CRX_TIMER_MAX)
              window.clearInterval(printerClient.crxTimerID);
            else
              printerClient.crxTimerCount++;
          },
          CRX_TIMER_TIMEOUT);
    }
}

