if (typeof $Client == "undefined") var $Client = {
    serviceURL: function(service) {
        return $G.API_BASE + "server/api/s.php?service=" + service;
    },

    loginURL: function() {
        return $G.API_BASE + "server/api/login.php";
    },

    postData: function(method, params) {
        if (params == "") {
			var params = {
				skey: $Auth.getSessionKey()
			}
		} else {
  			params.skey = $Auth.getSessionKey();
		}

        adata = { jsonrpc: "2.0", method: method, params: params, id: Math.floor(Math.random() * 100) };
		jdata = $.toJSON(adata);

        return jdata;
    },

  	req: function(service, method, params, callback, base_url) {
		/*
		 * always append the sKey to the params object
		 */

        $Loading.addRequest();

        jdata = $Client.postData(method, params);
		service = $Client.serviceURL(service);
		$.ajax({
			url: service,
			type: "POST",
			contentType: "application/json-rpc",
			data: jdata,
			processData: false,
			success: function(data) {
				$Loading.removeRequest();
                if ($Client.chkPost(data)) {
                    data = $Client.decode(data);
					callback(data.result);
				}
			},
			error: function(data) {
                $Loading.removeRequest();
			}
		});
  	},

/*
 *  Same as req but not showing the Loading Dialog
 */
  	hiddenReq: function(service, method, params, callback) {

  		jdata = $Client.postData(method, params);
		service = $Client.serviceURL(service);
		$.ajax({
			url: service,
			type: "POST",
			contentType: "application/json-rpc",
			data: jdata,
			processData: false,
			success: function(data) {
				if ($Client.chkPost(data)) {
                    data = $Client.decode(data);
					callback(data.result);
				}
			},
			error: function(data) { }
		});
  	},

	chkPost: function(data) {
		data = $Client.decode(data);
		if (data.error) {
            if (data.error.message=='Unauthorized user' && ($G.URL_UNAUTH_REDIRECT)){
                $U.eraseCookie('sKey');
                open($G.URL_UNAUTH_REDIRECT,'_self');
                return false;
            }
            else{
                $U.errMsg($Lang.tr("A system exception was thrown."), data.error.message);
                return false;
            }
		}
		return true;
	},
	decode: function(jdata) {
		jdata = jdata.replace('Array{', '{'); // what's this problem about?
		data = $.evalJSON(jdata);
		return data;
	},
    httpsGo: function(w, force_private, force_public) {
        if (force_private) {
            goto_location = 'https://' + window.location.host + '/page/private#' + w + '?secure';
        } else if (force_public) {
            goto_location = 'https://' + window.location.host + '/page/' + w;
        } else {
            goto_location = 'https://' + window.location.host + window.location.pathname + '#' + w + '?secure';
	}
        window.location = goto_location;
	},
    upload: function(bt, method, data, ftype, callback) {
        if (data) {
            data.method = method;
        } else {
            data = { method: method };
        }
        data.skey = $Auth.getSessionKey();
        
        return new AjaxUpload(bt, {
            // Location of the server-side upload script
            action: "server/api/s.php?service=Uploader",
            // File upload name
            name: 'userfile',
            // Additional data to send
            data: data,
            // Submit file after selection
            autoSubmit: true,
            // The type of data that you're expecting back from the server.
            // HTML (text) and XML are detected automatically.
            // Useful when you are using JSON data as a response, set to "json" in that case.
            // Also set server response type to text/html, otherwise it will not work in IE6
            responseType: false,
            // Fired after the file is selected
            // Useful when autoSubmit is disabled
            // You can return false to cancel upload
            // @param file basename of uploaded file
            // @param extension of that file
            onChange: function(file, ext) {
            },
            // Fired before the file is uploaded
            // You can return false to cancel upload
            // @param file basename of uploaded file
            // @param extension of that file
            onSubmit: function(file, ext) {
                var cancel_upload = false;
                if (ftype == 'image') {
                    if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
                        cancel_upload = true;
                    }
                }
                if (ftype == 'video') {
                    //if (!(ext && /^(3gp|avi|wmv|mpg|mpeg|mov|rm|flv|mpe)$/.test(ext))) {
                    if (!(ext && /^flv$/.test(ext))) {
                        cancel_upload = true;
                    }
                }
                if (cancel_upload) {
                    $U.errMsg($Lang.tr("Error: invalid file extension."));
                    return false;
                } else {
                    $U.okMsg($Lang.tr("Uploading ..."));
                }
            },
            // Fired when file upload is completed
            // WARNING! DO NOT USE "FALSE" STRING AS A RESPONSE!
            // @param file basename of uploaded file
            // @param response server response
            onComplete: function(file, response) {
                var r = $Client.decode(response);
                if (r.status) {
                    callback(r.data);
                } else {
                    $U.errMsg($Lang.tr("An error occured"), r.message);
                }
            }
        });
    }
}

