var Basket = function(summ_place, quantity_place)
{
    if (!Basket.id)
        Basket.id = 0;
    Basket.id++;
    this.basket = {};
    this.summPlace = summ_place;
    this.quantityPlace = quantity_place;
    this.name = "basket"+Basket.id;
    this.onChange = null;
    this.onClear = null;
}

Basket.prototype = 
{
    id : 0,
    toJSON : function()
    {
        var f = true;
        var json = '{';
        
        for (var product_id in this.basket)
        {
            if (!f)
                json += ',';
        
            json += '"' + product_id + '":';
            json += ' {'
            json +=     '"price": '+this.basket[product_id]['price']+",";
            json +=     '"size": "'+this.basket[product_id]['size']+'",';
            json +=     '"quantity": '+this.basket[product_id]['quantity'];
            json += ' }'
            
            f = false;
        }
        json += '}';
        return json;
    },
    
    fromJSON : function(json)
    {
        this.basket = eval("("+json+")");
        this.refresh();
    },
    removeProduct : function(product_id, quantity)
    {
        if (quantity < 0) 
        {
            delete this.basket[product_id];
            return;
        }
        if (this.basket[product_id])
        {
            var minus_quantity = quantity?quantity:1;
            this.basket[product_id].quantity -= minus_quantity;
            if (this.basket[product_id].quantity <= 0)
                delete this.basket[product_id];
        }
    },
    addProduct : function(product_id, price, quantity, name, imgFileName)
    {
        if (this.basket[product_id])
        {
            this.basket[product_id].quantity++;
        }
        else
        {
            this.basket[product_id] = {};
            this.basket[product_id].price = price;
            this.basket[product_id].quantity = quantity?quantity:1;
            this.basket[product_id].imgFileName = imgFileName;
            this.basket[product_id].name = name;
        }
        this.refresh();

    },
    clear : function()
    {
        this.basket = {};
        this.refresh();
        if (this.onClear)
            this.onClear();
    },
    refresh : function()
    {
    
        var avg_quantity = 0;
        var summa = 0;
        for (var product_id in this.basket)
        {
            avg_quantity += parseInt(this.basket[product_id].quantity);
             
            summa += this.basket[product_id].price * this.basket[product_id].quantity;
            
        }     
        if (this.summPlace != null && this.summPlace.innerHTML)
            this.summPlace.innerHTML = summa;
            
        if (this.quantityPlace != null && this.quantityPlace.innerHTML)
            this.quantityPlace.innerHTML = avg_quantity;
        
        if (this.onChange)
            this.onChange();
    },
    getAsArray : function()
    {
        var tmp = this.basket;
        return tmp;
    },
    changeQuantity : function(productID, quantity)
    {
        
        if (this.basket[productID]) {
            this.basket[productID].quantity = quantity;
        }
        this.refresh();
    }
    ,applySize : function(id_product, size)
    {
        this.basket[id_product].size = size;
    }
    
}
