//
// Description: contains the base library common to all BET javascript code
// Date: 8/6/2008
//

var BET = {

    registerNamespace : function(ns) {
        
        //i doubt anyone is going to have a namespace with 499 parts, but there are depth limits and thus
        //we use the iterative approach
        var parts = ns.split(".");
        
        //declares the new namespace
        var defns = function(ns, part) {
          
            var newns;
            
            if((ns != null) && (ns != ''))
                newns = ns + "." + part;
            else
                newns = part;
                
            var code = "if(typeof(" + newns + ") == 'undefined') { eval('" + newns + " = {};'); }";
            
            eval(code);
            
        };
        
        if(parts.length > 0) {
            
            var currentns = '';
            for(var i = 0; i < parts.length; i++) {
                
                defns(currentns, parts[i]);
                
                if(i > 0)
                    currentns = currentns + "." + parts[i];
                else
                    currentns = parts[i];
                
            }
            
        }
        
    }
    
};


BET.registerNamespace("BET.js");

BET.js.Context = function(config) {
    
    var _private = {
      
        serviceRoot : function() {
            
            if(typeof(config.serviceRoot) == undefined)
                return "http://services.bet.com";
            else
                return config.serviceRoot;
            
        }(),
        
        webRoot : function() {
            
            if(typeof(config.webRoot) == undefined)
                return "http://www.bet.com";
            else
                return config.webRoot;
            
        }()
        
    };
    
    return {
      
        urlFromServiceRoot : function(url) {
            
            return _private.serviceRoot + url;
               
        },
        
        urlFromWebRoot : function(url) {
            
            return _private.webRoot + url;
        
        }
        
    };
    
};


BET.js.JsonRPC = function() {
    
    var _private = {
        
        _requestID : 0,
        
        nextID : function() {
            
            this._requestID = this._requestID + 1;
            return this._requestID;
            
        },
        
        isJsonRpcResponse : function(obj) {
            
            var ok = false;
            
            if((typeof(obj) != undefined) && (obj != null)) {
                
                if(typeof(obj.id) != undefined) {
                    
                    if(typeof(obj.result) != undefined) {
                        
                        if(typeof(obj.error != undefined)) {
                            
                            ok = true;
                            
                        }
                        
                    }
                    
                }
                
            }
            
            return ok;
            
        }
        
    }
    
    return {
      
        invoke : function(url, method, params, completed) {
        
            var request = {
            
                method : method,
                
                params : params,
                
                id : _private.nextID()
                
            };
            
            var ajaxSettings = {
            
                async : false,
                
                data : 'jsonr=' + JSON.stringify(request),
                
                dataType : "jsonp",
                
                processData : true,
                
                url : url,
                
                success : function(data, textStatus) {                                        
                    
                    var response = data;
                    
                    if(!_private.isJsonRpcResponse(response)) {                
                
                        response = {
                            
                            result : null,
                            error : 'Response from service located at: ' + url + ' with method: ' + method + ' was null or improperly formatted: ' + JSON.stringify(response),
                            id : request.id
                            
                        }
                        
                    }
                    else if(response.id != request.id) {                
                        
                        response = {
                            
                            result : null,
                            error : 'Response ID ' + response.id + ' does not match Request ID ' + request.id,
                            id : response.id
                            
                        }
                        
                    }
                    else {
                        
                        response = response.result;
                        
                    }
                    
                    completed(response);
                    
                }
                
            };
            
            //make the call synchronously
            jQuery.ajax(ajaxSettings);            
        
        }
      
    };
    
}();

