if (!naverThemePlugin){
  var naverThemePlugin = function (opt) {
    this.plugin_id = opt.id;
    this.opt = opt;
    this.api_url = 'http://apis.theme.naver.jp/objRanking?partnerId=livedoor&siteId=p_blog&version=1.0&ts='+opt.ts+'&display=10';
  };
  naverThemePlugin.prototype = {
    init: function () {
      this.getData();
    },
    getData: function () {
      var obj = this;
      var elm = document.getElementById('naver-theme-plugin-'+this.plugin_id);
      this.callJSONP(this.api_url, function (json_data) {
        elm.innerHTML = obj.tmpl({ "data": json_data["result"], "ts": obj.opt.ts });
      });
    },
    tmpl: function (data) { // original by John Resig http://ejohn.org/ 'JavaScript Micro-Templating' - MIT Licensed.
      var tmpl_data = document.getElementById('naver-theme-plugin-template').innerHTML;
      var fn = new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('" +
        tmpl_data.replace(/[\r\t\n]/g, " ").split("<%").join("\t").replace(/((^|%>)[^\t]*)'/g, "$1\r").replace(/\t=(.*?)%>/g, "',$1,'").split("\t").join("');").split("%>").join("p.push('").split("\r").join("\\'") +
        "');}return p.join('');");
      return fn( data );
    },
    callJSONP : function (api_url, cb_func) {
      var uniq_name = "naver_theme_plugin_" + Math.random().toString(36).slice(2);
      var scr = document.createElement("script");
      scr.type = "text/javascript";
      scr.charset = "utf-8";
      scr.src = api_url + '&callback=' + uniq_name;
      scr.id = uniq_name;
      var obj = this;
      window[uniq_name] = function (json) {
        cb_func(json);
        var u_name = uniq_name;
        setTimeout(function() {
          obj.head.removeChild(document.getElementById(u_name));
          try{
            window[u_name] = null;
            delete window[u_name];
          } catch (e) {};
        }, 200);
      };
      obj.head = document.getElementsByTagName("head").item(0);
      setTimeout(function(){ obj.head.appendChild(scr); }, 100);
    }
  };
}


