/*
sns-base.js
requires: yahoo-dom-event
author: yubo@taobao.com
*/

if (typeof SNS === 'undefined' || !SNS) {
    SNS = function(o) {
        var S = SNS;

        if (!(this instanceof S)) {
            return new S(o);
        } else {
            return S._setup(o);
        }
    };
}

(function(S) {
    var Lang = YAHOO.lang;

	Lang.augmentObject(S, {

		/**
		 * 默认路径
		 */
		base: "http://assets.taobaocdn.com/app/sns/",

        /**
         * 所有添加的模块
         * 注：mod = { name: modName, fn: initFn, details: {...} }
         */
        mods: {},

        /**
         * 添加模块
         */
        add: function(name, fn, details) {

            this.mods[name] = {
                name: name,
                fn: fn,
                details: details || {}
            };

            return this; // chain support
        },

        /**
         * 配置信息
         */
        config: null,

        /**
         * setup to use
         */
        _setup: function(o) {
            o.debug = ('debug' in o) ? o.debug : false;
            this.config = o;
            return this;
        },

        /**
         * ready to use
         */
        use: function(callback) {
            this._loadModules();
            if(callback) callback(this);
        },

        /**
         * 已加载的模块
         */
        _attached: {},

        /**
         * 加载注册的所有模块
         */
        _loadModules: function() {
            var mods = this.mods,
                attached = this._attached,
                name, m;

            for (name in mods) {
                m = mods[name];

                if (!attached[name] && m) {
                    attached[name] = m;

                    if (m.fn) {
                        m.fn(this);
                    }
                }

                // 注意：m.details 暂时没用到，仅是预留的扩展接口
            }
        },

        /**
         * 创建命名空间
         */
        namespace: function() {
            var a = arguments, o = null, i, j, d;
            for (i = 0; i < a.length; i = i + 1) {
                d = ('' + a[i]).split('.');
                o = this;
                for (j = (d[0] == 'SNS') ? 1 : 0; j < d.length; j = j + 1) {
                    o[d[j]] = o[d[j]] || {};
                    o = o[d[j]];
                }
            }
            return o;
        },

        /**
         * 打印调试信息
         * @param {String} msg
         * @param {String} cat 支持 'info', 'warn', 'error', time', 'dir' 等
         */
        log: function(msg, cat) {
            var c = this.config, f;
            
            if(c && c.debug) {
            if (typeof console != 'undefined') {
                    f = (cat && console[cat]) ? cat : 'log';
                    console[f](msg);
                } else {
                    alert(cat + ':' + m);
                }
            }

            return this; // chain support
        }
    });
    // 预创建两个命名空间
    S.namespace('sys', 'app');

})(SNS);
/**
// 覆盖 pickDocumentDomain by mingcheng
if ('undefined' != TB.bom.pickDocumentDomain) {
    TB.bom.pickDocumentDomain = function() {
        var hostname = arguments[1] || location.hostname.toString();
        var hosts = hostname.split("."), len = hosts.length;
        var deep = arguments[0] || 2, deep = deep > len ? len : deep;
        for (var i = 0, result = [], tmp; i < deep; i++) {
            if ('undefined' != typeof hosts[len-i-1]) {
                result[deep-i-1] = hosts[len-i-1];
            }
        }
        return result.join('.');
    }
}
*/