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