﻿function SildeGallery(parms) {
    parms = $.extend({
        bigpic: "SwitchBigPic", //大图DIV之ID通用部分, 没使用
        step: 430,
        slideContainerID: "slides",    //图片滑动容器
        smallpic: "menuItem_", //小图之ID通用部分
        actstyle: "act",
        inactstyle: "inact",
        totalcount: 4, 			//图片数量
        autotimeintval: 4500
    }, parms);
    this._current = 0;
    this._timer = null;
    this._isFirst = true;
    this._parms = parms;
    this._smallpics = new Array();

    for (var i = 0; i < this._parms.totalcount; i++) {
        this._smallpics.push(this._parms.smallpic + i);
    }

    $('#' + this._parms.slideContainerID).width(this._parms.totalcount * this._parms.step); //修改容器的宽度

    var This = this;
    $("#" + This._parms.smallpic + "0").addClass(This._parms.actstyle).siblings().addClass(This._parms.inactstyle); //初始化第一元素样式
    for (var s = 0; s < This._parms.totalcount; s++) {

        $("#" + This._smallpics[s]).click(function(scopedValue) {
            return function() {
                if (This._timer != null) {
                    clearInterval(This._timer);
                }
                $("#" + This._smallpics[This._current]).removeClass(This._parms.actstyle);
                This._current = scopedValue;

                $("#" + This._parms.slideContainerID).stop();
                This.animate($("#" + This._parms.slideContainerID), This._current, This._parms.step, 450);

                $("#" + This._smallpics[This._current]).addClass(This._parms.actstyle).siblings().addClass(This._parms.inactstyle);
                This.play();
            }
        } (s));
    }
}
SildeGallery.prototype = {
    animate: function(animateObj, current, step, animatetime) {
        animateObj.animate({ marginLeft: -(current * step) + 'px' }, animatetime);
    },
    play: function() {
        var This = this;
        This._timer = setInterval(function() {
            if (This._isFirst) {
                This._isFirst = false;
                return;
            }
            This._current++;
            if (This._current >= This._parms.totalcount) {
                This._current = 0;
            }
            This.animate($("#" + This._parms.slideContainerID), This._current, This._parms.step, 450);
            var prev = This._current;
            if (prev == 0) {
                prev = This._parms.totalcount;
            }
            $("#" + This._smallpics[prev - 1]).removeClass(This._parms.actstyle);
            $("#" + This._smallpics[This._current]).addClass(This._parms.actstyle).siblings().addClass(This._parms.inactstyle);

        }, This._parms.autotimeintval);
    }
};
