﻿

var online = [];

function QQPanel(id, data) {
    if ((this.id = this.get(id)) == null) return alert('传入的id在页面内未找到相应对象.'); //如果传入的id,在页面中不存在,弹出错误,并返回.
    this.url = 'http://webpresence.qq.com/getonline?Type=1&'; //查询QQ在线状态的URL地址.
    this.header = document.getElementsByTagName('head'); //取页面的head标签.
    this.items = this.id.childNodes; //取传入id的所有子节点.
    this.lens = this.items.length; //取所有子节点的个数
    this.index = 0; //设置索引值为 0
    this.data = data; //保存 传入的QQ号码的数组
    this.start(); //开始获取QQ号的在线状态
}
QQPanel.prototype = {
    get: function(id) { return document.getElementById(id); }, //定义 get 方法, 根据传入的id值,返回页面中的相应的对象.
    start: function() {//start函数.
        if (this.index >= this.lens) return this.running = false; //如果 索引值 大于或者等于 this.lens 就返回 false(并同时设置 状态值(running)的值为 false,退出函数体.
        this.running = true; //设置 状态值(running)为true,代表 start 函数正在运行过程中,作用是为了防止 reload 函数在没有检测完所有QQ在线状态的情况下,再次调用 start 函数.
        var script = document.createElement('script'); //创建一个 script.
        script.type = 'text/javascript'; //设置它的类型为 text/javascript 是,脚本使用的是javascript
        script.src = this.url + this.data[this.index] + ':&' + (+new Date());
        /*
        设置 script 的src 值为 腾讯的返回QQ状态值的URL地址.
        this.data[this.index] 是根据this.index 取相对应的 QQ号码,最后面的是加上系统时间(为防止浏览器缓存结果)
        +new Date() 等价于  (new Date()).getTime()
        */
        this.header[0].appendChild(script); //将 创建的 script 添加到 head上.
        script.onload = script.onreadystatechange = (function(script) {
            /*
            设置script 对象的 onload 和 onreadystatechange ,即载入script以后,执行的js代码.
            (注:IE下为 onreadystatechange ,其他某些浏览器为 onload)
            */
            var _this = this; //将this(当前对象)保存在 _this 变量中.
            return (function() {
                if (script.readyState) {//如果是 IE 内核的浏览器
                    if (script.readyState.match(/loaded|complete/i)) {//根据 readyState 值来判断 js有没有被载入完成.(载入完成后,它的值会被设置为 complete 或者是 loaded
                        _this.show()//执行 show函数.
                        _this = script = null; //断开对 this 和 script 的闭包内的引用.
                    }
                } else {//非IE内核的浏览器
                    _this.show()
                    _this = script = null; //功能同上.
                }
            });
        }).call(this, script);

    },
    show: function() {
        this.items[this.index].className = ''; //设置 根据 this.index 确定的 ul中的某个 li的样式为 空.(即不在线状态的样式)
        this.items[this.index].title = '溜狗网QQ客服不在线，请您留言，您可以使用在线客服或QQ留言！'; //设置它的 title 为 不在线的提示信息.
        if (online[0] == 1) {//如果 online[0] == 1 (QQ用户在线),则设置 根据 this.index 确定的这个li的样式为 online (即在线状态的样式)
            this.items[this.index].className = 'online';
            this.items[this.index].title = '溜狗网QQ客服在线，点击图标进行QQ沟通！';
        }
        //设置 li 下的 firstChild (即a标签) 的 href 值为,QQ临时会话的 URL地址.
        this.items[this.index].firstChild.href = 'tencent://message/?uin=' + this.data[this.index++] + '&Site=溜狗网&Menu=yes';
        this.start(); //再次调用 start 函数.检测 传递过来的QQ号码数组里面的下一个 QQ号的在线状态,注意上面这一行里面有一个 this.index++.
    },
    reload: function() {//重新检测QQ号码状态
        if (this.running || this.id == null) return false; //如果 start 函数未将数组内的QQ号码检测完毕,或者 传递的 id在页面中不存在,则返回.
        this.index = 0; //重置 this.index 为 0,使期指向 QQ号码数组中的第一个(也是ul中li的第一个)
        this.start(); //调用start函数.
    }
}

var QQOnline = new QQPanel('QQPanel', [646185059, 904460483]);
setInterval(function() {//每隔10秒调用一次, QQOnline的 reload 方法来重新 检测一次QQ的在线状态
    QQOnline.reload();
}, 10000);
//]]>


