if (Ameba == undefined){
  var Ameba = {};
}


Ameba.Loader= function (){
  this.init.apply(this, arguments);
};


Ameba.Loader.prototype = {

  /**
   * @description 依存するファイルのnameをrequiresに設定すると、全ファイルロード後にAmeba[name]をnewする
   * clickTale実行部分をコメントアウト
   */
  scriptConfs : [
    //{name : "clicktale", src : "http://s.clicktale.net/WRc3.js"},
    //{name : "Logging",   src : "http://stat100.ameba.jp/blog/js/blog_top/logging.js", requires : ["clicktale"]}
    {name : "Logging",   src : "http://stat100.ameba.jp/blog/js/blog_top/logging.js", requires : []}
  ],


  /**
   * @description
   */
  init : function (){
    this.bindPageLoad();
  },


  /**
  * @description
  */
  bindPageLoad : function (){
    var self = this;

    if (window.addEventListener){
      window.addEventListener("load", function (){
        self.appendScripts.call(self);
      }, false);
    } else if (window.attachEvent){
      window.attachEvent("onload", function (){
        self.appendScripts.call(self);
      });
    }
  },


  /**
   * @description
   */
  appendScripts : function (){
    var script;
    var scriptConf;

    var name;
    var requires;

    var fragment = document.createDocumentFragment();

    var scriptProto = document.createElement("script");
    scriptProto.type = "text/javascript";

    for (var i = 0, length = this.scriptConfs.length; i < length; i++){
      script = scriptProto.cloneNode(false);

      scriptConf = this.scriptConfs[i];

      script.src = scriptConf.src;
      name = scriptConf.name;
      requires = scriptConf.requires;

      if (requires != undefined){
        scriptConf._requires = requires;
        scriptConf._requires.push(name);
      }

      this.bindHandleScriptLoad(name, script);

      fragment.appendChild(script);
    }

    document.body.appendChild(fragment);
  },


  /**
   * @param name scriptConf.name
   * @param script <script>
   * @description appendScriptで使うクロージャ
   */
  bindHandleScriptLoad: function (name, script){
    var self = this;

    script.onload = script.onreadystatechange = function (){
      self.handleScriptLoad.call(self, name, script)
    };
  },


  /**
   * @param name scriptConf.name
   * @param script <script>
   * @description 依存するファイルがある場合、全てがロードされた後にnewする
   */
  handleScriptLoad: function (scriptName, script){
    var scriptConf;
    var _requires;

    var scriptConfs = this.scriptConfs;

    var readyState = script.readyState;

    if (readyState == undefined || /loaded|complete/.test(readyState)){
      for (var i = 0, length = scriptConfs.length; i < length; i++){
        scriptConf = scriptConfs[i];
        _requires = scriptConf._requires;


        if (_requires != undefined){
          scriptConf._requires = _requires.join(",").replace(new RegExp(scriptName, "g"), "").split(",");

          if (scriptConf._requires.join("") == ""){
            new Ameba[scriptConf.name];
          }
        }
      }
    }
  }
};


new Ameba.Loader;

