﻿jQuery.fn.slideFadeToggle = function (speed, easing, callback) {

    return this.animate({ opacity: 'toggle', height: 'toggle' }, speed, easing, callback);

};
$(document).ready(function () {
    $(".cart_box").hide();

    $('.cart_product:odd').css({ backgroundColor: '#eee' });

    $('#cartPopup').click(function (event) {
        $('.cart_box').slideFadeToggle('slow');
        $('.expandCart').toggleClass('arrowUp');
    });

    $('.cart_up').click(function () {
        updateCart($(this).attr("productVar"), $(this).attr("addVar"), true, this);
    });
    $('.cart_down').click(function () {
        updateCart($(this).attr("productVar"), $(this).attr("addVar"), false, this);
    });
    $('.cart_remove').click(function () {
        removeItem($(this).attr("productVar"), $(this).attr("addVar"), this);
    });


    /*$(".cart_qty").each(function () {
    if ($(this).text() == "0") {
    $(this).parent().parent().hide();
    }

    $(".cart_removeCartItem").click(function () {
    $(this).parent().addClass('cart_highlight');
    $(this).parent().fadeTo("slow", 0, function () { //fade
    $(this).slideUp("slow", function () { //slide up
    $(this).remove(); //then remove from the DOM
    update();
    });
    });

    });
    });*/
})

function updateCart(productvar, addvar, directionvar, getthis) {
    var that = getthis;
    var addVar = addvar;
    numbervar = $(".cart_up[addvar='"+addVar+"']").closest(".cart_product").find(".cart_qty").text();
    $.ajax({
        type: "POST",
        url: "../ECommerce/updateCart.asmx/updateShoppingCart",
        data: "{'prod':'" + productvar + "','add': '" + addvar + "','direction': '" + directionvar + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var vars = response.d;
            var uStoreDirection;
            //var uStorePrice;
            var totalCount;
            var totalPrice;
            $.each(vars, function (index, var1) {
                uStoreDirection = var1.storeDirection;
                //uStorePrice = var1.storePrice;
                totalPrice = var1.totalPrice;
                totalCount = var1.totalCount;
            });
            if (uStoreDirection == "True") {
                //$(that).closest(".cart_product").find(".cart_qty").text(parseInt(numbervar) + 1);
                $('[addVar="' + addVar + '"]').closest(".cart_product").find(".cart_qty").text(parseInt(numbervar) + 1);
                //alert(numbervar);
            } else if (numbervar != 0) {
                $(that).closest(".cart_product").find(".cart_qty").text(parseInt(numbervar) - 1);
            } else {
                $(that).closest(".cart_product").find(".cart_qty").text(0);
            }

            $("#cart_total").text(totalPrice);
            $("#cart_items").text(totalCount);


            $("#cartPopup").animate({ backgroundColor: "#fcffa6" }, 500).animate({ backgroundColor: "#FFFFFF" }, 500);
;
        },
        error: function (msg) {
            alert("fail");
        }

    });

};
function removeItem(productvar, addvar, getthis) {
    var that = getthis;
    $.ajax({
        type: "POST",
        url: "../ECommerce/updateCart.asmx/removeItem",
        data: "{'prod':'" + productvar + "','add': '" + addvar + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var numbervar = $(that).closest(".cart_product").find(".cart_qty").text();
            var vars = response.d;
            var totalCount;
            var totalPrice;
            $.each(vars, function (index, var1) {
                totalPrice = var1.totalPrice;
                totalCount = var1.totalCount;
            });
            $(that).closest(".cart_product").find(".cart_qty").text(0);
            $("#cart_total").text(totalPrice);
            $("#cart_items").text(totalCount);
            $("#cartPopup").animate({ backgroundColor: "#fcffa6" }, 500).animate({ backgroundColor: "#FFFFFF" }, 500);
        },
        error: function (msg) {
            alert("fail");
        }

    });

};

function update() {
    var l = $('.cart_removeCartItem').closest('.cart_product').removeAttr('backgroundColor');
    l.filter(':even').css({ backgroundColor: '#fff' });
    l.filter(':odd').css({ backgroundColor: '#eee' });
}
