(function () {
  //Inject code v5.2

  var highcharts = document.createElement('script');
  var injectVersion = 6;
  var cdn = 'https://app.everviz.com/resources/highcharts/11.4.8/';
  var loaded = 0;  
  var encodedUrl = encodeURI('https://https://app.everviz.com//show/iiPaiUfYx');
  var chartCreated = false;
  var delayGen = 0;
  var scripts = [
    "highcharts.js",
    "modules/stock.js",
    "highcharts-more.js",
    "highcharts-3d.js",
    "modules/data.js",
    "modules/exporting.js",
    "modules/funnel.js",
    "modules/solid-gauge.js",
    "modules/export-data.js",
    "modules/accessibility.js"
  ];

  const inEmbedCode = window.location.pathname.indexOf('/embed/') > -1;
  const inEverviz = window.location.origin === 'https://app.everviz.com';
  const loadHighchartsModules = inEverviz && !inEmbedCode;

  var legacyElementClassName = 'highcharts-iiPaiUfYx';
  var elementClassName = 'everviz-iiPaiUfYx';
  var externalCSS = [
    "https://app.everviz.com/fonts/Xrz_xpok/font.css",
    "https://app.everviz.com/fonts/dg6xrkAH/font.css",
    "https://app.everviz.com/resources/css/highcharts-small-screen-annotations.css",
    "https://app.everviz.com/resources/css/highcharts-data-table.css"
  ];

  var extraScripts = [
  "https://app.everviz.com/resources/highcharts/11.4.8/modules/annotations.js",
  "https://app.everviz.com/resources/highcharts/11.4.8/modules/annotations-advanced.js",
  "https://app.everviz.com/resources/js/highcharts-small-screen-annotations.js",
  "https://app.everviz.com/resources/js/everviz-tooltip-helper.js",
  "https://app.everviz.com/resources/js/highcharts-scroll-animation.js",
  "https://app.everviz.com/resources/js/everviz-utils.js",
  "https://app.everviz.com/resources/js/highcharts-overrides.js",
  "https://app.everviz.com/resources/js/highcharts-resize.js",
  ];

  var options = {"title":{"text":"<div>Closing the housing gap would require a level of housing development matching the period post WWII</div>","align":"left","style":{"color":"#133d66","fontSize":"20px","fontFamily":"Graphik-Bold-Web"},"useHTML":true},"subtitle":{"text":"<div>Cumulative housing gap, households above supply (2012 = initial estimated housing gap)</div>","align":"left","style":{"color":"#133d66","fontSize":"14px","fontWeight":null,"fontFamily":"Graphik-Regular-Web"},"useHTML":true},"caption":{"style":{"color":"#133d66"},"text":"<div>Source: McKinsey Global Institute analysis, US Census, American Community Survey, U.S. Department of Housing and Urban Development</div>","useHTML":false},"credits":{"style":{"color":"#133d66"},"text":"","href":""},"chart":{"borderColor":"#335cad00","style":{"fontFamily":"Graphik-Regular-Web"},"backgroundColor":"#ffffff00","type":"line","borderWidth":1,"borderRadius":1,"width":0},"colors":["#ed4e25","#ed4e25","#f0bc4a","#f67a2b","#133d66","#133d66","#a5bd60","#926ea4","#625b61"],"plotOptions":{"series":{"borderRadius":2,"pointPadding":0.01,"groupPadding":0.05,"borderWidth":0,"shadow":false,"marker":{"enabled":false},"lineWidth":3,"dataLabels":{"enabled":true,"format":"{point.label:.0f}"}},"line":{"lineWidth":4}},"legend":{"align":"left","verticalAlign":"bottom","itemMarginBottom":10,"x":0,"symbolRadius":2},"yAxis":{"title":{"style":{"color":"#133d66","fontSize":"10px"},"text":"Housing Units<div><br></div>","useHTML":true},"labels":{"style":{"color":"#133d66"},"format":"{value:,.0f}","distance":"8"},"stackLabels":{"format":"{total} people"},"tickWidth":0,"lineWidth":1,"units":[],"max":2000000,"plotBands":[],"min":-5000},"xAxis":{"title":{"style":{"color":"#133d66","fontSize":"10px"},"text":"Years","useHTML":true},"labels":{"style":{"color":"#133d66"}},"type":"category","plotBands":[]},"series":[{"data":[],"turboThreshold":0,"type":"line","lineWidth":4},{"data":[],"turboThreshold":0,"type":"line","dashStyle":"Dash","lineWidth":4,"dataLabels":{"enabled":true,"format":"{point.extra}"}},{"data":[],"turboThreshold":0,"type":"line","dashStyle":"Dash","lineWidth":4},{"data":[],"turboThreshold":0,"type":"line","dashStyle":"Dash","lineWidth":4},{"data":[],"turboThreshold":0,"type":"line","dashStyle":"Dash","lineWidth":4},{"data":[],"turboThreshold":0,"type":"line"}],"tooltip":{"valuePrefix":"  ","valueSuffix":" ","pointFormat":"● {series.name}: {point.y:,.0f}<div><br></div>","footerFormat":"","useHTML":true},"everviz":{"tooltip":{"useEvervizHelper":true,"options":{"vars":{}}}},"exporting":{"url":"https://api.everviz.com/export"},"annotations":[{"langKey":"segment","type":"crookedLine","typeOptions":{"xAxis":0,"yAxis":0,"points":[{"x":6.023567644328067,"y":174396.28809444958,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}},{"x":5.978999399202465,"y":674311.9266055046,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}}],"line":{"stroke":"#133d66","strokeWidth":1,"fill":"none","r":0,"snap":2,"controlPointOptions":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10},"type":"path","points":[null,null],"markerEnd":"arrow"}},"animation":{"defer":0},"events":{},"labelOptions":{"format":"Insert Text"},"x":6.023567644328067,"y":174396.28809444958,"id":"51239e58-8bd8-411d-ba3e-052243f6425b","draggable":null},{"langKey":"label","labelOptions":{"format":"Insert Text"},"labels":[{"shape":"evervizCallout","point":{"x":6.65101862472,"y":532110.0917431192,"xAxis":0,"yAxis":0},"controlPoints":[{"symbol":"triangle-down","events":{}},{"symbol":"circle","events":{}}],"overflow":"none","crop":true,"format":"Housing Gap","uniqueId":"highcharts-cw5dyby-900","backgroundColor":"#c4c4c4","borderColor":"#c4c4c4","style":{"color":"#133d66","fontSize":"11px"},"x":-28,"y":34,"markerEnd":"evervizArrow"}],"animation":{"defer":0},"events":{},"x":6.65101862472,"y":532110.0917431192,"id":"b5485188-4842-487d-b2bc-49dfb338cddf","uniqueId":"highcharts-cw5dyby-844","draggable":null},{"langKey":"segment","type":"crookedLine","typeOptions":{"xAxis":0,"yAxis":0,"points":[{"x":10.973995003406836,"y":1551980.7696344953,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}},{"x":12.007457322551772,"y":1545871.5596330115,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}}],"line":{"stroke":"#133d66","strokeWidth":1,"fill":"none","r":0,"snap":2,"controlPointOptions":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10},"type":"path","points":[null,null],"markerEnd":"arrow"}},"animation":{"defer":0},"events":{},"labelOptions":{"format":"Insert Text"},"x":10.973995003406836,"y":1551980.7696344953,"id":"5c7c9fb6-c43c-4750-8994-cf2badad29ad","draggable":null},{"langKey":"label","labelOptions":{"format":"Insert Text"},"labels":[{"shape":"evervizCallout","point":{"x":12.063654384830771,"y":1520403.9686840326,"xAxis":0,"yAxis":0},"controlPoints":[{"symbol":"triangle-down","events":{}},{"symbol":"circle","events":{}}],"overflow":"none","crop":true,"format":"Projections","uniqueId":"highcharts-cw5dyby-876","x":35,"y":5,"backgroundColor":"#c4c4c4","borderColor":"#c4c4c4","style":{"color":"#133d66","fontSize":"11px"},"markerEnd":"evervizArrow"}],"animation":{"defer":0},"events":{},"x":12.063654384830771,"y":1520403.9686840326,"id":"4e612a87-8d16-42b2-97ce-536579fcabdf","uniqueId":"highcharts-cw5dyby-849","draggable":null},{"langKey":"segment","type":"crookedLine","typeOptions":{"xAxis":0,"yAxis":0,"points":[{"x":10.995798319327731,"y":1201834.862385321,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}},{"x":10.973389355742297,"y":1555045.871559633,"xAxis":0,"yAxis":0,"controlPoint":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10}}],"line":{"stroke":"#133d66","strokeWidth":1,"fill":"none","r":0,"snap":2,"controlPointOptions":{"events":{},"style":{"cursor":"pointer","fill":"#ffffff","stroke":"#000000","stroke-width":2},"height":10,"symbol":"circle","visible":false,"width":10},"type":"path","points":[null,null]}},"animation":{"defer":0},"events":{},"labelOptions":{"format":"Insert Text"},"x":10.995798319327731,"y":1201834.862385321,"id":"bd0aea88-22b5-4f64-b6d3-b7321aae2016","draggable":null}],"data":{"csv":"\"Year\";\"Historical available housing units\";\"Current projection\";\"1990s-2000s growth\";\"Post WWII growth\";\"Historical Housing Units Needed\";\"Projected Housing Units Needed\";\"Housing gap\"\n2012;0;;;;493594;;493594\n2013;-42071;;;;452167;;494238\n2014;52125;;;;538417;;486292\n2015;9614;;;;499096;;489481\n2016;3343;;;;493529;;496872\n2017;75993;;;;581175;;505181\n2018;158300;;;;684878;;526578\n2019;211351;;;;735905;;524554\n2020;555917;;;;1083005;;527088\n2021;515573;;;;1057459;;541885\n2022;627710;;;;1194625;;566914\n2023;662944;662944;662944;662944;1203631;1203631;540687\n2024;;684896;723906;761580;;1281956;597060\n2025;;712320;785280;861295;;1361531;649211\n2026;;741560;847069;962102;;1442383;700823\n2027;;770498;909276;1064013;;1489651;719154\n2028;;798278;971904;1167040;;1536943;738665\n2029;;824744;1034956;1271194;;1584258;759514\n2030;;849932;1098435;1376489;;1631598;781666\n2031;;873757;1162343;1482937;;1679400;805643\n2032;;895849;1226684;1590550;;1727670;831821\n2033;;915942;1291460;1699341;;1776414;860471\n2034;;934239;1356674;1809323;;1825636;891397\n2035;;951270;1422330;1920509;;1875343;924074","seriesMapping":[{"x":0,"y":1,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7},{"x":0,"y":2,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7},{"x":0,"y":3,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7},{"x":0,"y":4,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7},{"x":0,"y":6,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7},{"x":0,"y":5,"year":0,"historical_available_housing_units":1,"current_projection":2,"1990_s_2000_s_growth":3,"post_wwii_growth":4,"historical_housing_units_needed":5,"projected_housing_units_needed":6,"housing_gap":7}],"decimalPoint":".","assignDataFields":[{"labels":"A","values":"B"},{"labels":"A","values":"C"},{"labels":"A","values":"D"},{"labels":"A","values":"E"},{"labels":"A","values":"G"},{"labels":"A","values":"F"}]},"stockTools":{"gui":{"enabled":false}}};
  var optionsStub;
  
  var templateDataSettings = {};

  var pluginCode = {};;

  function isScriptAlreadyIncluded(src){

    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      if (scripts[i].hasAttribute('src')) {
        const scriptTag = scripts[i].getAttribute('src') || '';
        if ((scriptTag.indexOf(src) >= 0) || 
            (scriptTag.indexOf('highcharts.src.js') > -1 && src === 'highcharts.js') ||
            ((scriptTag.indexOf('stock/highstock.js') > -1) && src === 'modules/highstock.js') ||
             (loadHighchartsModules && src === 'highcharts.js')) {
          return true;
        }
      }
    }
    return false;
  }

  function loadCSS() {
    var cssLength = externalCSS.length;
    for(var i=0; i < cssLength; i++) {
      var css = document.createElement('link');
      css.rel  = 'stylesheet';
      css.type = "text/css";
      css.href = externalCSS[i];
      document.body.appendChild(css);
    }
  }

  function allScriptsIncluded(){
    var allScripts = scripts.concat(extraScripts);
    var haveAllScriptsBeenIncluded = true;
    var scriptsToUse = document.getElementsByTagName("script");
    for(var n=0;n<allScripts.length;n++) {
      if (!isScriptAlreadyIncluded(allScripts[n], scriptsToUse)) {
        haveAllScriptsBeenIncluded = false;
        break;
      }
    }
    return haveAllScriptsBeenIncluded;
  }

  function modifyOptions(){
    if (options.data && !options.data.complete) {
      options.data.complete = function(chartOptions) {
        const keys = Object.keys(templateDataSettings || {});
        keys.forEach(function(key) {
          const serie = chartOptions.series[key];
          (serie.data || []).forEach(function(d, i) {
            if (templateDataSettings[key].data && templateDataSettings[key].data[i]) {
              window["everviz"].merge(d, templateDataSettings[key].data[i]);
            }
          })
        })

        if (options && options.chart && options.chart.type === 'packedbubble') {
          chartOptions.series.forEach((series) => {
            series.data = series.data.filter((point) => point.value);
          });
        }

        return chartOptions;
      }
    }
    
    optionsStub = window["everviz"].createStub(options);
    window["everviz"].merge(options, pluginCode);
    if ('Chart' === 'Map') window["everviz"].modifyMapType(options);
  }

  function createChart() {
    if(chartCreated) return;

    var hasProj4 = extraScripts.indexOf('https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.6/proj4.js') > -1;

    if (!allScriptsIncluded() || 
      (hasProj4 && !window.proj4) || 
      !window["everviz"].merge ||
      !window["everviz"].createStub) {
      setTimeout(createChart, 200);
      return;
    }  

    modifyOptions();

    if (hasProj4) delayGen = 100;

    if (typeof window['Highcharts'] !== 'undefined') {
      
    if (window['everviz'] && window['everviz'].tooltipFormatter && 
        options.everviz && options.everviz.tooltip){
      if (!options.tooltip) options.tooltip = {};
      options.tooltip.formatter = window['everviz'].tooltipFormatter;
    }


    if (options.yAxis && options.yAxis.length === 1) options.yAxis = options.yAxis[0];
    if (options.xAxis && options.xAxis.length === 1) options.xAxis = options.xAxis[0];
    if (options.zAxis && options.zAxis.length === 1) options.zAxis = options.zAxis[0];

    /*
// Sample of extending options:
Highcharts.merge(true, options, {
    chart: {
        backgroundColor: "#bada55"
    },
    plotOptions: {
        series: {
            cursor: "pointer",
            events: {
                click: function(event) {
                    alert(this.name + " clicked\n" +
                          "Alt: " + event.altKey + "\n" +
                          "Control: " + event.ctrlKey + "\n" +
                          "Shift: " + event.shiftKey + "\n");
                }
            }
        }
    }
});
*/


      if (options && (options.lang || options.global)) {
        Highcharts.setOptions({
          global: options.global || {},
          lang: options.lang || {}
        });
      }

      if (Highcharts.Annotation) {
        Highcharts.Annotation.ControlPoint.prototype.redraw = function (animation) {
          this.graphic[animation ? 'animate' : 'attr'](
              this.options.positioner ? this.options.positioner.call(this, this.target) : null
          );
        };
      }

      chartCreated = true;
      window.HighchartsCloud.hasLoaded = true; 
      setTimeout(function(){
          if (window["everviz"] && window["everviz"].pendingAnim) {
            //TODO: change to using classes instead.
            //Bit of work to do there due to backward compatibility
            const elements = document.querySelectorAll(
              '#' + elementClassName + 
              ', #' + legacyElementClassName
            );
            const elementsArray = Array.prototype.slice.call(elements);
            elementsArray.forEach(function(element, elementIndex) {

              window["everviz"].updateRenderTo(optionsStub, element);
              const stubbedChart = new Highcharts.Chart(optionsStub);
              

              window["everviz"].pendingAnim({
                id: element.id + '-' + elementIndex, 
                loaded: false,
                options: options,
                appear:function(){
                  window.everviz.disconnectResizeObserver(stubbedChart);
                  window["everviz"].updateRenderTo(options, element);
                  new Highcharts.Chart(options);

                },
                element: element
              });
            });

          } else {
            const element = document.querySelector('#' + legacyElementClassName);

            if (element) new Highcharts.Chart(legacyElementClassName, options)
            else new Highcharts.Chart(elementClassName, options) 

            
          }
      }, delayGen);
    }
  }

  function check(scripts, cb) {
    if (loaded === scripts.length) {
      if (cb) {
        cb();
      } else {


        //Create first project
        if (!window.HighchartsCloud.versions ||
           (window.HighchartsCloud.versions && window.HighchartsCloud.versions.length !== window.HighchartsCloud.ondone.length)) {
             
          for (var i = 0; i < window.HighchartsCloud.ondone.length; i++) {
            try {
              window.HighchartsCloud.ondone[i]();
            } catch(e) {
              console.error(e);
            }
          }
        } else {
          if (window.HighchartsCloud.stack.length) {
            window.HighchartsCloud.stack[0]();
            window.HighchartsCloud.stack.shift();
            const shouldRenderNextChart = window.HighchartsCloud.stack.length > 0;
            if (shouldRenderNextChart) {
              window.HighchartsCloud.stack[0](true);
            }
          }
        }
        
      }
    }
  }

  function loadScript(s, scripts, cb) {
    if (!s) next();
    
    function next() {
      ++loaded;
      if (loaded < scripts.length) {
        loadScript(scripts[loaded], scripts, cb);
      }
      check(scripts, cb);
    }

    if (isScriptAlreadyIncluded(s)) {
      return next();
    }

    var n = document.createElement('script');

    n.onload = function () {
      next();
    };

    if (s.indexOf('http') >= 0) {
      n.src = s;
    } else {
      n.src = cdn + s;
    }
    document.body.appendChild(n);
  }

  function loadExtraScripts(){
    if (extraScripts.length > 0) {
      loaded = 0;
      loadScript(extraScripts[0], extraScripts);
    } else {
      check(scripts);
    }
  }

  function loadExtraScriptsAndMakeChart(isUsingStack){
    loaded = 0;
    loadCSS();
    if (extraScripts.length > 0) {
      if (isUsingStack) {
        window.HighchartsCloud.stack[0] = createChart;
        loadScript(extraScripts[0], extraScripts);
      }
      else loadScript(extraScripts[0], extraScripts, createChart);
    } else {
      check(extraScripts);
    }
  }

  function getAllScripts(){
    var evervizScripts = [];
    var allScripts = scripts.map(function(script) {
      return 'highcharts/'+ script.replace('highcharts/highcharts.js', 'highcharts.js').replace('.js', '');
    }).concat(
      extraScripts.map(function(script) {
        if (script.indexOf('https://app.everviz.com/resources/highcharts/11.4.8/') > -1) {
          return 'highcharts/'+ script.replace('https://app.everviz.com/resources/highcharts/11.4.8/', '').replace('.js', '');
        } else if (script.indexOf('https://app.everviz.com/') > -1) {
          evervizScripts.push(script);
          return null;
        } else return script;
      }).filter(function(s) {
        return s !== null;
      })
    );

    allScripts.forEach(function(script) {
      if (window.HighchartsCloud.allScripts)
        window.HighchartsCloud.allScripts[script] = 1
    });

    return {
      allHighchartsScripts: allScripts,
      evervizScripts: evervizScripts
    }
  }

  function loadRequireJSScript(){
    require.config({
      packages: [{
        name: 'highcharts',
        main: 'highcharts'
      }],
      paths: {
        'highcharts': 'https://app.everviz.com/resources/highcharts/11.4.8'
      }
    });

    const allScripts = getAllScripts();

    require(
      allScripts.allHighchartsScripts,
      function (Highcharts) {
        //Attach highcharts to the window for use in the everviz scripts
        window.Highcharts = Highcharts;
        if (allScripts && allScripts.evervizScripts.length) {
          require(allScripts.evervizScripts, function(){
            createChart();
            loadCSS();
          })
        } else {
          createChart();
          loadCSS();
        }
      }
    );
  }

  function loadInitialScript(){
    if (window['require'] && typeof require === "function" && typeof require.specified === "function") {
      loadRequireJSScript();
    } else {
      getAllScripts()
      loadScript(scripts[0], scripts, loadExtraScripts);
      loadCSS();
    }
  }

  if (typeof window['HighchartsCloud'] === 'undefined') {
    window.HighchartsCloud = {
      ondone: [createChart],
      stack: [createChart],
      hasWrapped: false,
      hasLoaded: false,
      externalCSS: externalCSS,
      versions: [injectVersion],
      types: ['chart'],
      allScripts: {},
      projectUUIDs: {'iiPaiUfYx': 1},
    };

    loadInitialScript();

  } else {
    if (window.HighchartsCloud.externalCSS) window.HighchartsCloud.externalCSS.push(externalCSS);
    if (!window.HighchartsCloud.hasLoaded) {
      if (!window.HighchartsCloud.projectUUIDs) window.HighchartsCloud.projectUUIDs = {};

      if (!window.HighchartsCloud.projectUUIDs['iiPaiUfYx']) {

        window.HighchartsCloud.projectUUIDs['iiPaiUfYx'] = 1;
        window.HighchartsCloud.ondone.push(loadExtraScriptsAndMakeChart);
        if (window.HighchartsCloud.stack) {
          window.HighchartsCloud.stack.push(loadExtraScriptsAndMakeChart);
          window.HighchartsCloud.versions.push(injectVersion);
          window.HighchartsCloud.types.push('chart');
        }

      }
      if (window['require'] && typeof require === "function" && typeof require.specified === "function") {
        getAllScripts()
        loadRequireJSScript();
      }
    }
    else {
      if (window['require'] && typeof require === "function" && typeof require.specified === "function") {
        loadRequireJSScript();
      } else {
        getAllScripts()
        loadExtraScriptsAndMakeChart();
      }
    }
  }

}());
