var WAVEPANEL_nextId = 0, DEFAULT_CONFIG = {bgcolor:"transparent", color:"black", font:"Arial", fontSize:"8pt", header:!1, footer:!1, toolbar:!1, rootUrl:"https://wave.google.com/a/wavesandbox.com/"};
(typeof gadgets == "undefined" || !gadgets.rpc) && document.write('<script src="https://wave.google.com/gadgets/js/core:rpc.js?debug=1&c=1" type="text/javascript"><\/script>');
function WavePanel(a) {
  this.id_ = WAVEPANEL_nextId++;
  this.frameId_ = "iframe_panel_" + this.id_;
  this.eventListeners_ = {};
  this.init_ = !1;
  if(a) {
    if(typeof a == "string") {
      this.uiConfig_ = DEFAULT_CONFIG, this.waveRootUrl = a
    }else {
      this.uiConfig_ = {};
      this.waveRootUrl = a.rootUrl || "https://wave.google.com/a/wavesandbox.com/";
      for(var b in DEFAULT_CONFIG) {
        this.uiConfig_[b] = a[b] || DEFAULT_CONFIG[b]
      }
    }
  }else {
    this.uiConfig_ = DEFAULT_CONFIG, this.waveRootUrl = "https://wave.google.com/a/wavesandbox.com/"
  }
}
WavePanel.prototype.getId = function() {
  return this.id_
};
WavePanel.prototype.init = function(a, b) {
  this.init_ = !0;
  if(this.initWaveId_ && this.initSearch_) {
    throw"Both an initial wave ID and a search were specified";
  }
  navigator && navigator.userAgent.indexOf("MSIE") != -1 ? a.innerHTML = "<div style='font-family: sans-serif; padding: 8px'>Google Wave does not yet support embedding waves in Internet Explorer. <p>Please open this webpage in <a href='http://google.com/chrome'>Google Chrome</a> or <a href='http://mozilla.org/firefox'>Firefox</a> to view the wave.</div>" : (this.setupRpc_(b), this.createFrame_(a), gadgets.rpc.setRelayUrl(this.getFrameId(), this.getRelayUrl_(), !1), delete this.initWaveId_)
};
WavePanel.rpcSetup_ = !1;
WavePanel.callbackQueue_ = {};
WavePanel.nextCallbackId = 0;
WavePanel.pushCallback_ = function(a) {
  if(!a) {
    return""
  }
  var b = "" + WavePanel.nextCallbackId;
  WavePanel.nextCallbackId++;
  WavePanel.callbackQueue_[b] = a;
  return b
};
WavePanel.popCallback_ = function(a) {
  if(a == "") {
    return null
  }
  var b = WavePanel.callbackQueue_[a];
  delete WavePanel.callbackQueue_[a];
  return b
};
WavePanel.prototype.setupRpc_ = function(a) {
  if(!this.rpcSetup_) {
    this.rpcSetup_ = !0;
    var b = this;
    gadgets.rpc.register("load_done", function() {
      a && a()
    });
    var c = function(a) {
      var b = WavePanel.popCallback_(a.a[0]);
      b && b(a.a[1])
    };
    gadgets.rpc.register("load_wave_done", function() {
      c(this)
    });
    gadgets.rpc.register("digest_search_done", function() {
      c(this)
    });
    gadgets.rpc.register("request_profiles", function() {
      if(!b.profileProvider_) {
        throw"Got a profiles request but no profile provider is set.";
      }
      b.profileProvider_(this.a[0])
    });
    gadgets.rpc.registerDefault(function() {
      var a = this.s, c = this.a[0];
      if(a = b.eventListeners_[a]) {
        for(var e in a) {
          a[e](c)
        }
      }
    })
  }
};
WavePanel.prototype.createFrame_ = function(a) {
  var b = document.createElement("div");
  b.innerHTML = '<iframe name="' + this.frameId_ + '" >';
  b = b.firstChild;
  b.id = this.frameId_;
  b.width = "100%";
  b.height = "100%";
  b.frameBorder = "no";
  b.scrolling = "no";
  b.marginHeight = 0;
  b.marginWidth = 0;
  b.className = "embed-iframe";
  a = a || document.body;
  a.appendChild(b);
  this.uiConfig_.width = a.clientWidth;
  this.uiConfig_.height = a.clientHeight - 80;
  b.src = this.iframeUrl_();
  return b
};
WavePanel.prototype.iframeUrl_ = function() {
  var a = [];
  a.push("client.type=embedded");
  a.push("parent=" + escape(window.location.protocol + "//" + window.location.host + window.location.pathname));
  this.authToken_ && a.push("auth=" + encodeURIComponent(this.authToken_));
  this.initWaveId_ && a.push("wave_id=" + encodeURIComponent(this.initWaveId_));
  if(this.uiConfig_) {
    var b = this.uiConfig_;
    a.push("bgcolor=" + encodeURIComponent(b.bgcolor));
    a.push("color=" + encodeURIComponent(b.color));
    a.push("font=" + encodeURIComponent(b.font));
    a.push("fontsize=" + encodeURIComponent(b.fontSize));
    a.push("embed_header=" + b.header);
    a.push("embed_footer=" + b.footer);
    a.push("embed_toolbar=" + b.toolbar);
    a.push("width=" + b.width + "px");
    a.push("height=" + b.height + "px")
  }
  return this.waveRootUrl + "?" + a.join("&")
};
WavePanel.prototype.addListener = function(a, b) {
  var c = this.eventListeners_, d = c[a];
  d || (d = [], c[a] = d);
  d.push(b)
};
WavePanel.prototype.getFrameId = function() {
  return this.frameId_
};
WavePanel.prototype.getRelayUrl_ = function() {
  var a = this.waveRootUrl, b = a.split("://");
  if(b && b.length > 0) {
    for(var c = b[b.length - 1], a = b[0] + "://", b = 0;b < c.length;++b) {
      if(c[b] == "/") {
        break
      }
      a += c[b]
    }
    a += "/"
  }
  return a + "gadgets/files/container/rpc_relay.html"
};
WavePanel.prototype.loadWave = function(a, b) {
  if(this.init_) {
    var c = WavePanel.pushCallback_(b);
    gadgets.rpc.call(this.getFrameId(), "load_wave", null, c, a)
  }else {
    this.initWaveId_ = a
  }
};
WavePanel.prototype.setAuthToken = function(a) {
  this.authToken_ = a
};
WavePanel.prototype.setEditMode = function(a) {
  if(!this.init_) {
    throw"Init not called.";
  }
  a = a ? "true" : "false";
  gadgets.rpc.call(this.getFrameId(), "set_edit_mode", null, a)
};
WavePanel.prototype.setUIConfig = function(a, b, c, d) {
  if(this.init_) {
    throw"Cannot change the UIConfig after Init has been called.";
  }else {
    this.uiConfig_.bgcolor = a, this.uiConfig_.color = b, this.uiConfig_.font = c, this.uiConfig_.fontSize = d
  }
};


