/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */

/**
 * Created by huang.dy on 2016/04/21 13:56
 */
define([], function () {
    var visibilityListener = new Array();
    var visibilityRunning = false;
    var frequency = 400;

    function findValue(id) {
        for (var i = 0; i < visibilityListener.length; i++) {
            if (visibilityListener[i].key === id) {
                return visibilityListener[i];
            }
        }
        return null;
    }

    function refresh() {
        if (visibilityRunning) {
            if (visibilityListener.length === 0) {
                visibilityRunning = false;
            }
        } else {
            if (visibilityListener.length !== 0) {
                visibilityRunning = true;
                setTimeout(handleVisibilityChanged, frequency);
            }
        }
    }

    function handleVisibilityChanged() {
        if (visibilityRunning) {
            // 需copy一份副本，否则callback时又调用了removeVisibilityListener就会导致error
            var copyVisibilityListener = visibilityListener.slice(0, visibilityListener.length);
            for (var i = 0; i < copyVisibilityListener.length; i++) {
                var oldVisible = copyVisibilityListener[i].visible;
                var element = $("#" + copyVisibilityListener[i].key);
                if (element.length === 0) {
                    //说明节点已经销毁，无需监听，并从队列中remove(由于循环的是副本，可以在此remove)
                    this.removeVisibilityListener($("<div></div>").attr("id", copyVisibilityListener[i].key));
                    continue;
                }
                var visible = element.is(":visible");
                if (visible !== oldVisible) {
                    copyVisibilityListener[i].visible = visible;
                    for (var j = 0; j < copyVisibilityListener[i].callbacks.length; j++) {
                        copyVisibilityListener[i].callbacks[j](visible);//callback
                    }
                }
            }
            setTimeout(handleVisibilityChanged, frequency);
        }
    }

    return {
        addVisibilityListener: function (element, callback) {
            var id = element.attr("id");
            if (id) {
                var value = findValue(id);
                if (!value) {
                    visibilityListener.push({
                        key: id,
                        callbacks: [callback],
                        visible: element.is(":visible")
                    });
                    refresh();
                } else {
                    value.callbacks.push(callback);
                }
            }
        },
        removeVisibilityListener: function (element) {
            var id = element.attr("id");
            if (id) {
                var value = findValue(id);
                if (value) {
                    visibilityListener.splice(visibilityListener.indexOf(value), 1);
                    refresh();
                }
            }
        }
    };
});