/**
 * @fileoverview Contains the definition of {@link ExampleSubscriber}, an example SL4B subscriber.
 */
 
 // This file follows JSLint and JSDoc standards.
 
 // Information for JSLint
/*extern RTSL_CreateUpdate, RTSL_DisplayUpdate, SL4B_AbstractSubscriber, SL4B_Accessor*/
/*members getElementId, getFieldName, getFieldValue, getObjects, 
    getRttpProvider, initialise, join, m_pObjectList, m_sFieldList, 
    prototype, ready, recordMultiUpdated, replace, size, split
*/

/** 
 * The constructor for the ExampleSubscriber object
 * If you are making subclasses of this object, you may want to override getElementId.
 * @param {String} sObjectList a comma separated list of object names to subscribe to.
 * @param {String} sFieldList a comma separated list of fields on the objects to subscribe to.
 * @param {Function} fElementMapper a function that accepts two strings - the object name and the field name
 * 					 and returns a String - the HTML element name to display the update in.
 */
function ExampleSubscriber(sObjectList, sFieldList, fElementMapper)
{
	if (typeof(sObjectList) !== "string") {
		throw new Error("The object list must be a string");
	}
	if (sObjectList === "") {
		throw new Error("The object list may not be empty");
	}
	if (typeof(sFieldList) !== "string") {
		throw new Error("The field list must be a string");
	}
	if (typeof(fElementMapper) !== "function") {
		throw new Error("The element mapper must be a function that maps objects to the ids of HTML elements.\nWas a \""+typeof(fElementMapper)+"\"");
	}

	this.m_pObjectList = sObjectList.split(",");
	this.m_sFieldList = sFieldList;
	this.m_fElementMapper = fElementMapper;

	// Any subclass of SL4B_AbstractSubscriber must call initialise in its constructor.
	// Once initialisation is finished, SL4B will call ready() on this object.
	this.initialise();
}

// Declares that the ExampleSubscriber is a subclass of SL4B_AbstractSubscriber
ExampleSubscriber.prototype = new SL4B_AbstractSubscriber();

/**
 * Ready is called by SL4B once initialisation is finished.
 */
ExampleSubscriber.prototype.ready = function() {
	// getObjects takes a space separated list of objects, and a comma separated list of fields.
	var sObjList = this.m_pObjectList.join(" ");

	// request the objects and fields specified within the ExampleSubscriber's constructor
	SL4B_Accessor.getRttpProvider().getObjects(this, sObjList, this.m_sFieldList);
};

/**
 * recordMultiUpdated is called by SL4B when it receives changes from Liberator.
 * @param {String} sObjectName The name of the object that updated
 * @param {SL4B_RecordFieldData} oFieldData The fields and values that have updated.
 */
ExampleSubscriber.prototype.recordMultiUpdated = function(sObjectName, oFieldData) {
	for (var i = 0, size = oFieldData.size(); i < size; i = i + 1) {
		var sFieldName = oFieldData.getFieldName(i);
		var sValue = oFieldData.getFieldValue(i);

		// We have to find the right HTML element to update with the information we have received
		var sElementId = this.m_fElementMapper(sObjectName, sFieldName);
		
		if (typeof(sElementId) == "string") {
			// Use the flash library to display the update.
			var oUpdate = RTSL_CreateUpdate(sElementId, "Relative", sValue, false, false, null, 1000);
			RTSL_DisplayUpdate(oUpdate);
		}
	}
};