/*

	Class: VIEWZI.datastore

	The Viewzi datastore class is for general providing a local memory cache off
	all the results pulled by the datapull class.
	

*/

/*

	init the class and kick things off.

*/
	
	VIEWZI.datastore.init = function()
	{
		VIEWZI.datastore.pullResults	= [];
	};
	
/*
	Function: VIEWZI.datastore.dataresult

		Catch method for data call returns from the server.

		This method is listening to all DQO calls and storing the results in a memory data cache. 
		The data store is used to lookup node information from data pull results and using data pulls
		to perform data manipulation such as deduplication and whatnot.

	Parameters:

		evtType 	- the event type that was executed.
		evtData		- the data result of the call event

	Returns:

		Null
*/

	VIEWZI.datastore.dataresult = function (evtType, evtData)
	{
		var id = evtData[0].id;
		if (!VIEWZI.datastore.pullResults[id]) VIEWZI.datastore.pullResults[id] = [];

		var c = evtData[0].data.length;
		for (var x=0; x<c; x++) {
			var num = parseInt(evtData[0].data[x].id,10);
			VIEWZI.datastore.pullResults[id][num] = evtData[0].data[x];
		}

		VIEWZI.trace.fire({module:"DATASTORE",msg:"stored pull ("+id+")"});
	};
	
/*
	Function: VIEWZI.datastore.getdata

		Retrieval method for getting previously pulled data out of the data store.

	Parameters:

		id		- the original unique identifier for the data call
		num		- the length of the results to return
		page	- the page of data results you want returned

	Returns:

		Object
*/
	
	VIEWZI.datastore.getdata = function (id, num, page)
	{
		VIEWZI.trace.fire({module:"DATASTORE",msg:"data requested ("+id+")"});
		
		if (id == "*") return VIEWZI.datastore.pullResults;
		
		if (typeof id  == "string") {
			if (VIEWZI.datastore.pullResults[id]) return VIEWZI.datastore.pullResults[id];
		} else if (typeof id  == "object") {
			var d_arr	= [];
			for (var x=0; x<id.length; x++) {
				if (VIEWZI.datastore.pullResults[id[x]]) d_arr[id[x]] = VIEWZI.datastore.pullResults[id[x]];
			}
			return d_arr;
		}
		return 0;
	};
	
/*
	Function: VIEWZI.datastore.listall

		return a listing of all currently stored data pulls in the store.

	Returns:

		String
*/

		VIEWZI.datastore.listall = function()
		{
			var d	= [];
			for (var x in VIEWZI.datastore.pullResults) {
				d.push(x);
			}
			return d.toString();
		};
	
	VIEWZI.datastore.init();
	VIEWZI.dataresult.subscribe(VIEWZI.datastore.dataresult,VIEWZI.datastore);