if (typeof BITWINE == "undefined" || !BITWINE) { var BITWINE = {}; }

BITWINE.api = function() {
  var onload_sinkers = [];

  function inject_chat_post_iframe() {
    var e = document.createElement("iframe");
    e.setAttribute("id", generate_uuid("chat_post_iframe"));
    try {
      e.frameBorder = 0;
    } catch(e) {}
    try {
      e.allowTransparency = true;
    } catch(e) {}
    document.body.appendChild(e);
    e.src = "http://bitwine.com/chats/post_iframe" + "?connector=" + BITWINE.api.connector_name;
    e.style.cssText = "position:fixed;top:0px;right:0px;height:10px;width:10px;background:transparent;overflow:hidden;border:none";
  }
  
  function run_onload_sinkers() {
    for (var i=0; i<onload_sinkers.length; i++) {
      onload_sinkers[i]();
    }
  }
  
  
  function api_onload() {
    if (!document.getElementById("bitwine_connector")) {
      if (BITWINE.api.allow_debug) {
        alert("BitWine JS API error: BITWINE.api.render_no_flash_div() was not called on this page");
      }
      return;
    }

    BITWINE.connector("bitwine.com", "bitwine_connector", 
      BITWINE.api.connector_name, 
      function(connector) {
        connector.subscribe("hide_popup", function(opts) {
          BITWINE.popups.hide(opts.name, opts.parameters);
        });
        connector.subscribe("chat:starting", function(opts) {
          BITWINE.api.chat && BITWINE.api.chat.on_starting(opts);
        });
        connector.subscribe("chat:failed", function(opts) {
          BITWINE.api.chat && BITWINE.api.chat.on_failed(opts);
        });
        connector.subscribe("chat:post_iframe_ready", function(opts) {
          BITWINE.api.chat_post_connector = opts.chat_post_connector;
          run_onload_sinkers();
        });
        connector.subscribe("leave_message:show", function(opts) {
          BITWINE.api.leave_message && BITWINE.api.leave_message.show(opts);
        });
        connector.subscribe("appointment:show", function(url){
          BITWINE.popups.show("appointment", url);
        });
        connector.subscribe("users:set_chat_status", function(opts){
          BITWINE.api.buttons && BITWINE.api.buttons.on_users_chat_status(opts);
        });

        inject_chat_post_iframe();
      }, 
      function(error_message) {
        if (BITWINE.api.allow_debug) {
          alert("BitWine JS API error while embedding connector + " + error_message);
        }
      }
    );
  }
  
  if (window.addEventListener) {
    window.addEventListener("load", api_onload, false);
  }
  else if (window.attachEvent) {
    window.attachEvent("onload", api_onload, false);
  }

  function generate_uuid(prefix) {
    prefix = prefix || "";
    return prefix + (new Date()).getTime() + "_" + (Math.random() * 100000000000000000);
  }


  return {
    uuid: function(prefix) {
      return generate_uuid(prefix);
    },
    render_no_flash_div: function(opts) {
      document.write('<div id="bitwine_connector" style="position:fixed;top:0;left:0;width:10px;height:10px;background:transparent;"></div>');
    },
    host: "http://bitwine.com",
    connector_name: generate_uuid("main_connector"),
    new_poll_vote_url: "http://bitwine.com/poll_votes/new",
    verify_params: function(opts, params) {
      if (typeof opts == "undefined" || !opts) {
        return false;
      }
      for (var i=0; i<params.length; i++) {
        if (typeof opts[params[i]] == "undefined" || !opts[params[i]]) {
          return false;
        }
        return true;
      }
    },
    on_available: function(func) {
      onload_sinkers.push(func);
    },
    start: function(opts) {
      if (!BITWINE.api.verify_params(opts, ["auid"])) {
        if (BITWINE.api.allow_debug) {
          alert("BitWine JS API error: No opts.auid passed to BITWINE.api.start");
        }
        return false;
      }
      BITWINE.api.auid = opts.auid;
      return true;
    }
  };

}();

document.write("<script src=\"http://bitwine.com/javascripts/js_api_281949768489412648962353822266799178366.js\" type=\"text/javascript\"></script>");
