/* Minification failed. Returning unminified contents.
(160,30-31): run-time error JS1014: Invalid character: `
(160,32-33): run-time error JS1004: Expected ';': {
(161,37-38): run-time error JS1014: Invalid character: `
(161,39-40): run-time error JS1004: Expected ';': {
(161,70-71): run-time error JS1014: Invalid character: `
(168,15-19): run-time error JS1034: Unmatched 'else'; no 'if' defined: else
 */
$(document).on("click", ".js-loading-gif", function () {
    var targets = $(this);

    if ($(this).attr("data-loading-gif-target")) {
        var targets = $($(this).data("loading-gif-target"));
    }

    targets.each(function () {

        $(this).css("width", $(this).outerWidth());
        $(this).css("height", $(this).outerHeight());

        if ($(this).hasClass("c-button--primary")) {
            $(this).html("<img class=\"c-icon__loading\" src=\"/Assets/images/loading_white.gif\" />")
        } else if ($(this).hasClass("c-button--secondary")) {
            $(this).html("<img class=\"c-icon__loading\" src=\"/Assets/images/loading_grey_thin.gif\" />")
        } else if ($(this).hasClass("o-dropdown__box__link")) {
            $("#js-cartLoadingGif").css("width", $("#js-cartLoadingGif").outerWidth());
            $("#js-cartLoadingGif").css("height", $("#js-cartLoadingGif").outerHeight());
            $("#js-cartLoadingGif").html("<img class=\"c-icon__loading\" src=\"/Assets/images/loading_white.gif\" />")
        }
    });
});
//Allow maxlength to be used for number inputs
$("input[type=number]").on('keypress', function (e) {
    var $numInput = $(this),
    maxlength = $numInput.attr('maxlength')
    if ($.isNumeric(maxlength)) {
        if ($numInput.val().length == maxlength) {
            e.preventDefault();
            return;
        }
        $numInput.val($numInput.val().substr(0, maxlength));
    };
});

$(function () {
    $('input').change(function () {
        if (this.getAttribute('type') != "password") {
            this.value = $.trim(this.value);
        }
    });
    $('textarea').blur(function () {
        this.value = $.trim(this.value);
    });
});

(function (l) { var i, s = { touchend: function () { } }; for (i in s) l.addEventListener(i, s) })(document);
function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
}
function toStringC(x) {
    return "$" + numberWithCommas(parseFloat(x).toFixed(2));
}
var messageQueue = $({});
function successMessage(text) {
    text = (typeof (text) == "undefined") ? "Updated Successfully!" : HTMLDecode(text);

    messageQueue.queue(function () {
        $("#js-message-card").find("#js-message-card-text").text(text);

        //console.log({ "$(this)": $(this), "jquery": $("#js-message-card") });

        $("#js-message-card").removeClass("c-saveResponseCard--error")
            .removeClass("c-saveResponseCard--warning")
            .addClass("c-saveResponseCard--success")
            .show()
            .delay(4000)
            .fadeOut(800, function () { messageQueue.dequeue() });
    });
}
function errorMessage(text) {
    text = (typeof (text) == "undefined") ? "Something went wrong.  Please try again or contact us." : HTMLDecode(text);

    messageQueue.queue(function () {
        $("#js-message-card").find("#js-message-card-text").text(text);

        $("#js-message-card").removeClass("c-saveResponseCard--success")
            .removeClass("c-saveResponseCard--warning")
            .addClass("c-saveResponseCard--error")
            .show()
            .delay(4000)
            .fadeOut(800, function () { messageQueue.dequeue() });
    });
}
function warningMessage(text) {
    text = HTMLDecode(text);

    messageQueue.queue(function () {
        $("#js-message-card").find("#js-message-card-text").text(text);

        $("#js-message-card").removeClass("c-saveResponseCard--success")
            .removeClass("c-saveResponseCard--error")
            .addClass("c-saveResponseCard--warning")
            .show()
            .delay(4000)
            .fadeOut(800, function () { messageQueue.dequeue() });
    });
}

function displaySuccessMessage() {

}
function updateCartIcon(path) {
    $.ajax(path, {
        method: "GET",
        cache: false,
        success: function (data) {
            $("#js-cart-icon").html(data);
        }
    });
}
function HTMLDecode(value) {
    var elem = document.createElement("textarea");
    elem.innerHTML = value;
    return elem.value;
}

var lemur = {};
lemur.ajax = function (options) {

    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
        /*
         * DHM - I got this from here: https://stackoverflow.com/a/12840617/2449028
         * I had to do some modifications because I had no idea what "Common" was.
         * Instead, I passed that stuff in the "options" from the lemur.ajax
         */

        const retryAttempts = 2; // once for the original call and once for the retry attempt

        // Don't infinitely recurse
        originalOptions._retry = isNaN(originalOptions._retry)
            ? retryAttempts
            : originalOptions._retry - 1;

        // set up lemur stuff
        jqXHR.xhrFields = { withCredentials: true };

        // save the original error callback for later
        if (originalOptions.error)
            originalOptions._error = originalOptions.error;

        // overwrite *current request* error callback
        options.error = $.noop();

        // setup our own deferred object to also support promises that are only invoked
        // once all of the retry attempts have been exhausted
        var dfd = $.Deferred();
        jqXHR.done(dfd.resolve);

        // if the request fails, do something else yet still resolve
        jqXHR.fail(function () {
            var args = Array.prototype.slice.call(arguments);

            if ((jqXHR.status === 401 || jqXHR.status === 403) && originalOptions._retry > 0) {

                // refresh the LemurJwt and then retry the call. I have to use a ".done" because I'm not allowed to touch the jwt cookie (httponly)
                var apeUrl = `${location.protocol}//${location.hostname}${(location.port ? ':' + location.port : '')}`;
                let jwtRefreshUrl = `${apeUrl}/User/RefreshLemurToken`;
                
                $.get(jwtRefreshUrl).done(function () {
                    // retry with our new Jwt
                    $.ajax(originalOptions).then(dfd.resolve, dfd.reject);
                });
                
            } else {
                // add our _error callback to our promise object
                if (originalOptions._error)
                    dfd.fail(originalOptions._error);
                dfd.rejectWith(jqXHR, args);
            }
        });

        // NOW override the jqXHR's promise functions with our deferred
        return dfd.promise(jqXHR);
    });

    return $.ajax(options);
}

$(document).on("click", "#js-toolTipContainer", function () {
    if ($('#js-toolTipContainer').hasClass('c-toolTips--expanded')) {
        setTimeout(function () {
            $('#js-toolTipContainer').removeClass('c-toolTips--expanded');
            $('.c-toolTips').css('align-items', 'center');
        }, 800);
    } else {
        $(this).addClass('c-toolTips--expanded');
        $('.c-toolTips').css('align-items', 'flex-start');
    }
    $(this).find('#js-showToolTip').toggle(600);
});

function showHideCareer(element) {
    var info = $(element).prev(".js-showHideCareerInfo");
    if ($(info).is(':visible')) {
        $(info).slideUp();
        $(element).text("Show More +")
    } else {
        $(info).slideDown();
        $(element).text("Show Less -")
    }
}
function setData(element, dataName, value) {
    element.data(dataName, value);
    element.attr("data-" + dataName, value);
}


function isEmpty() {
    if ($('#search-header').val().length == 0) {
        return true;
    } else {
        return false;
    }
}

$(document).on("submit", "#js-product-search", function (e) {
    if (isEmpty()) {
        e.preventDefault();
    }
});



;
/*
    A simple jQuery modal (http://github.com/kylefox/jquery-modal)
    Version 0.8.0
*/

(function (factory) {
    // Making your jQuery plugin work better with npm tools
    // http://blog.npmjs.org/post/112712169830/making-your-jquery-plugin-work-better-with-npm
    if (typeof module === "object" && typeof module.exports === "object") {
        factory(require("./jquery-3.2.1.min.js"), window, document);
    }
    else {
        factory(jQuery, window, document);
    }
}(function ($, window, document, undefined) {

    var isMouseDownOnBackGround = false;

    var modals = [],
        getCurrent = function () {
            return modals.length ? modals[modals.length - 1] : null;
        },
        selectCurrent = function () {
            var i,
                selected = false;
            for (i = modals.length - 1; i >= 0; i--) {
                if (modals[i].$blocker) {
                    modals[i].$blocker.toggleClass('current', !selected).toggleClass('behind', selected);
                    selected = true;
                }
            }
        };

    $.modal = function (el, options) {
        var remove, target;
        this.$body = $('body');
        this.options = $.extend({}, $.modal.defaults, options);
        this.options.doFade = !isNaN(parseInt(this.options.fadeDuration, 10));
        this.$blocker = null;
        if (this.options.closeExisting)
            while ($.modal.isActive())
                $.modal.close(); // Close any open modals.
        modals.push(this);
        if (el.is('a')) {
            target = el.attr('href');
            //Select element by id from href
            if (/^#/.test(target)) {
                this.$elm = $(target);
                if (this.$elm.length !== 1) return null;
                this.$body.append(this.$elm);
                this.open();
                //AJAX
            } else {
                this.$elm = $('<div>');
                this.$body.append(this.$elm);
                remove = function (event, modal) { modal.elm.remove(); };
                this.showSpinner();
                el.trigger($.modal.AJAX_SEND);
                $.get(target).done(function (html) {
                    if (!$.modal.isActive()) return;
                    el.trigger($.modal.AJAX_SUCCESS);
                    var current = getCurrent();
                    current.$elm.empty().append(html).on($.modal.CLOSE, remove);
                    current.hideSpinner();
                    current.open();
                    el.trigger($.modal.AJAX_COMPLETE);
                }).fail(function () {
                    el.trigger($.modal.AJAX_FAIL);
                    var current = getCurrent();
                    current.hideSpinner();
                    modals.pop(); // remove expected modal from the list
                    el.trigger($.modal.AJAX_COMPLETE);
                });
            }
        } else {
            this.$elm = el;
            this.$body.append(this.$elm);
            this.open();
        }
    };

    $.modal.prototype = {
        constructor: $.modal,

        open: function () {
            var m = this;
            this.block();
            $(".o-dropdown__box").hide()
            if (this.options.doFade) {
                setTimeout(function () {
                    m.show();
                }, this.options.fadeDuration * this.options.fadeDelay);
            } else {
                this.show();
            }
            $(document).off('keydown.modal').on('keydown.modal', function (event) {
                var current = getCurrent();
                if (event.which == 27 && current.options.escapeClose) current.close();
            });
            if (this.options.clickClose) {
                this.$blocker.mousedown(function (e) {
                    isMouseDownOnBackGround = e.target.classList.contains("blocker") && e.target.clientWidth > e.clientX;
                });
                this.$blocker.mouseup(function (e) {
                    if (isMouseDownOnBackGround && e.target.classList.contains("blocker")) {
                        $.modal.close();
                    }
                    isMouseDownOnBackGround = false;
                });
                //this.$blocker.click(function (e) {
                //    if (e.target == this && e.target.classList.contains("blocker")) {
                //        $.modal.close();
                //    }
                //});
            }
        },

        close: function () {
            $(".o-dropdown__box").show()
            modals.pop();
            this.unblock();
            this.hide();
            if (!$.modal.isActive())
                $(document).off('keydown.modal');
        },

        block: function () {
            this.$elm.trigger($.modal.BEFORE_BLOCK, [this._ctx()]);
            this.$body.css('overflow', 'hidden');
            this.$blocker = $('<div class="jquery-modal blocker current"></div>').appendTo(this.$body);
            selectCurrent();
            if (this.options.doFade) {
                this.$blocker.css('opacity', 0).animate({ opacity: 1 }, this.options.fadeDuration);
            }
            this.$elm.trigger($.modal.BLOCK, [this._ctx()]);
        },

        unblock: function (now) {
            if (!now && this.options.doFade) {
                this.$blocker.fadeOut(this.options.fadeDuration, this.unblock.bind(this, true))
                }
            else if (this.$elm[0].dataset.container == "hiddenContainer") {
                this.$blocker.children().appendTo($(".c-modalHiddenContentWrapper"));
                this.$blocker.remove();
                this.$blocker = null;
                selectCurrent();
                if (!$.modal.isActive())
                    this.$body.css('overflow', '');
            }
            else if (this.$elm[0].dataset.container == "body") {
                this.$blocker.children().appendTo($(".o-body__leftBar"));
                this.$blocker.remove();
                this.$blocker = null;
                selectCurrent();
                if (!$.modal.isActive())
                    this.$body.css('overflow', '');
            }
            else if (this.$elm[0].dataset.container == "globalModal") {
                this.$blocker.children().appendTo($(".c-modalHiddenGlobalContentWrapper"));
                this.$blocker.remove();
                this.$blocker = null;
                selectCurrent();
                if (!$.modal.isActive())
                    this.$body.css('overflow', '');
            }
        },

        show: function () {
            this.$elm.trigger($.modal.BEFORE_OPEN, [this._ctx()]);
            if (this.options.showClose) {
                this.closeButton = $('<a href="#close-modal" rel="modal:close" class="close-modal ' + this.options.closeClass + '">' + this.options.closeText + '</a>');
                this.$elm.append(this.closeButton);
            }
            this.$elm.addClass(this.options.modalClass).appendTo(this.$blocker);
            if (this.options.doFade) {
                this.$elm.css('opacity', 0).show().animate({ opacity: 1 }, this.options.fadeDuration);
            } else {
                this.$elm.show();
            }
            this.$elm.trigger($.modal.OPEN, [this._ctx()]);
        },

        hide: function () {
            this.$elm.trigger($.modal.BEFORE_CLOSE, [this._ctx()]);
            if (this.closeButton) this.closeButton.remove();
            var _this = this;
            $(".js-modal").removeClass("modal");
            this.$elm.trigger($.modal.CLOSE, [this._ctx()]);
        },

        showSpinner: function () {
            if (!this.options.showSpinner) return;
            this.spinner = this.spinner || $('<div class="' + this.options.modalClass + '-spinner"></div>')
              .append(this.options.spinnerHtml);
            this.$body.append(this.spinner);
            this.spinner.show();
        },

        hideSpinner: function () {
            if (this.spinner) this.spinner.remove();
        },

        //Return context for custom events
        _ctx: function () {
            return { elm: this.$elm, $blocker: this.$blocker, options: this.options };
        }
    };

    $.modal.close = function (event) {
        if (!$.modal.isActive()) return;
        if (event) event.preventDefault();
        var current = getCurrent();
        current.close();
        return current.$elm;
    };


    // Returns if there currently is an active modal
    $.modal.isActive = function () {
        return modals.length > 0;
    }

    $.modal.getCurrent = getCurrent;

    $.modal.defaults = {
        closeExisting: true,
        escapeClose: true,
        clickClose: true,
        closeText: 'Close',
        closeClass: '',
        modalClass: "modal",
        spinnerHtml: null,
        showSpinner: true,
        showClose: true,
        fadeDuration: null,   // Number of milliseconds the fade animation takes.
        fadeDelay: 1.0        // Point during the overlay's fade-in that the modal begins to fade in (.5 = 50%, 1.5 = 150%, etc.)
    };

    // Event constants
    $.modal.BEFORE_BLOCK = 'modal:before-block';
    $.modal.BLOCK = 'modal:block';
    $.modal.BEFORE_OPEN = 'modal:before-open';
    $.modal.OPEN = 'modal:open';
    $.modal.BEFORE_CLOSE = 'modal:before-close';
    $.modal.CLOSE = 'modal:close';
    $.modal.AFTER_CLOSE = 'modal:after-close';
    $.modal.AJAX_SEND = 'modal:ajax:send';
    $.modal.AJAX_SUCCESS = 'modal:ajax:success';
    $.modal.AJAX_FAIL = 'modal:ajax:fail';
    $.modal.AJAX_COMPLETE = 'modal:ajax:complete';

    $.fn.modal = function (options) {
        if (this.length === 1) {
            new $.modal(this, options);
        }
        return this;
    };

    // Automatically bind links with rel="modal:close" to, well, close the modal.
    $(document).on('click.modal', 'a[rel~="modal:close"]', $.modal.close);
    $(document).on('click.modal', 'a[rel~="modal:open"]', function (event) {
        event.preventDefault();
        $(this).modal();
    });
}));;
function orderStatusChart(targetid, status) {
    var targetParent = "div#" + targetid;
    var colors = {
        'pink': '#E1499A',
        'yellow': '#f0ff08',
        'green': '#47e495',
        'red': 'rgb(204,17,0)',
        'grey': 'rgb(180,180,180)',
        'blue':'#006EA8'
    };

    var color = colors.green;
    var endPercent;

    var statusText = "";
    switch (status) {
        case 1:
            endPercent = 0;
            statusText = "in cart";
            break;
        case 2:
            endPercent = (1 / 5);
            statusText = "submitted";
            break;
        case 9:
            endPercent = (1 / 5);
            statusText = "unreleased";
            color = colors.blue;
            break;
        case 3:
            endPercent = (2 / 5);
            statusText = "pending";
            break;
        case 4:
            endPercent = (3 / 5);
            statusText = "processed";
            break;
        case 5:
            endPercent = (4 / 5);
            statusText = "partial";
            break;
        case 6:
            endPercent = 1;
            statusText = "complete";
            break;
        case 7:
            endPercent = 1;
            statusText = "cancelled";
            color = colors.grey;
            break;
        case 8:
            endPercent = .5;
            statusText = "on hold";
            color = colors.red;
            break;
        default:
            endPercent = 0;
            statusText = "N/A";
    }
    var radius = 20;
    var border = 5;
    var padding = 20;
    var startPercent = 0;


    var twoPi = Math.PI * 2;
    var formatPercent = d3.format('.0%');
    var boxSize = (radius + padding) * 2;


    var count = Math.abs((endPercent - startPercent) / 0.01);
    var step = endPercent < startPercent ? -0.01 : 0.01;

    var arc = d3.svg.arc()
        .startAngle(0)
        .innerRadius(radius)
        .outerRadius(radius - border);

    var parent = d3.select(targetParent);

    var svg = parent.append('svg')
        .attr('width', boxSize)
        .attr('height', boxSize);

    var defs = svg.append('defs');

    var filter = defs.append('filter')
        .attr('id', 'blur');



    var g = svg.append('g')
        .attr('transform', 'translate(' + boxSize / 2 + ',' + boxSize / 2.5 + ')');

    var meter = g.append('g')
        .attr('class', 'progress-meter');

    meter.append('path')
        .attr('class', 'background')
        .attr('fill', '#ccc')
        .attr('fill-opacity', 0.5)
        .attr('d', arc.endAngle(twoPi));

    var foreground = meter.append('path')
        .attr('class', 'foreground')
        .attr('fill', color)
        .attr('fill-opacity', 1)
        .attr('stroke', color)
        .attr('stroke-width', 5)
        .attr('stroke-opacity', 1)
        .attr('filter', 'url(#blur)');

    var front = meter.append('path')
        .attr('class', 'foreground')
        .attr('fill', color)
        .attr('fill-opacity', 1);

    var numberText = meter.append('text')
        .attr('fill', '#000')
        .attr('text-anchor', 'middle')
        .attr('dy', '2.5em');

    updateProgress(endPercent, foreground, arc, twoPi, front, numberText, statusText);
}
function updateProgress(progress, foreground, arc, twoPi, front, numberText, statusText) {
    if (foreground) {
        foreground.attr('d', arc.endAngle(twoPi * progress));
    }
    if (front) {
        front.attr('d', arc.endAngle(twoPi * progress));
    }
    if (numberText) {
        numberText.text(statusText);
    }
};
function rmaStatusChart(targetid, status) {
    var targetParent = "div#" + targetid;
    var colors = {
        'pink': '#E1499A',
        'yellow': '#f0ff08',
        'green': '#47e495',
        'red': 'rgb(204,17,0)',
        'grey': 'rgb(180,180,180)'
    };

    var color = colors.green;
    var endPercent;

    var statusText = "";
    switch (status) {
        case 1:
            endPercent = 0;
            statusText = "";
            break;
        case 2:
            endPercent = (1 / 6);
            statusText = "";
            break;
        case 3:
            endPercent = (2 / 6);
            statusText = "";
            break;
        case 4:
            endPercent = (3 / 6);
            statusText = "";
            break;
        case 5:
            endPercent = (4 / 6);
            statusText = "";
            break;
        case 6:
            endPercent = (5 / 6);
            statusText = "";
            break;
        case 7:
            endPercent = 1;
            statusText = "";
            break;
        case 9:
            endPercent = 1;
            statusText = "";
            color = colors.grey;
            break;
        default:
            endPercent = 0;
            statusText = "N/A";
    }
    var radius = 20;
    var border = 5;
    var padding = 5;
    var startPercent = 0;


    var twoPi = Math.PI * 2;
    var formatPercent = d3.format('.0%');
    var boxSize = (radius + padding) * 2;


    var count = Math.abs((endPercent - startPercent) / 0.01);
    var step = endPercent < startPercent ? -0.01 : 0.01;

    var arc = d3.svg.arc()
        .startAngle(0)
        .innerRadius(radius)
        .outerRadius(radius - border);

    var parent = d3.select(targetParent);

    var svg = parent.append('svg')
        .attr('width', boxSize)
        .attr('height', "60");

    var defs = svg.append('defs');

    var filter = defs.append('filter')
        .attr('id', 'blur');



    var g = svg.append('g')
        .attr('transform', 'translate(' + boxSize / 2 + ',' + boxSize / 2.5 + ')');

    var meter = g.append('g')
        .attr('class', 'progress-meter');

    meter.append('path')
        .attr('class', 'background')
        .attr('fill', '#ccc')
        .attr('fill-opacity', 0.5)
        .attr('d', arc.endAngle(twoPi));

    var foreground = meter.append('path')
        .attr('class', 'foreground')
        .attr('fill', color)
        .attr('fill-opacity', 1)
        .attr('stroke', color)
        .attr('stroke-width', 5)
        .attr('stroke-opacity', 1)
        .attr('filter', 'url(#blur)');

    var front = meter.append('path')
        .attr('class', 'foreground')
        .attr('fill', color)
        .attr('fill-opacity', 1);

    var numberText = meter.append('text')
        .attr('fill', '#000')
        //.attr('text-anchor', 'middle')
        //.attr('dy', '2.5em');

    updateProgress(endPercent, foreground, arc, twoPi, front, numberText, statusText);
}
function updateProgress(progress, foreground, arc, twoPi, front, numberText, statusText) {
    if (foreground) {
        foreground.attr('d', arc.endAngle(twoPi * progress));
    }
    if (front) {
        front.attr('d', arc.endAngle(twoPi * progress));
    }
    if (numberText) {
        numberText.text(statusText);
    }
};
/*
    Alex Boguszewski 3/19/2018
    Universal tabbed content.
    (For an example look at the AddTo.cshtml)

    Tab Header Buttons
    (the buttons that have underlines and you click on to make stuff happen.)

    <div>
        <ul id="nameOfTabSystemHere" class="c-tabs">
            <li class="c-tabs__navItem"><a href="#showMeSamus" class="c-link c-tabs__link"></a></li>
            <li class="c-tabs__navItem"><a href="#showMeLink" class="c-link c-tabs__link"></a></li>
            <li class="c-tabs__navItem"><a href="#showMeKirby" class="c-link c-tabs__link"></a></li>
        </ul>
    </div>
    
    Tabs of Content

    <div id="showMeSamus" class="c-tabs__tab">
    </div>
    <div id="showMeLink" class="c-tabs__tab">
    </div>
    <div id="showMeKirby" class="c-tabs__tab">
    </div>
*/


function createTabs(parentUL) {
    $("#" + parentUL + " li").each(function (n) {
        $(this).attr("id", parentUL + "-tab-" + n);

        if (n == 0) {
            $(this).addClass("c-tabs__navItem--active")
        }
    });
}
function switchTabs(selectedTab, tabName) {
    //remove all underlines
    $("#" + tabName + " li").each(function (n) {
        $(this).removeClass("c-tabs__navItem--active");
    });

    //hide all tabs
    $(".c-tabs__tab").removeClass("c-tabs__tab--active");

    //activate underline and show tab content
    $(selectedTab).addClass("c-tabs__navItem--active");
    var contentToShow = $(selectedTab).find("a").attr("href");
    $(contentToShow).addClass("c-tabs__tab--active")

    event.preventDefault();
};
