/**
 * Provides applegate.resources.TextBundle to aid i18n intergration.
 */
(function(){
  YAHOO.namespace("applegate.resources");

  var Dom = YAHOO.util.Dom,
      global = YAHOO.applegate.global,
      resources = YAHOO.applegate.resources;

  /**
   * TextBundle can be created either with a HTMLElement or a str of a id of a HTMLElement.
   * The expected structure of the elment is:
   * <div>
   *   <span key="i18n.message.resource">Translated message</span>
   *   <span key="another.resource">Label 1</span>
   *   <span key="other.message.resource">Thing</span>
   * </div>
   * Note the given element will be removed from the DOM.
   * @param elm HTMLElement | String
   */
  resources.TextBundle = function(elm){
    var element = Dom.get(elm);
    this._messages = new Array();
    var children = Dom.getChildrenBy(element, function(e){ return e.getAttribute("key"); });
    Dom.batch(children, function(e){
      this._messages[e.getAttribute("key")] = e.innerHTML;
    }, this, true);
    global.removeNode(element);
  };

  /**
   * AuthBundle can be created either with a HTMLElement or a str of a id of a HTMLElement.
   * The expected structure of the elment is:
   * <div>
   *   <span auth="MY_AUTHORITY"></span>
   *   <span auth="ANOTHER_AUTHORITY"></span>
   *   <span auth="YET_ANOTHER_AUTHORITY"></span>
   * </div>
   * Note the given element will be removed from the DOM.
   * @param elm HTMLElement | String
   */
  resources.AuthBundle = function(elm){
    var element = Dom.get(elm);
    this._auths = [];
    var children = Dom.getChildrenBy(element, function(e){ return e.getAttribute("auth"); });
    Dom.batch(children, function(e){
      this._auths [e.getAttribute("auth")] = e.innerHTML;
    }, this, true);
    global.removeNode(element);
  };

  /**
   * Primary method for accessing the messages held in the bundle.
   * @param key required key, if the resource isn't found the key is returned as is.
   */
  resources.TextBundle.prototype.getText = function(key){
    return this._messages[key] ? this._messages[key] : key ;
  };

  /**
   * Primary method for accessing the authorities held in the bundle.
   * @param auth required key, if the authority isn't found false is returned.
   * @return boolean
   */
  resources.AuthBundle.prototype.hasAuth = function(auth){
    return this._auths[auth] ;
  };
})();