var _ns_ = {
  "id": "wisp.engine.browser"
};
var wisp_runtime = require("./../runtime");
var str = wisp_runtime.str;;
var wisp_sequence = require("./../sequence");
var rest = wisp_sequence.rest;;
var wisp_reader = require("./../reader");
var readFromString = wisp_reader.readFromString;;
var wisp_compiler = require("./../compiler");
var compile_ = wisp_compiler.compile_;;;

var evaluate = function evaluate(code, url) {
  return eval(compile_(read_(code, url)));
};
exports.evaluate = evaluate;

var run = function run(code, url) {
  return (Function(compile_(read_(code, url))))();
};
exports.run = run;

var load = function load(url, callback) {
  var request = window.XMLHttpRequest ?
    new XMLHttpRequest() :
    new ActiveXObject("Microsoft.XMLHTTP");
  request.open("GET", url, true);
  request.overrideMimeType ?
    request.overrideMimeType("application/wisp") :
    void(0);
  request.onreadystatechange = function() {
    return request.readyState === 4 ?
      (request.status === 0) || (request.status === 200) ?
        callback(run(request.responseText, url)) :
        callback("Could not load") :
      void(0);
  };
  return request.send(null);
};
exports.load = load;

var runScripts = function runScripts() {
  var scripts = Array.prototype.filter.call(document.getElementsByTagName("script"), function(script) {
    return script.type === "application/wisp";
  });
  var next = function next() {
    return scripts.length ?
      (function() {
        var script = scripts.shift();
        return script.src ?
          load(script.src, next) :
          next(run(script.innerHTML));
      })() :
      void(0);
  };
  return next();
};
exports.runScripts = runScripts;

(document.readyState === "complete") || (document.readyState === "interactive") ?
  runScripts() :
window.addEventListener ?
  window.addEventListener("DOMContentLoaded", runScripts, false) :
  window.attachEvent("onload", runScripts)