﻿;(function($) {
    $.fn.extend({
        "slider": function(options) {
            options = jQuery.extend({
                rowCount: 8, //总图片数减去已经显示的图片数+1
                slider: "#" + $(this).find("ul").attr("id"),
                left: "#left",
                right: "#right",
                autoScroll: true
            }, options);
            var rightcount = 1;
            var leftcount = 1;
            var thiswidth = [];
            var canenter = true;
            var flag = true;
            var rowCount = rowCount;
            var scrolldiv = "#" + this.attr("id");
            $(options.right).click(function() {


                var srollwidth2 = 0;
                if (!canenter) return;
                canenter = false;
                var srolldivwidth = $(scrolldiv).innerWidth(); //整个div的宽度
                var srollwidth = $(options.slider).find("li:first").innerWidth(); //第一个li的宽度

                srollwidth2 = srollwidth * leftcount; //已经往左滚动的宽度
                var wholewidth = $(options.slider).innerWidth(); //全部图片的宽度
                if (srollwidth2 + srolldivwidth >= wholewidth+20) {
                    canenter = true;
                    return;
                }
                thiswidth.push(srollwidth2);
                $(options.slider).animate({ left: "-" + srollwidth2 + 'px' }, 300, function() {
                    leftcount = leftcount + 1;
                    if (rightcount > 1) rightcount--;
                    canenter = true;
                });
                
                
                
            });
            $(options.left).click(function() {
            var srollwidth2 = 0;
            if (!canenter) return;
            canenter = false;
            var srollwidth2 = 0;
            try {
                if (thiswidth.length == 0) {
                    srollwidth2 = 0;
                } else {
                    srollwidth2 = thiswidth[thiswidth.length - 2];
                }
            } catch (e) {
                alert(e);
                return;
            }
            if (typeof (srollwidth2) == "undefined") {
                srollwidth2 = 0;
            }
            $(options.slider).animate({ left: "-" + srollwidth2 + 'px' }, 300, function() {
                rightcount = rightcount + 1;
                if (leftcount > 1) leftcount--;
                canenter = true;
                thiswidth.pop();
            });
            });
            //            if (options.autoScroll) {
            //                setTimeout(Scroll, 5000);
            //            }
            //            function Scroll() {		
            //                if (leftcount == options.rowCount && rightcount == 1) {
            //                    flag = false;
            //                }
            //                else if (rightcount == options.rowCount && leftcount == 1) {
            //                    flag = true;
            //                }
            //                //$("#showtext").append("rightcount:" + rightcount + "   leftcount:" + leftcount + "   flag:" + flag + "<br />");
            //                if (flag) {
            //                    $(options.left).click();
            //                }
            //                else {
            //                    $(options.right).click();
            //                }
            //                setTimeout(Scroll, 5000);
            //            }
        }
    });

})(jQuery);

