HJ.BO_Data = Class.create();

HJ.BO_Data.prototype = {
    
    initialize: function() {
        // doesn't do anything.  
        // getDataWrapper to get the correct type of object
    },

    // WARNING: no type-checking here. 
    setData: function(data) {
        this.data = data;
    },

    getData: function() {
        return this.data;
    },

    getId: function() {
        return this.data.UUID;
    },

    getLastUpdateDate: function() {
        var millis;
        if (this.data.lastUpdateDate != null) {
            millis = this.data.lastUpdateDate;
        } else if (this.data.createDate != null) {
            millis = this.data.createDate;
        } else {
            // Should not happen for new objects, only old ones from the 
            // database.  Choose an arbitraty old date
            return new Date(0);
        }
        return new Date(parseInt(millis));
    },

    // by default, return empty
    getTemplateType: function() {
        return "";
    }, 

    /**
     * Peform a shallow copy of an object
     *
     * @param object 
     * the object to copy
     * 
     */
    copyObject: function(old, clearIds) {
        copy = new Object();
        for (field in old) {
            copy[field] = old[field];
        }
        if (!clearIds) {
            return copy;
        }
        copy.UUID = null;
        copy.__id = null;
        copy.createDate = null;
        return copy;
    }

};

HJ.Event_Data = Class.create();

HJ.Event_Data.prototype = Object.extend(new HJ.BO_Data(), {

    initialize: function(data) {
        this.data = data;
    },
    
    getDisplayName: function() {
        return this.data.displayName;
    }
});

HJ.MyList_Data = Class.create();

HJ.MyList_Data.prototype = Object.extend(new HJ.BO_Data(), {

    initialize: function(data) {
        this.data = data;
    },
    
    getDisplayName: function() {
        return this.data.name;
    },
    
    getSaveSend: function() {
    	return this.data.saveSend;
    },

    getTemplateType: function() {
        if (this.data.templateType != null) {
            return this.data.templateType;
        }
/*
        // must be an old-style list or signup
        // XXX: it's not clear that isHelperSelected 
        // is enough.  The backend query might need to check the 
        // MyListItem helper fields
        if (this.data.startDT != null || this.data.isHelperSelected == "Yes") {
            return "GenericSignup";
        }
		/*
        return "SimpleList";
        */

        return "CheckList";
    },

    // XXX: I'm not at all sure this works yet
    // also, we might just want to add fields in the copy rather than 
    // copy then set some fields to null, or maybe even better, 
    // do the copy in the backend.
    copy: function() {
        var old = this.getData();
        var copy = this.copyObject(old, true);
        copy.lastEmailString = null;
        copy.lastEmailSubject = null;
        copy.saveSend = null;
        if (old.item == null) {
            return copy;
        }
        copy.item = new Array(old.item.length);
        // make the item array a copy, not references
        for (var i = 0; i < old.item.length; i++) {
            copy.item[i] = this.copyObject(old.item[i], true);
            copy.listId = null;
            copy.helperEventId = null;
            copy.taskDone = false;
            copy.referenceUUID = null;
            copy.referenceName = null;
        }
        return copy;
    }

});

HJ.Data_ObjectFactory = Class.create();

HJ.Data_ObjectFactory.prototype = {

    initialize: function() {
        // nothing
    },
    
    // Get the appropriate object for the type of 
    // data
    get: function(data) {
        if (data.__className == "Event") {
            return new HJ.Event_Data(data);
        }
        if (data.__className == "MyList") {
            return new HJ.MyList_Data(data);
        }
        if (data.__className == "Response") {
            return new HJ.Response_Data(data);
        }
        if (data.__className == "Attachment") {
            return new HJ.Attachment_Data(data);
        }
    }
}

HJ.Attachment_Data = Class.create();

HJ.Attachment_Data.prototype = Object.extend(new HJ.BO_Data(), {

    initialize: function(data) {
        this.data = data;
    },
    
    getDisplayName: function() {
        // XXX: Truncate if too long?
        return this.data.path;
    }
});

HJ.Response_Data = Class.create();

HJ.Response_Data.prototype = Object.extend(new HJ.BO_Data(), {

    initialize: function(data) {
        this.data = data;
    },
    
    getSaveSend: function() {
    	return this.data.reqType;
    },

    getInviteeId: function() {
    	return this.data.inviteeId;
    },

    getParentId: function() {
    	return this.data.parentId;
    },

    getUserId: function() {
    	return this.data.userId;
    },

    getDisplayName: function() {
        // XXX: Truncate if too long?
        return this.data.name;
    }
});

