//~ REQUIRES PROTOTYPE

//~ usage:
    //~ predictor = new DeastorePredictor();
    //~ predictor.updateWithPredictions('user', '/predictions/products/detail.html', 'userRecsDisplay', '62200'); 
    //~ predictor.updateWithPredictions('product', '/predictions/products/detail.html', 'prodRecsDisplay', '98950');
    //~ predictor.updateWithPredictions('search', '/predictions/products/detail.html', 'searchRecsDisplay', 'einaudi');
    //~ predictor.logPurchase('62200', new Array('98950', '9850', '5950', '50'), new Array('10.5', '11.0', '154.95', '12.0'));
    //~ ...several actions and finally commit
    //~ predictor.commit();
    
    //~ methods:
      //~ updateWithPredictions    ** dynamically update html container with avail recommendations **
          //~ params:
              //~  prediction type  /user|product|search/
              //~ url from wich ajax update retrieves content
              //~ html container for content update
              //~ object id required for prediction type: /accountid|productid/
      //~ logPurchase         **  log user purchase **
          //~ params:
              //~ account id
              //~ product id array 
              //~ prices array

var DeastorePredictor = Class.create({
  emark: null,
  templateName: 'ProdPageRelated',
  resultLog: '',
  initialize: function(){
    this.emark = new Emark();
    this.onCommit = $A();
    this.resultLog = "";
	},
  updateWithPredictions: function(type, deastore_predictions_url, html_container, obj_id, avail_template){
    var response = null;
    var template = (avail_template || this.templateName);
    if(type == 'product'){
      response = this.emark.getProductsPredictions(obj_id, template);
    }else if(type == 'user'){
      response = this.emark.getUserPredictions(obj_id, template);
    }else if(type == 'search'){
      response = this.emark.getSearchPredictions(obj_id, template);
    }
    this.onCommit.push( (function(){ 
          this.debugResult("updateWithPredictions called for template: " + template); 
          this.updateContent(html_container, response, deastore_predictions_url);
      }).bind(this) );
  },
  productPredictions: function(obj_id, avail_template){
    var template = (avail_template || this.templateName);
    var response = this.emark.getProductsPredictions(obj_id, template, null, ['ActionId']);
    this.onCommit.push( (function(){ return response; }).bind(this) );
  },
  updateContent: function(obj_id, avail_response, deastore_predictions_url){
    if(avail_response){
      ids = $A(avail_response.values).flatten();
      pars = $H({'id[]': ids}).toQueryString();
      new Ajax.Updater(obj_id, deastore_predictions_url, {asynchronous: true, evalScripts: true, method: 'get', parameters: pars});
      this.debugResult("updateContent with predictions ids: " + ids.inspect()); 
    }
  },
  logPurchase: function(user_id, product_ids, prices)
  {
    var response = this.emark.logPurchase(user_id, product_ids, prices);
    this.onCommit.push( (function(){ 
      this.debugResult("log purchase for user_id " + user_id + " product ids: " + product_ids.inspect() + " response: "+ response); 
    }).bind(this) );
  },
  logSearch: function(keyword, purchased_product_id)
  {
    var response = this.emark.saveSearch(keyword, purchased_product_id);
    this.onCommit.push( (function(){ 
      this.debugResult("log Search for keyword " + keyword + " product id: " + purchased_product_id + " response: "+ response); 
    }).bind(this) );
  },
  commit: function(){
    var callbacks = this.onCommit;
    this.emark.commit(function(){ 
      callbacks.each(function(callback){ 
        if (Object.isFunction(callback)){ callback.apply(); } 
      });
    });
  },
  debugResult: function(message){
      this.resultLog += "\n " + message;
  },
  getEventsLog: function(){
    return this.resultLog;
  }
});



