var basket = null;
function BasketController()
{
    this.initialize(); 
} 

BasketController.prototype =
{
    initialize: function( )
    {  
        //get shoping cart elements
        this.cart_count = $('#cart_count');    
    },
    
    updateCart: function( count )
    {
        //header cart
        if(this.cart_count == null)
            this.cart_count = $('#cart_count');
        
        if(this.cart_count == null)
            setTimeout(function updt_fnc(){ basket.updateCart(count); } , 100);
            
        this.cart_count.html( this.declension(count, Array('товар', 'товара', 'товаров')) ); 
    },
    
    add: function( productID )
    {
        productID = this.checkID(productID);
        if(!productID) return;
        
        var qty = 1;
        var me = this;
        $.post('/basket?ev=ajax_action&action=add_item', {'productID': productID, 'quantity': qty}, function(data){
            if(data.error == 0)
            {
                //result.cart.total
                me.updateCart( data.cart.count );
            } else { alert('error'); }
        }, "json");
        return false;
    },
    
    change: function(itemID ,action)
    {
        var me = this;
        var item   = $('#number_tovar_'+itemID);
        var number = item.val();
        if (action=='munus')    {
            number--;    
        }
        else    {
            number++;
        }
        if (number<1)    {
            number = 1;
        }
        $.post('/basket?ev=ajax_action&action=change_item', {'itemID': itemID,'quantity':number}, function(data){
            if (data.error == 0) {
                    me.updateCart( data.cart.count );
                    var balans = parseInt($('#item_prise_'+itemID).val());
                    $('#items_prise_'+itemID).val((number*balans)+' руб');
                    item.val(number);
                    $('#final_sum').val(data.cart.total+' руб');
                    $('#number_items').val(data.cart.count);
                }
                else {
                    alert('error');
                }
        }, "json");
        return false;
    },
    
    clear: function()
    {
         var me = this;
        $.post('/basket?ev=ajax_action&action=update_items_quantity', $('#basket_form').serialize(), 
        function(data){
            me.updateCart( data.cart.count ); 
        });
    },
    
    del: function( itemID)
    {
        itemID = this.checkID(itemID);
        if(!itemID) return;

        var me = this;
        $.post('/basket?ev=ajax_action&action=delete_item', {'itemID': itemID}, function(data){
            if (data.error == 0) {
                    me.updateCart( data.cart.count );
                    $('#tovar_'+itemID).remove(); 
                    $('#final_sum').val(data.cart.total+' руб');
                    $('#number_items').val(data.cart.count);        
                                            
                    if(data.cart.count == 0)
                    {
                        $('#send_letter').hide();//show 'empty shopping cart'
                    }
                }
                else {
                    alert('error');
                }
        }, "json");
        return false;
    },
    
    refresh: function( ) 
    {
        $.post('/basket?ev=ajax_action&action=update_items_quantity', $('#basket_form').serialize(), 
        function(data){
            location.reload();
        });
    },
    
    checkID: function(ID)
    {
        ID = parseInt(ID);
        if(ID<=0 || isNaN(ID)) 
            return 0;
        return ID;
    },

    declension: function(count, forms)
    {
        n = Math.abs(count) % 100;
        n1 = n % 10;  
        if (n > 10 && n < 20) {
            return count+' '+forms[2];
        }
        if (n1 > 1 && n1 < 5) {
            return count+' '+forms[1];  
        }
        if (n1 == 1) {
            return count+' '+forms[0];  
        }
        return count+' '+forms[2];
    }
    
}
