/**
 * This file contains extra helper functions for Handlebars.js.
 *
 * @see http://handlebarsjs.com/#helpers
 */

 /**
  * Improved comparison operator
  * See https://stackoverflow.com/a/16315366/2970321
  */
Handlebars.registerHelper('ifx', function (v1, operator, v2, options) {
    switch (operator) {
        case '==':
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
        case '===':
            return (v1 === v2) ? options.fn(this) : options.inverse(this);
        case '!=':
            return (v1 != v2) ? options.fn(this) : options.inverse(this);
        case '!==':
            return (v1 !== v2) ? options.fn(this) : options.inverse(this);
        case '<':
            return (v1 < v2) ? options.fn(this) : options.inverse(this);
        case '<=':
            return (v1 <= v2) ? options.fn(this) : options.inverse(this);
        case '>':
            return (v1 > v2) ? options.fn(this) : options.inverse(this);
        case '>=':
            return (v1 >= v2) ? options.fn(this) : options.inverse(this);
        case '&&':
            return (v1 && v2) ? options.fn(this) : options.inverse(this);
        case '||':
            return (v1 || v2) ? options.fn(this) : options.inverse(this);
        default:
            return (v1 == v2) ? options.fn(this) : options.inverse(this);
    }
});

/**
 * format an ISO date using Moment.js
 * 
 * moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
 * usage: {{dateFormat creation_date format="MMMM YYYY"}}
 * @requires momentjs http://momentjs.com/
 */
Handlebars.registerHelper('dateFormat', function(context, block) {
    if (window.moment) {
        var f = block.hash.format || "MMM Do, YYYY";
        return moment(context).format(f);
    } else {
        //  moment plugin not available. return data as is.
        console.log("The moment.js plugin is not loaded.  Please make sure you have included moment.js on this page.");
        return context;
    }
});

/**
 * Format a phone number.
 */
Handlebars.registerHelper("phoneUSFormat", function(phoneNumber) {
  if (typeof phoneNumber === 'undefined') {
    return '';
  }

  phoneNumber = phoneNumber.toString();
  return "(" + phoneNumber.substr(0,3) + ") " + phoneNumber.substr(3,3) + "-" + phoneNumber.substr(6,4);
});

/**
 * Format currency (USD).
 */
Handlebars.registerHelper("currencyUsdFormat", function(amount) {
    var parsedAmount = parseFloat(amount);
    if (parsedAmount < 0) {
        return "-$" + Math.abs(parsedAmount).toFixed(2);
    } else {
        return "$" + parsedAmount.toFixed(2);
    }
});

/**
 * Convert a string to a slug using speakingurl.js.
 *
 * @requires speakingurl https://pid.github.io/speakingurl/
 */
Handlebars.registerHelper('slug', function(text) {
    return getSlug(text);
});

/**
 * Equality helper for Handlebars
 * http://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional/21915381#21915381
 * @deprecated since 4.1 - use ifx instead
 * usage: {{ifCond apple orange}}
 */
Handlebars.registerHelper('ifCond', function(v1, v2, options) {
    if(v1 == v2) {
        return options.fn(this);
    }

    return options.inverse(this);
});

@@ -1330,24 +1330,411 @@
 %0A    %7D%0A%7D);%0A%0A
+/**%0A * Perform simple calculations.%0A * %0A * usage: %7B%7Bcalc x '+' 2%7D%7D%0A */%0AHandlebars.registerHelper('calc', function (v1, operator, v2, options) %7B%0A    lvalue = parseFloat(v1);%0A    rvalue = parseFloat(v2);%0A%0A    return %7B%0A        %22+%22: lvalue + rvalue,%0A        %22-%22: lvalue - rvalue,%0A        %22*%22: lvalue * rvalue,%0A        %22/%22: lvalue / rvalue,%0A        %22%25%22: lvalue %25 rvalue%0A    %7D%5Boperator%5D;%0A%7D);%0A%0A
 /**%0A * forma

implement `calc` Handlebars helper