text: /**
   * @author Mat Groves http://matgroves.com/ @Doormat23
   */
  'use strict';
  
  var EventTarget = require('../events/EventTarget');
  
  /**
   * Maps file extension to loader types
   *
   * @property loadersByType
   * @type Object
   */
  var loadersByType = {};
  
  function getDataType(str)
  {
      var test = 'data:';
      //starts with 'data:'
      var start = str.slice(0, test.length).toLowerCase();
      if (start === test) {
          var data = str.slice(test.length);
  
          var sepIdx = data.indexOf(',');
          if (sepIdx === -1) //malformed data URI scheme
              return null;
  
          //e.g. 'image/gif;base64' => 'image/gif'
          var info = data.slice(0, sepIdx).split(';')[0];
  
          //We might need to handle some special cases here...
          //standardize text/plain to 'txt' file extension
          if (!info || info.toLowerCase() === 'text/plain')
              return 'txt';
  
          //User specified mime type, try splitting it by '/'
          return info.split('/').pop().toLowerCase();
      }
  
      return null;
  }
  
  /**
   * A Class that loads a bunch of images / sprite sheet / bitmap font files. Once the
   * assets have been loaded they are added to the Texture cache and can be accessed
   * easily through Texture.fromImage() and Sprite.fromImage()
   * When all items have been loaded this class will dispatch a "onLoaded" event
   * As each individual item is loaded this class will dispatch a "onProgress" event
   *
   * @class AssetLoader
   * @constructor
   * @uses EventTarget
   * @param {Array<String>} assetURLs an array of image/sprite sheet urls that you would like loaded
   *      supported. Supported image formats include 'jpeg', 'jpg', 'png', 'gif'. Supported
   *      sprite sheet data formats only include 'JSON' at this time. Supported bitmap font
   *      data formats include 'xml' and 'fnt'.
   * @param crossorigin {Boolean} Whether requests should be treated as crossorigin
   */
  function AssetLoader(assetURLs, crossorigin)
  {
      EventTarget.call(this);
  
      /**
       * The array of asset URLs that are going to be loaded
       *
       * @property assetURLs
       * @type Array<String>
       */
      this.assetURLs = assetURLs;
  
      /**
       * Whether the requests should be treated as cross origin
       *
       * @property crossorigin
       * @type Boolean
       */
      this.crossorigin = crossorigin;
  }
  
  var proto = AssetLoader.prototype;
  
  /**
   * Fired when an item has loaded
   * @event onProgress
   */
  
  /**
   * Fired when all the assets have loaded
   * @event onComplete
   */
  
  /**
   * Starts loading the assets sequentially
   *
   * @method load
   */
  proto.load = function load()
  {
      var scope = this;
  
      function onLoad() {
          scope.onAssetLoaded();
      }
  
      this.loadCount = this.assetURLs.length;
  
      for (var i = 0, l = this.assetURLs.length; i < l; i++)
      {
          var fileName = this.assetURLs[i];
          //first see if we have a data URI scheme..
          var fileType = getDataType(fileName);
  
          //if not, assume it's a file URI
          if (!fileType)
              fileType = fileName.split('?').shift().split('.').pop().toLowerCase();
  
          var Constructor = loadersByType[fileType];
          if(!Constructor)
              throw new Error(fileType + ' is an unsupported file type');
  
          var loader = new Constructor(fileName, this.crossorigin);
  
          loader.addEventListener('loaded', onLoad);
          loader.load();
      }
  };
  
  /**
   * Invoked after each file is loaded
   *
   * @method onAssetLoaded
   * @private
   */
  proto.onAssetLoaded = function onAssetLoaded()
  {
      this.loadCount--;
      this.dispatchEvent({type: 'onProgress', content: this});
      if (this.onProgress) this.onProgress();
  
      if (!this.loadCount)
      {
          this.dispatchEvent({type: 'onComplete', content: this});
          if(this.onComplete) this.onComplete();
      }
  };
  
  AssetLoader.registerLoaderType = function registerLoaderType(type, constructor)
  {
      loadersByType[type] = constructor;
  };
  
  module.exports = AssetLoader;
  