/*
==轮播{对象|对象属性}==
对象属性{宽度|高度|文字大小|自动切换时间}
*/
function dk_slideplayer(object, config) {
    this.obj = object;
    this.config = config ? config : { width: "256px", height: "288px", fontsize: "12px", right: "10px", bottom: "10px", time: "5000" };
    this.pause = false;
    var _this = this;
    if (!this.config.right) {
        this.config.right = "0px"
    }
    if (!this.config.bottom) {
        this.config.bottom = "3px"
    }
    if (this.config.fontsize == "12px" || !this.config.fontsize) {
        this.size = "12px";
        this.height = "25px";
        this.right = "6px";
        this.bottom = "10px";
    } else if (this.config.fontsize == "14px") {
        this.size = "14px";
        this.height = "25px";
        this.right = "6px";
        this.bottom = "15px";
    }
    this.count = jQuery(this.obj + " li").size();
    this.n = 0;
    this.j = 0;
    var t;
    this.factory = function () {
        jQuery(this.obj).css({ position: "relative", zIndex: "0", margin: "0", padding: "0", width: this.config.width, height: this.config.height, overflow: "hidden" })
        jQuery(this.obj).prepend("<div style='position:absolute;z-index:20;right:" + this.config.right + ";bottom:" + this.config.bottom + "'></div>");
        jQuery(this.obj + " li").css({ width: "100%", height: "100%", overflow: "hidden" }).each(function (i) { jQuery(_this.obj + " div").append("<a>" + (i + 1) + "</a>") });

        jQuery(this.obj + " img").css({ border: "none", width: "100%", height: "100%" })

        this.resetclass(this.obj + " div a", 0);

        jQuery(this.obj + " p").each(function (i) {
            jQuery(this).parent().append(jQuery(this).clone(true));
            jQuery(this).html("");/*黑色背景*/
            jQuery(this).css({ position: "absolute", margin: "0", padding: "0", zIndex: "1", bottom: "0", left: "0", height: _this.height, width: "100%", background: "#000", opacity: "0.4", overflow: "hidden" })/*背景字体颜色*/
            jQuery(this).next().css({ position: "absolute", margin: "0", padding: "0", zIndex: "2", bottom: "0", left: "0", height: _this.height, lineHeight: _this.height, textIndent: "5px", width: "100%", textDecoration: "none", fontSize: _this.size, color: "#FFFFFF", background: "none", zIndex: "1", opacity: "1", overflow: "hidden" })
            if (i != 0) { jQuery(this).hide().next().hide() }
        });

        this.slide();
        this.addhover();
        t = setInterval(this.autoplay, this.config.time);
    }

    this.slide = function () {
        jQuery(this.obj + " div a").mouseover(function () {
            _this.j = jQuery(this).text() - 1;
            _this.n = _this.j;
            if (_this.j >= _this.count) { return; }
            jQuery(_this.obj + " li").hide();
            jQuery(_this.obj + " p").hide();
            jQuery(_this.obj + " li").eq(_this.j).fadeIn("slow");
            jQuery(_this.obj + " li").eq(_this.j).find("p").show();
            _this.resetclass(_this.obj + " div a", _this.j);
        });
    }

    this.addhover = function () {
        jQuery(this.obj).hover(function () { clearInterval(t); }, function () { t = setInterval(_this.autoplay, _this.config.time) });
    }

    this.autoplay = function () {
        _this.n = _this.n >= (_this.count - 1) ? 0 : ++_this.n;
        jQuery(_this.obj + " div a").eq(_this.n).triggerHandler('mouseover');
    }

    this.resetclass = function (obj, i) {
        jQuery(obj).css({ float: "left", marginRight: "3px", width: "15px", height: "14px", lineHeight: "15px", textAlign: "center", fontWeight: "800", fontSize: "12px", color: "#000", background: "#FFFFFF", cursor: "pointer" });
        jQuery(obj).eq(i).css({ color: "#FFFFFF", background: "#CE0307", textDecoration: "none" }); /*移上去的颜色*/
    }

    this.factory();
}


(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array(n + 1).join(str);
        }

        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')

            singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);

            /* TASKS */

            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }

            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');

            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);

            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft: '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }

                    currentPage = page;
                });
            }

            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow vl">&lt;</a><a href="#" class="arrow vr">&gt;</a>');

            // 6. bind the back and forward links
            $('a.vl', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });

            $('a.vr', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });

            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

