﻿function NroTool(   nro_check, 
                    nro_box,
                    calc_box,
                    show_calc_btn, 
                    freq_input, 
                    prod_price, 
                    prod_qty ) {
                    
    this.nro_check = nro_check;
    this.nro_box = nro_box;
    this.calc_box = calc_box;
    this.show_calc_btn = show_calc_btn;
    this.freq_input = freq_input;
    this.prod_price = prod_price;
    this.prod_qty = prod_qty;
    this.invert_mode = false;
    
    this.toggleNroBox = function() {
        if (this.nro_box.css("display") == "none") {
            this.nro_box.slideDown(500);
        } else {
            this.nro_box.slideUp(500);
        }   
    }
    
    this.openNroBox = function(state) {
        if (state) {
            this.nro_box.show();
        } else {
            this.nro_box.hide();
        }
    }

    this.showNroCalculator = function() {
        modalOn(true);
        
        var calculator = this.calc_box;
        var nro_freq = this.freq_input;
        var nro_freq_pos = nro_freq.offset();
        nro_freq_pos.left += nro_freq.outerWidth() / 2
        var calc_width = calculator.outerWidth();
        
        var nro_freq_scrolled_pos = nro_freq_pos.top - $(window).scrollTop();
        var win_height = $(window).height();
        var offset_top;
        var offset_left = nro_freq_pos.left - (calc_width / 2)
        
        if (nro_freq_scrolled_pos < (win_height / 2)) {
            // msg below input
            offset_top = nro_freq_pos.top + nro_freq.height()
            calculator.find(".pd_nro_calculator_top_invert").addClass("pd_nro_calculator_top").removeClass("pd_nro_calculator_top_invert");
            calculator.find(".pd_nro_calculator_bottom_invert").addClass("pd_nro_calculator_bottom").removeClass("pd_nro_calculator_bottom_invert");
            this.invert_mode = false;
        } else {
            // msg above input
            offset_top = nro_freq_pos.top - calculator.height() - nro_freq.height();
            calculator.find(".pd_nro_calculator_top").addClass("pd_nro_calculator_top_invert").removeClass("pd_nro_calculator_top");
            calculator.find(".pd_nro_calculator_bottom").addClass("pd_nro_calculator_bottom_invert").removeClass("pd_nro_calculator_bottom");
            this.invert_mode = true;
        }
        
        calculator.find(".pd_nro_calc_caseqty").eq(0).text(this.prod_qty);
        calculator.css("top", offset_top);
        calculator.css("left", offset_left);
        
        calculator.show();
    }

    this.cancelNroCalculator = function() {
        this.calc_box.find(".pcsperday").val("")
        this.calc_box.hide();
        this.calc_box.find(".pd_nro_calculator_results").hide()
        this.calc_box.find(".pd_nro_calculator_question").show();
        modalOn(false);
    }

    this.doCalculation = function() {
        var res = this.calc_box.find(".pd_nro_calculator_results");
        var pcsperday = this.calc_box.find(".pcsperday").val();
        if (pcsperday.trim() == "" || isNaN(pcsperday) || pcsperday == 0) {
            return;
        }
        res.find(".delivery_freq").text(Math.round(this.prod_qty / pcsperday) + " ");
        res.find(".immediate_savings").text("$" + (8 > this.prod_price * 0.05 ? 8 : this.prod_price * 0.05).toFixed(2) + " ");
        res.find(".future_savings").text("$" + (this.prod_price * 0.05).toFixed(2) + " ");
        res.find(".immediate_cost").text("$" + ((this.prod_price * 0.95) < (this.prod_price - 8) ? (this.prod_price * 0.95) : (this.prod_price - 8)).toFixed(2) + " ");
        res.find(".future_cost").text("$" + (this.prod_price * 0.95).toFixed(2) + " ");
        
        this.calc_box.find(".pd_nro_calculator_question").hide();
        
        if (!this.invert_mode) {
            res.show()
        } else {
            res.show();
            var top = this.freq_input.offset().top - this.calc_box.height() - this.freq_input.height();
            this.calc_box.css("top", top);
        }
    }

    this.submitNroCalculator = function() {
        this.freq_input.val(this.calc_box.find(".delivery_freq").text());
        this.cancelNroCalculator();
    }
    
    /** Constructor */
    this.nro_check.bind("click", {self: this}, 
        function(event) { event.data.self.toggleNroBox(); }
    );
    
    this.show_calc_btn.bind("click", {self: this},
        function(event) { event.data.self.showNroCalculator(); }
    );
    this.calc_box.find(".cancel_nro_results_btn").bind("click", {self: this},
        function(event) { event.data.self.cancelNroCalculator(); }
    );
    this.calc_box.find(".do_nro_calculations_btn").bind("click", {self: this},
        function(event) { event.data.self.doCalculation(); }
    );
    this.calc_box.find(".submit_nro_results_btn").bind("click", {self: this},
        function(event) { event.data.self.submitNroCalculator(); }
    );
}

function showLearnMore() {
    showModalMessage($("#pd_nro_learnmore"), 400);
}