Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };

function shcartAdd( ida, q ){
    
    if( $('input[name^="a_aog"]').length > 0 ){
        
        var added = false;
        
        $('input[name^="a_aog"]:checked').each(function(){
            var gid = $(this).attr('name').replace(/[^0-9]+/g, "");
            
            $.getJSON( URL_BASE + "Trgovina/KosaricaDodaj/" + ida + "/" + q + "/" + gid, function(json){

                if(json.status == 1){

                    //samo prištej količino json.quantity

                    /*$('#shcart-article-' + ida + ' input.article-quantity').val(json.quantity);
                    shcartTotal();*/
                    
                    $('#shcart-items').html(json.totalcount);

                }else if( json.status == 2 ){

                    /*$('#shcart-content').show();

                    //dodaj artikel

                    $.getJSON( URL_BASE + "Trgovina/ArtikelInfo/" + ida, function(json){

                        html = '<div id="shcart-article-' + ida + '" class="shcart-article">';
                        if(json.picture != null){
                            html += '<img src="' + json.picture.replace('PIC_SIZE', '50') + '" style="float: left; margin-right: 10px;" />';
                        }
                        html += json.title + '<br />'
                        html += '<input class="article-quantity" type="text" onkeyup="if($(this).val().length > 0){shcartAdd(' + ida + ', $(this).val());}" style="width: 20px;" value="1" /> kos&nbsp;&nbsp;&nbsp;<img src="' + URL_BASE + 'media/img/icons/icon-x.gif" class="pointer" onclick="shcartDelete(' + ida + ');" />';
                        html += '<br /><span>' + parseFloat(json.price).formatMoney(2, ',', '.') +  '</span> €';
                        html += '<br /><input type="hidden" name="article-price" value="' + json.price + '" class="article-price" />';
                        html += '<hr /></div>';


                        $('#shcart-content').append(html);
                        shcartTotal();

                    });*/
                    
                    $('#shcart-items').html(json.totalcount);

                }

            });
            
            added = true;
            
        });
        
        if(!added){
            showNotification(lang['vsaj_1']);
        }else{
            showNotification(lang['artikel_dodan']);
        }
        
    }else{
        
        $.getJSON( URL_BASE + "Trgovina/KosaricaDodaj/" + ida + "/" + q, function(json){

            $('#shcart-items').html(json.totalcount);
            
            showNotification(lang['artikel_dodan']);

        });
        
    }

}


function shcartDelete(ida){

    $.getJSON( URL_BASE + "Trgovina/KosaricaIzbrisi/" + ida, function(json){

        if(json.status == true){

            $('#shcart-article-' + ida).slideUp(500, function(){
                $(this).remove();
                shcartTotal();
            });

        }

    });

}

function deleteShCart(){
    $.getJSON( URL_BASE + "Trgovina/KosaricaIzprazni/", function(json){
        $('#shcart-content').slideUp(500, function(){
           $(this).html();
           shcartTotal();
        });
    });
}

function shcartTotal(){

    var total = 0;

    $('.shcart-article').each(function(){
        total += $(this).find('input[name=article-price]').val() * $(this).find('.article-quantity').val();
    });

    $('#shcart-total').html(total.formatMoney(2, ',', '.') + ' &euro;');

}

function shcartFinalChangeQ(ida){

    $.getJSON( URL_BASE + "Trgovina/KosaricaDodaj/" + ida + '/' + $('#shcart-article-' + ida + ' .article-quantity').val(), function(json){

        if(json.status  > 0){
            shcartFinalTotal();
        }

    });
    
}

function shcartFinalDelete(ida){

    $.getJSON( URL_BASE + "Trgovina/KosaricaIzbrisi/" + ida, function(json){

        if(json.status == true){

            $('#shcart-article-' + ida).slideUp(500, function(){
                $(this).remove();
                shcartFinalTotal();
            });

        }

    });

}

function shcartFinalTotal(){
    
    var total = 0;

    $('.shcart-article').each(function(){
        total += $(this).find('input').val() * $(this).find('.article-price').val();
    });

    $('#shcart-final').html(total.formatMoney(2, ',', '.') + ' &euro;');

}

