/* 
 * Created by David Galvis
 */

/**
 * Instanciates ta new comment item on feed
 * @construct
 * @param int id Id of the switch
 * @param array location Locations the autocomplete will search
 */
function BuyPts(selector, settings)
{
   var _this = this;

   jQuery.extend(this, settings);

   this.container = jQuery(selector);
   this.container.addClass('buy-pts-container');
   this.container.append("<p class='buy-pts-title'>" + Dico.purchase_points + "</p>");
   var div = "<div class='buy-pts-actions'>\n\
                  <div class='buy-pts-balance'>\n\
                       <label class='buy-pts-balance-label'>" + Dico.pp_current_balance + "</label>\n\
                       <label class='buy-pts-balance-label-value'>" + this.currentPts + " pts</label>\n\
                        <div style='clear:both'></div>\n\
                  </div>\n\
                  <div class='buy-pts-slider'>\n\
                    <label class='buy-pts-add-label'>" + Dico.pp_add + "</label>\n\
                    <div class='buy-pts-option'><select name='contrib_product'></select> pts</div>\n\
                    <div style='clear:both'></div>\n\
                  </div>\n\
                  <div>\n\
                    <label class='buy-pts-newbalance-label'>" + Dico.pp_new_balance + "</label>\n\
                  <label class='buy-pts-newbalance-value'>" + (this.currentPts + this.products[0].contributionPts) + " pts</label>\n\
                  </div>\n\
              </div>\n\
              <div class='buy-pts-buy'>\n\
                  <div class='buy-pts-total'>" + Dico.pp_total + " : <span class='buy-pts-total-value'>50</span> " + this.currency + "</div>\n\
                  <div class=''><a href='javascript:void(0)' class='buy-pts-total-trigger'>" + Dico.purchase_points + "</a></div>\n\
              </div>\n\
              <div style='clear:both'></div>";
    this.container.append(div);

    // Slider
    this.changePts = this.container.find('.buy-pts-option select');
    jQuery(this.products).each(function(){
        var product = this;
        var option = JQ("<option value='" + product.id + "' price='" + product.price + "'>" + product.contributionPts + "</option>");
        _this.changePts.append(option);
    });

    _this.changePts.change(function(){
        _this.value = parseInt(_this.changePts.find('option:selected').text());
        _this.total = _this.changePts.find('option:selected').attr('price');
        _this.update();
    });
    
    /*this.changePts.slider({
        value: _this.defaultValue,
        min: _this.min,
        max: _this.max,
        stepping: 1,
        range: false,
        slide:  function(e, ui){
            _this.value = ui.value;
            _this.update();
        }
    });*/

    // Slider value
    this.sliderValue = this.container.find('.buy-pts-add-label-value');

    // Balance
    this.balanceValue = this.container.find('.buy-pts-newbalance-value');

    // Total cost
    this.totalValue = this.container.find('.buy-pts-total-value');

    // Checks the firs product
    //_this.changePts.find('option:first').select();
    
    // Click to buy event
    this.submitButton = this.container.find('.buy-pts-total-trigger');
    this.submitButton.click(function(){
        var product = [{
                id_product : _this.changePts.val(),
                total : 1
            }];
        window.location = "/application/payment/payment-multi.php?format=json&products=" + JQ.toJSON(product);
    });
}

BuyPts.prototype =
{
    /**
     * Min pts to buy
     */
    min : 10,

    /**
     * Max pts to buy
     */
    max : 5000,

    /**
     * Cost per point
     */
    cost : 0.05,

    /**
     * Current user's pts
     */
    currentPts : 0,

    /**
     * The default value
     */
    defaultValue : 1000,

    /**
     * The number of points to buy
     */
    value : null,

    /**
     * Total amount to pay
     */
    total : null,

    /**
     * Products
     */
    products : [],

    /**
     * Default currency
     */
    currency : 'CHF',

    /**
     * Updates the current html view with the correct points
     */
    update : function ()
    {
        this.sliderValue.html('+' + this.value);
        this.balanceValue.html((this.value + this.currentPts) + ' pts');

        //this.total = this.roundValue(this.value * this.cost);
        this.totalValue.html(this.total);
    },

    roundValue : function(value){
        return Math.round(value * 100) / 100;
    }
};


