if (!com) {
    var com = {};
}
if (!com.qq) {
    com.qq = {};
}
if (!com.qq.qzone) {
    com.qq.qzone = {};
}

if (!$) {
    $ = function(id){
        return document.getElementById(id);
    }
}

/**
 * 获取通过URL传递的参数值
 * @param {String} name 参数的名称
 */
function getParameter(name){
    var r = new RegExp("(\\?|#|&)" + name + "=([^&#]*)(&|#|$)");
    var m = location.href.match(r);
    return (!m ? "" : m[2]);
}
/**
 * 显示出一条信息，用于最大限度取代JS的alert()
 * @param {String} msg 信息的内容
 */
function showMsg(msg, t){
	var time = (t)? t : 3000;
	if(t == 0){
		time = 0;
	}
	try{
		top.QZONE.widget.msgbox.show(msg, 1, time);
	} catch (e){
		alert(msg);
	}
}

/**
 * 清除显示在首页的消息框
 */
function removeAllMsg(){
	try{
		top.QZONE.widget.msgbox.hide();
	} catch (e){}
}

/**
 * 提供基于QZFL构建的网络请求方法的包
 */
com.qq.qzone.net = {
	/**
	 * 模拟YouYee ViewPoint中的Gateway静态类
	 */
    Gateway: {
		//指定数据编码
        CHARSET: "utf-8",
		/**
		 * 请求一个CGI，并且根据请求的方法来确定使用JSONGetter还是FormSender
		 * @param {String} url CGI方法对应的地址
		 * @param {Object} data 需要发送的数据
		 * @param {Function} succ_handler 当成功返回时调用的函数
		 * @param {String} method 请求的方式，默认为 get
		 * @param {String} callbackFuncName CGI返回的回调函数名默认 _Callback
		 */
        call: function(url, data, succ_handler, method, callbackFuncName){
            if (method == "get" || !method) {
                var _loader = new QZONE.JSONGetter(url, void (0), data, this.CHARSET.toString());
                _loader.onSuccess = succ_handler;
                var cbfunc = (callbackFuncName != null && callbackFuncName != "") ? callbackFuncName : "_Callback";
                _loader.send(cbfunc);
            } else {
                var _loader = new QZONE.FormSender(url, "post", data, this.CHARSET.toString());
                if (_loader) {
                    _loader.onSuccess = succ_handler;
                    _loader.onError = function(){
                    };
                    _loader.send();
                }
            }
        },
		
		/**
		 * 显示CGI返回的提示，如果没有登录会自动弹出登录框
		 * @param {Object} re CGI返回的结果
		 * @param {int} t 时间默认为2秒
		 */
        showReturnMsg: function(re, t){
            if (re.ret) {
				showMsg(re.msg, t);
                if (re.ret == "error") {
                    if (re.type == "login") {
                        try {
                            top.showLoginBox();
                        } catch (e) {}
                    }
                }
            } else {
                showMsg(re.error.msg, t);
                if (re.error.type == "login") {
                    try {
                        top.showLoginBox();
                    } catch (e) {}
                }
            }
        }
    }
}

/**
 * 提供相关的实用工具
 */
com.qq.qzone.utils = {
    /**
     * 创建一个Timer对象，相当于ActionScript 3.0当中的Timer，能够指定频率和呼叫的次数
     * @param {int} delay Timer的调用频率
     * @param {uint} count Timer的调用次数，如果不指定则不断调用下去
     * @return {Timer} 一个Timer对象
     */
    Timer: function(delay, count){
        var _t = {};
        _t._t = null;
        _t.delay = delay;
        _t.count = (count) ? count : null;
        _t._cnt = _t.count;
        _t.onTimer = null;
        _t.onComplete = null;
        _t.start = function(){
            this.stop();
            this._t = setInterval(this._handleTimerEvent, delay);
        }
        _t.stop = function(){
			try{
            	clearInterval(this._t);
			} catch (e){}
            this._cnt = 0;
        }
        _t._handleTimerEvent = function(){
            if (_t._cnt < _t.count || _t.count == null) {
                if (_t.onTimer) {
                    _t.onTimer(_t._cnt);
                }
                _t._cnt++;
            }
            else {
                _t.stop();
                if (_t.onComplete) {
                    _t.onComplete();
                }
            }
        }
        return _t;
    }
}
