﻿PathSystem.Timer = function () {

    var _TimerDic = new Dictionary();
    var _PopupTimerDic = new Dictionary();

    var FTN = {};
    FTN.PUBLIC = {};


    FTN.PUBLIC = {
        Add: function (pOption) {
            var name = pOption.Name;
            var timerFunc = pOption.TimerFunc;
            var option = pOption.SetOption;
            var isPopupTimer = isValue(pOption.IsPopupTimer) ? pOption.IsPopupTimer : false;

            if (isPopupTimer == true) {
                if (_PopupTimerDic.containsKey(name)) {
                    FTN.PUBLIC.ClearForPopup(name);
                }

                _PopupTimerDic.add(name, $.timer(timerFunc));

                _PopupTimerDic[name].set(option);
                _PopupTimerDic[name].play();    // auto play
            }
            else {
                if (_TimerDic.containsKey(name)) {
                    FTN.PUBLIC.Clear(name);
                }

                _TimerDic.add(name, $.timer(timerFunc));

                _TimerDic[name].set(option);
                _TimerDic[name].play();    // auto play
            }
        },
        Clear: function (pName) {
            if (_TimerDic.containsKey(pName)) {
                _TimerDic[pName].stop();
                _TimerDic[pName].clearTimer();
                _TimerDic.remove(pName);
            }
        },
        ClearForPopup: function (pName) {
            if (_PopupTimerDic.containsKey(pName)) {
                _PopupTimerDic[pName].stop();
                _PopupTimerDic[pName].clearTimer();
                _PopupTimerDic.remove(pName);
            }
        },
        ClearAll: function () {
            _TimerDic.forEach(function (k, v) {
                v.stop();
                v.clearTimer();
            });
            _TimerDic.clear();
        },
        ClearAllForPopup: function () {
            _PopupTimerDic.forEach(function (k, v) {
                v.stop();
                v.clearTimer();
            });
            _PopupTimerDic.clear();
        }
    };

    return {
        Add: function () {
            FTN.PUBLIC.Add.apply(this, arguments);
        },
        Clear: function () {
            FTN.PUBLIC.Clear.apply(this, arguments);
        },
        ClearForPopup: function () {
            FTN.PUBLIC.ClearForPopup.apply(this, arguments);
        },
        ClearAll: function () {
            FTN.PUBLIC.ClearAll.apply(this, arguments);
        },
        ClearAllForPopup: function () {
            FTN.PUBLIC.ClearAllForPopup.apply(this, arguments);
        }
    };
}();