function selectProduct(id) {
    
    $('loader').show();
    
    var url = '/loja/select/'+id+'/'+$F('numitems_'+id);
    
    new Ajax.Request(url, {
                        method : 'get',
                        onSuccess: function(transport) {
                            selectUpdate(transport.responseJSON); 
                        }
    });
}

function selectUpdate(product) {
    
    if(!$('li_'+product.id)) {
        var li = new Element('li');
        li.id  = 'li_'+product.id;
        
        var img_remove = new Element('img').addClassName('hand removeImg');
        
        img_remove.writeAttribute( { 
                                      id : 'img_remove_'+product.id, 
                                      src: '/img/remover.gif',
                                      alt: 'Remover',
                                      title: 'Remover'
                                    } 
                                     );
        
        img_remove.observe('click', removeProduct); 
        
        var n_items = new Element('span');
        n_items.id = 'n_items_'+product.id;
        n_items.innerHTML = product.n_items + ' Und.';
        
        li.insert(img_remove);
        li.insert('&nbsp;' + product.name + '<br />');
        li.insert(n_items);
        
        $('cart').insert(li);
    } 
    else
        $('n_items_'+product.id).innerHTML = product.n_items + ' Und.';    
    
    $('loader').hide();
}

function initRemoveProduct() {
    var remove_btn = $$('.removeProduct');
    
    remove_btn.each(function(btn) { 
                        btn.observe('click', removeProduct);
                    });
}

function removeProduct() {
    $('loader').show();
    
    var url = '/loja/remove/'+this.id.replace('img_remove_', '')+'/';
    
    new Ajax.Request(url, {
                        method : 'get',
                        onSuccess: function(transport) {
                            removeUpdate(transport.responseJSON); 
                        }
    });
}

function removeUpdate(product) {
    
    if($('total'))
        $('total').replace(product.total);
    
    $('loader').hide();
    $('li_'+product.id).remove();
}

//Event.observe(window, 'load', initRemoveProduct);

