/* Changes:
    1.07    Fixed comma formatting
            Added pause button and version check
*/
javascript:void(//)
function () {
    var version='FreeBank 1.07',
        wait=0,
        xmlHTTP,
        amount=9,
        bank_url='http://apps.facebook.com/inthemafia/remote/html_server.php?xw_controller=bank&xw_action=deposit&amount=',
        last_url=null,
        resume_msg,
        s, m, d, uc,
        deposited=0,
        balance=0,
        dollarsign='$',
        first=true,
        retries=0,
        content=$('app10979261223_content_row'),
        mwtools_div=$('mwtools');
    if (!mwtools_div) {
        mwtools_div=document.createElement('div');
        mwtools_div.setAttribute('id','mwtools');
        mwtools_div.innerHTML='';
        content.insertBefore(mwtools_div,content.firstChild);
    }
    mwtools_div.innerHTML=
        '<style type="text/css">'+
            '.messages img{margin:0 3px;vertical-align:top}'+
            '.messages iframe{margin:0 3px;border:0}'+
        '</style>'+
        '<table class="messages">'+
            '<tr>'+
                '<td align="center"><a href="http://vern.com/mwtools/">Vern\'s MW Tools</a></td>'+
                '<td class="title">'+version+'</td>'+
                '<td style="text-align:right;font-size:0.8em">'+
                    '<iframe src="http://vern.com/mwtools/ver/?'+version+'" width="250" height="15" scrolling="no" allowtransparency="true"></iframe>'+
                    '<a href="#" id="pa"><img src="http://vern.com/mwtools/pa.gif" title="Pause" width="14" height="14"></a>'+
                    '<a href="#" id="pl"><img src="http://vern.com/mwtools/pl.gif" title="Play" width="14" height="14"></a>'+
                    '<a href="#" id="cl"><img src="http://vern.com/mwtools/cl.gif" title="Close" width="14" height="14"></a>'+
                    '<form name="mwtform">'+
                        '<input type="hidden" name="go" id="go" value="1">'+
                    '</form>'+
                '<td>'+
            '</tr>'+
            '<tr>'+
                '<td width="20%">Deposited:</td>'+
                '<td id="deposited"></td>'+
            '</tr>'+
            '<tr>'+
                '<td>Bank Balance:</td>'+
                '<td id="balance"></td>'+
            '</tr>'+
            '<tr>'+
                '<td>Status:</td>'+
                '<td id="status" colspan="2"></td>'+
            '</tr>'+
        '</table>';
    function get_xmlHTTP() {
        if(window.XMLHttpRequest)
            return new XMLHttpRequest();
        if(window.ActiveXObject)
            return new ActiveXObject('Microsoft.XMLHTTP');
        return null;
    }
    function request(url) {
        function f() {
            if (document.mwtform.go.value == 1) {
                xmlHTTP.onreadystatechange=state_change;
                xmlHTTP.open('GET',url,true);
                xmlHTTP.send(null);
            } else {
                resume_msg=$('status').innerHTML;
                status('Paused');
            }
            last_url=url;
        }
        if (first || wait == 0) {
            first=false;
            f();
        } else {
            status('Waiting for '+wait+' seconds...');
            setTimeout(f,wait*1000);
        }
    }
    $("pl").style.display = 'none';
    $("pl").onclick=function(e) { // Play
        document.mwtform.go.value = 1;
        $("pl").style.display = 'none';
        $("pa").style.display = 'inline';
        status('Resuming: '+resume_msg);
        request(last_url);
        return false;
    }
    $("pa").onclick=function(e) { // Pause
        document.mwtform.go.value = 0;
        $("pa").style.display = 'none';
        $("pl").style.display = 'inline';
        return false;
    }
    $("cl").onclick=function(e) { // Close
        delete xmlHTTP['onreadystatechange'];
        $('app10979261223_content_row').removeChild($("mwtools"));
        return false;
    }
    function commas(s) {
        while (d=/(\d+)(\d{3}.*)/.exec(s)) {
            s = d[1] + ',' + d[2];
        }
        return s;
    }
    function status(s) {
        $('deposited').innerHTML=dollarsign+commas(deposited);
        $('balance').innerHTML=balance;
        $('status').innerHTML=s;
    }
    function retry(s) {
        if(retries>9) {
            status(s+'; not retrying any more.');
        } else {
            setTimeout(function() {
                retries++;
                status(s+'; retry #'+retries+'...');
                request(last_url);
            }, 3000);
        }
    }
    function state_change() {
        if(xmlHTTP.readyState==4) {
            if(xmlHTTP.status==200) {
                s=xmlHTTP.responseText;
                if(m=/app10979261223_user_cash[^>]*>([^<]*)</.exec(s)) {
                    uc=$('app10979261223_user_cash_nyc');
                    if (uc) uc.innerHTML=m[1];
                    uc=$('app10979261223_user_cash_cuba');
                    if (uc) uc.innerHTML=m[1];
                }
                if(m=/Account Balance: <[^>]*>([^<]*)</.exec(s)) {
                    balance=m[1];
                    if (balance.substring(0,1) == "C") {
                        dollarsign = "C$";
                    }
                }
                if(m=/\$(.+) was deposited/.exec(s)) {
                    deposited+=parseInt(m[1]);
                    status('Depositing...');
                    request(bank_url+amount);
                } else if(/app10979261223_user_cash[^>]*>C?\$0</.test(s)) {
                    status('Done depositing.');
                    return;
                } else if(/You don.t have enough money/.test(s)) {
                    if (amount>1) {
                        status('Reducing amount...');
                        amount-=1;
                        request(bank_url+amount);
                    } else {
                        status('Finished depositing.');
                        return;
                    }
                } else {
                    retry('Unknown response');
                    return;
                }
                retries=0;
            } else {
                retry('Problem retrieving XML data');
                return;
            }
        }
    }
    xmlHTTP=get_xmlHTTP();
    if(!xmlHTTP) {
        alert('Your browser does not support XMLHTTP.');
        return;
    }
    status('Trying first deposit...');
    request(bank_url+amount);
}()
);
