// Copyright 2022 Rocket Referrals LLC All rights reserved.

; (function (w, d, RRAPI, undefined) {

    // private vars
    var methods = {};
    var testimonial_urls = ['https://cf.rocketreferrals.com/testimonialwidget/QXDYDkiFqQxLODuZ7fWT6g.js'];
    var testimonial_template = "<style type=\'text/css\'>\n\n.rr-testimonial {\n margin: 0;\n padding: 0 0 0 15px;\n border-left: 5px solid #EEE;\n font-weight: 300;\n font-family: \'Open Sans\', \'Helvetica Neue\', Helvetica, Arial, sans-serif;\n margin-bottom: 20px;\n}\n\n.rr-testimonial p {\n  margin: 0 0 10px;\n  font-size: 15.5px;\n  margin-bottom: 0;\n font-weight: 300;\n line-height: 1.25;\n}\n\n.rr-testimonial small {\n display: block;\n line-height: 20px;\n color: #999;\n font-size: 85%;\n}\n\n.rr-testimonial:after {\n content: \'\';\n background: none;\n}\n\nblockquote.rr-testimonial {\n  background: none;\n}\n<\/style>\n\n{{#testimonials}}<blockquote class=\'rr-testimonial\'><p>{{comment}}<\/p><small><cite>{{signature}}<\/cite><\/small><\/blockquote>{{/testimonials}}"
    var compiled_template;
    var callbacks = {
        testimonials_retrieved: null
    };


    // public methods
    // redefine the rrcall method on the global scope after being redefined
    w.rrcall = RRAPI.execute = function () {
        var method = [].shift.apply(arguments);
        if (typeof methods[method] === "function") {
            methods[method].apply(this, arguments);
        }
    };

    // executable methods
    methods.set_testimonial_template = function(template) {
        compiled_template = undefined;
        testimonial_template = template;
    };

    methods.clear_testimonial_endpoints = function() {
        testimonial_urls = [];
    };

    methods.add_testimonial_endpoint = function(testimonial_endpoint) {
        testimonial_urls.push(testimonial_endpoint);
    };

    // Set a callback.
    // rrcall('set_callback', 'callback_name', function(){})
    //
    // Available callbacks:
    //   'testimonials_retrieved'
    //     a function which will get data passed to it containing testimonials after the testimonials have been retrieved from Rocket Referrals.
    //     It can add methods, change data etc. but the function must return an object resembling the original data pass in to it.
    //     it can add additional data attributes as long as those attributes are used in the template.
    methods.set_callback = function(callback_name, cb) {
        callbacks[callback_name] = cb;
    };

    methods.load_testimonials = function(container_id, cb) {
        var tmpl;

        // compile while it's getting the json
        if(typeof compiled_template !== 'undefined'){
            tmpl = compiled_template;
        } else {
            compiled_template = RRAPI.Hogan.compile(testimonial_template);
            tmpl = compiled_template;
        }

        var t_len = testimonial_urls.length;
        for (var i = 0; i < t_len; i++) {
            var testimonial_url = testimonial_urls[i];

            // load the testimonials and insert into the div.
            jsonp.send(testimonial_url, {
                onSuccess: function(testimonials){
                    if(callbacks['testimonials_retrieved'] != null){
                        testimonials = callbacks['testimonials_retrieved'](testimonials);
                    }
                    if(container_id !== null){
                        var container = d.getElementById(container_id);
                        container.innerHTML = container.innerHTML + tmpl.render(testimonials);
                    }
                    if(typeof cb !== 'undefined'){
                        cb(null, testimonials);
                    }
                },
                timeout: 10
            });
        }
    };

    var add_chat_widget = function(web_chat_channel, test){
      var web_chat = web_chats[web_chat_channel];
      if(!web_chat) return;
      if(web_chat.status === 'inactive' && !test){
        if(window.console) console.log('Your web chat has loaded successfully. You can show it by running this line in the console: rrcall("show_web_chat", "' + web_chat_channel + '");');
        return;
      }
      web_chat.channel_number = web_chat_channel;
      RRAPI.WebChat = new WebChatWidget(web_chat);
      document.body.append(RRAPI.WebChat.element);
    };

    methods.load_web_chat = function(web_chat_channel) {
      add_chat_widget(web_chat_channel);
    };

    methods.show_web_chat = function(web_chat_channel) {
      add_chat_widget(web_chat_channel, true);
    };

    // helper functions and backwards compatability
    if (typeof Array.isArray === 'undefined') {
        Array.isArray = function(obj) {
            return Object.prototype.toString.call(obj) === '[object Array]';
        }
    }

    // string hash code returns unique hash 32bit integer for each string.
    var hashCode = function(s) {
        var hash = 0, i, chr;
        if (s.length === 0) return hash;
        for (i = 0; i < s.length; i++) {
            chr   = s.charCodeAt(i);
            hash  = ((hash << 5) - hash) + chr;
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
    };

    // simple json p caller
    var jsonp = (function(){
        var that = {};
        RRAPI.callbacks = {};

        that.send = function(src, options) {
            var on_success = options.onSuccess || function(){},
                on_timeout = options.onTimeout || function(){},
                timeout = options.timeout || 10,
                cbname = ("cb" + hashCode(src)).replace("-","_");

            var timeout_trigger = w.setTimeout(function(){
                RRAPI.callbacks[cbname] = function(){};
                on_timeout();
            }, timeout * 1000);

            RRAPI.callbacks[cbname] = function(data){
                w.clearTimeout(timeout_trigger);
                on_success(data);
            }

            var script = d.createElement('script');
            script.type = 'text/javascript';
            script.async = true;
            script.src = src + '?callback=RRAPI.callbacks.' + cbname;



            d.getElementsByTagName('head')[0].appendChild(script);
        }

        return that;
    })();

    // Hogan.js license
    /*!
     *  Copyright 2011 Twitter, Inc.
     *  Licensed under the Apache License, Version 2.0 (the "License");
     *  you may not use this file except in compliance with the License.
     *  You may obtain a copy of the License at
     *
     *  http://www.apache.org/licenses/LICENSE-2.0
     *
     *  Unless required by applicable law or agreed to in writing, software
     *  distributed under the License is distributed on an "AS IS" BASIS,
     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     *  See the License for the specific language governing permissions and
     *  limitations under the License.
     */
    RRAPI.Hogan = {};
    !function(t,n){t.Template=function(t,n,r,e){this.r=t||this.r,this.c=r,this.options=e,this.text=n||"",this.buf=""},t.Template.prototype={r:function(t,n,r){return""},v:function(t){return t=o(t)},t:o,render:function(t,n,r){return this.ri([t],n||{},r)},ri:function(t,n,r){return this.r(t,n,r)},rp:function(t,n,r,e){var i=r[t];return i?(this.c&&"string"==typeof i&&(i=this.c.compile(i,this.options)),i.ri(n,r,e)):""},rs:function(t,n,r){var e=t[t.length-1];if(s(e))for(var i=0;i<e.length;i++)t.push(e[i]),r(t,n,this),t.pop();else r(t,n,this)},s:function(t,n,r,e,i,o,u){var c;return(!s(t)||0!==t.length)&&("function"==typeof t&&(t=this.ls(t,n,r,e,i,o,u)),c=""===t||!!t,!e&&c&&n&&n.push("object"==typeof t?t:n[n.length-1]),c)},d:function(t,n,r,e){var i=t.split("."),o=this.f(i[0],n,r,e),u=null;if("."===t&&s(n[n.length-2]))return n[n.length-1];for(var c=1;c<i.length;c++)o=o&&"object"==typeof o&&i[c]in o?(u=o)[i[c]]:"";return!(e&&!o)&&(e||"function"!=typeof o||(n.push(u),o=this.lv(o,n,r),n.pop()),o)},f:function(t,n,r,e){for(var i=!1,o=null,u=!1,c=n.length-1;0<=c;c--)if((o=n[c])&&"object"==typeof o&&t in o){i=o[t],u=!0;break}return u?(e||"function"!=typeof i||(i=this.lv(i,n,r)),i):!e&&""},ho:function(t,n,r,e,i){var o=this.c,u=this.options;return u.delimiters=i,e=null==(e=t.call(n,e))?String(e):e.toString(),this.b(o.compile(e,u).render(n,r)),!1},b:function(t){this.buf+=t},fl:function(){var t=this.buf;return this.buf="",t},ls:function(t,n,r,e,i,o,u){var c,s=n[n.length-1];if(!e&&this.c&&0<t.length)return this.ho(t,s,r,this.text.substring(i,o),u);if("function"==typeof(c=t.call(s))){if(e)return!0;if(this.c)return this.ho(c,s,r,this.text.substring(i,o),u)}return c},lv:function(t,n,r){var e=n[n.length-1],i=t.call(e);return"function"==typeof i&&(i=o(i.call(e)),this.c&&~i.indexOf("{{"))?this.c.compile(i,this.options).render(e,r):o(i)}};function o(t){return String(null==t?"":t)}var s=Array.isArray||function(t){return"[object Array]"===Object.prototype.toString.call(t)}}("undefined"!=typeof exports?exports:RRAPI.Hogan),function(i){var A=/\S/,n=/\"/g,r=/\n/g,e=/\r/g,o=/\\/g,m={"#":1,"^":2,"/":3,"!":4,">":5,"<":6,"=":7,_v:8,"{":9,"&":10};function x(t){"}"===t.n.substr(t.n.length-1)&&(t.n=t.n.substring(0,t.n.length-1))}function w(t){return t.trim?t.trim():t.replace(/^\s*|\s*$/g,"")}function S(t,n,r){if(n.charAt(r)!=t.charAt(0))return!1;for(var e=1,i=t.length;e<i;e++)if(n.charAt(r+e)!=t.charAt(e))return!1;return!0}function s(t,n){for(var r=0,e=n.length;r<e;r++)if(n[r].o==t.n)return t.tag="#",!0}function f(t,n,r){for(var e=0,i=r.length;e<i;e++)if(r[e].c==t&&r[e].o==n)return!0}function d(t){return t.replace(o,"\\\\").replace(n,'\\"').replace(r,"\\n").replace(e,"\\r")}function b(t){return~t.indexOf(".")?"d":"f"}function _(t){for(var n,r,e,i,o,u,c,s,f,h,l,a="",g=0,p=t.length;g<p;g++){var v=t[g].tag;"#"==v?a+=(u=t[g].nodes,c=t[g].n,s=b(t[g].n),f=t[g].i,h=t[g].end,l=t[g].otag+" "+t[g].ctag,"if(_.s(_."+s+'("'+d(c)+'",c,p,1),c,p,0,'+f+","+h+',"'+l+'")){_.rs(c,p,function(c,p,_){'+_(u)+"});c.pop();}"):"^"==v?a+=(i=t[g].nodes,o=t[g].n,"if(!_.s(_."+b(t[g].n)+'("'+d(o)+'",c,p,1),c,p,1,0,0,"")){'+_(i)+"};"):"<"==v||">"==v?a+='_.b(_.rp("'+d((e=t[g]).n)+'",c,p,"'+(e.indent||"")+'"));':"{"==v||"&"==v?a+=(r=t[g].n,"_.b(_.t(_."+b(t[g].n)+'("'+d(r)+'",c,p,0)));'):"\n"==v?a+=y('"\\n"'+(t.length-1==g?"":" + i")):"_v"==v?a+=(n=t[g].n,"_.b(_.v(_."+b(t[g].n)+'("'+d(n)+'",c,p,0)));'):void 0===v&&(a+=y('"'+d(t[g])+'"'))}return a}function y(t){return"_.b("+t+");"}i.scan=function(t,n){var r,e,i,o,u,c=t.length,s=0,f=null,h=null,l="",a=[],g=!1,p=0,v=0,d="{{",b="}}";function _(){0<l.length&&(a.push(new String(l)),l="")}function y(t,n){if(_(),t&&function(){for(var t=!0,n=v;n<a.length;n++)if(!(t=a[n].tag&&m[a[n].tag]<m._v||!a[n].tag&&null===a[n].match(A)))return!1;return t}())for(var r,e=v;e<a.length;e++)a[e].tag||((r=a[e+1])&&">"==r.tag&&(r.indent=a[e].toString()),a.splice(e,1));else n||a.push({tag:"\n"});g=!1,v=a.length}for(n&&(n=n.split(" "),d=n[0],b=n[1]),p=0;p<c;p++)0==s?S(d,t,p)?(--p,_(),s=1):"\n"==t.charAt(p)?y(g):l+=t.charAt(p):1==s?(p+=d.length-1,s="="==(f=(h=m[t.charAt(p+1)])?t.charAt(p+1):"_v")?(e=p,void 0,i="="+b,o=(r=t).indexOf(i,e),u=w(r.substring(r.indexOf("=",e)+1,o)).split(" "),d=u[0],b=u[1],p=o+i.length-1,0):(h&&p++,2),g=p):S(b,t,p)?(a.push({tag:f,n:w(l),otag:d,ctag:b,i:"/"==f?g-b.length:p+d.length}),l="",p+=b.length-1,s=0,"{"==f&&("}}"==b?p++:x(a[a.length-1]))):l+=t.charAt(p);return y(g,!0),a},i.generate=function(t,n,r){var e='var _=this;_.b(i=i||"");'+_(t)+"return _.fl();";return r.asString?"function(c,p,i){"+e+";}":new i.Template(new Function("c","p","i",e),n,i,r)},i.parse=function(t,n,r){return function t(n,r,e,i){for(var o=[],u=null,c=null;0<n.length;)if("#"==(c=n.shift()).tag||"^"==c.tag||s(c,i))e.push(c),c.nodes=t(n,c.tag,e,i),o.push(c);else{if("/"==c.tag){if(0===e.length)throw new Error("Closing tag without opener: /"+c.n);if(u=e.pop(),c.n!=u.n&&!f(c.n,u.n,i))throw new Error("Nesting error: "+u.n+" vs. "+c.n);return u.end=c.i,o}o.push(c)}if(0<e.length)throw new Error("missing closing tag: "+e.pop().n);return o}(t,0,[],(r=r||{}).sectionTags||[])},i.cache={},i.compile=function(t,n){var r=t+"||"+!!(n=n||{}).asString,e=this.cache[r];return e||(e=this.generate(this.parse(this.scan(t,n.delimiters),t,n),t,n),this.cache[r]=e)}}(RRAPI.Hogan);

    // at the end we'll execute all of the current methods in the stack
    var callItem;
    if (Array.isArray(w.RRLoader)) {
        while (typeof (callItem = w.RRLoader.shift()) !== 'undefined') {
            RRAPI.execute.apply(this, callItem);
        }
    }

})(window, document, window.RRAPI = window.RRAPI || {});