Payments.prototype._init = function() {
    this.payments = {};
    this.newestPayment = false;
}


Payments.prototype.newPayment = function(inProcessor, inAmount, inReason, inOnStatus) {
    this.rmi_newPayment(inProcessor, inAmount, inReason, function(e) {
        pymt.getUpdatedPayment_cb(e, inOnStatus);
    });
}

Payments.prototype.newPaymentFromUI = function(inProcessor) {
    //$.log(document.getElementById("account_panel__transaction_amount").value, true, "account_panel__transaction_amount");
    this.newPayment(inProcessor, $("#account_panel__transaction_amount").val(), "add credit", {
        needstoken:function() {
            //var winObj = window.open("/_ext/payments/" + this.processor + "/authorize.php?transaction__id=" + this.id, "paymentProcessor", "location=0,status=0,width=647,height=500,scrollbars=1");
            var winObj = ui.window("externalPayments", {url:"/_ext/payments/" + this.processor + "/authorize.php?transaction__id=" + this.id});
            
            if(winObj) {
                $("#modalContent").snippet("modal__wait", {
                    messageBelow:"Please complete your payment in the popup window."

                });
                $("#modalTopRight").snippet("modal__top_right", {});
            }

        },
        completed:function(){
            ui.modalSetContent({modalTitle:"Payment processed",
                        content:$.snippet("modal__message",  {
                            messageAbove:"Thank you.",
                            messageBelow:"Your payment has been processed.<br><br>Any letters in your outbox will be updated shortly."})
                    });
            setTimeout(function() {lman.searchSent();}, 5000);
            ui.modalClose(3000);
        },
        pending:function(){
            ui.modalSetContent({modalTitle:"Payment processed",
                        content:$.snippet("modal__message",  {
                            messageAbove:"Thank you.",
                            messageBelow:"Your payment is being processed.<br><br>Any letters in your outbox will be updated shortly."})
                    });
            setTimeout(function() {lman.searchSent();}, 5000);
            ui.modalClose(3000);
        },
        failed:function() {
            alert("Your transaction did not complete successfully\n");
            ui.modalSetContent({modalTitle:"Oops!",
                        content:$.snippet("modal__message",  {
                            messageAbove:"We were not able to process your payment.<br>The error returned by " + inProcessor + " was " + this.processor_transaction_status,
                            messageBelow:"Please close this message and try again."})
                    });
            //ui.modalSetContent("Your transaction did not complete successfully<br><br>);
        }
    });
}


Payments.prototype.getUpdatedPayment = function(inPaymentID) {
    this.rmi_getUpdatedPayment(inPaymentID);
}


Payments.prototype.getUpdatedPayment_cb = function(e, inOnStatus) {
    if(!ui.ro(e)) return;
    if(!e.data.hasOwnProperty("onStatus")) e.data.onStatus = {};
    //alert(this.payments);
    //map the currently defined onStatus actions
    if(this.payments.hasOwnProperty(e.data.id)) {
        for(var i in this.payments[e.data.id].onStatus) if(this.payments[e.data.id].onStatus.hasOwnProperty(i)) {
            e.data.onStatus[i] = this.payments[e.data.id].onStatus[i];
        }
    }

    //map any new onStatus actions
    if(typeof inOnStatus == "object") {
        for(var i in inOnStatus) if(inOnStatus.hasOwnProperty(i)) {
            e.data.onStatus[i] = inOnStatus[i];
        }
    }

    this.payments[e.data.id] = e.data;

    //
    if(e.data.onStatus.hasOwnProperty(e.data.status)) {
        switch(typeof e.data.onStatus[e.data.status]) {
            case "function" :
                e.data.onStatus[e.data.status].call(e.data);
                break;
            case "string" :
                eval(e.data.onStatus[e.data.status]);
                break;
        }
    }
}


/**
 * Currently unused
 */
Payments.prototype.execProperty = function(inObj, inProperty, inDefault) {
    if(inObj.hasOwnProperty(inProperty)) {
        switch(typeof inObj[inProperty]) {
            case "function" :
                inObj[inProperty]();
                break;
            case "string" :
                eval(inObj[inProperty]);
                break;
        }
    } else {
        switch(typeof inDefault) {
            case "function" :
                inDefault();
                break;
            case "string" :
                eval(inDefault);
                break;
        }
    }
}

