/**
 * liquidsite.js
 *
 * This work is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * This work is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (c) 2007 Per Cederberg. All rights reserved.
 */

/**
 * The liquidsite namespace. This is implemented as a singleton
 * object, so that all global variables, functions and objects are
 * hidden from other JavaScript components.
 */
var liquidsite = {

    /**
     * The last request id used.
     */
    _lastRequestId: 0,

    /**
     * The request callback functions.
     */
    _requestCallbacks: {},

    /**
	 * Processes an asynchronous request. This function will add the
     * API request to the processing queue and return immediately. If
     * response notification is to be used, the callback argument
     * must specify a function to call.
     *
     * @param fun            the API function to call
     * @param callback       the optional response callback function
     * @param args           the API function arguments (varargs)
     */
    _processRequest: function(fun, callback) {
        var requestId = ++this._lastRequestId;
        var url = "api.js?requestId=" + requestId + "&function=" + fun;
        var parent;
        var script;

        this._requestCallbacks[requestId] = callback;
        for (var i = 0; i + 2 < arguments.length; i++) {
            url += "&arg" + i + "=" + arguments[i + 2];
        }
        parent = document.getElementsByTagName("head")[0];
        script = document.createElement('script');
        script.type = "text/javascript";
        script.src = url;
        parent.appendChild(script);
        return requestId;
    },

    /**
	 * Processes an asynchronous response. This function is called
     * from the JavaScript code generated by the response.
     *
     * @param requestId          the unique request id
	 * @param response           the response object
     */
    _processResponse: function(requestId, response) {
        var callback = this._requestCallbacks[requestId];

        delete this._requestCallbacks[requestId];
        if (callback != null) {
            callback.call(callback.object, response);
        } else {
            // TODO: store response for later polling?
        }
    }
}

/**
 * Creates a new liquidsite user object.
 *
 * @param login              the unique login name
 * @param realName           the real user name
 * @param email              the user email address
 */
liquidsite.User = function(login, realName, email) {
    this.login = login;
    this.realName = realName;
    this.email = email;
}

/**
 * Searches for a user by a specified email address. This function is
 * asynchronous and the resulting user object (or null) will be
 * delivered to the specified callback function.
 *
 * @param email              the email address
 * @param callback           the callback function for the response
 */
liquidsite.User.findByEmail = function(email, callback) {
    liquidsite._processRequest("findUserByEmail", callback, email);
}

