if (!naverVideoPlugin){
  var naverVideoPlugin = function (opt) {
    this.plugin_id = opt.id;
    this.opt = opt;
    this.api_url = 'http://partner.api.naver.jp/search/1.0/video?q='+encodeURI(opt.q)+'&display='+opt.display+'&o_sf='+opt.o_sf+'&order='+opt.order+'&partnerId=livedoor&siteId=p_blog&$format=json&';
  };
  naverVideoPlugin.prototype = {
    init: function () {
      this.getData();
    },
    getData: function () {
      var obj = this;
      var elm = document.getElementById('naver-video-plugin-'+this.plugin_id);
      this.callJSONP(this.api_url, function (json_data) {
        elm.innerHTML = obj.tmpl({ "data": json_data["d"]["results"], "q": obj.opt.q });
      });
    },
    tmpl: function (data) { // original by John Resig http://ejohn.org/ 'JavaScript Micro-Templating' - MIT Licensed.
      var tmpl_data = document.getElementById('naver-video-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_video_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);
    }
  };
}


