﻿function clearCart() {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Clear'),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml);
            initCartSlide();

            /* redirect to homepage if on order page */
            if (window.location.toString().toLowerCase().indexOf($Url.resolve("~/order").toLowerCase()) >= 0) {
                window.location = $Url.resolve("~/");
                return;
            }

            $().toastmessage('showSuccessToast', "Кошницата е празна.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Кошницата не може да бъде изпразнена. Опитайте отново."); }
    });
    return false;
}

function increaseQuantity(productId) {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Increase/' + productId),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml); initCartSlide(); $('#cart_bag_sec').show();
            $().toastmessage('showSuccessToast', "Количеството за продукт " + productId + " е увеличено.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Възникна грешка при опит да се увеличи количеството за продукт " + productId + ". Опитайте отново."); }
    });
    return false;
}

function decreaseQuantity(productId) {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Decrease/' + productId),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml); initCartSlide(); $('#cart_bag_sec').show();
            $().toastmessage('showSuccessToast', "Количеството за продукт " + productId + " е намалено.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Възникна грешка при опит да се намали количеството за продукт " + productId + ". Опитайте отново."); }
    });
    return false;
}

function removeFromCart(productId) {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Remove/' + productId),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml); initCartSlide(); $('#cart_bag_sec').show();
            $().toastmessage('showSuccessToast', "Продукт " + productId + " е премахнат от кошницата.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Възникна грешка при опит да се премахне продукт " + productId + " от кошницата. Опитайте отново."); }
    });
    return false;
}

function addToCartBL(productId) {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Add/' + productId),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml); initCartSlide();
            $("#pbl" + productId).effect("transfer", { to: $("#manageMyAccount") }, 500);
            $().toastmessage('showSuccessToast', "Продукт " + productId + " е добавен в кошницата.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Възникна грешка при опит да се добави продукт " + productId + " в кошницата. Опитайте отново."); }
    });
    return false;
}

function addToCartPD(productId) {
    $.ajax({
        type: "GET",
        url: $Url.resolve('~/ShoppingCart/Add/' + productId),
        success: function (responseHtml) {
            $("#cart").empty().html(responseHtml); initCartSlide();
            $("#pd" + productId).effect("transfer", { to: $("#manageMyAccount") }, 500);
            $().toastmessage('showSuccessToast', "Продукт " + productId + " е добавен в кошницата.");
        },
        error: function (e) { $().toastmessage('showErrorToast', "Възникна грешка при опит да се добави продукт " + productId + " в кошницата. Опитайте отново."); }
    });
    return false;
}

function initCartSlide() {
    $('#manageMyAccount').click(function () { $('#cart_bag_sec').slideToggle('slow'); });
}

jQuery(document).ready(function ($) {
    initCartSlide();
});

$(document).ready(function () {
    // open external links in new window
    $('a[href^=http]').click(function () {
        window.open(this.href);
        return false;
    });
});

