(function ($) {
    $.fn.extend({
        "gallery": function (options) {
            if (!isValid(options))
                return this;
            opts = $.extend({}, defaults, options);

            return this.each(function () {
                var $this = $(this);
                var imgNum = $this.children("." + opts.galleryClass).find("span").length;  //ͼƬ������
                var galleryWidth = $this.children("." + opts.galleryClass).width();  //չʾͼƬ���ֿ��
                var imgWidth = $this.children("." + opts.galleryClass).find("span").width();  //ÿ��ͼƬ�Ŀ��
                var imgHeight = $this.children("." + opts.galleryClass).find("span").height();  //ÿ��ͼƬ�ĸ߶�
                $this.prepend("<span class='glyphicon glyphicon-backward prev'></span>");
                $this.append("<span class='glyphicon glyphicon-forward next'></span>");
                var arrowHeight = $this.children("span").height();  //ǰ���ͷ�ĸ߶�
                var arrowTop = (imgHeight - arrowHeight) / 2;  //ǰ���ͷ�ඥ���ľ���
                $this.children("span").css({"top": 0 + "px"});
                assignImgWidth = galleryWidth / opts.showImgNum;  //��ÿ��ͼƬ����Ŀ��
                var ulWidth = imgNum * assignImgWidth;  //ul���ܿ��
                $this.find("ul").width(ulWidth);
                // var imgMarginWidth = (assignImgWidth - imgWidth) / 2;  //ÿ��ͼƬ��������߾�
                $this.find("li").css({'margin-right': 1 + "px"});
                hiddenWidth = ulWidth - galleryWidth;  //����ͼƬ��ʾ���ֵĿ��
                // var t = setTimeout(function () {
                //     rightScroll($this, t);
                // }, opts.interval);
                // console.log(t);
                bindEvent($this, 0);
            });
        }
    });
    var opts, assignImgWidth, hiddenWidth;
    var defaults = {
        duration: 500,  //����ͼƬ�ֲ�����ʱ��
        interval: 5000,  //ͼƬ�ֲ���������һ��ͼƬ�ֲ���ʼ���ʱ��
        showImgNum: 5,  //ͬʱչʾ��ͼƬ����
        galleryClass: "gallery"  //����class
    };

    function isValid(options) {
        return !options || (options && typeof options === "object") ? true : false;
    }

    function bindEvent($this, t) {
        $this.children(".next").click(function () {
            rightScroll($this, t);
        });
        $this.children(".prev").click(function () {
            leftScroll($this, t);
        });
    }

    function unbindEvent($this, t) {
        $this.children(".next").unbind("click");
        $this.children(".prev").unbind("click");
    }

    function rightScroll($this, t) {
        clearTimeout(t);
        unbindEvent($this, t);
        var left = parseInt($this.find("ul").css("left"));
        if (left > -hiddenWidth)
            $this.find("ul").animate({left: "-=" + assignImgWidth + "px"}, opts.duration, function () {
                bindEvent($this, t);
            });
        else
            $this.find("ul").animate({}, opts.duration, function () {
                bindEvent($this, t);
            });
        // var t=setTimeout(function(){rightScroll($this,t);},opts.interval+opts.duration);
    }

    function leftScroll($this, t) {
        clearTimeout(t);
        unbindEvent($this, t);
        var left = parseInt($this.find("ul").css("left"));
        if (left < 0)
            $this.find("ul").animate({left: "+=" + assignImgWidth + "px"}, opts.duration, function () {
                bindEvent($this, t);
            });
        else
            $this.find("ul").animate({}, opts.duration, function () {
                bindEvent($this, t);
            });
        // var t=setTimeout(function(){rightScroll($this,t);},opts.interval+opts.duration);
    }
})(window.jQuery);