
var memOption = newObject({
    aPrice    : null,
    nDiscount : null,
    nRest     : null,
    cur_label : null,

    opt_formula : null,
    opt_sum     : null,

    benefit     : null,

    init : function(aPrice, nDiscount, nRest)
    {
        this.aPrice    = aPrice;
        this.nDiscount = nDiscount;
        this.nRest     = nRest;
        this.$w0.createStyle("div." + this.config.isJsClass, "display: block;");
        this.$w0.createStyle(this.config.bnf_block, "display: none;");
    },

    onready : function()
    {
        var conf, inp, i;
        conf = this.config;

        this.$(conf.opt_form).elm.action = conf.redir_url;

        inp = this.$$(conf.opt_form + " " + conf.opt_input);
        if (inp) {
            inp.addListener(this, 'onclick', 'onSelect');

            this.opt_chose   = this.$(conf.opt_chose);
            this.opt_formula = this.$(conf.opt_formula);
            this.opt_sum     = this.$(conf.opt_sum);

            for (i = 0; i < inp.length; i++) {
                if (inp[i].elm.checked) {
                    this._showChose(inp[i]);
                }
            }
        }

        // Control of internal
        this.benefit = this.$(conf.bnf_block);
        this.benefit.addClass("intWin");
        this.benefit.addListener(this, 'onclick', 'onClickBnft');
        this.$(conf.bnf_call).addListener(this, 'onclick', 'onCallBnft');
        this.$(conf.bnf_close).addListener(this, 'onclick', 'onCloseBnft', 1);
        this.$w0.addListener(this, 'onclick', 'onCloseBnft', 0);
    },

    onSelect : function(evtWr)
    {
        this._showChose(evtWr.elmWr);
    },

    onClickBnft : function(evtWr)
    {
        evtWr.stopBubbling();
    },
    onCallBnft : function(evtWr)
    {
        evtWr.eventDrop();
        evtWr.stopBubbling();
        this.benefit.show();
        this.benefit.setAbsLeft((this.$w0.getWindowWidth() - this.benefit.getWidth())/2);
    },

    onCloseBnft : function(evtWr, isBut)
    {
        if (isBut) {
            evtWr.eventDrop();
        }
        this.benefit.hide();
    },

    _showChose : function(elmWr)
    {
        var chCl, formula, sum, tsum;
        chCl = this.config.choseClass;
        if (this.cur_label) {
            this.cur_label.removeClass(chCl);
        }
        this.cur_label = elmWr.getPreviousSibling();
        this.cur_label.addClass(chCl);

        sum = this.aPrice[elmWr.elm.value];
        tsum = 'ВЈ' + this._priceFormat((sum - this.nRest) * (1 - this.nDiscount), 2);

        if (this.nRest > 0 || this.nDiscount > 0) {
            formula = 'ВЈ' + this._priceFormat(sum, 2) + '<sup>1</sup>';
            if (this.nRest) {
                formula += ' - ВЈ' + this._priceFormat(this.nRest, 2) + '<sup>2</sup>';
                if (this.nDiscount) {
                    formula = '(' + formula + ')';
                }
            }
            if (this.nDiscount) {
                formula += ' - ВЈ' + this._priceFormat((sum - this.nRest) * this.nDiscount, 2) + '<sup>' + (this.nRest ? 3 : 2) + '</sup>';
            }
        } else {
            formula = '';
        }

        this.opt_chose.write(this.cur_label.elm.title);
        this.opt_formula.elm.innerHTML = formula;
        this.opt_sum.write(tsum);
    },

    _priceFormat : function(txt, fl)
    {
        var intp0, intp1, fractp, a, l, n, i;
        fractp = ".";
        for (i = 0; i < fl; i++) {
            fractp += "0";
        }
        if (!txt || txt == 0) {
            return '0' + fractp;
        }
        txt = parseInt(txt * Math.pow(10, fl) + 0.000001).toString();
        a = /^([0-9\-]+)/.exec(txt);
        if (a) {
            l = a[1].length - fl;
            intp0 = l < 1 ? '0' : a[1].substr(0, l);
            fractp = "." + a[1].substr(l);
            for (i = fl + 1 - fractp.length; i > 0; i--) {
                fractp += "0";
            }
        } else {
            intp0 = 0;
        }
        intp1 = "";
        l = intp0.length;
        for (i = 0; i < l; i += 3) {
            n = l - i;
            intp1 = intp0.substring((n < 3 ? 0 : n - 3), n) + " " + intp1;
        }
        return intp1.substring(0, intp1.length - 1) + fractp;
    },

    config : {
        "redir_url"   : "/pay_start.php",
        "opt_form"    : "#payment_select",
        "opt_input"   : "input[name=key]",
        "opt_chose"   : "#opt_chose",
        "opt_formula" : "#opt_formula",
        "opt_sum"     : "#opt_sum",
        "bnf_block"   : "#membership_bl",
        "bnf_call"    : "#opt_benefits a",
        "bnf_close"   : "#membership_bl div.intWinClose",
        "choseClass"  : "chosen",
        "isJsClass"   : "isJS"
    }
});
