/*******************************************************************************
 * Model
 */

OMVC.FPListModel = function () {
  var that = this;

  /*
   * Observable subjects
   */
  this.modelInitialized     = OMVC.makeObservableSubject();
  this.productsLoaded       = OMVC.makeObservableSubject();

  /*
   * Properties
   */
  var uids;                        // Product uid
  var details = {};               // Product properties
  var relatedProducts = [];       // Related products 

  /*
   * Methods
   */
  // Any initial stuff
  this.initialize = function(ids) {
    uids = ids;
    that.getDetails();
    that.modelInitialized.notifyObservers();
  };

  // Get details
  this.getDetails = function() {
    jQuery.ajax({
      url: mwwoolfAjaxUrl,
      data: {action: 'getProductsSet', uids: uids},
      dataType: 'json',
      success: function(result) {
        products = result;
        that.productsLoaded.notifyObservers();
      }
    });
  };

  // Getter
  this.get = function(varName) {
    return eval(varName);
  }

};

/*******************************************************************************
 * View
 */
OMVC.FPListView = function (model, rootObject) {
  var that = this;
  /*
   * HTML oblects
   */

  // Draw product
  that.renderProduct = function(product) {
    return jQuery('<div/>')
        .addClass('product')
        .append(jQuery('<div/>')
            .addClass('image')
            .append(jQuery('<a/>')
                .attr('href', '/')
                .bind('click', function() {
                  document.location.hash = '#p' + product.uid;
                  return false;
                })
                .append(jQuery('<img/>')
                .attr('src', product.pictures[0])
                //.width('108px'))))
                .height('61px'))))
        .append(jQuery('<div/>')
            .addClass('underline'))
        .append(jQuery('<p/>')
            .html(product.article_number))
        .append(jQuery('<p/>')
            .html(product.title));
  };


  /*
   * Listening methods
   */
  // Product rendering
  model.productsLoaded.addObserver(function () {
    var counter = 0;
    var product;
    var products = model.get('products');
    jQuery.each(products, function() {
      product = that.renderProduct(this);
      if(counter == 3) {
        product.addClass('last');
      }
      rootObject.append(product);
      counter++;
    })
  });


};

/*******************************************************************************
 * Controller
 */
OMVC.FPListController = function (model, view, ids) {
  model.initialize(ids);
};

