/**
 *    Copyright 2006 - 2011 Opera Software ASA
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 *
 **/


/**
 * Registry that maintains a mapping between object IDs and objects.
 * Mostly used by the template system and UI framework to avoid
 * storing objects in the DOM and to reduce the coupling between
 * ui component objects and dom
 */
function ObjectRegistry()
{
  if (ObjectRegistry.instance)
  {
    return ObjectRegistry.instance;
  }
  ObjectRegistry.instance = this;

  this._objects = {};

  this._make_id = function() {
    return "oid_" + (new Date().getTime());
  }

  /**
   * Save object in registry, with the key id. If id is not given,
   * a new unique id string is created.
   * The id is returned.
   */
  this.set_object = function(obj) {
    var id = this._make_id();
    this._objects[id] = obj;
    return id;
  }

  /**
   * Return object with id. If not found, return undefined
   */
  this.get_object = function(id) {
    return this._objects[id];
  }

  this.del_object = function(id) {
    //del this._objects[id];
  }
}

ObjectRegistry.get_instance = function()
{
  return this.instance || new ObjectRegistry();
}
