(function($) {
    "use strict";
    $.Products = function(settings) {
        var config = {
            url: "",
            lang: {
                err: "Error",
                err1: "Invalid file type!"
            }
        };
        if (settings) {
            $.extend(config, settings);
        }

        var timeout;

        // item history
        if ($.inArray("history", $.url().segment()) !== -1) {
            $("#payment_chart").parent().addClass('loading');
            $.ajax({
                type: 'GET',
                url: config.url + '/helper.php',
                data: {
					action: true,
                    page: "itemChart",
                    id: $.url().segment(-1)
                },
                dataType: 'json'
            }).done(function(json) {
                var legend = '';
                json.legend.map(function(val) {
                    legend += val;
                });
                $("#legend").html(legend);
                Morris.Line({
                    element: 'payment_chart',
                    data: json.data,
                    xkey: 'm',
                    ykeys: json.label,
                    labels: json.label,
                    parseTime: false,
                    lineWidth: 4,
                    pointSize: 6,
                    lineColors: json.color,
                    gridTextFamily: "wSansRegular",
                    gridTextColor: "rgba(0,0,0,0.6)",
                    gridTextSize: 12,
                    fillOpacity: '.75',
                    hideHover: 'auto',
                    preUnits: json.preUnits,
                    hoverCallback: function(index, json, content) {
                        var text = $(content)[1].textContent;
                        return content.replace(text, text.replace(json.preUnits, ""));
                    },
                    smooth: true,
                    resize: true,
                });
                $("#payment_chart").parent().removeClass('loading');
            });
        }

        // payment history
        if ($.inArray("payments", $.url().segment()) !== -1) {
            getStats('all');
            $("#timeRange").on('click', '.item', function() {
                $("#payment_chart").html('');
                getStats($(this).data('value'));
            });
        }

        // sort images
        $("#sortable").sortables({
            ghostClass: "ghost",
            animation: 600,
            onUpdate: function() {
                var order = this.toArray();
                $.post(config.url + '/helper.php', {
                    sortImages: 1,
                    sorting: order
                }, function() {}, "json");

            }
        });

        // add images
        $('#images').simpleUpload({
            url: config.url + '/helper.php',
            types: ['jpg', 'png', 'JPG', 'PNG'],
            error: function(error) {
                if (error.type === 'fileType') {
                    $.notice(config.lang.err1, {
                        autoclose: 12000,
                        type: "error",
                        title: config.lang.err
                    });
                }
            },
            beforeSend: function() {
                $('#sortable').closest('.segment').addClass('loading');
            },
            success: function(data) {
                $('#sortable').prepend(data).sortables();
                $('#sortable').closest('.segment').removeClass('loading');
            }
        });

        // filter files
        $("#filter input").on("keyup", function() {
            window.clearTimeout(timeout);
            var filter = $(this).val(),
                count = 0;
            timeout = window.setTimeout(function() {
                $("#fsearch .item").each(function() {
                    if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                        $(this).velocity('transition.slideUpOut');
                    } else {
                        $(this).velocity('transition.slideDownIn');
                        count++;
                    }
                });
            }, 500);
        });

        // type switcher
        $("input[type=radio][name=type]").on("change", function() {
            switch ($(this).val()) {
                case 'affiliate':
                    $("#cdkey").velocity("transition.fadeOut", 100, function() {
                        $("#affiliate").velocity("transition.fadeIn", 100);
                    });

                    break;
                case 'cdkey':
                    $("#affiliate").velocity("transition.fadeOut", 100, function() {
                        $("#cdkey").velocity("transition.fadeIn", 100);
                    });

                    break;
                default:
                    $("#affiliate, #cdkey").velocity("transition.fadeOut", 100);
                    break;
            }
        });

        function getStats(range) {
            $("#payment_chart").parent().addClass('loading');
            $.ajax({
                type: 'GET',
                url: config.url + '/helper.php',
                data: {
                    action: "itemSalesChart",
                    range: range,
                },
                dataType: 'json'
            }).done(function(json) {
                var legend = '';
                json.legend.map(function(val) {
                    legend += val;
                });
                $("#legend").html(legend);
                Morris.Line({
                    element: 'payment_chart',
                    data: json.data,
                    xkey: 'm',
                    ykeys: json.label,
                    labels: json.label,
                    parseTime: false,
                    lineWidth: 4,
                    pointSize: 6,
                    lineColors: json.color,
                    gridTextFamily: "wSansRegular",
                    gridTextColor: "rgba(0,0,0,0.6)",
                    gridTextSize: 12,
                    fillOpacity: '.75',
                    hideHover: 'auto',
                    preUnits: json.preUnits,
                    hoverCallback: function(index, json, content) {
                        var text = $(content)[1].textContent;
                        return content.replace(text, text.replace(json.preUnits, ""));
                    },
                    smooth: true,
                    resize: true,
                });
                $("#payment_chart").parent().removeClass('loading');
            });
        }
    };
})(jQuery);