/**
 * @provides javelin-resource
 * @requires javelin-magical-init
 *           javelin-stratcom
 *           javelin-util
 *           javelin-uri
 *
 * @javelin
 */

JX.install('Resource', {

  statics: {

    _loading: {},
    _loaded: {},
    _links: [],
    _callbacks: [],

    /**
     * Loads one or many static resources (JavaScript or CSS) and executes a
     * callback once these resources have finished loading.
     *
     * @param string|array  static resource or list of resources to be loaded
     * @param function      callback when resources have finished loading
     */
    load: function(list, callback) {
      var resources = {},
        uri, resource, path, type;

      list = JX.$AX(list);

      // In the event there are no resources to wait on, call the callback and
      // exit. NOTE: it's better to do this check outside this function and not
      // call through JX.Resource, but it's not always easy/possible to do so
      if (!list.length) {
        setTimeout(callback, 0);
        return;
      }

      for (var ii = 0; ii < list.length; ii++) {
        uri = new JX.URI(list[ii]);
        resource = uri.toString();
        path = uri.getPath();
        resources[resource] = true;

        if (JX.Resource._loaded[resource]) {
          setTimeout(JX.bind(JX.Resource, JX.Resource._complete, resource), 0);
        } else if (!JX.Resource._loading[resource]) {
          JX.Resource._loading[resource] = true;
          if (path.indexOf('.css') == path.length - 4) {
            JX.Resource._loadCSS(resource);
          } else {
            JX.Resource._loadJS(resource);
          }
        }
      }

      JX.Resource._callbacks.push({
        resources: resources,
        callback: callback
      });
    },

    _loadJS: function(uri) {
      var script = document.createElement('script');
      var callback = function() {
        this.onload = this.onerror = this.onreadystatechange = null;
        JX.Resource._complete(uri);
      };

      JX.copy(script, {
        type: 'text/javascript',
        src: uri
      });
      script.onload = script.onerror = callback;
      script.onreadystatechange = function() {
        var state = this.readyState;
        if (state == 'complete' || state == 'loaded') {
          callback();
        }
      };
      document.getElementsByTagName('head')[0].appendChild(script);
    },

    _loadCSS: function(uri) {
      var link = JX.copy(document.createElement('link'), {
        type: 'text/css',
        rel: 'stylesheet',
        href: uri,
        'data-href': uri // don't trust href
      });
      document.getElementsByTagName('head')[0].appendChild(link);

      JX.Resource._links.push(link);
      if (!JX.Resource._timer) {
        JX.Resource._timer = setInterval(JX.Resource._poll, 20);
      }
    },

    _poll: function() {
      var sheets = document.styleSheets,
        ii = sheets.length,
        links = JX.Resource._links;

      // Cross Origin CSS loading
      // http://yearofmoo.com/2011/03/cross-browser-stylesheet-preloading/
      while (ii--) {
        var link = sheets[ii],
          owner = link.ownerNode || link.owningElement,
          jj = links.length;
        if (owner) {
          while (jj--) {
            if (owner == links[jj]) {
              JX.Resource._complete(links[jj]['data-href']);
              links.splice(jj, 1);
            }
          }
        }
      }

      if (!links.length) {
        clearInterval(JX.Resource._timer);
        JX.Resource._timer = null;
      }
    },

    _complete: function(uri) {
      var list = JX.Resource._callbacks,
        current, ii;

      delete JX.Resource._loading[uri];
      JX.Resource._loaded[uri] = true;

      for (ii = 0; ii < list.length; ii++) {
        current = list[ii];
        delete current.resources[uri];
        if (!JX.Resource._hasResources(current.resources)) {
          current.callback();
          list.splice(ii--, 1);
        }
      }
    },

    _hasResources: function(resources) {
      for (var hasResources in resources) {
        return true;
      }
      return false;
    }

  },

  initialize: function() {
    var list = JX.$A(document.getElementsByTagName('link')),
      ii = list.length,
      node;
    while ((node = list[--ii])) {
      if (node.type == 'text/css' && node.href) {
        JX.Resource._loaded[(new JX.URI(node.href)).toString()] = true;
      }
    }

    list = JX.$A(document.getElementsByTagName('script'));
    ii = list.length;
    while ((node = list[--ii])) {
      if (node.type == 'text/javascript' && node.src) {
        JX.Resource._loaded[(new JX.URI(node.src)).toString()] = true;
      }
    }
  }

});

@@ -1846,16 +1846,21 @@
     var 
+load_
 callback
@@ -1887,68 +1887,84 @@
     
-this.onload = this.onerror = this.onreadystatechange = null;
+JX.Resource._complete(uri);%0A      %7D;%0A      var error_callback = function() %7B
 %0A   
@@ -1967,32 +1967,36 @@
 %0A        JX.
+$E('
 Resource
 ._complete(u
@@ -1979,35 +1979,56 @@
 $E('Resource
-._complete(
+: JS file download failure: ' + 
 uri);%0A      
@@ -2111,24 +2111,25 @@
 i%0A      %7D);%0A
+%0A
       script
@@ -2137,16 +2137,37 @@
 onload =
+ load_callback;%0A     
  script.
@@ -2176,16 +2176,22 @@
 error = 
+error_
 callback

Improve error handling in javelin lib - Resource.js (fail to download)