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>");


BITWINE.api.buttons = function(){
  
  var buttons = [];
  var please_wait_image = BITWINE.api.host + "/images/chat2/loading.gif";
  var chatme_offline_image = BITWINE.api.host + "/images/profile/chatme_offline.gif"
  var chatme_image = BITWINE.api.host + "/images/profile/chatme.gif"
  
  function add_button_div(opts) {
    if (!BITWINE.api.verify_params(opts, ["user_id"])) {
      if (BITWINE.api.allow_debug) {
        alert("BitWine JS API error: No opts.user_id passed to BITWINE.api.buttons.render_" + opts.type);
      }
      return false;
    }
    if (!opts.div_id) {
      opts.div_id = BITWINE.api.uuid(opts.type + opts.user_id);
      document.write('<div style="display:inline-block;padding:0 !important;margin:0 !important;" id="' + opts.div_id + '"></div>');
    }
    buttons.push(opts);
    return true;
  }
  
  function create_please_wait_div(div_id) {
    var div = document.createElement("div");
    div.setAttribute("id", div_id);
    div.style.display = "none";
    div.style.fontSize = "0.8em";
    div.style.fontWeight = "normal";
    div.style.paddingLeft = "8px";
    div.style.verticalAlign = "middle";
    div.innerHTML = '<img src="' + please_wait_image + '" style="border:0" />Please wait...';
    return div;
  }
  
  function set_container_css(div, double_size) {
    div.style.fontSize = "14px";
    div.style.width = double_size ? "256px" : "118px";
    div.style.marginLeft = "auto";
    div.style.marginRight = "auto";
    div.style.padding = "2px 0";
    div.style.fontFamily = "Arial";
    div.style.fontWeight = "bold";
  }
  
  function leave_message_button_html(indx) {
    return '<a href="javascript:void(0)" style="display:block;height:34px;padding:14px 0 0 0;color:#009900;" onclick="BITWINE.api.buttons.onclick('+indx+', \'message\');return false;">Leave a Message</a>';
  }
  
  function render_leave_message(indx, opts) {
    opts.please_wait_div_id = BITWINE.api.uuid("wait_" + indx);
    set_container_css(opts.div);
    opts.div.innerHTML = leave_message_button_html(indx);
    opts.div.appendChild(create_please_wait_div(opts.please_wait_div_id));
  }
  
  function chat_button_html(indx, ready_to_accept_chat) {
    if (ready_to_accept_chat) {
      var res = '<a href="javascript:void(0)" style="display:block;height:34px;padding:14px 0 0 46px;background:url('+chatme_image+') top left no-repeat;color:#009900;text-decoration:underline;" onclick="BITWINE.api.buttons.onclick(' + indx + ', \'chat\');return false;">Start Chat</a>';
      return res;
    }
    return '<div style="height:34px;padding:13px 0 0 46px;background:url('+chatme_offline_image+') top left no-repeat;color:#999;decoration:none;vertical-align:middle;">Start Chat</div>';
  }
  
  function render_start_chat(indx, opts) {
    set_container_css(opts.div);
    opts.div.innerHTML = chat_button_html(indx, opts.ready_to_accept_chat);
    if (opts.ready_to_accept_chat) {
      opts.please_wait_div_id = BITWINE.api.uuid("wait_" + indx);
      opts.div.appendChild(create_please_wait_div(opts.please_wait_div_id));
    }
  }
  
  function render_chat_and_message(indx, opts) {
    set_container_css(opts.div, true);
    opts.div.innerHTML = '<div style="float:left;margin-right:19px">' + chat_button_html(indx, opts.ready_to_accept_chat) + "</div>" + leave_message_button_html(indx) + '<div style="clear:both;">&nbsp;</div>';
    if (opts.ready_to_accept_chat) {
      opts.div.appendChild(create_please_wait_div(opts.please_wait_div_id));
    }
  }
  
  function render(opts) {
    for (var i=0; i<buttons.length; i++) {
      buttons[i].div = document.getElementById(buttons[i].div_id);
      if (buttons[i].type.match(/chat/)) {
        buttons[i].ready_to_accept_chat = buttons[i].ready_to_accept_chat || opts[buttons[i].user_id];
      }
      switch (buttons[i].type) {
        case "start_chat":
          render_start_chat(i, buttons[i]);
          break;
        case "leave_message":
          render_leave_message(i, buttons[i]);
          break;
        case "chat_and_message":
          render_chat_and_message(i, buttons[i]);
          break;
      }
    }
  }

  BITWINE.api.on_available(function() {
    var uids = [];
    for (var i=0; i<buttons.length; i++) {
      if (buttons[i].type.match(/chat/) && (typeof buttons[i].ready_to_accept_chat == "undefined" || !buttons[i].ready_to_accept_chat)) {
        uids.push(buttons[i].user_id);
      }
    }
    if (0 == uids.length) {
      render({});
      return;
    }
    BITWINE.connectors[BITWINE.api.connector_name].send_message(BITWINE.api.chat_post_connector, "users:query_chat_status", uids);
  });
  
  function leave_a_message(opts) {
    opts.wait_div.style.display = "";
    BITWINE.api.leave_message.show({user_id: opts.user_id, on_window_opened:function(){
      opts.wait_div.style.display = "none";
    }});
  }

  function start_chat(opts) {
    opts.wait_div.style.display = "";
    BITWINE.api.chat.start({user_id: opts.user_id, 
      on_ringing: function(){
        opts.wait_div.style.display = "none";
      },
      on_chat_failed: function(reason) {
        opts.wait_div.style.display = "none";
        alert(reason);
      }
    });
  }
  
  return {
    on_users_chat_status: function(opts) {
      render(opts);
    },
    render_start_chat: function(opts) {
      opts.type = "start_chat";
      return add_button_div(opts);
    },
    render_leave_message: function(opts) {
      opts.type = "leave_message";
      return add_button_div(opts);
    },
    render_chat_and_message: function(opts) {
      opts.type = "chat_and_message";
      return add_button_div(opts);
    },
    onclick: function(button_id, button_type) {
      var b = buttons[button_id];
      b.wait_div = document.getElementById(buttons[button_id].please_wait_div_id);
      switch (b.type) {
        case "start_chat":
          start_chat(b);
          break;
        case "leave_message":
          leave_a_message(b);
          break;
        case "chat_and_message":
          if (button_type == 'message') {
            leave_a_message(b);
          }
          else if (button_type == 'chat') {
            start_chat(b);
          }
          break;
      }
    }
  };
  
}();
