//banner
$(document).ready(function () {
    var totWidth = 0;
    var positions = new Array();
    $('#slides .slide').each(function (i) {
        positions[i] = totWidth;
        totWidth += $(this).width();
        if (!$(this).width()) {
            alert("Please, fill in width & height for all your images!");
            return false;
        }
    });
    $('#slides').width(totWidth);
    $('#dots ul li a').click(function (e, keepScroll) {
        $('li.menuItem').removeClass('act').addClass('inact');
        $(this).parent().addClass('act');

        var pos = $(this).parent().prevAll('.menuItem').length;

        $('#slides').stop().animate({ marginLeft: -positions[pos] + 'px' }, 450);
        e.preventDefault();
        if (!keepScroll) clearInterval(itvl);
    });
    $('#dots ul li.menuItem:first').addClass('act').siblings().addClass('inact');
    var current = 1;
    function autoAdvance() {
        if (current == -1) return false;
        $('#dots ul li a').eq(current % $('#dots ul li a').length).trigger('click', [true]);
        current++;
    }
    var changeEvery = 20;
    var itvl = setInterval(function () { autoAdvance() }, changeEvery * 200);//设置自动播放时间，越大越慢
});




//图片
$(function (e) {

    var navLi = $(".nav ul li"),
        navUl = $(".nav ul"),
        speed = 400;
   
    function returnToOn()
    {
        n = navUl.find("li.on").index();
        navUl.stop().animate({ backgroundPosition: navLi.width() * n }, speed);
    }
    returnToOn();
    navLi.hover(
     function () {
         n = $(this).index();
         navUl.stop().animate({ backgroundPosition: navLi.width() * n }, speed);
         //$(this).addClass("on").siblings().removeClass("on");
     },
     function () {
         returnToOn();
     });
   

    $(".nav ul li").hover(function () {
        if (!$(".erji").is(":animated"))
            $(".erji").slideDown(500);
        $(".erji ul:eq(" + $(this).index(".ej") + ")").slideDown(200).siblings().hide();
    }, function () { });
    $("nav-side").hover(function () { }, function () {
        alert('me');
        $(".erji").stop().slideUp(500);
        $(".nav-side .nav_mainbav ul li:first").addClass("on").siblings().removeClass("on");
        n = $(".nav-side .nav_mainbav ul li:first").index();
        navUl.stop().animate({ backgroundPosition: navLi.width() * n }, speed);
    });
});


//图片滚动
var SellerScroll = function (options) {
    this.SetOptions(options);
    this.lButton = this.options.lButton;
    this.rButton = this.options.rButton;
    this.oList = this.options.oList;
    this.showSum = this.options.showSum;

    this.iList = $("#" + this.options.oList + " > li");
    this.iListSum = this.iList.length;
    this.iListWidth = this.iList.outerWidth(true);
    this.moveWidth = this.iListWidth * this.showSum;

    this.dividers = Math.ceil(this.iListSum / this.showSum);//共分为多少块 
    this.moveMaxOffset = (this.dividers - 1) * this.moveWidth;
    this.LeftScroll();
    this.RightScroll();
};
SellerScroll.prototype = {
    SetOptions: function (options) {
        this.options = {
            lButton: "left_scroll",
            rButton: "right_scroll",
            oList: "carousel_ul",
            showSum: 4//一次滚动多少个items 
        };
        $.extend(this.options, options || {});
    },
    ReturnLeft: function () {
        return isNaN(parseInt($("#" + this.oList).css("left"))) ? 0 : parseInt($("#" + this.oList).css("left"));
    },
    LeftScroll: function () {
        if (this.dividers == 1) return;
        var _this = this, currentOffset;
        $("#" + this.lButton).click(function () {
            currentOffset = _this.ReturnLeft();
            if (currentOffset == 0) {
                for (var i = 1; i <= _this.showSum; i++) {
                    $(_this.iList[_this.iListSum - i]).prependTo($("#" + _this.oList));
                }
                $("#" + _this.oList).css({ left: -_this.moveWidth });
                $("#" + _this.oList + ":not(:animated)").animate({ left: "+=" + _this.moveWidth }, {
                    duration: "slow", complete: function () {
                        for (var j = _this.showSum + 1; j <= _this.iListSum; j++) {
                            $(_this.iList[_this.iListSum - j]).prependTo($("#" + _this.oList));
                        }
                        $("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 1) });
                    }
                });
            } else {
                $("#" + _this.oList + ":not(:animated)").animate({ left: "+=" + _this.moveWidth }, "slow");
            }
        });
    },
    RightScroll: function () {
        if (this.dividers == 1) return;
        var _this = this, currentOffset;
        $("#" + this.rButton).click(function () {
            currentOffset = _this.ReturnLeft();
            if (Math.abs(currentOffset) >= _this.moveMaxOffset) {
                for (var i = 0; i < _this.showSum; i++) {
                    $(_this.iList[i]).appendTo($("#" + _this.oList));
                }
                $("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 2) });

                $("#" + _this.oList + ":not(:animated)").animate({ left: "-=" + _this.moveWidth }, {
                    duration: "slow", complete: function () {
                        for (var j = _this.showSum; j < _this.iListSum; j++) {
                            $(_this.iList[j]).appendTo($("#" + _this.oList));
                        }
                        $("#" + _this.oList).css({ left: 0 });
                    }
                });
            } else {
                $("#" + _this.oList + ":not(:animated)").animate({ left: "-=" + _this.moveWidth }, "slow");
            }
        });
    }
};
$(document).ready(function () {
    var ff = new SellerScroll();
});