// ==UserScript==
// @name           meebo all at once
// @namespace      meebo
// @description    Send IM to everyone in the IM list
// @include        http://www.meebo.com/
// ==/UserScript==

var w = unsafeWindow;
var buddyTimer;

/**************** CUSTOMIZATION OPTIONS ***************/

// If you change the options below, you must reload www.meebo.com for the 
// changes to take effect.

// true = Send IM to people in w.sendList
// false = Send IM to all people except to those in w.omitList
w.useSendList = false;
w.sendList = [ ];       // E.g. w.sendList = ["buddy1", "buddy2@gmail.com"];
w.omitList = [ ];       // E.g. w.omitList = ["buddy3", "buddy4@gmail.com"];

/**************** END OF CUSTOMIZATION OPTIONS ***************/

window.addEventListener("load", 
        function() {
            buddyTimer = setInterval(
                function() {
                    if (w.gBuddyList.m_buddies.length !== 0) {
                        clearInterval(buddyTimer);
                        startMusic();
                    } 
                }, 1000);
        }, false);

function startMusic() {
    addElements();
}

function addElements() {
    var two = document.getElementById("2");
    var twoleft = parseInt(two.style.left.substring(0, two.style.left.length-2), 10);
    var twotop = parseInt(two.style.top.substring(0, two.style.top.length-2), 10);
    var twotw = twoleft + two.offsetWidth;
    var twoth = twotop + two.offsetHeight + 50;

    var div = document.createElement("div");
    div.setAttribute("class", "meeboConsoleContainer");
    div.setAttribute("style", "display:block; left: 20px; top: " + twoth + "px;");
    div.innerHTML = '<textarea id="newim">hi</textarea> <input type="button" value="Send" id="sendMsgToAll" />';
    document.getElementById("ConsolePageMain").appendChild(div);

    document.getElementById("sendMsgToAll").addEventListener("click", sendMessages, false);
}

function sendMessages() {
    var len = w.gBuddyList.m_buddies.length;
    var sessionKey = getSessionKey();
    for (var i=0; i<len; i++) {
        var args = {};
        var buddy = w.gBuddyList.m_buddies.getByIndex(i);
        args.sender = buddy.m_logon.m_name;
        args.receiver = buddy.m_name;
        if (buddy.m_network == null) continue;
        args.protocol = buddy.m_network.m_id;
        args.clientId = "" + w.gIMGateway.getClientId();
        args.mt = "k";
        args.msg = document.getElementById("newim").value;
        args.sessionKey = getSessionKey();
        if (w.useSendList) {
            if (isNameInList(args.receiver, w.sendList)) {
                w.gNetworkMgr.doRequest("send", args);
            }
        } else {
            if (!isNameInList(args.receiver, w.omitList)) {
                w.gNetworkMgr.doRequest("send", args);
            }
        }
    }
    return false;
}

function isNameInList(name, list) {
    if (list.length === 0) {
        return w.useSendList ? true : false;
    }
    for (var i=0; i<list.length; i++) {
        if (name.indexOf(list[i]) === 0) {
            return true;
        }
    }
    return false;
}

function getSessionKey() {
    var cookies = w.document.cookie.split("; ");
    for (var i=0; i<cookies.length; i++) {
        if (cookies[i].indexOf("attachKey=") === 0) {
            return cookies[i].substring("attachKey=".length);
        }
    }
}

