﻿/*
    base on jquery
    2011.10.14 lcheng
    轮播图的js
    所有样式class 是 TabSmartImgCss    对应所有的小图
    所有样式class 是 TabBigImgCss      对应所有的大图
    所有样式class 是 TabFont           对应所有的文字
*/

$(document).ready(function () {
    TabImgModel.TabSmartImgCss = ".TabSmartImg";
    TabImgModel.TabBigImgCss = ".TabBigImg";
    TabImgModel.TabFontCss = ".TabFont";
    TabImgModel.init();
});

var TabImgModel = {
    timer: null, //内部变量
    offset: 3000, //轮播时间间隔
    index: 0, //当前轮换位置
    imsSelectCss: "selected", //选中的图片的样式
    target: null, //内部变量
    targetBig: null, //内部变量
    targetFont: null, //内部变量
    ImgCount: 0, //可轮换的总数
    PreImgId: "gif_simg_bl", //上一个图片的按钮的ID
    NextImgId: "gif_simg_br", //下一个图片的按钮的ID
    TabSmartImgCss: ".TabSmartImg", //每个小图必须具有的样式
    TabBigImgCss: ".TabBigImg", //每个大图必须具有的样式
    TabFontCss: ".TabFont", //每个标题必须具有的样式
    init: function () {
        this.target = $(TabImgModel.TabSmartImgCss);
        this.targetBig = $(TabImgModel.TabBigImgCss);
        this.targetFont = $(TabImgModel.TabFontCss);
        this.ImgCount = this.target.length; //可轮换的总数
        TabImgModel.index = -1;
        this.auto();
        this.bindEvent();
    },
    auto: function () {//自动轮换
        TabImgModel.index++;
        if (TabImgModel.index > TabImgModel.ImgCount - 1) {
            TabImgModel.index = 0;
        }
        TabImgModel.TabImg(TabImgModel.index);
    },
    TabImg: function (_tempIndex) {//切换图片
        this.rechange(this.index);   //图片切换到某个位置
        this.slideImage(this.index); //大图切换到某个位置
        this.timer = window.setTimeout(this.auto, this.offset);
    },
    rechange: function (loop) {//图片切换到某个位置
        $(TabImgModel.TabSmartImgCss).parent().parent().removeClass(this.imsSelectCss);
        $(this.target[loop]).parent().parent().addClass(this.imsSelectCss);
    },
    slideImage: function (i) {//大图交替轮换
        $(this.targetBig).hide();
        $(this.targetBig[i]).show();
        $(this.targetFont).hide();
        $(this.targetFont[i]).show();
    },
    bindEvent: function () {//bind  上一个 下一个 
        $("#" + this.PreImgId + ",#" + this.NextImgId)
            .bind('click', function () {
                if (TabImgModel.timer) {
                    clearTimeout(TabImgModel.timer);
                }
                var id = this.id;
                if (id == TabImgModel.PreImgId) {
                    TabImgModel.index--;
                    if (TabImgModel.index < 0)
                        TabImgModel.index = TabImgModel.ImgCount - 1;
                } else {
                    TabImgModel.index++;
                    if (TabImgModel.index > TabImgModel.ImgCount - 1)
                        TabImgModel.index = 0;
                }
                TabImgModel.TabImg(TabImgModel.index);
            });
    },
    getIndex: function (v) {//get index
        for (var i = 0; i < target.length; i++) {
            if (TabImgModel.target[i] == v) return i;
        }
    }
};
