// This JavaScript was automatically generated by
// Jayrock.JsonRpc.Web.JsonRpcProxyGenerator, Jayrock, Version=0.9.8316.0, Culture=neutral, PublicKeyToken=null
// on 30. oktober 2009 at 12:43:19 (Rom, normaltid)

// Proxy version 1.0

function Api(url) {
    /* Gets the current list of mast objects */

    this["getMasts"] = function(callback) {
        return call("getMasts", [], callback);
    }

    /* Gets the current list of mast objects */

    this["cleanMastsRepository"] = function(callback) {
        return call("cleanMastsRepository", [], callback);
    }

    /* Gets the current list of mast objects */

    this["populateMasts"] = function(callback) {
        return call("populateMasts", [], callback);
    }

    /* Saves a mast into the repository. Session aware method */

    this["saveMast"] = function(jsonMast, callback) {
        return call("saveMast", [jsonMast], callback);
    }

    /* Deletes a mast from the repository. Session aware method */

    this["deleteMast"] = function(id, callback) {
        return call("deleteMast", [id], callback);
    }

    /* Gets the time a mast was last updated */

    this["getLastUpdate"] = function(callback) {
        return call("getLastUpdate", [], callback);
    }

    /* Gets a dictoionary of mux text description */

    this["getMuxDictionary"] = function(callback) {
        return call("getMuxDictionary", [], callback);
    }

    /* Gets the keys of the mux dictoionary */

    this["getMuxKeys"] = function(callback) {
        return call("getMuxKeys", [], callback);
    }

    /* Gets the values of the mux dictoionary */

    this["getMuxValues"] = function(callback) {
        return call("getMuxValues", [], callback);
    }

    /* Logs in a user. Throws exception when password is wrong or an error occured */

    this["logIn"] = function(password, callback) {
        return call("logIn", [password], callback);
    }

    /* Determines whether a the requesting user is logged in. */

    this["amLoggedIn"] = function(callback) {
        return call("amLoggedIn", [], callback);
    }

    /* Logs out the current user */

    this["logOff"] = function(callback) {
        return call("logOff", [], callback);
    }

    /* Updates the main password, given the correct master password. Taken jsonobject of format {'password:'newpassword','master':'masterpassword'} */

    this["newPassword"] = function(jsonNewPassword, callback) {
        return call("newPassword", [jsonNewPassword], callback);
    }

    /* Returns an array of method names implemented by this service. */

    this["system.listMethods"] = function(callback) {
        return call("system.listMethods", [], callback);
    }

    /* Returns the version server implementation using the major, minor, build and revision format. */

    this["system.version"] = function(callback) {
        return call("system.version", [], callback);
    }

    /* Returns a summary about the server implementation for display purposes. */

    this["system.about"] = function(callback) {
        return call("system.about", [], callback);
    }

    var url = typeof (url) === 'string' ? url : '/ma/api.ashx';
    var self = this;
    var nextId = 0;

    function call(method, params, callback) {
        var request = { id: nextId++, method: method, params: params };
        return callback == null ?
            callSync(method, request) : callAsync(method, request, callback);
    }

    function callSync(method, request) {
        var http = newHTTP();
        http.open('POST', url, false, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.send(JSON.stringify(request));
        if (http.status != 200)
            throw { message: http.status + ' ' + http.statusText, toString: function() { return message; } };
        var response = JSON.eval(http.responseText);
        if (response.error != null) throw response.error;
        return response.result;
    }

    function callAsync(method, request, callback) {
        var http = newHTTP();
        http.open('POST', url, true, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.onreadystatechange = function() { http_onreadystatechange(http, callback); }
        http.send(JSON.stringify(request));
        return request.id;
    }

    function setupHeaders(http, method) {
        http.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
        http.setRequestHeader('X-JSON-RPC', method);
    }

    function http_onreadystatechange(sender, callback) {
        if (sender.readyState == /* complete */4) {
            var response = sender.status == 200 ?
                JSON.eval(sender.responseText) : {};

            response.xmlHTTP = sender;

            callback(response);
        }
    }

    function newHTTP() {
        if (typeof (window) != 'undefined' && window.XMLHttpRequest)
            return new XMLHttpRequest(); /* IE7, Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */
        else
            return new ActiveXObject('Microsoft.XMLHTTP'); /* WSH and IE 5 to IE 6 */
    }
}

Api.rpcMethods = ["getMasts", "cleanMastsRepository", "populateMasts", "saveMast", "deleteMast", "getLastUpdate", "getMuxDictionary", "getMuxKeys", "getMuxValues", "logIn", "amLoggedIn", "logOff", "newPassword", "system.listMethods", "system.version", "system.about"];
