/**
 * Module
 * @author Jae Cho
 * Represents a module that can be embedded in a page.
 *
 * NOTE: in JavaScript, class is hierarchy is established using prototype.
 * Since prototype object is shared by all child objects, note that
 * properties of the prototypes are also shared.
 *
 * ChildModule.prototype = new Module();
 */
 function Module() {

    /** parent page - to be set when module is registered with page */
    this._page = null;

    /** module name: this should be overwritten in the child class
     * when a module is registered in a page, name is used as an ID of the module.
     * Thus make sure the name is unique to a given module.
     * */
    this._name = null;

    /** response object names - to be defined in child class */
    this.resObjectNames = null;

    /** owner of the module */
    this.ownerId = null;

    /** set page object - this needs to be called by page object */
    this.setPage = function(page) {
        this._page = page;
    };

    /** return module name */
    this.getName = function() {
        return this._name;
    };

    this.makeAjaxRequest = function() {
        var command = "";
        this._page.makeAjaxRequest(command);
    };

    /** initialize the module. child modules should imlement this method to
     * intiialize UI state and bind actions to elements.
     */
    this.initialize = function() {
    };

    /** list of initial AJAX request that needs to be made when a module loads */
    this.getInitRequests = function() {
        return [];
    };

    /** list of AJAX request that should be called to update the module upon login/logout
     * some pages refreshes themselves upon login state change
     * some pages can handle login/logout more 'gracefully' by simply iterating through its modules
     * calling this method. Modules that need to be updated w/o page refresh should implement this method
     */
     this.updateAndGetLoginChangeRequests = function() {
        return [];
     };


    /** handle array of response object - this is to be defined by child class
     * @name name of parameter
     * @value list of response objects
     */
    this.handleResObjects = function(resObjects) { };

    /** @return list of request actions that can be handled by this module */
    this.getPageActionNames = function() {
        return [];
    };

    /** handle request action
    * @action name of action to invoke
    * @params bean containing request parameters
    *
    * */
    this.handlePageAction = function(action, params) { };

    this.isViewedBySelf = function() {
        var uid = this._page.getLoggedInUserId();
        if (uid && uid == this.ownerId) {
            return true;
        }
        return false;
    };
}