/* This Source Code Form is subject to the terms of the MIT license
 * If a copy of the MIT license was not distributed with this file, you can
 * obtain one at http://www.mozillapopcorn.org/butter-license.txt */

define([], function(){

  var DEFAULT_DIRS = {
        "popcorn-js": "../external/popcorn-js",
        "css": "css"
      },
      VAR_REGEX = /\{([\w\-\._]+)\}/g,
      CSS_POLL_INTERVAL = 10;

  var DEFAULT_CHECK_FUNCTION = function(){
    var index = 0;
    return function(){
      return index++ > 0;
    };
  };

  return function( config ){

    var _configDirs = config.dirs;

    function fixUrl( url ){
      var replaceVars = url.match( VAR_REGEX );
      if( replaceVars ){
        for( var i = 0; i < replaceVars.length; ++i ){
          var rv = replaceVars[ i ],
              varOnly = VAR_REGEX.exec( rv )[ 1 ],
              regex = new RegExp( rv, "g" ),
              replacement = _configDirs[ varOnly ] || DEFAULT_DIRS[ varOnly ] || "";
          url = url.replace( regex, replacement );
        }
      }
      return url.replace( "//", "/" );
    }

    var _loaders = {
      js: function( url, exclude, callback, checkFn ){
        checkFn = checkFn || DEFAULT_CHECK_FUNCTION();

        url = fixUrl( url );

        if( !checkFn() ){
          var scriptElement = document.createElement( "script" );
          scriptElement.src = url;
          scriptElement.type = "text/javascript";
          document.head.appendChild( scriptElement );
          scriptElement.onload = scriptElement.onreadystatechange = callback;
        }
        else{
          callback();  
        }
      },
      css: function( url, exclude, callback, checkFn ){
        var scriptElement;
        checkFn = checkFn || function(){
          if( !scriptElement ){
            return false;
          }
          for( var i = 0; i < document.styleSheets.length; ++i ){
            if( document.styleSheets[ i ].href === scriptElement.href ){
              return true;
            }
          }
          return false;
        };

        url = fixUrl( url );
        if( !checkFn() ){
          scriptElement = document.createElement( "link" );
          scriptElement.rel = "stylesheet";
          scriptElement.href = url;
          document.head.appendChild( scriptElement );
        }

        var interval = setInterval(function(){
          if( checkFn() ){
            clearInterval( interval );
            callback();
          }
        }, CSS_POLL_INTERVAL );

      }
    };

    function generateLoaderCallback( items, callback ){
      var loaded = 0;
      return function(){
        ++loaded;
        if( loaded === items.length ){
          if( callback ){
            callback();
          }
        }
      };
    }

    function generateNextFunction( items, callback ){
      var index = 0;
      function next(){
        if( index === items.length ){
          callback();
        }
        else{
          Loader.load( items[ index++ ], next );
        }
      }
      return next;
    }

    var Loader = {

      load: function( items, callback, ordered ){
        if( items instanceof Array && items.length > 0 ){
          var onLoad = generateLoaderCallback( items, callback );
          if( !ordered ){
            for( var i = 0; i < items.length; ++i ){
              Loader.load( items[ i ], onLoad );
            }
          }
          else {
            var next = generateNextFunction( items, callback );
            next();
          }

        }
        else {
          var item = items;

          if( _loaders[ item.type ] ){
            if( item.url ){
              _loaders[ item.type ]( item.url, item.exclude, callback, item.check );
            }
            else{
              throw new Error( "Attempted to load resource without url." ); 
            }
          }
          else {
            throw new Error( "Loader type " + item.type + " not found! Attempted: " + item.url );
          }

        }
      }
    };

    return Loader;

  };

});
@@ -372,11 +372,10 @@
 )%5C%7D/
-g
 ,%0A
+
     
@@ -633,271 +633,126 @@
 var 
-replaceVars = url.match( VAR_REGEX );%0A      if( replaceVars )%7B%0A        for( var i = 0; i %3C replaceVars.length; ++i )%7B%0A          var rv = replaceVars%5B i %5D,%0A              varOnly = VAR_REGEX.exec( rv )%5B 1 %5D,%0A              regex = new RegExp( rv, %22g%22 ),%0A    
+match,%0A          replacement;%0A%0A      while ( VAR_REGEX.test( url ) ) %7B%0A        match = VAR_REGEX.exec( url );%0A
         
   re
@@ -739,34 +739,32 @@
  url );%0A        
-  
 replacement = _c
@@ -774,23 +774,26 @@
 igDirs%5B 
-varOnly
+match%5B 1 %5D
  %5D %7C%7C DE
@@ -808,15 +808,18 @@
 RS%5B 
-varOnly
+match%5B 1 %5D
  %5D %7C
@@ -822,24 +822,65 @@
  %5D %7C%7C %22%22;%0A  
+      console.log( match, replacement );%0A
         url 
@@ -898,13 +898,16 @@
 ce( 
-regex
+match%5B0%5D
 , re
@@ -921,35 +921,26 @@
 nt );%0A      
-  %7D%0A      
 %7D
+%0A
 %0A      retur

Work around RegExp inconsistencies across browser [#718]