(function() {
  var JSONP, WIDGET_SRC_MATCH, Widget;
  WIDGET_SRC_MATCH = /goodscout\.icorpa\.com\/js\/widget\.js/;
  /*
      JSONP utility
  */
  JSONP = function(url, data, callback) {
    var cbname, done, head, script;
    cbname = "JSONP" + ((new Date()).getTime());
    window[cbname] = function(json) {
      callback(json);
      return delete window[cbname];
    };
    script = document.createElement('script');
    done = false;
    data.callback = cbname;
    script.src = url + ("?" + (JSONP.objToQuery(data || {})));
    script.onload = script.onreadystatechange = function() {
      if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) {
        done = true;
        script.onload = script.onreadystatechange = null;
        if (script && script.parentNode) {
          return script.parentNode.removeChild(script);
        }
      }
    };
    head = document.getElementsByTagName('head')[0];
    return head.appendChild(script);
  };
  JSONP.objToQuery = function(obj) {
    var param, query, value;
    query = [];
    for (param in obj) {
      value = obj[param];
      query.push("" + (encodeURIComponent(param)) + "=" + (encodeURIComponent(value)));
    }
    return query.join('&');
  };
  /*
      Widget
  */
  Widget = {
    cssReset: 'border:0;margin:0;padding:0',
    /*
        Find the first instance of this widget that does not already have an ID
    */
    findInjectionPoint: function() {
      var index, scripts, tag;
      scripts = document.getElementsByTagName('script');
      for (index in scripts) {
        tag = scripts[index];
        if (WIDGET_SRC_MATCH.test(tag.src) && !tag.hasAttribute('data-widget-id')) {
          tag.setAttribute('data-widget-id', Widget.name);
          return tag;
        }
      }
    },
    /*
        Read the embeded JSON config from a script tag
    */
    readJSONConfig: function(script) {
      var out;
      out = Widget.defaults;
      try {
        out = JSON.parse(script.innerHTML);
      } catch (E) {
        out = Widget.defaults;
      }
      return out;
    },
    init: function() {
      var k, script, title, v, _ref;
      script = Widget.findInjectionPoint();
      Widget.config = Widget.readJSONConfig(script);
      _ref = Widget.defaults;
      for (k in _ref) {
        v = _ref[k];
        if (typeof Widget.config[k] === 'undefined') {
          Widget.config[k] = v;
        }
      }
      if (script) {
        Widget.element = document.createElement('DIV');
        Widget.element.setAttribute('style', "" + Widget.cssReset + ";width:" + Widget.config.width + ";min-width:" 
+ Widget.config.width + ";height:" + Widget.config.height + ";min-height:" + Widget.config.height + ";background:" + 
Widget.config.background_color);
        Widget.element.setAttribute('class', 'goodscout-widget');
        if (Widget.config.title && Widget.config.title !== '') {
          title = document.createElement('H3');
          title.innerHTML = Widget.config.title;
          Widget.element.appendChild(title);
        }
        script.parentNode.insertBefore(Widget.element, script);
        script.parentNode.removeChild(script);
        return Widget.generate();
      }
    },
    generate: function() {
      var injector, url;
      url = 'http://goodscout.icorpa.com/api/badges';
      injector = function(json) {
        var data, idstr, img, _ref, _results;
        console.log(json);
        console.log(json.meta && json.meta.code);
        if (json.meta && json.meta.code === 200) {
          _ref = json.response.badges;
          _results = [];
          for (idstr in _ref) {
            data = _ref[idstr];
            _results.push(data.unlocks.length > 0 ? (img = document.createElement('img'), img.setAttribute('src', "" 
+ data.image.prefix + "57" + data.image.name), Widget.element.appendChild(img)) : void 0);
          }
          return _results;
        } else {
          return Widget.element.innerHTML = JSON.stringify(json);
        }
      };
      return JSONP(url, {
        userid: Widget.config.userid
      }, injector);
    }
  };
  /*
     Give widget unique name, so we can include more than once.
  */
  Widget.name = (function() {
    var uniq;
    uniq = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].map(function() {
      return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
    });
    return "goodscout-" + (uniq.join(''));
  })();
  Widget.defaults = {
    userid: '',
    width: 120,
    height: 200,
    background_color: '#ffffff',
    title: ''
  };
  if (typeof window.addEventListener !== 'undefined') {
    window.addEventListener('load', Widget.init, false);
  } else if (typeof window.attachEvent !== 'undefined') {
    window.attachEvent('onload', Widget.init);
  }
}).call(this);


