$(function () {
    $("a.addToCart").click (function () {
        $(this).addClass("loading");
        var idProduct = $(this).attr("rel");
        var self = $(this);
        $.ajax({
            url: url_site + "index.php",
            data: "action=cart_add&id=" + idProduct + "&qty=1",
            dataType: "json",
            type: "post",
            success: function (data) {
                if (data.html) {
                    $("div#sidebarCartContainer").html(data.html);
                    $("div#sidebarCartContainer").animate({
                        'opacity': ['show', 'swing'],
                        'height' : 'show'
                    }, 400);
                    $("div#cartInfo").show(300);
                    $("span#headerCartCount").html(data.countProducts);
                    if ($('html')[0].scrollTop > 400) {
                        $('html,body').animate({scrollTop: 100}, 500);
                    }
                } else {
                    $("div#sidebarCartContainer").hide(500);
                    $("div#cartInfo").hide(300);
                }
                self.removeClass("loading");
            }
        });
        return false;
    });
});

function addOverlay () {
    $("#dialog-loading").first().dialog({
        modal: true,
        open: function(event, ui) {
            $(".ui-dialog-titlebar-close").hide();
        },
        minHeight: 60,
        minWidth: 100,
        resizable : false
    });
}

function removeOverlay() {
    $("#dialog-loading").first().dialog('close');
}
