var DEBUG;

function debug(message) {
  var console = window['console'];
  if (console && console.log) {
    console.log(message);
  }
}

var OpenSpending = OpenSpending || {};
OpenSpending.Utils = OpenSpending.Utils || {};

OpenSpending.Utils.formatAmount = function (num) {
  var billion = 1000000000;
  var million = 1000000;
  var thousand = 1000;
  var numabs = Math.abs(num);
  if (numabs > billion) {
    return num / billion + 'bn';
  }
  if (numabs > (million / 2)) {
    return num / million + 'm';
  }
  if (numabs > thousand) {
    return num / thousand + 'k';
  } else {
    return num;
  }
};

OpenSpending.Utils.formatAmountWithCommas = function (num, decimalPlaces) {
  if (decimalPlaces === undefined) {
    decimalPlaces = 0;
  }
  num = num.toFixed(decimalPlaces);
  x = num.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
};

/*
	Parse a URL query string (?xyz=abc...) into a dictionary.
*/

function parseQueryString() {
  var q = arguments.length > 0 ? arguments[0] : window.location.search.substring(1);
  var urlParams = [],
    e, d = function (s) {
      return unescape(s.replace(/\+/g, " "));
    },
    r = /([^&=]+)=?([^&]*)/g;

  while (e = r.exec(q)) {
    urlParams.push([d(e[1]), d(e[2])]);
  }
  return urlParams;
}

/*
Write tabular data as HTML table.

	:tabular: tabular data object (dict with header and data keys).
	:options: optional keyword arguments:
		colTypes: types of columns keyed by column name
		displayNames: ditto for display names for columns
*/
writeTabularAsHtml = function (tabular) {
  var options = {};
  if (arguments.length > 1) {
    options = arguments[1];
  }
  var colTypes = {};
  var displayNames = {};
  if (options.colTypes) {
    colTypes = options.colTypes;
  }
  if (options.displayNames) {
    displayNames = options.displayNames;
  }
  // knows how to format and justify based on combination of col type labelling (via colTypes)
  // and value based logic
  var _ColType = [];
  var _thead = $('<thead></thead>');
  $.each(tabular.header, function (i, col) {
    var tempDisplayName = displayNames[col] ? displayNames[col] : col;
    _thead.append($('<th></th>').append(tempDisplayName));
    if (colTypes[col]) {
      _ColType[i] = colTypes[col];
    }
  });
  var _tbody = $('<tbody></tbody>');
  // var red = /^(19|20)\d{2}$/; - replaced by _ColType
  // var rep = /^([\d\.\-]+)\%$/; - replaced by _ColType
  var ren = /^[\d\.\-]+$/;
  var reb = /^$/;
  $.each(tabular.data, function (i, row) {
    var _newrow = $('<tr></tr>');
    $.each(row, function (j, cell) {
      // decide action depending on type
      var cell2;
      if (_ColType[j] == 'range') {
        // year range
        var cell3 = parseFloat(cell);
        cell3++;
        cell2 = cell + '-' + cell3;
        _newrow.append($('<td></td>').append(cell2));
      } else if (reb.test(cell)) {
        // blank
        cell2 = '';
        _newrow.append($('<td></td>').append(cell2));
      } else if (_ColType[j] == 'percent') {
        // percent
        // 20.5% saved as 0.205. Converted back here to 20.5%
        var cell3 = cell * 100;
        cell2 = cell3.toFixed(1) + '%';
        _newrow.append($('<td class="amount"></td>').append(cell2));
      } else if (ren.test(cell)) {
        // number
        var cell3 = parseFloat(cell);
        cell2 = cell3.toFixed(0);
        _newrow.append($('<td class="amount"></td>').append(cell2));
      } else {
        // other
        cell2 = cell;
        _newrow.append($('<td></td>').append(cell2));
      }
    });
    _tbody.append(_newrow);
  });
  return {
    'thead': _thead,
    'tbody': _tbody
  };
}

function loadingMessage() {
  $.blockUI({
    message: 'Please wait, loading ...',
    timeout: 30000,
    css: {
      border: 'none',
      padding: '15px',
      backgroundColor: '#000',
      '-webkit-border-radius': '10px',
      '-moz-border-radius': '10px',
      opacity: .5,
      color: '#fff'
    }
  });
  $('.blockMsg').attr('title', 'Click to unblock').click($.unblockUI);
}
@@ -408,24 +408,35 @@
 %0A    return 
+Math.round(
 num / billio
@@ -432,24 +432,25 @@
 um / billion
+)
  + 'bn';%0A  %7D
@@ -489,24 +489,35 @@
 %0A    return 
+Math.round(
 num / millio
@@ -517,16 +517,17 @@
  million
+)
  + 'm';%0A
@@ -568,16 +568,27 @@
  return 
+Math.round(
 num / th
@@ -593,16 +593,17 @@
 thousand
+)
  + 'k';%0A
@@ -4162,8 +4162,9 @@
 ckUI);%0A%7D
+%0A

Make js number formatter round up numbers