var basket = null;
var BasketController = Class.create(); 
BasketController.prototype =
{
    maxQuantity: 100,
    elementsCart: {},
    currency: '$',
    sending: false,
    
    initialize: function( )
    {  
        //get shoping cart elements
        this.elementsCart.count1 = $('cart_count1');
        this.elementsCart.total1 = $('cart_total1');   
        this.elementsCart.count2 = $('cart_count2');
        this.elementsCart.total2 = $('cart_total2');   
    },
    
    updateCart: function( count, total )
    {
        total = this.currency+''+total;
        //header cart
        this.elementsCart.count1.update(count);     
        this.elementsCart.total1.update(total);
        //right cart
        if(this.elementsCart.count2!=undefined) {
            this.elementsCart.count2.update(count);     
            this.elementsCart.total2.update(total);
        }
    },
    
    add: function( productID, redirect_url, is_tape )
    {
        is_tape = is_tape || 0;
        
        if(this.sending) return;
        this.sending = true;
        
        productID = this.checkProductID(productID);
        if(!productID) return;
        
        new Ajax.Request('/basket?ev=ajax_action&action=add_product', {
            parameters:{'productID': productID, 'quantity': this.getQuantity(productID, is_tape) }, 
            onSuccess: function (transport) 
            {
                try
                {
                    var result = transport.responseText.evalJSON();
                    if (result.error == 0) {
                        this.updateCart( result.cart.count, result.cart.total );
                        if(redirect_url)
                            document.location.href=redirect_url;
                    }
                    else {
                        alert('error');
                    }
                    this.sending = false;
                }
                catch(e){}
            }.bind(this),
             
            onFailure: function (transport) 
            {
                alert('error');
            }.bind(this)
        });
    },
    
    del: function( productID, redirect_url )
    {
        productID = this.checkProductID(productID);
        if(!productID) return;
        
        new Ajax.Request('/basket?ev=ajax_action&action=delete_product', {
            parameters:{'productID': productID}, 
            onSuccess: function (transport) 
            {
                try
                {
                    var result = transport.responseText.evalJSON();
                    if (result.error == 0) {
                        this.updateCart( result.cart.count, result.cart.total );
                        Effect.Fade('item_'+productID);
                        $('basket_total').update( result.cart.total );        
                                                
                        if(result.cart.count == 0)
                        {
                            if(redirect_url)
                                document.location.href=redirect_url;
                        }
                    }
                    else {
                        alert('error');
                    }
                }
                catch(e){}
            }.bind(this),
             
            onFailure: function (transport) 
            {
                alert('error');
            }.bind(this)
        });
    },
    
    refresh: function( ) 
    {
        new Ajax.Request('/basket?ev=ajax_action&action=update_products_quantity', {
            parameters: $('basket_form').serialize(true),
            onComplete: function (t) { location.reload(); }
        });
    },
    
    checkProductID: function(productID)
    {
        productID = parseInt(productID);
        if(productID<=0 || isNaN(productID)) 
            return 0;
        
        return productID;
    },
    
    getQuantity: function(productID, is_tape)
    {
        var elQuantity = $((is_tape ? 'tape' : '')+'item_'+productID+'_quantity');
        var quantity = parseFloat(elQuantity.value);

        if(quantity <= 0 || isNaN(quantity) )
            quantity = 1;
        
        return quantity;
    }
}
