var BYPASS_REDIRECT = false;

var widget2nets = {"irc.mibbit.com":1,
                   "irc.mibbit.net":1,
                   "webirc.gamesurge.net:6007":1,
                   "irc.twit.tv":1,
                   "irc.barafranca.com":1,
                   "webchat.irchighway.net":1};

// What shall we exclude?
var excludeIDs = {
"5263646d44a3e9f3490c02c14ce1fb99":1,
"02aa0dd78d194f35fa406182b7561227":1,
"47ae0b68eefb61910d132dda0e07d422":1,
"80e49f805cec00055cd55e64fc9e7fd0":1,
"1ca65222083650aad0392a56c512e546":1,
"37fdfae242b75baff4b1eb1c69c229ae":1,
"d613bbccb82d5b272cf0553c3768d9f5":1,
"b480f2e652a182a2ca138b971a101aec":1,
"1b02d691fcc53aa85307546b51b6f120":1,
"7d520a439a076a8b8c5f0b02cb2a392f":1,

"01fecd4908889cb8b7ef3cfc64afca59":1,
"60a8b9627e2de301847275e920894328":1,
"13499c74bdf9cfa41b68a69704fdb479":1,
"5e2d3666747b26b1570313c1a77123fa":1,
"1d9c1fc433373b006bd1df0a474a2ca4":1,
"877bcc546fb8913663c8f06ecb8c3b71":1,
"3af6f9d8dc4069476c0b324b74bb5fbc":1,
"0bc732457ddacdde2d252dcb6b8dd63d":1,
"bfa9cc793e103ca867a58b8a01799130":1,
"b7e5bf5fce71fa4691b9c58f3f55300d":1,

"57068b0d19df842b174a0ffbdbf1b90f":1,
"6c9968db698a8df83efa13f66f016c61":1,
"a96520ce4e669856b9b360e23d76bf40":1,
"ecca6386ce250fc7d4ef4e641c2ee86a":1,
"d9081747dbbe58a9b9ec930e54d95dd5":1,
"50a135edefd4fb0ad3fa1b5e5f7be5e9":1,
"cdbcbe79250e821d4d48bc11a514f153":1,
"88844e5557266e9ab4709caab0827038":1,
"2ad7127ef89b0c4a366b371dc521018c":1,
"a89d21d92145e729bbdf0bba0f08cfdf":1

};

/**
 * Return true if we can use widget2
 *
 */
function onMibbitIRC() {
    if (window.location.protocol=="https:") return false;       // widget2 can't do https atm

    try {
        if (!window.location.search) return true;               // No args? :/ ok, use widget2
        
        var ss = window.location.search.substring(1);
        var args = ss.split("&");

        for (var i=0;i<args.length;i++) {
            // Check it's ok...
            var key = args[i];
            if (key=="bypassredirect") BYPASS_REDIRECT = true;  // Bypass redirect
            var val = "";
            var f = key.indexOf("=");
            if (f!=-1) {
                val = decodeURIComponent(key.substr(f+1, key.length));
                key = key.substr(0, f);
            }
            if (key=="server" && !widget2nets[val.toLowerCase()]) return false;     // If it's a server not listed, then NO
            if (key=="settings" && excludeIDs[val.toLowerCase()]) {
                BYPASS_REDIRECT = true;                         // Bypass
            }
        }
    } catch(e) {
        return false;
    }
    return true;                                                // Sure, lets use widget2 then
}

if (window.location.host!="localhost:8080") {       // For testing...
    if (onMibbitIRC()) {
        if (!BYPASS_REDIRECT) {
            if (window.location.host=="widget.mibbit.com" || window.location.host=="embed.mibbit.com") {
                window.location = "http://widget2.mibbit.com/" + window.location.search + "&ref=" + document.referrer;
            }
        }
    } else {
        if (!BYPASS_REDIRECT) {
            if (window.location.host=="widget2.mibbit.com") {
                window.location = "http://widget.mibbit.com/" + window.location.search + "&ref=" + document.referrer;
            }
        }
    }
}
