(function ($) {
    $.fn.extend({
        'SimpleNewsFade': function (ele = {title_box: '', prev: '', next: '', timer: 5000}) {
            var box = $(this);
            var len = box.find('ul li').length;
            var index = box.find('ul li').index(box.find('ul li:visible'));
            var title = box.find('ul li').eq(index).find('a').attr('title');
            var url = box.find('ul li').eq(index).find('a').attr('href');
            box.find(ele.title_box).find('p a').text(title).attr('href', url);
            var lock = false;
            var st = setInterval(function () {
                index++;
                if (index >= len) {
                    index = 0;
                }
                fadeIn(index);
            }, ele.timer);
            function fadeIn(index) {
                box.find('ul li').fadeOut();
                box.find('ul li').eq(index).fadeIn(500)
                title = box.find('ul li').eq(index).find('a').attr('title');
                url = box.find('ul li').eq(index).find('a').attr('href');
                box.find(ele.title_box).find('p a').text(title).attr('href', url);
                setTimeout(function () {
                    lock = false;
                }, 400)
            }
            box.mouseenter(function () {
                clearInterval(st);
            })
            box.mouseleave(function () {
                st = setInterval(function () {
                    index++;
                    if (index >= len) {
                        index = 0;
                    }
                    fadeIn(index);
                }, ele.timer);
            })
            box.find(ele.prev).click(function () {
                if (lock) return;
                lock = true;
                if (index == 0) {
                    index = len;
                }
                index--;
                fadeIn(index)
            })
            box.find(ele.next).click(function () {
                if (lock) return;
                lock = true;
                index++;
                if (index >= len) {
                    index = 0;
                }
                fadeIn(index)
            })
        },
        'zhuanTi': function (data) {
            var box = $(this);
            var item_width = data.iW;
            var width = 0;
            var len = box.find('ul li').length;
            var margin = parseInt(box.find('ul li').css('margin-right'));
            if (len > 0) {
                box.find('ul li').each(function () {
                    width += item_width == 'auto' ? ($(this).innerWidth() + margin) : item_width;
                })
            }
            var page = Math.ceil(len / 4);
            box.find('ul').width(width).data('page', 0);
            var lock = false
            box.find('em.prev').click(function () {
                if (lock) return;
                lock = true;
                var index = box.find('ul').data('page');
                if (index <= 0){
                    index = page - 1;
                } else {
                    index--
                }
                box.find('ul').data('page', index);
                box.find('ul').animate({left: -index * (1200 + margin)}, 400)
                setTimeout(function () {
                    lock = false;
                }, 400)
            })
            box.find('em.next').click(function () {

                if (lock) return;
                lock = true;
                var index = box.find('ul').data('page');
                if (index >= page - 1) {
                    index = 0;
                } else {
                    index++
                }

                box.find('ul').data('page', index);
                box.find('ul').animate({left: -index * (1200 + margin)}, 400)
                setTimeout(function () {
                    lock = false;
                }, 400)
            })
        },
        showToast: function (msg, time = 2000, callback) {
            $(this).append('<div id="toast" style="position: fixed; display: none; width: 368px; min-height: 30px; padding: 10px 0; line-height: 30px; text-align: center; background: rgba(0,0,0,0.5); color: #fff; left: calc(50% - 184px); top: 50%; border-radius: 5px; z-index: 1000">' + msg + '</div>');
            $('#toast').fadeIn(200, function () {
                setTimeout(function () {
                    $('#toast').fadeOut(200, function () {
                        $('#toast').remove();
                        if (typeof callback == 'function') {
                            callback();
                        }
                    })
                }, time)
            })
        },
        AjaxUploadFile: function (url, callback, progress) {
            var annex = $(this)[0].files[0];
            var form = new FormData();
            form.append('annex', annex);
            $.ajax({
                url: url,
                type: 'POST',
                data: form,
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (res) {
                    if (typeof callback == 'function') {
                        callback(res);
                    }
                },
                error: function(err) {
                    $('body').showToast('网络出了点小问题', 2000)
                },
                xhr: function () {
                    var xhr = $.ajaxSettings.xhr();
                    if (xhr.upload) {
                        xhr.upload.addEventListener("progress", function (e) {
                            var loaded = e.loaded; //已经上传大小情况
                            var tot = e.total; //附件总大小
                            var per = Math.floor(100 * loaded / tot); //已经上传的百分比
                            if (typeof progress == 'function') {
                                progress(per);
                            }
                        }, false);
                    }
                    return xhr;
                }
            })
        },
        newsFadeIn: function(p) {
            var dotCurrClass = p.dotCurr;
            var dot_box = $(this).find(p.dot);
            var count = $(this).find('ul li').length;
            var delay = p.delay || 4000;
            var dotHtml = '';
            var slt;
            var _this = $(this);
            for(var i = 0; i < count; i++) {
                if (i == 0) {
                    dotHtml += '<span class="' + dotCurrClass + '"></span>';
                } else {
                    dotHtml += '<span></span>';
                }
            }
            var index = 0;
            dot_box.html(dotHtml);
            dot_box.find('span').click(function () {
                $(this).addClass(dotCurrClass).siblings().removeClass(dotCurrClass);
                index = dot_box.find('span').index($(this));
                fadeFun(index);
            });

            slt = setInterval(function () {
                active();
            }, delay);
            $(this).find('li').mouseenter(function () {
                clearInterval(slt);
            });
            $(this).find('li').mouseleave(function () {
                clearInterval(slt);
                slt = setInterval(function () {
                    active();
                }, delay);
            })
            function active () {
                var index = dot_box.find('span').index(dot_box.find('span' + '.' + dotCurrClass));
                index++;
                if (index == count) index = 0;
                fadeFun(index);
                dot_box.find('span').removeClass(dotCurrClass);
                dot_box.find('span').eq(index).addClass(dotCurrClass);
            }

            function fadeFun(index) {
                _this.find('li').fadeOut(500);
                _this.find('li').eq(index).fadeIn(500);
            }
        }
    })
})(jQuery)