/*
 * <copyright>
 *  Copyright (c) 2003 by Hyperwave AG
 * </copyright>
 *
 * <file>
 *  Name:        Resources2Javascript.js
 *  Created:     2003-10-14 by sweber
 *  Description: This file contains the JS class Resources2Javascript for client side use.
 *
 *  $Id: Resources2Javascript.js,v 1.1.2.14 2004/08/30 13:09:59 sweber Exp $
 * </file>
 */

//--------------------------------------------------------------------
/** 
 * This class handles the client side use of javascript converted resource files. (html and langres)
 * 
 * @author sweber
 */
// <JSClass Name="Resources2Javascript">
{
  /**
   * The Resources2Javascript constructor.
   *
   * @param aParam: string: nothing
   */
  function Resources2Javascript (aParam)
  {
    if ( aParam == "__proto__" )
      return;

  }
  //--------------------------------------------------------------------
  // Inheritance call for JavaScript.
  class$ = doInherit (Resources2Javascript);

  /**
   * Stores the client side resources.
   */
  class$.static_._resources_ = {};

  /**
   * The postfix for cache files.
   */
  class$.static_.JS_FILE_POSTFIX = ".cache.js";

  //--------------------------------------------------------------------
  /**
   * Registers a resource. For use in cache files only.
   *
   * @param theFilename: string: name of resource file as used for a ResourceReader
   * @param theResourceReader: object: the related ResourceReader
   */
  class$.static_.registerCSResourceReader = function (theFilename, theResourceReader)
  {
    this._resources_[theFilename] = theResourceReader;
  }

  //--------------------------------------------------------------------
  /**
   * This method returns a ResourceReader.
   *
   * @param theFilename: string: name of resource file as used for a ResourceReader
   * @return CSResourceReader: the related ResourceReader
   */
  class$.static_.getCSResourceReader = function (theFilename)
  {
    if (!this._resources_[theFilename]) {
      // <FIXXXME date="2003/11/11" author="sweber" descr="should this stay in?">
      alert("Resource '" + theFilename + "' not registered.");
      throw new lang.Exception("Resource '" + theFilename + "' not registered.");
      // </FIXXXME>
    }
    return this._resources_[theFilename];
  }

  //--------------------------------------------------------------------
  /**
   * This method returns the timestamp when the resources was converted to javascript files.
   *
   * @return number: the cache timestamp
   */
  class$.static_.getTimestamp = function ()
  {
    if (this._timestamp) {
      return this._timestamp;
    }
    else {
      this._timestamp = new File(wavemaster.htmldir + "/cache.timestamp").modified();
      return this._timestamp;
    }
  }

  //--------------------------------------------------------------------
  /**
   * This method return a javascript include string for a resource file.
   *
   * @param theFilename: string: the file as used for the ResourceReader class
   * @return string: a valid HTML string to include a resource file as javascript file
   */
  class$.static_.getJsInclude = function (theFilename, theResourceManager, theShortId)
  {
    if (CACHING) {
      var real_filename = theFilename.replace(/\%lang\%/, client.language || "ge");
      if (typeof theResourceManager != "undefined")
      {
        real_filename = real_filename.replace(/\%resroot\%/, theResourceManager.getResRoot());
        real_filename = real_filename.replace(/\%skinroot\%/, theResourceManager.getSkinRoot());
        var path = PREFIX_WAVEMASTER + real_filename + "." + theResourceManager.getSkinId() + this.JS_FILE_POSTFIX + '?' + this.getTimestamp();
      }
      else {
        var path = PREFIX_WAVEMASTER + real_filename + this.JS_FILE_POSTFIX + '?' + this.getTimestamp();
      }
  
      var ret = '<script src="' + path + '"></script>\n';
      if (typeof theShortId != "undefined") {
        ret += '<script>Resources2Javascript.static_.shortCut("' + real_filename + '", "' + theFilename + '", "' + theShortId + '");';
      }
      else {
        ret += '<script>Resources2Javascript.static_.shortCut("' + real_filename + '", "' + theFilename + '");';
      }
      
      ret += "</script>\n";
      return ret;
    }
    else {
      if (typeof theResourceManager != "undefined") {
        var reader = new com.hyperwave.res.SkinXMLResourceReader({xmlFileName_: theFilename,
                                                                  manager_: theResourceManager});
      }
      else {
        var reader = new com.hyperwave.res.XMLResourceReader({xmlFileName_: theFilename});
      }
      var ret = [];
      ret[ret.length] = "<SCRIPT>";
      ret[ret.length] = "var __temp__ = ";
      ret[ret.length] = reader.toSource();
      ret[ret.length] = "Resources2Javascript.static_.registerCSResourceReader(";
      ret[ret.length] = '"' + theFilename + '", __temp__);'
      if (typeof theResourceManager != "undefined")
      {
        var real_filename = theFilename.replace(/\%lang\%/, client.language || "ge");
        real_filename = real_filename.replace(/\%resroot\%/, theResourceManager.getResRoot());
        real_filename = real_filename.replace(/\%skinroot\%/, theResourceManager.getSkinRoot());
        
        ret[ret.length] = 'Resources2Javascript.static_.registerCSResourceReader("' + real_filename + 
        '",\nResources2Javascript.static_.getCSResourceReader("' + theFilename + '"));';
      }
      else
      {
        var real_filename = theFilename.replace(/\%lang\%/, client.language || "ge");
        
        ret[ret.length] = 'Resources2Javascript.static_.registerCSResourceReader("' + real_filename + 
        '",\nResources2Javascript.static_.getCSResourceReader("' + theFilename + '"));';
      }
      if (typeof theShortId != "undefined") {
        ret[ret.length] = 'Resources2Javascript.static_.registerCSResourceReader("' + theShortId + 
        '",\nResources2Javascript.static_.getCSResourceReader("' + theFilename + '"));';
      }
      ret[ret.length] = "</SCRIPT>";
      return ret.join("\n");
    }
  }

  //--------------------------------------------------------------------
  /**
   * ###METHOD DESCRIPTION###
   *
   * @param theResName: string: ###PARAMETER DESCRIPTION###
   * @param theShortCut: string: ###PARAMETER DESCRIPTION###
   */
  class$.static_.shortCut = function (theResName, theShortCut, theShortCut2)
  {
    this.registerCSResourceReader(theShortCut, this.getCSResourceReader(theResName));
    if (theShortCut2) {
      this.registerCSResourceReader(theShortCut2, this.getCSResourceReader(theResName));
    }
  }

  //--------------------------------------------------------------------
  /**
   * This method returns the additional includes (client side handler) needed for the client side resource reader.
   *
   * @return string: a valid html string to include javascript files
   */
  class$.static_.getSelfJsIncludes = function ()
  {
    var ret = ['<SCRIPT SRC="' + PREFIX_WAVEMASTER + 'lib/util/Caching/Resources2Javascript.js"></SCRIPT>'];
    return ret.join("\n");
  }

}
// </JSClass>

