// jQuery.noConflict();
(function ($) {
    var baseUrl = 'https://ac-web-develop.api.everforth.com/capi';
    var defaultQuery = {
        limit: '2'
    };
    var attrs = ['apikey', 'limit', 'start', 'brands', 'tags', 'used_by', 'using_to_product_number'];
    var templateAttrs = ['src', 'href', 'alt', 'title'];
    var prefix = 'ef-';

    $.ef_ajax = function (path, options) {
        options['url'] = baseUrl + '/' + path;
        options['dataType'] = 'jsonp';
        $.ajax(options);
    };

    $.fn.ef = function (path, params) {
        if (this.length) {
            var target = jQuery(this);
            var query = jQuery.extend(true, {}, defaultQuery);
            for (a in params) {
                query[a] = params[a]
            }
            attrs.forEach(function (attr) {
                var value = target.attr(prefix + attr);
                if (value) query[attr] = value;
            });
            var item = $('.' + prefix + 'item', target);
            if (item) ef_jsonp(path, query, item);
        }
        return this;
    };

    function ef_jsonp(path, query, item) {
        item.hide();
        $.ajax({
            url: baseUrl + '/' + path,
            data: query,
            dataType: 'jsonp',
            success: function (json) {
                var datas = json['data'];
                datas.forEach(function (data,index) {
                    var clone = item.clone(true);
                    clone.show();

                    for (attr in data) {
                        if (clone.hasClass(prefix + attr)) {
                            clone.text(data[attr])
                        }
                        templateAttrs.forEach(function (viewAttr) {
                            if (clone.hasClass(prefix + attr + "-" + viewAttr)) {
                                clone.attr(viewAttr, data[attr])
                            }
                        });
                        $('.' + prefix + attr, clone).text(data[attr]);
                        templateAttrs.forEach(function (viewAttr) {
                            $('.' + prefix + attr + "-" + viewAttr, clone).attr(viewAttr, data[attr]);
                        })
                    }

                    //template
                    var processTemplate = function (text) {
                        if (text) for (attr in data) {
                            if (data[attr]) text = text.split('{{' + attr + '}}').join(data[attr])
                        }
                        return text;
                    }

//                    if (clone.hasClass(prefix + 'template')) {
//                        clone.text(function(index, text) {
//                            return processTemplate(text);
//                        })
//                    }
                    if (clone.hasClass(prefix + 'template')) {
                        clone.html(function (index, html) {
                            return processTemplate(html);
                        })
                    }
                    templateAttrs.forEach(function (viewAttr) {
                        if (clone.hasClass(prefix + 'template' + "-" + viewAttr)) {
                            clone.attr(viewAttr, processTemplate(clone.attr(viewAttr)))
                        }
                    });
//                    $('.' + prefix + 'template', clone).text(function(index, text) {
//                        return processTemplate(text);
//                    });
                    $('.' + prefix + 'template', clone).html(function (index, html) {
                        return processTemplate(html);
                    });
                    templateAttrs.forEach(function (viewAttr) {

                        $('.' + prefix + 'template' + "-" + viewAttr, clone).each(function () {
                            $(this).attr(viewAttr, processTemplate($(this).attr(viewAttr)));
                        });

                    });
                    //モーダル表示を設定
                    if(item.parents().hasClass("ef-update-modal")){
                        (function() {
                            //cloneしたitemにイベントを設定
                            clone.click(function() {
                                update_coordinate_box(datas, index);
                            });
                        })();
                    }
                    //セール価格の表示を設定
                    var price = clone.find(".ef-price").text();
                    var sell_price = clone.find(".ef-sell_price").text();
                    var diff_price = price - sell_price;

                    if(price == 'null' || price == '' || diff_price >= 0){
                        // セール価格を表示
                        clone.find(".ef-price").hide();
                        clone.find(".ef-sell_price").text(sell_price.toString().replace(/(\d)(?=(\d{3})+$)/g , '$1,')).prepend('&yen;');
                    }
                    if(sell_price == 'null' || sell_price == '' || diff_price > 0){
                        // 通常価格を表示
                        clone.find(".ef-price").text(price.toString().replace(/(\d)(?=(\d{3})+$)/g , '$1,')).prepend('&yen;');
                        clone.find(".ef-sell_price").hide();
                    }
                    clone.insertBefore(item);
                });

                //商品のリンクにブランドidを設定
                if(item.parents().hasClass("ef-productclasses")){
                    //商品情報が0件の場合はSTYLING ITEMを非表示にする
                    if(datas.length == 0){
                        $('.cont-title').hide();
                    }else{
                        $('.cont-title').show();
                    }
                    add_brandid_to_product_link();
                }

                if(datas.length > 0){
                    item.remove();
                }

                //bxsliderを設定
                if(! $('.bxslider').hasClass('on_bxslider')){
                    coordinate_bxslider(datas);
                }
            }
        });
    }
})(jQuery);

// jQuery(function () {
//     jQuery('.ef-productclasses').ef('productclass');
//     jQuery('.ef-shops').ef('shop');
//     jQuery('.ef-snaps').ef('snap');
//     jQuery('.ef-blogs').ef('blog');
//     jQuery('.ef-news').ef('news');
// });
