/** 
 * Data model for history. 
 */



/** 
*
* @constructor
* @class History data model 
*/
function HistoryModel() {
	this.results = [];
	this.blGetData = false;
	TSWIDGET.EVENTS.createEvent("evtHistoryLoaded");			
}

/**
 * Initialize the model from static data
 */
HistoryModel.prototype.init = function(){
	TSWIDGET.log("History Model initialized");	
}

HistoryModel.prototype.fetch = function(){
	TSWIDGET.log("HistoryModel::fetch()");
	if (this.blGetData == false) {								// first request
		var responseSchema = {
							resultNode: "PRODUCT",
							fields: [{key: "ID"},{key: "THUMBNAIL"},{key:"ARTIST"},
									 {key:"URL"},{key:"PRICE"},{key:"NAME"},{key:"FORMAT_NAME"}],
							metaNode : "TSCoreAPI_SEARCHR2_GETRECENTLYVIEWED_REPLY",
						    metaFields : { 
								error: "ERROR",
						        errorMessage : "ERROR_MESSAGE",
								errorId: "ERROR_ID"
						    } 
						}; 
		var clearCache = Math.round(Math.random()*10000);
		TSWIDGET.XMGR.sendRequestAsync("format=xmlcomplex&afc=Null&package=searchr2&op=GetRecentlyViewed&cache="+clearCache,
										this.successHandler,
										this.failureHandler,
										responseSchema,
										this);
	}
	else{
		TSWIDGET.EVENTS.fireEvent('evtHistoryLoaded',this.results);
	}
}

HistoryModel.prototype.successHandler = function(response,ref){ //ref-->this (HistoryModel)
	ref.blGetData = true;	
	ref.results = response.results;
	TSWIDGET.log("fetch data, length="+ref.results.length);
	TSWIDGET.EVENTS.fireEvent('evtHistoryLoaded',ref.results);
}
HistoryModel.prototype.failureHandler = function(response,ref){
	ref.blGetData = true;
	//alert(response.meta.errorMessage);
}

