if (typeof discover === 'undefined') {
	var discover = {};
}

/**
 * The Discover Bank Common Library
 *
 * @namespace discover
 */
discover.bank = function() {
    return {
        rolloverInit: function() {
            var rollover_images = mcd.dom.getElementsByAttribute("class", "rollover", document.body, "IMG", true);
            var rollover_inputs = mcd.dom.getElementsByAttribute("class", "rollover", document.body, "INPUT", true);

            var rollovers = rollover_images.concat(rollover_inputs);

            for (var i = 0; i < rollovers.length; i++) {
	            mcd.event.add(rollovers[i], "mouseover", mcd.util.rollOver);
	            mcd.event.add(rollovers[i], "mouseout", mcd.util.rollOut);
            }
        },

        /**
         * A wrapper for window.print that is called from the flash
         * calculators via externalInterface.
         *
         * Due to a bug in Firefox, Flash elements do not print using
         * normal methods. This is a workaround.
         *
         */
        printCalculator: function() {
            window.print();
        },

        /**
         * When the time period slider of the CD or MMA Flash calculators change,
         * bold the corresponding row in the rate chart.
         *
         * This function is called from the calculators via externalInterface
         */
        featureRateInChart: function(label, dfs_apr, dfs_apy, min_balance) {
            // The CD calculator will pass a label whose value matches the first
            // cell of the rate table. Since the MMA calculator displays balance ranges,
            // the apy and apr arguments will be used to identify the target row.

            var rate_chart = document.getElementById("rate-chart");
            var rows = rate_chart.getElementsByTagName("TR");

            while (rows.length < 4) {
                // The calculator has loaded, but the rate chart hasn't populated yet.
                // We'll setTimeout and try again in a bit.
                var callback = function() {
                    discover.bank.featureRateInChart(label, dfs_apy, dfs_apr, min_balance);
                };
                window.setTimeout(callback, 1000);
                return;
            }

            dfs_apr = (parseFloat(dfs_apr) * 100).toFixed(2) + "%";
            dfs_apy = (parseFloat(dfs_apy) * 100).toFixed(2) + "%";
	      var check ='0';	
            for (var i=0; i < rows.length; i++) {
		  if(rows[i].cells[0].innerHTML.replace(/[^\d]+/g, "").indexOf(min_balance) != -1 && check == '0')
		   {	mcd.dom.addClass(rows[i], "featured");
			check=1;
		   }
		  else if (rows[i].cells[0].innerHTML == label) {
                    mcd.dom.addClass(rows[i], "featured");
                }
		   else if (rows[i].cells[1].innerHTML == dfs_apr && rows[i].cells[2].innerHTML == dfs_apy) {
                    var balance = rows[i].cells[0].innerHTML.replace(/[^\d]+/g, "");
                    if (balance.indexOf(min_balance) > -1) {
                        mcd.dom.addClass(rows[i], "featured");
                    } else {
                        mcd.dom.removeClass(rows[i], "featured");
                    }
                } else {
                    mcd.dom.removeClass(rows[i], "featured");
                }
		  
            }
        }
    };
}();
