﻿//create by chenhao 2009-08-31  version 1.0.0.09083100

function newPageChange() {
    ///	<summary>
    ///	创建多个页签切换的新闻脚本的实例
    ///	</summary>
    ///<returns>返回新的实例</returns>
    return {
        jsonData: [],   //总条数     jsonData格式：[{title:value,content:value}]
        isShowForEach: false, //是否打开自动切换
        lastSelectIndex: 0, //自动切换结束后选中的节点,如果设置的索引小于0则进入无限循环
        selectIndex: 0, //当前的选中节点
        titleBackgroundImage: null, //标题的普通的图片
        titleSelectImage: null, //标题的选中的图片
        delay: 4000, //延迟设定  只有当isshowforeach打开时才有效果
        showIndexAndStopInit: function(index) {
            ///	<summary>
            ///	显示指定的对象 外部调用建议使用这个
            ///	</summary>
            ///<param name="index">指定的索引,如果超出范围 则认为全部不选</param>
            this.isShowForEach = false;
            this.selectIndex = index;
            this.show();
        },
        showIndex: function() {
            ///	<summary>
            ///	显示指定的对象 主要是提供给内部使用
            ///	</summary>
            ///<param name="index">指定的索引,如果超出范围 则认为全部不选</param>
            if (this.isShowForEach) {
                this.show();
                this.selectIndex++;
                var _this = this;
                if (this.selectIndex < this.jsonData.length) {
                    setTimeout(function() { _this.showIndex() }, this.delay);
                } else {
                    //2010-02-24增加支持死循环控制 开始，如果需要恢复原来功能  去掉此注释到结束部分
                    if (this.lastSelectIndex < 0) {
                        this.selectIndex = 0;
                        setTimeout(function() { _this.showIndex() }, this.delay);
                    }
                    else
                    //2010-02-24增加支持死循环控制 结束
                        setTimeout(function() { _this.showIndexAndStopInit(_this.lastSelectIndex) }, this.delay);
                }
            }
        },
        show: function() {
            ///	<summary>
            ///	显示selectIndex指定的对象
            ///	</summary>
            if (this.jsonData != null) {
                for (var i = 0; i < this.jsonData.length; i++) {
                    if (i == this.selectIndex) {
                        this.jsonData[i].title.style.backgroundImage = this.titleSelectImage;
                        this.jsonData[i].title.style.fontWeight = 'bold';
                        this.jsonData[i].content.style.display = "";
                        this.jsonData[i].more.style.display = "";
                    } else {
                        this.jsonData[i].title.style.backgroundImage = this.titleBackgroundImage;
                        this.jsonData[i].title.style.fontWeight = 'normal';
                        this.jsonData[i].content.style.display = "none";
                        this.jsonData[i].more.style.display = "none";
                    }
                }
            }
        },
        init: function(objectId, titleBackgroundImageUrl, titleSelectImageUrl, isShowForEach, lastSelectIndex, delayTime) {
            ///	<summary>
            ///	初始化对象
            ///	</summary>
            ///<param name="objectId">对象的id</param>
            ///<param name="titleBackgroundImageUrl">未选择状态的图片路径</param>
            ///<param name="titleSelectImageUrl">选择状态的图片路径</param>
            ///<param name="isShowEach">是否需要在初始化后进行页签自动切换浏览</param>
            ///<param name="lastSelectIndex">最后选中的模块的索引,如果设置的索引小于0则进入无限循环</param>
            ///<param name="delayTime">自动切换时间差,只有isshoweach为true时才生效</param>
            var obj = document.getElementById(objectId);
            var tempData = obj.getElementsByTagName("li")
            this.lastSelectIndex = lastSelectIndex;
            this.titleBackgroundImage = "url('" + titleBackgroundImageUrl + "')";
            this.titleSelectImage = "url('" + titleSelectImageUrl + "')";
            this.isShowForEach = isShowForEach;
            if (delayTime > 0) {
                this.delay = delayTime;
            }
            function getJsonObject(index) {
                function getTitle() { return tempData[index].getElementsByTagName("span")[0]; }
                function getContent() { return document.getElementById(getTitle().getAttribute("rel")); }
                function getMoreLink() { return document.getElementById(getTitle().getAttribute("relMore")); }
                return { title: getTitle(), content: getContent(), more: getMoreLink() };
            }

            //生成对象数据
            var _this = this;
            this.jsonData = [];
            for (var i = 0; i < tempData.length; i++) {
                this.jsonData[i] = getJsonObject(i);
                //                this.jsonData[i].title.onclick = function() {
                //                    _this.showIndexAndStopInit(i);
                //                }
            }
            //自动切换显示功能
            if (isShowForEach) {
                this.showIndex();
            } else {
                this.showIndexAndStopInit(this.lastSelectIndex);
            }
        }
    }

}
