﻿// JScript 文件
    //阿良.NET原创 http://www.chenjiliang.com 2008-12-20
    /* 轮换广告 ******************** ******************************** */
    function AD(text, imageSrc, url)
    {
        this.Text = text;
        this.ImageSrc = imageSrc;
        this.Url = url;
        this.Html = "";
    }
    String.prototype.Format = function()
    {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function(m, i)
        {
            return args[i];
        });
    };

    function Rotator(container, varName, interval, textClass, backColor, currentIndexClass, indexClass)
    {
        this.Container = container;
        this.DataSource = new Array();
        this.CurrentIndex = this.DataSource.Length - 1;
        this.VarName = varName;
        this.TextClass = textClass;
        this.CurrentIndexClass = currentIndexClass;
        this.IndexClass = indexClass;
        this.Inited = false;
        this.TimerID = null;
        this.Interval = interval;

        this.Add = function(ad)
        {
            this.DataSource.push(ad);
        }

        this.Init = function()
        {
            var imageHtml = "";
            var indexHtml = "";
            for (var i = 0; i < this.DataSource.length; i++)
            {
                var ad = this.DataSource[i];

                var temp;
                if (ad.Html == "")
                {
                    temp = "<div id='ad{2}' style='display:none'>"
                 + "<a href='{0}'><img style='border:none;width:100%;height=160px' onmouseover='{3}' onmouseout='{4}'"
                 + " src='{1}' title='{5}' /></a></div>";
                } else
                {
                    temp = "<div id='ad{2}' style='display:none'>"
                 + "<div onmouseover='{3}' onmouseout='{4}' title='{5}'>{6}</div>"
                 + "</div>";
                }
                imageHtml += temp.Format(ad.Url,
                    ad.ImageSrc,
                    i.toString(),
                    this.VarName + ".Pause()",
                    this.VarName + ".Restart()",
                    ad.Text,
                    ad.Html);

                temp = "<span class='{1}' onmouseover='{2}' onmouseout='{3}'"
                 + " onclick='location=\"{4}\"' title='{5}'>{0}</span>";
                indexHtml += temp.Format((i + 1),
                        this.IndexClass,
                        this.VarName + ".Pause();" + this.VarName + ".Show(" + i + ")",
                        this.VarName + ".Restart()",
                        ad.Url,
                        ad.Text);
            }

            var html = "<div style='width:100%;'>" + imageHtml + "</div>" //图片 
             + "<div style='background-color:" + backColor + "'>" //下面是显示文本和索引
             + "<div style='float:left;width:40%'></div>" //左边文本
             + "<div style='float:right;width:60%;text-align:right;text-align:right'>"
             + indexHtml + "</div>"//右边索引
             + "</div>";
            this.Container.innerHTML = "";
            this.Container.innerHTML = html;
        }

        this.Show = function(index)
        {
            //显示当前图片
            var nodes = this.Container.firstChild.childNodes;
            for (var i = 0; i < nodes.length; i++)
            {
                nodes[i].style.display = "none";
            }
            nodes[index].style.display = "block";

            //显示当前索引
            nodes = this.Container.lastChild.lastChild.childNodes;
            for (var i = 0; i < nodes.length; i++)
            {
                nodes[i].className = this.IndexClass;
            }
            nodes[index].className = this.CurrentIndexClass;

            //显示当前文本
            this.Container.lastChild.firstChild.innerHTML
        = "<a href='{0}' target='_blank' class='{2}'>{1}</a>".Format(
            this.DataSource[index].Url,
            this.DataSource[index].Text,
            this.TextClass);

            this.CurrentIndex = index;
        }
        this.Start = function()
        {
            if (this.Inited == false)
            {
                this.Init();
                this.Inited = true;
            }

            var nodes = this.Container.firstChild.childNodes;
            if (this.CurrentIndex < nodes.length - 1) this.CurrentIndex++;
            else this.CurrentIndex = 0;

            this.Show(this.CurrentIndex);

            this.Restart();
        }
        this.Restart = function()
        {
            this.TimerID = setTimeout(this.VarName + ".Start()", this.Interval);
        }
        this.Pause = function()
        {
            clearTimeout(this.TimerID);
        }
    }



