(function () {
  var highcharts = document.createElement('script');

  var cdn = 'https://code.highcharts.com/';
  var loaded = 0;  
  var encodedUrl = encodeURI('https://https://cloud.highcharts.com//show/ogutuvy');

  var scripts = [
    "stock/highstock.js",
    "highcharts-more.js",
    "highcharts-3d.js",
    "modules/data.js",
    "modules/exporting.js",
    "modules/funnel.js",
    "modules/solid-gauge.js",
    "modules/annotations.js",
    "modules/export-data.js",
    "modules/accessibility.js"
    /* "modules/series-label.js" */
  ];

  var options = {"chart":{"type":"pie","style":{"fontFamily":"Arial"}},"plotOptions":{"pie":{"allowPointSelect":true,"cursor":true,"showInLegend":true,"innerSize":"60%","dataLabels":{"enabled":false,"useHTML":true}},"series":{"dataLabels":{"enabled":true,"style":{"color":"#000000","fontSize":"12px"},"format":"{point.y}","useHTML":true}}},"legend":{"y":45,"itemStyle":{"color":"#000000"},"verticalAlign":"top"},"credits":{"href":"prri.org","text":"PRRI"},"subtitle":{"style":{"color":"#000000","fontSize":"14px"},"text":"Among Republicans and Republican-leaners"},"yAxis":{"title":{"style":{"color":"#000000"}}},"tooltip":{"backgroundColor":"#ffffff"},"title":{"style":{"fontWeight":"bold","color":"#000000"},"text":"Who is your favorite Republican president?"},"xAxis":{"title":{"style":{"color":"#000000"}}},"exporting":{"allowHTML":true},"series":[{"colors":{"0":"#9e292c","1":"#b4604f","2":"#c6836f","3":"#cf9884","4":"#dcaf9d","5":"#e6c8b6","6":"#90918d","7":"#c1c0ba","8":"#f45b5b","9":"#91e8e1"},"index":0}],"meta":{"js":{"lib":{"version":"1.0.012"}},"highcharts":{"version":"4.1.8"}},"data":{"csv":",All Republicans and Republican-leaners\nRonald Reagan,69\nGeorge W. Bush,12\nDwight Eisenhower,7\nGeorge H.W. Bush,5\nRichard Nixon,2\nGerald Ford,1\nOther,3\nDon't know/Refused,1","seriesMapping":[{"x":0}]}};

  function isScriptAlreadyIncluded(src){
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      if (scripts[i].hasAttribute('src')) {
        if ((scripts[i].getAttribute('src') || '').indexOf(src) >= 0) {
          return true;
        }
      }
    }
    return false;
  }

  function createChart() {
    if (typeof window['Highcharts'] !== 'undefined') {


    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 from highcharts
var myTitle     = "Who is your favorite Republican president?",      // Set title for Twitter
    myURL       = "http://www.prri.org/research/divide-americas-future-1950-2050/";    // Set URL to the article

var items = options.exporting && options.exporting.buttons &&
            options.exporting.buttons.contextButton.menuItems,
    encodedUrl = encodeURIComponent(myURL),
    mySharers = [
      [
        '<a href="https://www.facebook.com/sharer/sharer.php?u=',
        encodedUrl,
        '" target="_blank">Share on Facebook</a>'
      ],
      [
        '<a href="https://plus.google.com/share?url=',
        encodedUrl,
        '" target="_blank">Share on Google+</a>'
      ],
      [
        '<a href="https://twitter.com/share?url=',
        encodedUrl,
        '&text=',
        myTitle,
        '" target="_blank">Share on Twitter</a>'
      ],
      [
        '<a href="http://www.linkedin.com/shareArticle?mini=true&url=',
        encodedUrl,
        '" target="_blank">Share on LinkedIn</a>'
      ]
        ]

if (items) {
    options.exporting.buttons.contextButton.menuItems = items.slice(0, -3);

    Highcharts.each(mySharers, function (item, i){
      options.exporting.buttons.contextButton.menuItems[i].text = item.join('');
    });
}


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

      new Highcharts.Chart('highcharts-ogutuvy', options);
    }
  }

  function check() {
    if (loaded === scripts.length) {
      for (var i = 0; i < window.HighchartsCloud.ondone.length; i++) {
        try {
          window.HighchartsCloud.ondone[i]();
        } catch(e) {
          console.error(e);
        }
      }
    }
    window.HighchartsCloud.hasLoaded = true;
  }

  function loadScript(s) {

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

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

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

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

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

  if (typeof window['HighchartsCloud'] === 'undefined') {
    window.HighchartsCloud = {
      ondone: [createChart],
      hasWrapped: false,
      hasLoaded: false
    };

    loadScript(scripts[0]);

  } else {
    if (window.HighchartsCloud.hasLoaded) {
      createChart();
    } else {
      window.HighchartsCloud.ondone.push(createChart);
    }
  }

  try {
    var r = new XMLHttpRequest();
    r.open('POST', 'https://cloud-api.highcharts.com:443/chart/78351/2/view', true);
    r.setRequestHeader('Content-Type', 'application/json');
    r.send();
  } catch (e) {
  }

}());
