﻿//<using>
//  nui/nui.js
//  nui/prototype.js
//  nui/lang.js
//</using>

/**
 * NUI.Ajax.
 * Copyright 2007, NetRanking.cn
 *
 * @namespace NUI.Ajax
 */
NUI.Ajax = (function() {
    var g_lang = NUI.Lang;
    
    /**
     * Call a server-side method
     * @method _call
     * @param vMethod {string}. The name of the server-side method
     * @param vParameters {object}. The parameters
     *      vParameters = {
     *          args : {array},             //Arguments for the server-side method
     *          func : {                    //The functions to execute after the server-side method return or expired
     *              success : function(res.value){},
     *              failed : function(){},
     *              expire : function(_run){}
     *          },
     *          timeout : {int}             //milliseconds of expire time
     *      }
     */
    function _call(vMethod, vParameters) {
        if(!g_lang.isString(vMethod)) {
            return;
        }
        
        var m_method = vMethod,
            m_args = [],
            m_func = {},
            m_timeout = 60000,
            m_end = false;
        if(vParameters != null) {
            m_args = (vParameters.args != null) ? vParameters.args : m_args;
            if(g_lang.isFunction(vParameters.func)) {
                NUI.Util.ObjectService.annex(m_func, {
                    success : vParameters.func
                });
            } else {
                NUI.Util.ObjectService.annex(m_func, vParameters.func);
            }
            m_timeout = (vParameters.timeout != null) ? vParameters.timeout : m_timeout;
        }
        m_args[m_args.length] = function(res) {
            if(m_end) {
                return;
            }
            if(res != null) {
                m_end = true;
                if(m_func.success) {
                    m_func.success(res.value);
                }
            } else {
                if(m_func.failed) {
                    m_func.failed();
                } else {
//                    if(confirm("抱歉，通信出错，是否重试？")) {
//                        _run();
//                    }
                }
            }
        };
        
        /**
         * Run the ajax method
         */
        function _run() {
            if(!Environment.Page || !Environment.Page[m_method]) {
                return;
            }
            Environment.Page[m_method].apply(Environment.Page, m_args);
            setTimeout(function() {
                if(m_end) {
                    return;
                }
                if(m_func.expire) {
                    m_func.expire(_run);
                } else {
//                    if(confirm("您刚才的操作过长时间没有反应，可能是网络中断或者请求超时造成，是否重试？")) {
//                        _run();
//                    }
                }
            }, m_timeout);
        }
        
        _run();
    }
    
    return {
        call : _call
    };
})();
