/**
 * Inji js core
 *
 * @author Alexey Krupskiy <admin@inji.ru>
 * @link http://inji.ru/
 * @copyright 2015 Alexey Krupskiy
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
 */

function Inji() {
  this.options = {};
  this.onLoadCallbacks = [];
  this.loaded = false;
  this.listeners = {};
  this.loadedScripts = {};
}
Inji.prototype.onLoad = function (callback, start) {
  if (typeof callback == 'function') {
    if (this.loaded) {
      callback();
    } else {
      if (start) {
        this.onLoadCallbacks.unshift(callback);
      } else {
        this.onLoadCallbacks.push(callback);
      }
    }
  }
}
Inji.prototype.startCallbacks = function () {
  console.log('inji start onload');
  while (callback = this.onLoadCallbacks.shift()) {
    if (typeof callback == 'function') {
      callback();
    }
  }
  if (this.onLoadCallbacks.length != 0) {
    this.startCallbacks();
  }
  var indicator = document.getElementById('loading-indicator');
  if (indicator) {
    indicator.style.display = 'none';
  }
  inji.loaded = true;
  console.log('inji start complete');
}
Inji.prototype.start = function (options) {
  console.log('Inji start');
  for (key in options.compresedScripts) {
    inji.loadedScripts[options.compresedScripts[key]] = true;
  }
  this.options = options;
  if (options.onLoadModules) {
    this.onLoad(function () {
      for (key in options.onLoadModules) {
        if (typeof inji[key] == 'undefined') {
          inji[key] = new window[key]();
          if (typeof (inji[key].init) == 'function') {
            console.log(key + ' init');
            inji[key].init();
          }
        }
      }
    }, true)
  }
  this.loadScripts(options.scripts, 0);
}
Inji.prototype.loadScripts = function (scripts, key) {
  this.addScript(scripts[key], function () {
    if (typeof (scripts[key].name) != 'undefined') {
      inji.loadedScripts[scripts[key].file] = true;
      if (typeof inji[scripts[key].name] == 'undefined') {
        console.log('js ' + scripts[key].name + '(' + scripts[key].file + ') loaded');
        inji[scripts[key].name] = new window[scripts[key].name]();
        if (typeof (inji[scripts[key].name].init) == 'function') {
          inji[scripts[key].name].init();
        }
      }
    } else {
      inji.loadedScripts[scripts[key]] = true;
      console.log('js ' + scripts[key] + ' loaded');
      inji.event('loadScript', scripts[key]);
    }
    if (typeof (scripts[key + 1]) != 'undefined') {
      inji.loadScripts(scripts, key + 1);
    } else {
      console.log('All scripts loaded');
      inji.startCallbacks();
    }
  });
}
Inji.prototype.addScript = function (script, callback) {
  var element = document.createElement('script');
  var src = '';
  if (typeof (script.file) != 'undefined') {
    src = script.file;
  } else {
    src = script;
  }
  if (inji.loadedScripts[src]) {
    if (typeof (callback) == 'function') {
      callback();
    }
    return true;
  }
  element.src = src;
  element.type = 'text/javascript';
  if (typeof (callback) == 'function') {
    element.onload = callback;
  }
  document.head.appendChild(element);


}
Inji.prototype.on = function (eventType, callback) {
  if (typeof this.listeners[eventType] == 'undefined') {
    this.listeners[eventType] = [];
  }
  this.listeners[eventType].push(callback);
}
Inji.prototype.event = function (eventType, object) {
  if (typeof this.listeners[eventType] != 'undefined') {
    for (key in this.listeners[eventType]) {
      this.listeners[eventType][key](eventType, object);
    }
  }
}
Inji.prototype.randomString = function (length) {
  if (!length) {
    length = 20;
  }
  var text = "";
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < length; i++)
    text += chars.charAt(Math.floor(Math.random() * chars.length));

  return text;
}
Inji.prototype.numberFormat = function (number, decimals, dec_point, thousands_sep) {
  //// Format a number with grouped thousands
  // 
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +	 bugfix by: Michael White (http://crestidg.com)

  var i, j, kw, kd, km;

  // input sanitation & defaults
  if (isNaN(decimals = Math.abs(decimals))) {
    decimals = 2;
  }
  if (dec_point == undefined) {
    dec_point = ",";
  }
  if (thousands_sep == undefined) {
    thousands_sep = ".";
  }

  i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

  if ((j = i.length) > 3) {
    j = j % 3;
  } else {
    j = 0;
  }

  km = (j ? i.substr(0, j) + thousands_sep : "");
  kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
  //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
  kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


  return km + kw + kd;
}
Inji.prototype.get = function (query) {
  var element = document.querySelector(query);
  if (element) {
    return new function () {
      this.element = element;
      this.attr = function (name) {
        for (var key in this.element.attributes) {
          var attr = element.attributes[key];
          if (attr.name == name) {
            return attr.value;
          }
        }
        return null;
      }
      this.data = function (name) {
        var data = this.attr('data-' + name);
        try {
          return JSON.parse(data);
        } catch (e) {
          return data;
        }
        
      }
    }
  }
}
var inji = new Inji();


@@ -2247,24 +2247,197 @@
   %7D%0A      %7D%0A
+    %7D else if (typeof (scripts%5Bkey%5D.file) != 'undefined') %7B%0A      inji.loadedScripts%5Bscripts%5Bkey%5D.file%5D = true;%0A        console.log('js ' + scripts%5Bkey%5D.file + ' loaded');%0A%0A
     %7D else %7B
@@ -5689,24 +5689,16 @@
       %7D%0A
-        
 %0A      %7D

fix display loaded js without name